    

 ## On this page

  

 

 # Example 16 - C# 

 Last update: 17.07.2025 

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

using System;

using [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

 

namespace CSharpUserOperandApplication

{

 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;

 }

 

 BeginUserOperand();

 }

 

 static void BeginUserOperand()

 {

 // 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 connect to the existing OpticStudio instance

 [IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication = null;

 try

 {

 TheApplication = TheConnection.[ConnectToApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aeb9010dade0f9685d455425d7eb7d8f5)(); // this will throw an exception if not launched from OpticStudio

 }

 catch (Exception ex)

 {

 HandleError(ex.Message);

 return;

 }

 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).Operand)

 {

 HandleError("User plugin was started in the wrong mode: expected Operand, found " + TheApplication.[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19).ToString());

 return;

 }

 

 // Read the operand arguments

 double Hx = TheApplication.[OperandArgument1](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae50eae8bffec8ed4b4cf511b52b8ccf4);

 double Hy = TheApplication.[OperandArgument2](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a7fc25b6936d0b0b840af213bb54d7314);

 double Px = TheApplication.[OperandArgument3](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#adefae0ab7222380382b8457207120086);

 double Py = TheApplication.[OperandArgument4](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ac14d8524751a4b765e91df2535542d48);

 

 // Initialize the output array

 int maxResultLength = TheApplication.[OperandResults](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a46fc7da26be5a482b4991b3b7157f59e).Length;

 double[] operandResults = new double[maxResultLength];

 

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

 

 // This is an example that calculates the sum of absolute distance between surfaces. 

 // Hx -&gt; Surf1

 // Hy -&gt; Surf2

 

 // Read the operand arguments (Note that we aren't using operand 3 and 4)

 int surf1 = (int)TheApplication.[OperandArgument1](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae50eae8bffec8ed4b4cf511b52b8ccf4);

 int surf2 = (int)TheApplication.[OperandArgument2](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a7fc25b6936d0b0b840af213bb54d7314);

 //double x = TheApplication.OperandArgument3;

 //double y = TheApplication.OperandArgument4; 

 

 //Make the sum of the absolute value of each surface thickness

 [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) surf;

 double thic = 0;

 

 if (surf1 &lt;= surf2)

 {

 for (int i = surf1; i &lt;= surf2; i++)

 {

 surf = TheSystem.[LDE](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a942995136e3c3cf875cc1fde38d33477).GetSurfaceAt(i);

 thic = thic + Math.Abs(surf.[Thickness](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml#a06e8f1479eeba5ae70b2f6082bb5ee71)); 

 }

 operandResults[0] = thic;

 }

 else

 {

 operandResults[0] = 1e9; // invalid input

 }

 

 // Clean up

 FinishUserOperand(TheApplication, operandResults);

 }

 

 static void FinishUserOperand([IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication, double[] resultData)

 {

 // Note - OpticStudio will wait for the operand to complete until this application exits 

 if (TheApplication != null)

 {

 TheApplication.[OperandResults](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a46fc7da26be5a482b4991b3b7157f59e).WriteData(resultData.Length, resultData);

 }

 }

 

 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.ConnectToApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aeb9010dade0f9685d455425d7eb7d8f5)

IZOSAPI_Application ConnectToApplication()

Attempts to connect to Optic Studio. This will only succeed when the client application was launched ...

**Definition:** ZemaxService.cs:536



[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.Thickness](interface_z_o_s_a_p_i_1_1_editors_1_1_l_d_e_1_1_i_l_d_e_row.xhtml#a06e8f1479eeba5ae70b2f6082bb5ee71)

double Thickness

Gets or sets the surface thickness.

**Definition:** InterfacesLDE.cs:1009



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

IVectorData OperandResults

In Operand mode, used to set the output data.

**Definition:** Interfaces.cs:543



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

double OperandArgument2

Argument 2 - Hy.

**Definition:** Interfaces.cs:567



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

double OperandArgument4

Argument 4 - Py.

**Definition:** Interfaces.cs:575



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

double OperandArgument3

Argument 3 - Px.

**Definition:** Interfaces.cs:571



[ZOSAPI.IZOSAPI\_Application.OperandArgument1](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae50eae8bffec8ed4b4cf511b52b8ccf4)

double OperandArgument1

Argument 1 - Hx.

**Definition:** Interfaces.cs:555



[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