    

 ## On this page

  

 

 # Example 02 - C# 

 Last update: 17.07.2025 

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

using System;

using [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

using [ZOSAPI.Editors.NCE](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.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...

 

 // creates a new API directory

 string strPath = System.IO.Path.Combine(TheApplication.[SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb), @"API\\CS");

 System.IO.Directory.CreateDirectory(strPath);

 

 // Set up primary optical system

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

 string sampleDir = TheApplication.[SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb);

 

 // Open file

 string testFile = sampleDir + "\\\\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);

 

 // Create ray trace

 [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 = false;

 

 // Run ray trace

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

 

 // Non-sequential component editor

 [INonSeqEditor](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml) TheNCE = TheSystem.[NCE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#ab3cb797b606066a251c1aecd58e73f16);

 

 // Get detector data

 System.Text.StringBuilder sbReport = new System.Text.StringBuilder();

 double[,] detectorData = new double[100, 100];

 for (int i = 0; i &lt; 99; i++)

 {

 for (int j = 0; j &lt; 99; j++)

 {

 TheNCE.[GetDetectorData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml#a89995c8b4a327b293c4205719ac91ca0)(4, j + i * 100, 1, out detectorData[i, j]);

 sbReport.Append(detectorData[i, j].ToString() + "\\t");

 }

 sbReport.AppendLine("");

 }

 string resFile = TheApplication.SamplesDir + "\\\\API\\\\CS\\\\e02\_NSC\_ray\_trace.txt";

 System.IO.File.WriteAllText(resFile, sbReport.ToString());

 

 // 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.Editors.NCE.INonSeqEditor](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml)

This interface defines all properties and methods needed to interact with the Non-Sequential Componen...

**Definition:** InterfacesNCE.cs:363



[ZOSAPI.Editors.NCE.INonSeqEditor.GetDetectorData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml#a89995c8b4a327b293c4205719ac91ca0)

bool GetDetectorData(int ObjectNumber, int pixel, int Data, out double Value)

Gets data from a NSC detector.



[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.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.Editors.NCE](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml)

**Definition:** InterfacesNCE.cs:19



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

**Definition:** RayTrace.cs:12



[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