Example 16 - C#
Last update: 17.07.2025C#
using System;
using ZOSAPI;
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
// Attempt to connect to the existing OpticStudio instance
IZOSAPI_Application TheApplication = null;
try
{
TheApplication = TheConnection.ConnectToApplication(); // 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)
{
HandleError("Failed to connect to OpticStudio: " + TheApplication.LicenseStatus);
return;
}
{
HandleError("User plugin was started in the wrong mode: expected Operand, found " + TheApplication.Mode.ToString());
return;
}
// Read the operand arguments
double Hx = TheApplication.OperandArgument1;
double Hy = TheApplication.OperandArgument2;
double Px = TheApplication.OperandArgument3;
double Py = TheApplication.OperandArgument4;
// Initialize the output array
int maxResultLength = TheApplication.OperandResults.Length;
double[] operandResults = new double[maxResultLength];
IOpticalSystem TheSystem = TheApplication.PrimarySystem;
// Add your custom code here...
// This is an example that calculates the sum of absolute distance between surfaces.
// Hx -> Surf1
// Hy -> Surf2
// Read the operand arguments (Note that we aren't using operand 3 and 4)
int surf1 = (int)TheApplication.OperandArgument1;
//double x = TheApplication.OperandArgument3;
//double y = TheApplication.OperandArgument4;
//Make the sum of the absolute value of each surface thickness
double thic = 0;
if (surf1 <= surf2)
{
for (int i = surf1; i <= surf2; i++)
{
surf = TheSystem.LDE.GetSurfaceAt(i);
thic = thic + Math.Abs(surf.Thickness);
}
operandResults[0] = thic;
}
else
{
operandResults[0] = 1e9; // invalid input
}
// Clean up
FinishUserOperand(TheApplication, operandResults);
}
{
// Note - OpticStudio will wait for the operand to complete until this application exits
if (TheApplication != null)
{
TheApplication.OperandResults.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);
}
}
}
Definition: ZemaxService.cs:198
IZOSAPI_Application ConnectToApplication()
Attempts to connect to Optic Studio. This will only succeed when the client application was launched ...
Definition: ZemaxService.cs:536
All data for a Lens Data Editor surface. This interface can be accessed via the ILensDataEditor inter...
Definition: InterfacesLDE.cs:791
double Thickness
Gets or sets the surface thickness.
Definition: InterfacesLDE.cs:1009
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:687
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:261
IVectorData OperandResults
In Operand mode, used to set the output data.
Definition: Interfaces.cs:543
bool IsValidLicenseForAPI
Gets a value indicating whether this the API is currently useable.
Definition: Interfaces.cs:287
double OperandArgument2
Argument 2 - Hy.
Definition: Interfaces.cs:567
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
LicenseStatusType LicenseStatus
Gets the license status. Note that this displays the license edition if successful,...
Definition: Interfaces.cs:280
double OperandArgument4
Argument 4 - Py.
Definition: Interfaces.cs:575
IOpticalSystem PrimarySystem
Gets the primary system. When Mode is ZOSAPI_Mode.Server, this will initially be an empty sequential ...
Definition: Interfaces.cs:328
double OperandArgument3
Argument 3 - Px.
Definition: Interfaces.cs:571
double OperandArgument1
Argument 1 - Hx.
Definition: Interfaces.cs:555
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5