    

 ## On this page

  

 

 # Example 06 - C# 

 Last update: 17.07.2025 

# <a class="anchor" id="ex06_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);

 

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

 

 // Create new non- sequential file

 // Set up primary optical system

 TheSystem = TheApplication.[CreateNewSystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a024aef7880a5ba49d60db78b9b9140b5)([ZOSAPI](namespace_z_o_s_a_p_i.xhtml).SystemType.NonSequential);

 [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 as [INonSeqEditor](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml);

 

 // inserts objects and changes type

 [INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml) o1 = TheNCE.[GetObjectAt](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml#a4788d547c88f21153f0d2da623c3c9ec)(1) as [INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml);

 [INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml) o2 = TheNCE.[InsertNewObjectAt](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml#a93d23b9aeba9b67ea2d4690e09c64457)(2) as [INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml);

 o1.[ChangeType](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a922f21c9a14d00066023074f68b04d12)(o1.[GetObjectTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#adc6a589b37811074cc08bdd6c3ef2804)([ObjectType](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml#afaeb02568d1df3bbfabfe7d8f9b59285).SourcePoint));

 o2.[ChangeType](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a922f21c9a14d00066023074f68b04d12)(o2.[GetObjectTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#adc6a589b37811074cc08bdd6c3ef2804)([ObjectType](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml#afaeb02568d1df3bbfabfe7d8f9b59285).DetectorRectangle));

 

 // modify object's cell values in the NCE

 (([IObjectSources](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_sources.xhtml))o1.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).NumberOfAnalysisRays = (int)1e6;

 (([IObjectSources](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_sources.xhtml))o1.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).NumberOfLayoutRays = 10;

 (([IObjectSourcePoint](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_source_point.xhtml))o1.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).ConeAngle = 2.5;

 

 o2.ZPosition = 1;

 (([IObjectDetectorRectangle](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_detector_rectangle.xhtml))o2.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).XHalfWidth = 0.1;

 (([IObjectDetectorRectangle](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_detector_rectangle.xhtml))o2.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).YHalfWidth = 0.1;

 (([IObjectDetectorRectangle](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_detector_rectangle.xhtml))o2.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).NumberXPixels = 100;

 (([IObjectDetectorRectangle](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_detector_rectangle.xhtml))o2.[ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)).NumberYPixels = 100;

 

 // Setup and run the ray trace

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

 NSCRayTrace.SplitNSCRays = false;

 NSCRayTrace.ScatterNSCRays = true;

 NSCRayTrace.UsePolarization = false;

 NSCRayTrace.IgnoreErrors = true;

 NSCRayTrace.SaveRays = false;

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

 

 int det = 2;

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

 

 // extracts the irradiance data from detector

 double[,] irradiance = TheSystem.[NCE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#ab3cb797b606066a251c1aecd58e73f16).GetAllDetectorDataSafe(det, 1);

 for (int i = irradiance.GetLength(0) - 1; i &gt;= 0; i--)

 {

 for (int j = 0; j &lt; irradiance.GetLength(1); j++)

 {

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

 }

 sbReport.AppendLine("");

 }

 

 sbReport.Append("\\n");

 

 // Calculates phase data from Er &amp; Ei

 double[,] real = TheSystem.[NCE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#ab3cb797b606066a251c1aecd58e73f16).GetAllCoherentDataSafe(det, [DetectorDataType](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml#a93fa70c58cbcff63609161131c91e1ba).Real);

 double[,] imag = TheSystem.[NCE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#ab3cb797b606066a251c1aecd58e73f16).GetAllCoherentDataSafe(det, [DetectorDataType](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml#a93fa70c58cbcff63609161131c91e1ba).Imaginary);

 double[,] phase = new double[real.GetLength(0), real.GetLength(1)];

 for (int i = real.GetLength(0) - 1; i &gt;= 0; i--)

 {

 for (int j = 0; j &lt; 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](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a32d2f472d9a6d3277f3cda76afb6d85d)(TheApplication.[SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb) + "\\\\API\\\\CS\\\\e06\_nsc\_phase.zos");

 

 // 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.INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml)

All data for a Non-Sequential Component Editor object. This interface can be accessed via the INonSeq...

**Definition:** InterfacesNCE.cs:1198



[ZOSAPI.Editors.NCE.INCERow.ObjectData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a8451dfc9aeed7cc20438614fa1565392)

IObject ObjectData

**Definition:** InterfacesNCE.cs:1314



[ZOSAPI.Editors.NCE.INCERow.ChangeType](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a922f21c9a14d00066023074f68b04d12)

bool ChangeType(IObjectTypeSettings settings)

Changes the current object to the specified type. Use GetObjectTypeSettings to get the relevant setti...



[ZOSAPI.Editors.NCE.INCERow.GetObjectTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#adc6a589b37811074cc08bdd6c3ef2804)

IObjectTypeSettings GetObjectTypeSettings(ObjectType type)

Create the settings for the specified type. Use this method to specify any extra data required to cha...



[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.GetObjectAt](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml#a4788d547c88f21153f0d2da623c3c9ec)

INCERow GetObjectAt(int ObjectNumber)

Gets the object at the specified position.



[ZOSAPI.Editors.NCE.INonSeqEditor.InsertNewObjectAt](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_non_seq_editor.xhtml#a93d23b9aeba9b67ea2d4690e09c64457)

INCERow InsertNewObjectAt(int ObjectNumber)

Inserts a new object at the specified position.



[ZOSAPI.Editors.NCE.IObjectDetectorRectangle](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_detector_rectangle.xhtml)

**Definition:** InterfacesNCE.cs:3274



[ZOSAPI.Editors.NCE.IObjectSourcePoint](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_source_point.xhtml)

**Definition:** InterfacesNCE.cs:4937



[ZOSAPI.Editors.NCE.IObjectSources](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_sources.xhtml)

**Definition:** InterfacesNCE.cs:4712



[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.SaveAs](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a32d2f472d9a6d3277f3cda76afb6d85d)

void SaveAs(string fileName)

Saves the current system to the specified file. All future calls to Save will use the same 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.CreateNewSystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a024aef7880a5ba49d60db78b9b9140b5)

IOpticalSystem CreateNewSystem(SystemType mode)

Creates a new empty optical system, in the specified mode.



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

**Definition:** InterfacesNCE.cs:19



[ZOSAPI.Editors.NCE.DetectorDataType](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml#a93fa70c58cbcff63609161131c91e1ba)

DetectorDataType

**Definition:** InterfacesNCE.cs:252



[ZOSAPI.Editors.NCE.ObjectType](namespace_z_o_s_a_p_i_1_1_editors_1_1_n_c_e.xhtml#afaeb02568d1df3bbfabfe7d8f9b59285)

ObjectType

All supported non-sequential object types.

**Definition:** InterfacesNCE.cs:38



[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