Example 23 - C#
Last update: 17.07.2025C#
using System;
using ZOSAPI;
namespace CSharpStandaloneApplication
{
class Program
{
static void Main(string[] args)
{
// Find the installed version of OpticStudio
bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize();
// Note -- uncomment the following line to use a custom initialization path
//bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(@"C:\Program Files\OpticStudio\");
if (isInitialized)
{
LogInfo("Found OpticStudio at: " + ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory());
}
else
{
HandleError("Failed to locate OpticStudio!");
return;
}
BeginStandaloneApplication();
}
static void BeginStandaloneApplication()
{
// Create the initial connection class
// Attempt to create a Standalone connection
IZOSAPI_Application TheApplication = TheConnection.CreateNewApplication();
if (TheApplication == null)
{
HandleError("An unknown connection error occurred!");
return;
}
// Check the connection status
if (!TheApplication.IsValidLicenseForAPI)
{
HandleError("Failed to connect to OpticStudio: " + TheApplication.LicenseStatus);
return;
}
{
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
return;
}
IOpticalSystem TheSystem = TheApplication.PrimarySystem;
// creates new directory
System.IO.Directory.CreateDirectory(strPath);
// Set up primary optical system
string sampleDir = TheApplication.SamplesDir;
string testFile = System.IO.Path.Combine(sampleDir, "Sequential\\Objectives\\Double Gauss 28 degree field.zos");
TheSystem.LoadFile(testFile, false);
int max_rays = 150;
// determines the max field number and max field value
double max_field = 0;
{
if (TheSystem.SystemData.Fields.GetField(i).Y > max_field) max_field = TheSystem.SystemData.Fields.GetField(i).Y;
}
int max_num_field = TheSystem.SystemData.Fields.NumberOfFields;
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
// Set up Batch Ray Trace
ZOSAPI.Tools.RayTrace.IBatchRayTrace raytrace = TheSystem.Tools.OpenBatchRayTrace();
int nsur = TheSystem.LDE.NumberOfSurfaces;
ZOSAPI.Tools.RayTrace.IRayTraceNormUnpolData normUnPolData = raytrace.CreateNormUnpol(max_rays + 1, ZOSAPI.Tools.RayTrace.RaysType.Real, nsur);
// define batch ray trace constants
double hx = 0, px = 0;
double[] py_ary = new double[max_rays + 1];
for (int i = 0; i <= max_rays; i++)
{
py_ary[i] = i / (double)max_rays * 2 - 1;
}
int max_wave = TheSystem.SystemData.Wavelengths.NumberOfWavelengths;
// image surface number and primary wavelength
int pwav = 0;
{
if (TheSystem.SystemData.Wavelengths.GetWavelength(a).IsPrimary)
pwav = a;
}
// creates array of Y coordinate chief ray values
double[] chief_ary = new double[max_num_field];
for (int field = 1; field <= max_num_field; field++)
{
double hy = TheSystem.SystemData.Fields.GetField(field).Y / max_field;
// gets single value without using MFE(see ZPL OPEV)
chief_ary[field - 1] = TheSystem.MFE.GetOperandValue(ZOSAPI.Editors.MFE.MeritOperandType.REAY, nsur, pwav, 0, hy, 0, 0, 0, 0);
}
// initialize x / y image plane arrays
double[,,] y_ary = new double[max_num_field, max_wave, max_rays + 1];
System.Text.StringBuilder sbReport = new System.Text.StringBuilder();
sbReport.AppendLine("Manual Code");
sbReport.AppendLine("Field \t Wave \t x \t y");
for (int field = 1; field <= max_num_field; field++)
{
double hy = TheSystem.SystemData.Fields.GetField(field).Y / max_field;
for (int wave = 1; wave <= max_wave; wave++)
{
// Adding Rays to Batch, varying normalised object height hy
normUnPolData.ClearData();
for (int i = 0; i <= max_rays; i++)
{
double py = py_ary[i];
}
// Run Batch Ray Trace
raytrace.RunAndWaitForCompletion();
// Read batch raytrace and save results
normUnPolData.StartReadingResults();
int rayNumber, ErrorCode, vignetteCode;
double double_X, double_Y, double_Z, double_L, double_M, double_N, double_L2, double_M2, double_N2, OPD, Intensity;
bool success;
success = normUnPolData.ReadNextResult(out rayNumber, out ErrorCode, out vignetteCode, out double_X, out double_Y, out double_Z, out double_L, out double_M, out double_N, out double_L2, out double_M2, out double_N2, out OPD, out Intensity);
while (success)
{
if ((ErrorCode == 0) && (vignetteCode == 0))
{
y_ary[field - 1, wave - 1, rayNumber - 1] = double_Y;
sbReport.AppendLine((field).ToString() + "\t" + (wave).ToString() + "\t" + py_ary[rayNumber - 1].ToString() + "\t" + ((double_Y - chief_ary[field - 1]) * 1000).ToString());
}
success = normUnPolData.ReadNextResult(out rayNumber, out ErrorCode, out vignetteCode, out double_X, out double_Y, out double_Z, out double_L, out double_M, out double_N, out double_L2, out double_M2, out double_N2, out OPD, out Intensity);
}
sbReport.AppendLine("");
}
}
double manual = (double)stopWatch.ElapsedMilliseconds / 1000;
stopWatch.Reset();
stopWatch.Start();
// runs a ray fan analysis and extracts
ZOSAPI.Analysis.Settings.Fans.IAS_Fan ray_settings = (ZOSAPI.Analysis.Settings.Fans.IAS_Fan)ray.GetSettings();
ray_settings.NumberOfRays = max_rays / 2;
ray_settings.Field.UseAllFields();
ray_settings.Wavelength.UseAllWavelengths();
ray.ApplyAndWaitForCompletion();
ZOSAPI.Analysis.Data.IAR_ ray_results = ray.GetResults();
double native = (double)stopWatch.ElapsedMilliseconds / 1000;
sbReport.AppendLine("Native Code");
for (int field = 1; field <= max_num_field; field++)
{
// Read the results
double[,] y = new double[ray_results.DataSeries[field * 2 - 2].YData.Rows, ray_results.DataSeries[field * 2 - 2].YData.Cols];
x = ray_results.DataSeries[field * 2 - 2].XData.Data;
y = ray_results.DataSeries[field * 2 - 2].YData.Data;
for (int row = 0; row < x.Length; row++)
{
sbReport.Append(x[row].ToString() + '\t');
{
sbReport.Append(y[row, col].ToString() + '\t');
}
sbReport.Append('\n');
}
sbReport.AppendLine("");
}
// outputs execution time
Console.WriteLine("Elapsed time is {0:F4} seconds with native code", native);
Console.WriteLine("Elapsed time is {0:F4} seconds with manual code", manual);
// writes data to file
string resFile = System.IO.Path.Combine(sampleDir, "API\\CS\\e23_ray_fan_native_manual_comparison.txt");
System.IO.File.WriteAllText(resFile, sbReport.ToString());
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
// Clean up
FinishStandaloneApplication(TheApplication);
}
{
// Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases
if (TheApplication != null)
{
TheApplication.CloseApplication();
}
}
static void LogInfo(string message)
{
// TODO - add custom logging
Console.WriteLine(message);
}
static void HandleError(string errorMessage)
{
// TODO - add custom error handling
throw new Exception(errorMessage);
}
}
}
Definition: ZemaxService.cs:198
IZOSAPI_Application CreateNewApplication()
Attempts to launch a new instance of Optic Studio in 'headless' mode. Note that although the Optic St...
Definition: ZemaxService.cs:863
Properties and methods for retrieving analysis window data. This interface can be accessed via the IA...
Definition: IAR_Base.cs:21
Base interface for all analysis windows. This interface can be accessed via the I_Analyses interface.
Definition: IA_Base.cs:29
IAR_ GetResults()
Gets the result data (if available) for the current analysis.
IMessage ApplyAndWaitForCompletion()
Re-runs the analysis with the current settings and waits for it to finish calculating.
Definition: IAS_Fan.cs:24
IMessage UseAllFields()
IMessage UseAllWavelengths()
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:690
ISystemData SystemData
Data for configuring everything in the System Explorer.
Definition: Interfaces.cs:848
bool LoadFile(string LensFile, bool saveIfNeeded)
Replaces the current system with data from the specified file.
I_Analyses Analyses
Gets the analyses for the current system.
Definition: Interfaces.cs:928
IMeritFunctionEditor MFE
Gets the merit function editor.
Definition: Interfaces.cs:913
IOpticalSystemTools Tools
Gets an interface used to run various tools on the optical system.
Definition: Interfaces.cs:935
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:264
bool IsValidLicenseForAPI
Gets a value indicating whether this the API is currently useable.
Definition: Interfaces.cs:290
ZOSAPI_Mode Mode
Gets the current connetion mode. Use this to check if Optic Studio is expecting a user operand / anal...
Definition: Interfaces.cs:309
LicenseStatusType LicenseStatus
Gets the license status. Note that this displays the license edition if successful,...
Definition: Interfaces.cs:283
void CloseApplication()
Shut down the Optic Studio process.
IOpticalSystem PrimarySystem
Gets the primary system. When Mode is ZOSAPI_Mode.Server, this will initially be an empty sequential ...
Definition: Interfaces.cs:331
string SamplesDir
Gets the full path for the current user's samples directory (in the My Documents\Zemax\Samples\ folde...
Definition: Interfaces.cs:508
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
Interfaces and methods for running a ray trace on multiple rays at a time. This interface can be acce...
Definition: RayTrace.cs:239
IRayTraceNormUnpolData CreateNormUnpol(int MaxRays, RaysType rayType, int toSurface)
Performs a batch unpolarized ray trace, using normalized pupil coordinates. See the OpticStudio help ...
Definition: RayTrace.cs:624
bool AddRay(int waveNumber, double Hx, double Hy, double Px, double Py, OPDMode calcOPD)
Add a ray to be traced.
bool StartReadingResults()
void ClearData()
bool ReadNextResult(out int rayNumber, out int ErrorCode, out int vignetteCode, out double X, out double Y, out double Z, out double L, out double M, out double N, out double l2, out double m2, out double n2, out double opd, out double intensity)
Definition: IAS_Fan.cs:5
Definition: IAS_FieldCurvatureAndDistortion.cs:5
Definition: IAS_FieldCurvatureAndDistortion.cs:5
Definition: InterfacesMFE.cs:12
Definition: InterfacesEditors.cs:12
Definition: RayTrace.cs:12
Definition: FileSource.cs:4
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5