CSharpStandalone_22_seq_spot_diagram
Last update: 17.07.2025using 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
if (TheApplication == null)
{
HandleError("An unknown connection error occurred!");
return;
}
// Check the connection status
{
return;
}
{
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
return;
}
// 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");
// Set up Batch Ray Trace
int max_rays = 30;
ZOSAPI.Tools.RayTrace.IRayTraceNormUnpolData normUnPolData = raytrace.CreateNormUnpol((max_rays + 1) * (max_rays + 1), ZOSAPI.Tools.RayTrace.RaysType.Real, nsur);
// Define batch ray trace constants
double hx = 0.0;
int num_fields = TheSystem.SystemData.Fields.NumberOfFields;
double[] hy_ary = new double[] { 0, 0.707, 1 };
// Initialize x/y image plane arrays
double[,,] x_ary = new double[num_fields, max_wave, (max_rays + 1) * (max_rays + 1)];
double[,,] y_ary = new double[num_fields, max_wave, (max_rays + 1) * (max_rays + 1)];
// Determine maximum field in Y only
double max_field = 0.0;
for (int i = 1; i <= num_fields; i++)
{
if (TheSystem.SystemData.Fields.GetField(i).Y > max_field)
{
max_field = TheSystem.SystemData.Fields.GetField(i).Y;
}
}
Random rand = new Random();
System.Text.StringBuilder sbReport = new System.Text.StringBuilder();
sbReport.AppendLine("Field \t Wave \t x \t y");
for (int field = 1; field <= num_fields; field++)
{
for (int wave = 1; wave <= max_wave; wave++)
{
// Adding Rays to Batch, varying normalised object height hy
normUnPolData.ClearData();
int waveNumber = wave;
for (int i = 1; i <= ((max_rays + 1) * (max_rays + 1)); i++)
{
double px, py;
px = rand.NextDouble() * 2 - 1;
py = rand.NextDouble() * 2 - 1;
while ((Math.Pow(px, 2) + Math.Pow(py, 2)) > 1)
{
py = rand.NextDouble() * 2 - 1;
}
normUnPolData.AddRay(waveNumber, hx, hy_ary[field - 1], px, py, ZOSAPI.Tools.RayTrace.OPDMode.None);
}
// 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))
{
x_ary[field - 1, wave - 1, rayNumber - 1] = double_X;
y_ary[field - 1, wave - 1, rayNumber - 1] = double_Y;
sbReport.AppendLine((field).ToString() + "\t" + (wave).ToString() + "\t" + double_X.ToString() + "\t" + double_Y.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("");
}
}
string resFile = System.IO.Path.Combine(sampleDir, "API\\CS\\e22_seq_spot_diagram.txt");
System.IO.File.WriteAllText(resFile, sbReport.ToString());
// Spot Diagram Analysis Results
ZOSAPI.Analysis.IA_ spot = TheSystem.Analyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.StandardSpot);
ZOSAPI.Analysis.Settings.Spot.IAS_Spot spot_setting = (ZOSAPI.Analysis.Settings.Spot.IAS_Spot)spot.GetSettings();
// Extract RMS & Geo spot size for field points
spot.ApplyAndWaitForCompletion();
Console.WriteLine("RMS radius: {0:F3} {1:F3} {2:F3}", spot_results.SpotData.GetRMSSpotSizeFor(1, 1), spot_results.SpotData.GetRMSSpotSizeFor(2, 1), spot_results.SpotData.GetRMSSpotSizeFor(3, 1));
Console.WriteLine("GEO radius: {0:F3} {1:F3} {2:F3}", spot_results.SpotData.GetGeoSpotSizeFor(1, 1), spot_results.SpotData.GetGeoSpotSizeFor(2, 1), spot_results.SpotData.GetGeoSpotSizeFor(3, 1));
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:864
double GetRMSSpotSizeFor(int fieldN, int waveN)
double GetGeoSpotSizeFor(int fieldN, int waveN)
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.
IMessage SetFieldNumber(int N)
IMessage SetWavelengthNumber(int N)
Definition: IAS_Spot.cs:50
IAS_Wavelength Wavelength
Definition: IAS_Spot.cs:53
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:687
ISystemData SystemData
Data for configuring everything in the System Explorer.
Definition: Interfaces.cs:845
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:925
IOpticalSystemTools Tools
Gets an interface used to run various tools on the optical system.
Definition: Interfaces.cs:932
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:261
bool IsValidLicenseForAPI
Gets a value indicating whether this the API is currently useable.
Definition: Interfaces.cs:287
ZOSAPI_Mode Mode
Gets the current connetion mode. Use this to check if Optic Studio is expecting a user operand / anal...
Definition: Interfaces.cs:306
LicenseStatusType LicenseStatus
Gets the license status. Note that this displays the license edition if successful,...
Definition: Interfaces.cs:280
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:328
string SamplesDir
Gets the full path for the current user's samples directory (in the My Documents\Zemax\Samples\ folde...
Definition: Interfaces.cs:505
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_Spot.cs:5
Definition: IAS_FieldCurvatureAndDistortion.cs:5
Definition: IAS_FieldCurvatureAndDistortion.cs:5
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