Skip to main content

ZOS-API interface 2024 R2 SP02

Example 05 - C#

Last update: 17.07.2025

C#

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...
// Open file and set Analysis Ryas to only 10
string testFile = TheApplication.SamplesDir + "\\Non-sequential\\Miscellaneous\\Digital_projector_flys_eye_homogenizer.zos";
TheSystem.LoadFile(testFile, false);
TheSystem.NCE.GetObjectAt(1).GetObjectCell(ZOSAPI.Editors.NCE.ObjectColumn.Par2).IntegerValue = 10;
// Trace and save a ZRD file for test later
INSCRayTrace NSCRayTrace = TheSystem.Tools.OpenNSCRayTrace();
NSCRayTrace.SplitNSCRays = true;
NSCRayTrace.ScatterNSCRays = false;
NSCRayTrace.UsePolarization = true;
NSCRayTrace.IgnoreErrors = true;
NSCRayTrace.SaveRays = true;
NSCRayTrace.SaveRaysFile = "Digital_projector_flys_eye_homogenizer.ZRD";
NSCRayTrace.ClearDetectors(0);
NSCRayTrace.RunAndWaitForCompletion();
NSCRayTrace.Close();
// Open ZRD Reader and read data
IZRDReader ZRDReader = TheSystem.Tools.OpenRayDatabaseReader();
ZRDReader.ZRDFile = System.String.Concat(TheApplication.SamplesDir, @"\Non-sequential\Miscellaneous\Digital_projector_flys_eye_homogenizer.ZRD");
if (ZRDReader.Succeeded == false)
{
Console.WriteLine("Raytracing failed!\n\n");
Console.WriteLine(ZRDReader.ErrorMessage);
}
else
{
Console.WriteLine("Raytracing completed!\n\n");
}
IZRDReaderResults ZRDResult = ZRDReader.GetResults();
Boolean success_NextResult, success_NextSegmentFull;
Int32 rayNumber, waveIndex, numSegments;
Double wlUM;
Int32 segmentLevel, segmentParent, hitObj, hitFace, insideOf, xybin, lmbin;
Double x, y, z, l, m, n, exr, exi, eyr, eyi, ezr, ezi, intensity, pathLength, xNorm, yNorm, zNorm, index, startingPhase, phaseOf, phaseAt;
RayStatus status;
// ReadNExtResult() returns data ray by ray
success_NextResult = ZRDResult.ReadNextResult(out rayNumber, out waveIndex, out wlUM, out numSegments);
while (success_NextResult)
{
Console.WriteLine("\n\nsuccess_NextResult: {0}, rayNumber: {1}, waveIndex: {2}, wlUM: {3}, numSegments: {4}\n\n",
success_NextResult, rayNumber, waveIndex, wlUM, numSegments);
// ReadNextSegmentFull() returns data segment by segment
success_NextSegmentFull = ZRDResult.ReadNextSegmentFull(out segmentLevel, out segmentParent, out hitObj, out hitFace, out insideOf,
out status, out x, out y, out z, out l, out m, out n, out exr, out exi, out eyr, out eyi, out ezr, out ezi,
out intensity, out pathLength, out xybin, out lmbin, out xNorm, out yNorm, out zNorm, out index, out startingPhase, out phaseOf,
out phaseAt);
while (success_NextSegmentFull)
{
Console.WriteLine(@"success_NextSegmentFull: {0}, segmentLevel: {1}, segmentParent: {2},
hitObj: {3}, hitFace: {4}, insideOf: {5}, status: {6},
x: {7}, y: {8}, z: {9},
l: {10}, m: {11}, n: {12},
exr: {13}, exi: {14},
eyr: {15}, eyi: {16},
ezr: {17}, ezi: {18},
intensity: {19}, pathLength: {20},
xybin: {21}, lmbin: {22},
xNorm: {23}, yNorm: {24}, zNorm: {25},
index: {26}, startingPhase: {27},
phaseOf: {28}, phaseAt: {29}
",
success_NextSegmentFull, segmentLevel, segmentParent, hitObj, hitFace, insideOf, status,
x, y, z, l, m, n, exr, exi, eyr, eyi, ezr, ezi, intensity, pathLength,
xybin, lmbin, xNorm, yNorm, zNorm, index, startingPhase, phaseOf, phaseAt);
success_NextSegmentFull = ZRDResult.ReadNextSegmentFull(out segmentLevel, out segmentParent, out hitObj, out hitFace, out insideOf,
out status, out x, out y, out z, out l, out m, out n, out exr, out exi, out eyr, out eyi, out ezr, out ezi,
out intensity, out pathLength, out xybin, out lmbin, out xNorm, out yNorm, out zNorm, out index, out startingPhase, out phaseOf,
out phaseAt);
}
success_NextResult = ZRDResult.ReadNextResult(out rayNumber, out waveIndex, out wlUM, out numSegments);
}
ZRDReader.Close();
Console.ReadKey();
// 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:864
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:687
bool LoadFile(string LensFile, bool saveIfNeeded)
Replaces the current system with data from the specified file.
INonSeqEditor NCE
Gets the non-sequential component editor.
Definition: Interfaces.cs:884
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.
bool Close()
Closes this tool and frees up and associated resources.
string ErrorMessage
If Succeeded is false, get the error messages; otherwise, null.
Definition: Tools.cs:691
bool Succeeded
Gets a value indicating whether the tool execution succeeded.
Definition: Tools.cs:684
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: RayTrace.cs:1041
bool ReadNextResult(out int rayNumber, out int waveIndex, out double wlUM, out int numSegments)
bool ReadNextSegmentFull(out int segmentLevel, out int segmentParent, out int hitObj, out int hitFace, out int insideOf, out RayStatus status, out double x, out double y, out double z, out double l, out double m, out double n, out double exr, out double exi, out double eyr, out double eyi, out double ezr, out double ezi, out double intensity, out double pathLength, out int xybin, out int lmbin, out double xNorm, out double yNorm, out double zNorm, out double index, out double startingPhase, out double phaseOf, out double phaseAt)
Definition: RayTrace.cs:1031
IZRDReaderResults GetResults()
Definition: InterfacesNCE.cs:19
Definition: InterfacesEditors.cs:12
Definition: RayTrace.cs:12
RayStatus
Definition: RayTrace.cs:1153
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5
ZOSAPI_Mode
Definition: Interfaces.cs:77

Connect with Ansys