Skip to main content

ZOS-API interface 2024 R2

Example 18 - C#

Last update: 17.07.2025

C#

using System;
using ZOSAPI;
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 TheConnection = new ZOSAPI_Connection();
// Attempt to create a Standalone connection
IZOSAPI_Application TheApplication = TheConnection.CreateNewApplication();
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;
}
if (TheApplication.Mode != ZOSAPI_Mode.Server)
{
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
return;
}
IOpticalSystem TheSystem = TheApplication.PrimarySystem;
// creates new directory
string strPath = System.IO.Path.Combine(TheApplication.SamplesDir, @"API\CS");
System.IO.Directory.CreateDirectory(strPath);
// Add your custom code here...
TheSystem.LoadFile(TheApplication.SamplesDir + "\\Sequential\\Objectives\\Double Gauss 28 degree field.zos", false);
// Get interface of the Multi-Configuration Editor
IMultiConfigEditor TheMCE = TheSystem.MCE;
// Add two configurations (totally 3)
TheMCE.AddConfiguration(false);
TheMCE.AddConfiguration(false);
// Add one operand (totally 2)
TheMCE.AddOperand();
// Get interface of each operand
IMCERow MCOperand1 = TheMCE.GetOperandAt(1);
IMCERow MCOperand2 = TheMCE.GetOperandAt(2);
// Change both operands' type to THIC
// Set parameters of operands
// If the type of operand is THIC, the first parameter here means surface number
MCOperand1.Param1 = 0;
MCOperand2.Param1 = 11;
// Set values of opeand for each configurations
MCOperand1.GetOperandCell(1).DoubleValue = 10000.0;
MCOperand1.GetOperandCell(2).DoubleValue = 5000.0;
MCOperand1.GetOperandCell(3).DoubleValue = 1000.0;
// Refocus for each configuration
IQuickFocus quickfocus = TheSystem.Tools.OpenQuickFocus();
TheSystem.SaveAs(TheApplication.SamplesDir + "\\API\\CS\\e18_Double_Gauss_28_degree_field_MultiConfig.zos");
// An exmple of manually "Make Thermal"
TheSystem.LoadFile(TheApplication.SamplesDir + "\\Sequential\\Objectives\\Doublet.zos", false);
// Add 1 configuration (totally 2)
TheMCE.AddConfiguration(false);
// Add 12 operand (totally 13)
for (int i = 0; i < 12; i++)
TheMCE.AddOperand();
// Create an operand type array and later we will input the array into MCE by a for loop
// Set parameter 1 for each config operand
int[] param1value = new int[11] { 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3 };
for (int i = 0; i < 11; i++)
TheMCE.GetOperandAt(i + 3).Param1 = param1value[i];
// Set type for each config operand
for (int i = 0; i < 13; i++)
TheMCE.GetOperandAt(i + 1).ChangeType(operandtype[i]);
// Set Thermal Pickup solves
ISolveData thermalpickupsolve = TheMCE.GetOperandAt(1).GetOperandCell(1).CreateSolveType(ZOSAPI.Editors.SolveType.ThermalPickup);
thermalpickupsolve._S_ThermalPickup.Configuration = 1;
int[] thermalpickup_num = new int[9] { 3, 4, 6, 7, 8, 10, 11, 12, 13 };
foreach (int i in thermalpickup_num)
TheMCE.GetOperandAt(i).GetOperandCell(2).SetSolveData(thermalpickupsolve);
// Set Pickup solve
ISolveData configpickupsolve = TheMCE.GetOperandAt(1).GetOperandCell(1).CreateSolveType(ZOSAPI.Editors.SolveType.ConfigPickup);
configpickupsolve._S_ConfigPickup.Configuration = 1;
TheMCE.GetOperandAt(5).GetOperandCell(2).SetSolveData(configpickupsolve);
TheMCE.GetOperandAt(9).GetOperandCell(2).SetSolveData(configpickupsolve);
// Set temperature of configuration 2 to 100 degrees
ISystemData TheSystemData = TheSystem.SystemData;
TheSystemData.Environment.AdjustIndexToEnvironment = true;
// Save the system
TheSystem.SaveAs(TheApplication.SamplesDir + "\\API\\CS\\e18_Doublet_MakeTermal.zos");
// Clean up
FinishStandaloneApplication(TheApplication);
}
static void FinishStandaloneApplication(IZOSAPI_Application TheApplication)
{
// Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases
if (TheApplication != null)
{
TheApplication.CloseApplication();
}
}
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 CreateNewApplication()
Attempts to launch a new instance of Optic Studio in 'headless' mode. Note that although the Optic St...
Definition: ZemaxService.cs:864
double DoubleValue
Gets or sets the double precision value of this cell. Note that if DataType is not CellDataType....
Definition: InterfacesEditors.cs:340
SolveStatus SetSolveData(ISolveData Data)
Updates the system with the new solve type settings (see also CreateSolveType and GetSolveData).
ISolveData CreateSolveType(SolveType type)
Creates the solve type settings for the specified solve type. Note that SetSolveData must be used to ...
Base interface for all solve types. This interface can be accesed via the IEditorCell interface....
Definition: InterfacesEditors.cs:563
All data for a Multiple Configuration Editor operand. This interface can be accessed via the IMultiCo...
Definition: InterfacesMCE.cs:405
int Param1
Definition: InterfacesMCE.cs:485
bool ChangeType(MultiConfigOperandType type)
Changes to the specified operand type.
IEditorCell GetOperandCell(int configuration)
Gets the specified operand cell data.
This interface defines all properties and methods needed to interact with the Multiple Configuration ...
Definition: InterfacesMCE.cs:158
IMCERow GetOperandAt(int OperandNumber)
Gets the specified operand data.
IMCERow AddOperand()
Adds a new operand to the end of the editor.
bool AddConfiguration(bool withPickups)
Adds a new, last configuration.
bool SetCurrentConfiguration(int ConfigurationNumber)
Sets the currently active configuration for the system (1 to NumberOfConfigurations).
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:687
void SaveAs(string fileName)
Saves the current system to the specified file. All future calls to Save will use the same file.
ISystemData SystemData
Data for configuring everything in the System Explorer.
Definition: Interfaces.cs:845
bool LoadFile(string LensFile, bool saveIfNeeded)
Replaces the current system with data from the specified file.
IOpticalSystemTools Tools
Gets an interface used to run various tools on the optical system.
Definition: Interfaces.cs:932
IMultiConfigEditor MCE
Gets the multi-configuration editor.
Definition: Interfaces.cs:917
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:261
bool IsValidLicenseForAPI
Gets a value indicating whether this the API is currently useable.
Definition: Interfaces.cs:287
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
void CloseApplication()
Shut down the Optic Studio process.
IOpticalSystem PrimarySystem
Gets the primary system. When Mode is ZOSAPI_Mode.Server, this will initially be an empty sequential ...
Definition: Interfaces.cs:328
string SamplesDir
Gets the full path for the current user's samples directory (in the My Documents\Zemax\Samples\ folde...
Definition: Interfaces.cs:505
Interfaces and methods for changing all System Explorer data. This interface can be accessed via the ...
Definition: InterfacesSE.cs:275
Interfaces and methods for running the Quick Focus tool. This interface can be accessed via the IOpti...
Definition: Tools.cs:793
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
Definition: InterfacesMCE.cs:11
MultiConfigOperandType
All supported Multi Configuration operand types.
Definition: InterfacesMCE.cs:30
Definition: InterfacesEditors.cs:12
Definition: InterfacesSE.cs:11
Definition: Tools.cs:697
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5
ZOSAPI_Mode
Definition: Interfaces.cs:77

Connect with Ansys