    

 ## On this page

  

 

 # Example 09 - C# 

 Last update: 17.07.2025 

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

 

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

 

 // Add your custom code here...

 

 // Open new NS system and save

 TheSystem = TheApplication.[CreateNewSystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a024aef7880a5ba49d60db78b9b9140b5)([SystemType](namespace_z_o_s_a_p_i.xhtml#a3c37540f7b61a272f4eb50e826d27daf).NonSequential); // Create New NSC File

 [String](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289a27118326006d3829667a400ad23d5d98) filename = TheApplication.SamplesDir + "\\\\API\\\\CS\\\\e09\_NSC\_CAD.zos"; // Define file path and name

 // Save New NSC File

 TheSystem.[SaveAs](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a32d2f472d9a6d3277f3cda76afb6d85d)(filename);

 

 // Insert CAD object

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

 [INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml) Obj1 = NSCE.[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);

 Obj1.ZPosition = -5;

 // Set object 1 as CAD object if CAD file exists in proper directory

 if (System.IO.File.Exists(TheApplication.[ObjectsDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a16084dd03845b352533c49fc9871ff93) + "\\\\CAD Files\\\\ExtPoly.stp")) // Check if the CAD part exists in correct directory

 {

 [IObjectTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_type_settings.xhtml) Obj1_Type = Obj1.[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).CADPartSTEPIGESSAT); // create CAD object type

 Obj1_Type.FileName1 = "ExtPoly.stp"; // set CAD file to be used (file must be in directory specified under OpticStudio Preferences...Folders...Objects)

 Obj1.[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)(Obj1_Type); // Set Object 1 as the previously specified CAD file

 }

 else

 Console.WriteLine("CAD file not found");

 

 TheSystem.[Save](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a8e1c511cdf7f6048f1287532edd3aadd)();

 

 // Access the CAD settings for object 1

 [INCECADData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml) Obj1_CAD = Obj1.[CADData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a57400c66874c8006863ecfd9323d05cc); // Retrieve CAD data

 

 // Determine if the surface-to-face mappint is available for this object. 

 // If available, update the mapping based on the total number of surfaces in the system

 if (Obj1_CAD.[HasFaceData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#aa8b2630798b34e69ff1d8d0e70381c06))

 {

 if (Obj1_CAD.[NumberOfSurfaces](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#a787f89799263dbae26e2c10dff24c802) &gt; 1)

 {

 // Assign the last face to surface 0. Now, the first and last surfaces will be defined with the same scatter and coating properties

 Obj1_CAD.[SetSurfaceFace](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#ad68d3799e5463c122e524117f2874268)(0, Obj1_CAD.[NumberOfSurfaces](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#a787f89799263dbae26e2c10dff24c802) - 1);

 }

 }

 

 using (System.IO.StreamWriter POBfile = new System.IO.StreamWriter(TheApplication.[ObjectsDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a16084dd03845b352533c49fc9871ff93) + "\\\\Polygon Objects\\\\API\_cube\_demo.POB", false)) // Open new POB file

 {

 // Append new POB file with polygon definitions (see help files for syntax information)

 POBfile.WriteLine("! A cube" + "\\n");

 POBfile.WriteLine("! front face vertices" + "\\n" + "V 1 -1 -1 0" + "\\n" + "V 2 1 -1 0" + "\\n" + "V 3 1 1 0" + "\\n" + "V 4 -1 1 0" + "\\n");

 POBfile.WriteLine("! back face vertices" + "\\n" + "V 5 -1 -1 2" + "\\n" + "V 6 1 -1 2" + "\\n" + "V 7 1 1 2" + "\\n" + "V 8 -1 1 2" + "\\n");

 POBfile.WriteLine("! Front" + "\\n" + "R 1 2 3 4 0 0" + "\\n" + "! Back" + "\\n" + "R 5 6 7 8 0 0" + "\\n");

 POBfile.WriteLine("! Top" + "\\n" + "R 4 3 7 8 0 0" + "\\n" + "! Bottom" + "\\n" + "R 1 2 6 5 0 0" + "\\n");

 POBfile.WriteLine("! Left Side" + "\\n" + "R 1 4 8 5 0 0" + "\\n" + "! Right Side" + "\\n" + "R 2 3 7 6 0 0");

 [INCERow](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml) Obj2 = NSCE.[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); // Add new line to NSCE

 [IObjectTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_type_settings.xhtml) Obj2_Type = Obj2.[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).PolygonObject);

 Obj2_Type.FileName1 = "API\_cube\_demo.POB";

 Obj2.[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)(Obj2_Type);

 }

 

 // opens 3d layout

 TheSystem.[Analyses](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a830cf50c046528c752605eea3cf5e899).New_Analysis([ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Analysis](namespace_z_o_s_a_p_i_1_1_analysis.xhtml).AnalysisIDM.NSC3DLayout);

 TheSystem.[Save](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a8e1c511cdf7f6048f1287532edd3aadd)(); // Save the file

 

 Console.WriteLine("Press any key to continue...");

 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.Editors.NCE.INCECADData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml)

Object properties CAD tab. This interface can be accessed via the INCERow interface.

**Definition:** InterfacesNCE.cs:6972



[ZOSAPI.Editors.NCE.INCECADData.NumberOfSurfaces](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#a787f89799263dbae26e2c10dff24c802)

int NumberOfSurfaces

**Definition:** InterfacesNCE.cs:7010



[ZOSAPI.Editors.NCE.INCECADData.HasFaceData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#aa8b2630798b34e69ff1d8d0e70381c06)

bool HasFaceData

**Definition:** InterfacesNCE.cs:7009



[ZOSAPI.Editors.NCE.INCECADData.SetSurfaceFace](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_c_a_d_data.xhtml#ad68d3799e5463c122e524117f2874268)

bool SetSurfaceFace(int surfaceIdx, int face)

Sets the face assigned to the specified surface.



[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.CADData](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_n_c_e_row.xhtml#a57400c66874c8006863ecfd9323d05cc)

INCECADData CADData

Gets the object CAD data.

**Definition:** InterfacesNCE.cs:1416



[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.IObjectTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_n_c_e_1_1_i_object_type_settings.xhtml)

This interface is used for selecting any files required by a object, and for changing object types....

**Definition:** InterfacesNCE.cs:1110



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

I_Analyses Analyses

Gets the analyses for the current system.

**Definition:** Interfaces.cs:925



[ZOSAPI.IOpticalSystem.Save](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a8e1c511cdf7f6048f1287532edd3aadd)

void Save()

Saves the current system.



[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.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.ObjectsDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a16084dd03845b352533c49fc9871ff93)

string ObjectsDir

Gets the Objects directory (e.g. C:\Users\John.Doe\My Documents\Zemax\OBJECTS).

**Definition:** Interfaces.cs:408



[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.Analysis](namespace_z_o_s_a_p_i_1_1_analysis.xhtml)

**Definition:** IAS_FieldCurvatureAndDistortion.cs:5



[ZOSAPI.Common.SettingsDataType.String](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289a27118326006d3829667a400ad23d5d98)

@ String



[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.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



[ZOSAPI.SystemType](namespace_z_o_s_a_p_i.xhtml#a3c37540f7b61a272f4eb50e826d27daf)

SystemType

**Definition:** Interfaces.cs:22