Skip to main content

ZOS-API interface 2024 R1

CSharpStandalone_06_nsc_phase

Last update: 17.07.2025
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
ZOSAPI_Connection TheConnection = new ZOSAPI_Connection();
// 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;
}
if (TheApplication.Mode != ZOSAPI_Mode.Server)
{
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
return;
}
IOpticalSystem TheSystem = TheApplication.PrimarySystem;
// Add your custom code here...
// creates new directory
string strPath = System.IO.Path.Combine(TheApplication.SamplesDir, @"API\CS");
System.IO.Directory.CreateDirectory(strPath);
// Create new non- sequential file
// Set up primary optical system
TheSystem = TheApplication.CreateNewSystem(ZOSAPI.SystemType.NonSequential);
INonSeqEditor TheNCE = TheSystem.NCE as INonSeqEditor;
// inserts objects and changes type
INCERow o1 = TheNCE.GetObjectAt(1) as INCERow;
INCERow o2 = TheNCE.InsertNewObjectAt(2) as INCERow;
o2.ChangeType(o2.GetObjectTypeSettings(ObjectType.DetectorRectangle));
// modify object's cell values in the NCE
((IObjectSources)o1.ObjectData).NumberOfAnalysisRays = (int)1e6;
((IObjectSources)o1.ObjectData).NumberOfLayoutRays = 10;
((IObjectSourcePoint)o1.ObjectData).ConeAngle = 2.5;
o2.ZPosition = 1;
((IObjectDetectorRectangle)o2.ObjectData).XHalfWidth = 0.1;
((IObjectDetectorRectangle)o2.ObjectData).YHalfWidth = 0.1;
((IObjectDetectorRectangle)o2.ObjectData).NumberXPixels = 100;
((IObjectDetectorRectangle)o2.ObjectData).NumberYPixels = 100;
// Setup and run the ray trace
ZOSAPI.Tools.RayTrace.INSCRayTrace NSCRayTrace = TheSystem.Tools.OpenNSCRayTrace();
NSCRayTrace.SplitNSCRays = false;
NSCRayTrace.ScatterNSCRays = true;
NSCRayTrace.UsePolarization = false;
NSCRayTrace.IgnoreErrors = true;
NSCRayTrace.SaveRays = false;
NSCRayTrace.ClearDetectors(0);
NSCRayTrace.RunAndWaitForCompletion();
NSCRayTrace.Close();
int det = 2;
System.Text.StringBuilder sbReport = new System.Text.StringBuilder();
// extracts the irradiance data from detector
double[,] irradiance = TheSystem.NCE.GetAllDetectorDataSafe(det, 1);
for (int i = irradiance.GetLength(0) - 1; i >= 0; i--)
{
for (int j = 0; j < irradiance.GetLength(1); j++)
{
sbReport.Append(irradiance[i, j] + "\t");
}
sbReport.AppendLine("");
}
sbReport.Append("\n");
// Calculates phase data from Er & Ei
double[,] real = TheSystem.NCE.GetAllCoherentDataSafe(det, DetectorDataType.Real);
double[,] imag = TheSystem.NCE.GetAllCoherentDataSafe(det, DetectorDataType.Imaginary);
double[,] phase = new double[real.GetLength(0), real.GetLength(1)];
for (int i = real.GetLength(0) - 1; i >= 0; i--)
{
for (int j = 0; j < real.GetLength(1); j++)
{
phase[i, j] = Math.Atan2(imag[i, j], real[i, j]) * 180 / Math.PI;
sbReport.Append(phase[i, j] + "\t");
}
sbReport.AppendLine("");
}
string resFile = TheApplication.SamplesDir + "\\API\\CS\\e06_nsc_phase.txt";
System.IO.File.WriteAllText(resFile, sbReport.ToString());
TheSystem.SaveAs(TheApplication.SamplesDir + "\\API\\CS\\e06_nsc_phase.zos");
// Clean up
FinishStandaloneApplication(TheApplication);
}
static void FinishStandaloneApplication(IZOSAPI_Application 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
All data for a Non-Sequential Component Editor object. This interface can be accessed via the INonSeq...
Definition: InterfacesNCE.cs:1198
IObject ObjectData
Definition: InterfacesNCE.cs:1314
bool ChangeType(IObjectTypeSettings settings)
Changes the current object to the specified type. Use GetObjectTypeSettings to get the relevant setti...
IObjectTypeSettings GetObjectTypeSettings(ObjectType type)
Create the settings for the specified type. Use this method to specify any extra data required to cha...
This interface defines all properties and methods needed to interact with the Non-Sequential Componen...
Definition: InterfacesNCE.cs:363
INCERow GetObjectAt(int ObjectNumber)
Gets the object at the specified position.
INCERow InsertNewObjectAt(int ObjectNumber)
Inserts a new object at the specified position.
Definition: InterfacesNCE.cs:3274
Definition: InterfacesNCE.cs:4937
Definition: InterfacesNCE.cs:4712
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:690
void SaveAs(string fileName)
Saves the current system to the specified file. All future calls to Save will use the same file.
INonSeqEditor NCE
Gets the non-sequential component editor.
Definition: Interfaces.cs:887
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
IOpticalSystem CreateNewSystem(SystemType mode)
Creates a new empty optical system, in the specified mode.
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.
bool Close()
Closes this tool and frees up and associated resources.
Interfaces and methods for running a non-sequential ray trace. This interface can be accessed via the...
Definition: RayTrace.cs:45
ErrorType ClearDetectors(int DetectorNumber)
Clears the specified detectors.
Definition: InterfacesNCE.cs:19
DetectorDataType
Definition: InterfacesNCE.cs:252
ObjectType
All supported non-sequential object types.
Definition: InterfacesNCE.cs:38
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5
ZOSAPI_Mode
Definition: Interfaces.cs:80

Connect with Ansys