    

 ## On this page

  

 

 # Example 14 - C# 

 Last update: 17.07.2025 

# <a class="anchor" id="ex14_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\\e15\_Seq\_Optimization");

 System.IO.Directory.CreateDirectory(strPath);

 

 // Open Double Gauss sample file

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

 string DGfile = System.IO.Path.Combine(samplesFolder, "Sequential\\\\Objectives\\\\Double Gauss 28 degree field.zos");

 TheSystem.[LoadFile](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a6eb1455ac53555ae6f30032d85b63735)(DGfile, false);

 

 // Set up the Tolerance Wizard and run it

 [ZOSAPI.Wizards.ISEQToleranceWizard](interface_z_o_s_a_p_i_1_1_wizards_1_1_i_s_e_q_tolerance_wizard.xhtml) tWiz = TheSystem.[TDE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a31577012730985e562da96019e8e1ad0).SEQToleranceWizard;

 // Specify surface tolerances

 tWiz.SurfaceRadius = 0.1;

 tWiz.SurfaceThickness = 0.1;

 tWiz.SurfaceDecenterX = 0.1;

 tWiz.SurfaceDecenterY = 0.1;

 tWiz.SurfaceTiltX = 0.2;

 tWiz.SurfaceTiltY = 0.2;

 // Specify element tolerances

 tWiz.ElementDecenterX = 0.1;

 tWiz.ElementDecenterY = 0.1;

 tWiz.ElementTiltXDegrees = 0.2;

 tWiz.ElementTiltYDegrees = 0.2;

 // Specify tolerances not to be used

 tWiz.IsSurfaceSandAIrregularityUsed = false;

 tWiz.IsIndexUsed = false;

 tWiz.IsIndexAbbePercentageUsed = false;

 // Select OK

 tWiz.[OK](interface_z_o_s_a_p_i_1_1_wizards_1_1_i_wizard.xhtml#a28e7e9235278fc9466072bada981041f)();

 

 // Create a "Double Gauss" folder in the Samples folder

 string dirLoc = System.IO.Path.Combine(samplesFolder, "API\\\\CS\\\\e14\_seq\_tolerance");

 System.IO.Directory.CreateDirectory(dirLoc);

 // Save a copy of the file in the Double Gauss folder

 string fileNameSeq = System.IO.Path.Combine(dirLoc, "Double Gauss (seq).zos");

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

 

 // Set up the Tolerancing analysis and run it

 [ZOSAPI.Tools.Tolerancing.ITolerancing](interface_z_o_s_a_p_i_1_1_tools_1_1_tolerancing_1_1_i_tolerancing.xhtml) tol = TheSystem.[Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8).OpenTolerancing();

 // Select Sensitivity mode

 tol.SetupMode = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml).[Tolerancing](namespace_z_o_s_a_p_i_1_1_tools_1_1_tolerancing.xhtml).SetupModes.Sensitivity;

 // Select Criterion and related settings 

 tol.Criterion = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml).[Tolerancing](namespace_z_o_s_a_p_i_1_1_tools_1_1_tolerancing.xhtml).Criterions.RMSSpotRadius;

 tol.CriterionSampling = 3;

 tol.CriterionComp = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml).[Tolerancing](namespace_z_o_s_a_p_i_1_1_tools_1_1_tolerancing.xhtml).CriterionComps.OptimizeAll_DLS;

 tol.CriterionCycle = 2;

 tol.CriterionField = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml).[Tolerancing](namespace_z_o_s_a_p_i_1_1_tools_1_1_tolerancing.xhtml).CriterionFields.UserDefined;

 // Select the number or MC runs and files to save

 tol.NumberOfRuns = 20;

 tol.NumberToSave = 20;

 // Run the Tolerancing analysis

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

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

 

 // Convert file to Non-sequential mode

 [ZOSAPI.Tools.IConvertToNSCGroup](interface_z_o_s_a_p_i_1_1_tools_1_1_i_convert_to_n_s_c_group.xhtml) convertNSmode = TheSystem.[Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8).OpenConvertToNSCGroup();

 convertNSmode.ConvertFileToNSC = true;

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

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

 // Save the Non-sequential file to the Double Gauss folder

 string fileNameNS = System.IO.Path.Combine(dirLoc, "Double Gauss (NS).zos");

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

 

 // 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.TDE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a31577012730985e562da96019e8e1ad0)

IToleranceDataEditor TDE

Gets the tolerance data editor.

**Definition:** Interfaces.cs:891



[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.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.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.IConvertToNSCGroup](interface_z_o_s_a_p_i_1_1_tools_1_1_i_convert_to_n_s_c_group.xhtml)

Interfaces and methods for converting the sequential surfaces to a non-sequential group....

**Definition:** IConvertToNSCGroup.cs:29



[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.Tolerancing.ITolerancing](interface_z_o_s_a_p_i_1_1_tools_1_1_tolerancing_1_1_i_tolerancing.xhtml)

Interfaces and methods for running Tolerancing. This interface can be accessed via the IOpticalSystem...

**Definition:** ITolerancing.cs:137



[ZOSAPI.Wizards.ISEQToleranceWizard](interface_z_o_s_a_p_i_1_1_wizards_1_1_i_s_e_q_tolerance_wizard.xhtml)

Interface for the Tolerance Editor, Sequential Wizard These settings can be retrieved from the ZOSAPI...

**Definition:** InterfacesWizards.cs:566



[ZOSAPI.Wizards.IWizard.OK](interface_z_o_s_a_p_i_1_1_wizards_1_1_i_wizard.xhtml#a28e7e9235278fc9466072bada981041f)

void OK()

Settings are set, perform the wizardry.



[ZOSAPI.Tools.Tolerancing](namespace_z_o_s_a_p_i_1_1_tools_1_1_tolerancing.xhtml)

**Definition:** ITolerancing.cs:5



[ZOSAPI.Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml)

**Definition:** FileSource.cs:4



[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