    

 ## On this page

  

 

 # Example 05 - C# 

 Last update: 16.07.2025 

# <a class="anchor" id="ex05_s1"></a>C#

using System;

using [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

using [ZOSAPI.Tools.RayTrace](namespace_z_o_s_a_p_i_1_1_tools_1_1_ray_trace.xhtml);

 

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](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml) TheConnection = new [ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)();

 

 // Attempt to create a Standalone connection

 [IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication = TheConnection.[CreateNewApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aadc54af7d7415a765592acf23d3a08ef)();

 if (TheApplication == null)

 {

 HandleError("An unknown connection error occurred!");

 return;

 }

 

 // Check the connection status

 if (!TheApplication.[IsValidLicenseForAPI](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a5b8d6c216c668d13d6dcb870698c247f))

 {

 HandleError("Failed to connect to OpticStudio: " + TheApplication.[LicenseStatus](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a95179eb8038b629caa12397723aacb3a));

 return;

 }

 if (TheApplication.[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19) != [ZOSAPI\_Mode](namespace_z_o_s_a_p_i.xhtml#a254f5f6cb4ff37c2245b456b9bb79d2a).Server)

 {

 HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19).ToString());

 return;

 }

 

 [IOpticalSystem](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml) TheSystem = TheApplication.[PrimarySystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#aca8045fe84351622e0ee58814a2a3364);

 

 // 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](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a6eb1455ac53555ae6f30032d85b63735)(testFile, false);

 TheSystem.[NCE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#ab3cb797b606066a251c1aecd58e73f16).GetObjectAt(1).GetObjectCell([ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Editors](namespace_z_o_s_a_p_i_1_1_editors.xhtml).[NCE](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml).ObjectColumn.Par2).IntegerValue = 10;

 

 // Trace and save a ZRD file for test later

 [INSCRayTrace](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_n_s_c_ray_trace.xhtml) NSCRayTrace = TheSystem.[Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8).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](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_n_s_c_ray_trace.xhtml#a4b118fa79a1c9056ded4d08c34549914)(0);

 NSCRayTrace.[RunAndWaitForCompletion](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a26d8cae18772fd31c1d18525a3b8d9ac)();

 NSCRayTrace.[Close](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a5bfa126385b9d0c5619b1aff9a354d31)();

 

 

 

 // Open ZRD Reader and read data

 [IZRDReader](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader.xhtml) ZRDReader = TheSystem.[Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8).OpenRayDatabaseReader();

 ZRDReader.ZRDFile = System.String.Concat(TheApplication.[SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb), @"\\Non-sequential\\Miscellaneous\\Digital\_projector\_flys\_eye\_homogenizer.ZRD");

 ZRDReader.[RunAndWaitForCompletion](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a26d8cae18772fd31c1d18525a3b8d9ac)();

 if (ZRDReader.[Succeeded](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#af1cc4f2a44ac378669ae576f332cc5de) == false)

 {

 Console.WriteLine("Raytracing failed!\\n\\n");

 Console.WriteLine(ZRDReader.[ErrorMessage](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#abb853f5e7c3b45ad5c17655ae4fcff24));

 }

 else

 {

 Console.WriteLine("Raytracing completed!\\n\\n");

 }

 

 [IZRDReaderResults](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml) ZRDResult = ZRDReader.[GetResults](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader.xhtml#a802d71c1d6a4d8fb5883b0a5e9d06699)();

 

 [Boolean](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289a27226c864bac7454a8504f8edb15d95b) success_NextResult, success_NextSegmentFull;

 Int32 rayNumber, waveIndex, numSegments;

 [Double](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289ad909d38d705ce75386dd86e611a82f5b) wlUM;

 

 Int32 segmentLevel, segmentParent, hitObj, hitFace, insideOf, xybin, lmbin;

 [Double](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289ad909d38d705ce75386dd86e611a82f5b) x, y, z, l, m, n, exr, exi, eyr, eyi, ezr, ezi, intensity, pathLength, xNorm, yNorm, zNorm, index, startingPhase, phaseOf, phaseAt;

 [RayStatus](namespace_z_o_s_a_p_i_1_1_tools_1_1_ray_trace.xhtml#a6c300f6f57a84075c1f63585133762eb) status;

 

 // ReadNExtResult() returns data ray by ray

 success_NextResult = ZRDResult.[ReadNextResult](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml#a7f06710879734210b571873a024ea25f)(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](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml#ac008883112947c62a59c18e8cc7c5adc)(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](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml#ac008883112947c62a59c18e8cc7c5adc)(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](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml#a7f06710879734210b571873a024ea25f)(out rayNumber, out waveIndex, out wlUM, out numSegments);

 }

 ZRDReader.[Close](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a5bfa126385b9d0c5619b1aff9a354d31)();

 

 Console.ReadKey();

 

 

 

 // Clean up

 FinishStandaloneApplication(TheApplication);

 }

 

 static void FinishStandaloneApplication([IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication)

 {

 // Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases

 if (TheApplication != null)

 {

 TheApplication.[CloseApplication](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ab662a3cc1b97d8632a324b1137fa943b)();

 }

 }

 

 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);

 }

 

 }

}

[ZOSAPI.ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)

**Definition:** ZemaxService.cs:198



[ZOSAPI.ZOSAPI\_Connection.CreateNewApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aadc54af7d7415a765592acf23d3a08ef)

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



[ZOSAPI.IOpticalSystem](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml)

Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....

**Definition:** Interfaces.cs:687



[ZOSAPI.IOpticalSystem.LoadFile](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a6eb1455ac53555ae6f30032d85b63735)

bool LoadFile(string LensFile, bool saveIfNeeded)

Replaces the current system with data from the specified file.



[ZOSAPI.IOpticalSystem.NCE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#ab3cb797b606066a251c1aecd58e73f16)

INonSeqEditor NCE

Gets the non-sequential component editor.

**Definition:** Interfaces.cs:884



[ZOSAPI.IOpticalSystem.Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8)

IOpticalSystemTools Tools

Gets an interface used to run various tools on the optical system.

**Definition:** Interfaces.cs:932



[ZOSAPI.IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml)

This interface contains all information about the current ZOS-API connection, as well as methods for ...

**Definition:** Interfaces.cs:261



[ZOSAPI.IZOSAPI\_Application.IsValidLicenseForAPI](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a5b8d6c216c668d13d6dcb870698c247f)

bool IsValidLicenseForAPI

Gets a value indicating whether this the API is currently useable.

**Definition:** Interfaces.cs:287



[ZOSAPI.IZOSAPI\_Application.Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19)

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



[ZOSAPI.IZOSAPI\_Application.LicenseStatus](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a95179eb8038b629caa12397723aacb3a)

LicenseStatusType LicenseStatus

Gets the license status. Note that this displays the license edition if successful,...

**Definition:** Interfaces.cs:280



[ZOSAPI.IZOSAPI\_Application.CloseApplication](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ab662a3cc1b97d8632a324b1137fa943b)

void CloseApplication()

Shut down the Optic Studio process.



[ZOSAPI.IZOSAPI\_Application.PrimarySystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#aca8045fe84351622e0ee58814a2a3364)

IOpticalSystem PrimarySystem

Gets the primary system. When Mode is ZOSAPI_Mode.Server, this will initially be an empty sequential ...

**Definition:** Interfaces.cs:328



[ZOSAPI.IZOSAPI\_Application.SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb)

string SamplesDir

Gets the full path for the current user's samples directory (in the My Documents\Zemax\Samples\ folde...

**Definition:** Interfaces.cs:505



[ZOSAPI.Tools.ISystemTool.RunAndWaitForCompletion](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a26d8cae18772fd31c1d18525a3b8d9ac)

bool RunAndWaitForCompletion()

Sames as calling Run followed by WaitForCompletion.



[ZOSAPI.Tools.ISystemTool.Close](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a5bfa126385b9d0c5619b1aff9a354d31)

bool Close()

Closes this tool and frees up and associated resources.



[ZOSAPI.Tools.ISystemTool.ErrorMessage](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#abb853f5e7c3b45ad5c17655ae4fcff24)

string ErrorMessage

If Succeeded is false, get the error messages; otherwise, null.

**Definition:** Tools.cs:698



[ZOSAPI.Tools.ISystemTool.Succeeded](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#af1cc4f2a44ac378669ae576f332cc5de)

bool Succeeded

Gets a value indicating whether the tool execution succeeded.

**Definition:** Tools.cs:691



[ZOSAPI.Tools.RayTrace.INSCRayTrace](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_n_s_c_ray_trace.xhtml)

Interfaces and methods for running a non-sequential ray trace. This interface can be accessed via the...

**Definition:** RayTrace.cs:45



[ZOSAPI.Tools.RayTrace.INSCRayTrace.ClearDetectors](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_n_s_c_ray_trace.xhtml#a4b118fa79a1c9056ded4d08c34549914)

ErrorType ClearDetectors(int DetectorNumber)

Clears the specified detectors.



[ZOSAPI.Tools.RayTrace.IZRDReaderResults](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml)

**Definition:** RayTrace.cs:1041



[ZOSAPI.Tools.RayTrace.IZRDReaderResults.ReadNextResult](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml#a7f06710879734210b571873a024ea25f)

bool ReadNextResult(out int rayNumber, out int waveIndex, out double wlUM, out int numSegments)



[ZOSAPI.Tools.RayTrace.IZRDReaderResults.ReadNextSegmentFull](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader_results.xhtml#ac008883112947c62a59c18e8cc7c5adc)

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)



[ZOSAPI.Tools.RayTrace.IZRDReader](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader.xhtml)

**Definition:** RayTrace.cs:1031



[ZOSAPI.Tools.RayTrace.IZRDReader.GetResults](interface_z_o_s_a_p_i_1_1_tools_1_1_ray_trace_1_1_i_z_r_d_reader.xhtml#a802d71c1d6a4d8fb5883b0a5e9d06699)

IZRDReaderResults GetResults()



[ZOSAPI.Common.SettingsDataType.Boolean](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289a27226c864bac7454a8504f8edb15d95b)

@ Boolean



[ZOSAPI.Common.SettingsDataType.Double](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289ad909d38d705ce75386dd86e611a82f5b)

@ Double



[ZOSAPI.Editors.NCE](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml)

**Definition:** InterfacesNCE.cs:19



[ZOSAPI.Editors](namespace_z_o_s_a_p_i_1_1_editors.xhtml)

**Definition:** InterfacesEditors.cs:12



[ZOSAPI.Tools.RayTrace](namespace_z_o_s_a_p_i_1_1_tools_1_1_ray_trace.xhtml)

**Definition:** RayTrace.cs:12



[ZOSAPI.Tools.RayTrace.RayStatus](namespace_z_o_s_a_p_i_1_1_tools_1_1_ray_trace.xhtml#a6c300f6f57a84075c1f63585133762eb)

RayStatus

**Definition:** RayTrace.cs:1153



[ZOSAPI](namespace_z_o_s_a_p_i.xhtml)

The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...

**Definition:** IAS_FieldCurvatureAndDistortion.cs:5



[ZOSAPI.ZOSAPI\_Mode](namespace_z_o_s_a_p_i.xhtml#a254f5f6cb4ff37c2245b456b9bb79d2a)

ZOSAPI_Mode

**Definition:** Interfaces.cs:77