    

 ## On this page

  

 

 # Example 12 - C# 

 Last update: 16.07.2025 

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

using System;

using [ZOSAPI](namespace_z_o_s_a_p_i.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);

 

 // Select 6 wavelengths using a Gaussian Quadrature algorithm

 [ZOSAPI.SystemData.IWavelengths](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_wavelengths.xhtml) sysWave = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).Wavelengths;

 sysWave.[GaussianQuadrature](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_wavelengths.xhtml#a8a2bf14cb6a1f3519592e6cf5406641e)(0.45, 0.65, [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[SystemData](namespace_z_o_s_a_p_i_1_1_system_data.xhtml).QuadratureSteps.S6);

 

 // Define fields using Paraxial Image Height

 [ZOSAPI.SystemData.IFields](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_fields.xhtml) sysField = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).Fields;

 sysField.[SetFieldType](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_fields.xhtml#a8b5cd1dbb8c9b4889f3ba1ff1653dbc4)([ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[SystemData](namespace_z_o_s_a_p_i_1_1_system_data.xhtml).FieldType.ParaxialImageHeight);

 

 // Change field 1 to be X=1.0 and Y=2.0

 [ZOSAPI.SystemData.IField](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_field.xhtml) field1 = sysField.[GetField](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_fields.xhtml#af2d2ae99d7ecf6e5866de571043f907d)(1);

 field1.X = 1.0;

 field1.Y = 2.0;

 

 // Inserts paraxial lens so there will not be an error when using ParaxialImageHeight

 TheSystem.SystemData.Aperture.ApertureValue = 10;

 [ZOSAPI.Editors.LDE.ILDERow](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml) s1 = TheSystem.[LDE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a942995136e3c3cf875cc1fde38d33477).GetSurfaceAt(1) as [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Editors](namespace_z_o_s_a_p_i_1_1_editors.xhtml).[LDE](namespace_z_o_s_a_p_i_1_1_editors_1_1_l_d_e.xhtml).[ILDERow](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml);

 [ZOSAPI.Editors.LDE.ISurfaceTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_surface_type_settings.xhtml) s1_type = s1.[GetSurfaceTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml#a664a633a9e227c6c637aaa86ca72f159)([ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Editors](namespace_z_o_s_a_p_i_1_1_editors.xhtml).[LDE](namespace_z_o_s_a_p_i_1_1_editors_1_1_l_d_e.xhtml).SurfaceType.Paraxial) as [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Editors](namespace_z_o_s_a_p_i_1_1_editors.xhtml).[LDE](namespace_z_o_s_a_p_i_1_1_editors_1_1_l_d_e.xhtml).[ISurfaceTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_surface_type_settings.xhtml);

 s1.[ChangeType](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml#a6ec5d7cedce07c2f4695061cd46cc6b8)(s1_type);

 s1.Thickness = 100;

 

 // Change polarization axis reference to be Y

 [ZOSAPI.SystemData.ISDPolarizationData](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_polarization_data.xhtml) sysPol = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).Polarization;

 sysPol.Method = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[SystemData](namespace_z_o_s_a_p_i_1_1_system_data.xhtml).PolarizationMethod.YAxisMethod;

 

 // Add Corning catalog and remove Schott catalog

 [ZOSAPI.SystemData.ISDMaterialCatalogData](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_material_catalog_data.xhtml) sysCat = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).MaterialCatalogs;

 sysCat.[AddCatalog](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_material_catalog_data.xhtml#ae348d808484e0c5a4193b164ce0a802a)("Corning");

 sysCat.[RemoveCatalog](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_material_catalog_data.xhtml#a97c9ef7850795c75aeb80366dbc75383)("Schott");

 

 // Add Title and Notes

 [ZOSAPI.SystemData.ISDTitleNotes](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_title_notes.xhtml) sysTitleNotes = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).TitleNotes;

 sysTitleNotes.Title = "Add here the title";

 sysTitleNotes.Notes = "Add here the notes";

 

 // As default Files choose: COATING.DAT, SCATTER\_PROFILE.DAT, ABG\_DATA.DAT 

 [ZOSAPI.SystemData.ISDFiles](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_files.xhtml) sysFiles = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).Files;

 sysFiles.CoatingFile = "COATING.DAT";

 sysFiles.ScatterProfile = "SCATTER\_PROFILE.DAT";

 sysFiles.ABgDataFile = "ABG\_DATA.DAT";

 TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).Files.ReloadFiles();

 

 // Change Lens Units to Inches

 [ZOSAPI.SystemData.ISDUnitsData](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_units_data.xhtml) sysUnits = TheSystem.[SystemData](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a4c4e9771e9ea51766885fdd6fb6627f5).Units;

 sysUnits.LensUnits = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[SystemData](namespace_z_o_s_a_p_i_1_1_system_data.xhtml).ZemaxSystemUnits.Inches;

 

 [String](namespace_z_o_s_a_p_i_1_1_common.xhtml#a3f54742c5ea7ea3449f67a65dea61289a27118326006d3829667a400ad23d5d98) file = "\\\\API\\\\CS\\\\e12\_seq\_system\_explorer.zos";

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

 

 // 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.LDE.ILDERow](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml)

All data for a Lens Data Editor surface. This interface can be accessed via the ILensDataEditor inter...

**Definition:** InterfacesLDE.cs:791



[ZOSAPI.Editors.LDE.ILDERow.GetSurfaceTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml#a664a633a9e227c6c637aaa86ca72f159)

ISurfaceTypeSettings GetSurfaceTypeSettings(SurfaceType type)

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



[ZOSAPI.Editors.LDE.ILDERow.ChangeType](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml#a6ec5d7cedce07c2f4695061cd46cc6b8)

bool ChangeType(ISurfaceTypeSettings settings)

Changes the current surface to the specified type. Use GetSurfaceTypeSettings to get the relevant set...



[ZOSAPI.Editors.LDE.ISurfaceTypeSettings](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_surface_type_settings.xhtml)

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

**Definition:** InterfacesLDE.cs:746



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

ISystemData SystemData

Data for configuring everything in the System Explorer.

**Definition:** Interfaces.cs:845



[ZOSAPI.IOpticalSystem.LDE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a942995136e3c3cf875cc1fde38d33477)

ILensDataEditor LDE

Gets the lens data editor.

**Definition:** Interfaces.cs:865



[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.SystemData.IField](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_field.xhtml)

System field data. This interface can be accessed via the IFields interface.

**Definition:** InterfacesSE.cs:977



[ZOSAPI.SystemData.IFields](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_fields.xhtml)

System Explorer - Fields Data. This interface can be accessed via the ISystemData interface.

**Definition:** InterfacesSE.cs:819



[ZOSAPI.SystemData.IFields.SetFieldType](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_fields.xhtml#a8b5cd1dbb8c9b4889f3ba1ff1653dbc4)

void SetFieldType(FieldType type)

Sets the field type definition for all fields.



[ZOSAPI.SystemData.IFields.GetField](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_fields.xhtml#af2d2ae99d7ecf6e5866de571043f907d)

IField GetField(int position)

Gets the specified field.



[ZOSAPI.SystemData.ISDFiles](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_files.xhtml)

System Explorer - Files Data. This interface can be accessed via the ISystemData interface.

**Definition:** InterfacesSE.cs:588



[ZOSAPI.SystemData.ISDMaterialCatalogData](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_material_catalog_data.xhtml)

System Explorer - Material Catalogs Data. This interface can be accessed via the ISystemData interfac...

**Definition:** InterfacesSE.cs:500



[ZOSAPI.SystemData.ISDMaterialCatalogData.RemoveCatalog](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_material_catalog_data.xhtml#a97c9ef7850795c75aeb80366dbc75383)

bool RemoveCatalog(string catalog)

Removes the specified material catalog from the system.



[ZOSAPI.SystemData.ISDMaterialCatalogData.AddCatalog](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_material_catalog_data.xhtml#ae348d808484e0c5a4193b164ce0a802a)

bool AddCatalog(string catalog)

Adds the specified material to the system.



[ZOSAPI.SystemData.ISDPolarizationData](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_polarization_data.xhtml)

System Explorer - Polarization Data. This interface can be accessed via the ISystemData interface.

**Definition:** InterfacesSE.cs:438



[ZOSAPI.SystemData.ISDTitleNotes](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_title_notes.xhtml)

System Explorer - Notes Data. This interface can be accessed via the ISystemData interface.

**Definition:** InterfacesSE.cs:562



[ZOSAPI.SystemData.ISDUnitsData](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_s_d_units_data.xhtml)

System Explorer - Units Data. This interface can be accessed via the ISystemData interface.

**Definition:** InterfacesSE.cs:620



[ZOSAPI.SystemData.IWavelengths](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_wavelengths.xhtml)

System Explorer - Wavelengths Data. This interface can be accessed via the ISystemData interface.

**Definition:** InterfacesSE.cs:651



[ZOSAPI.SystemData.IWavelengths.GaussianQuadrature](interface_z_o_s_a_p_i_1_1_system_data_1_1_i_wavelengths.xhtml#a8a2bf14cb6a1f3519592e6cf5406641e)

bool GaussianQuadrature(double minWave, double maxWave, QuadratureSteps numSteps)

Generates a new set of system wavelengths based on a Gaussian Quadrature method.



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

@ String



[ZOSAPI.Editors.LDE](namespace_z_o_s_a_p_i_1_1_editors_1_1_l_d_e.xhtml)

**Definition:** InterfacesLDE.cs:8



[ZOSAPI.Editors](namespace_z_o_s_a_p_i_1_1_editors.xhtml)

**Definition:** InterfacesEditors.cs:12



[ZOSAPI.SystemData](namespace_z_o_s_a_p_i_1_1_system_data.xhtml)

**Definition:** InterfacesSE.cs:11



[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