Skip to main content

ZOS-API interface 2025 R1

Example 19 - 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;
// Add your custom code here...
// creates new directory
string strPath = System.IO.Path.Combine(TheApplication.SamplesDir, @"API\CS");
System.IO.Directory.CreateDirectory(strPath);
// This code build a chain of prisms based on the KB article:
// http://zemax.com/os/resources/learn/knowledgebase/how-to-work-in-global-coordinates-in-a-sequential
TheSystem.New(false);
// ISystemData represents the System Explorer in GUI.
// We access options in System Explorer through ISystemData in ZOS-API
ISystemData TheSystemData = TheSystem.SystemData;
TheSystemData.Aperture.ApertureValue = 10;
TheSystemData.Aperture.AFocalImageSpace = true;
TheSystemData.Wavelengths.GetWavelength(1).Wavelength = 0.55;
// Get interface of Lens Data Editor and add 3 surfaces.
ILensDataEditor TheLDE = TheSystem.LDE;
TheLDE.InsertNewSurfaceAt(2);
TheLDE.InsertNewSurfaceAt(2);
TheLDE.InsertNewSurfaceAt(2);
// Set thickness and material for each surface.
TheLDE.GetSurfaceAt(1).Thickness = 30;
TheLDE.GetSurfaceAt(2).Thickness = 20;
TheLDE.GetSurfaceAt(4).Thickness = 30;
TheLDE.GetSurfaceAt(2).Material = "N-BK7";
// GetSurfaceAt(surface number shown in LDE) will return an interface ILDERow
// Through property TiltDecenterData of each interface ILDERow, we can modify data in Surface Properties > Tilt/Decenter section
// To specify an aperture to a surface, we need to first create an ISurfaceApertureType and then assign it.
Rect_Aper._S_RectangularAperture.XHalfWidth = 10;
Rect_Aper._S_RectangularAperture.YHalfWidth = 10;
// To change surface type, we need to first get an ISurfaceTypesettings and then assign it.
ISurfaceTypeSettings SurfaceType_CB = TheLDE.GetSurfaceAt(4).GetSurfaceTypeSettings(SurfaceType.CoordinateBreak);
TheLDE.GetSurfaceAt(4).ChangeType(SurfaceType_CB);
// Set Chief Ray solves to surface 4, which is Coordinate Break
// To set a solve to a cell in editor, we need to first create a ISolveData and then assign it.
ISolveData Solve_ChiefNormal = TheLDE.GetSurfaceAt(4).GetSurfaceCell(SurfaceColumn.Par1).CreateSolveType(SolveType.PickupChiefRay);
TheLDE.GetSurfaceAt(4).GetSurfaceCell(SurfaceColumn.Par1).SetSolveData(Solve_ChiefNormal);
TheLDE.GetSurfaceAt(4).GetSurfaceCell(SurfaceColumn.Par2).SetSolveData(Solve_ChiefNormal);
TheLDE.GetSurfaceAt(4).GetSurfaceCell(SurfaceColumn.Par3).SetSolveData(Solve_ChiefNormal);
TheLDE.GetSurfaceAt(4).GetSurfaceCell(SurfaceColumn.Par4).SetSolveData(Solve_ChiefNormal);
TheLDE.GetSurfaceAt(4).GetSurfaceCell(SurfaceColumn.Par5).SetSolveData(Solve_ChiefNormal);
// Copy 3 surfaces starting from surface number 2 in LDE and paste to surface number 5,
// which will become surface number 8 after pasting.
for (int i = 0; i < 10; i++)
{
TheLDE.CopySurfaces(2, 3, 5);
}
// Save file
TheSystem.SaveAs(TheApplication.SamplesDir + "\\API\\CS\\e19_Sample_Prism_Chain.zos");
// Run tool Convert Local To Global Coordinates to convert surface #2 to surface #35 to be globally referenced to surface #1
TheSystem.SaveAs(TheApplication.SamplesDir + "\\API\\CS\\e19_Sample_Prism_Chain_GlobalCoordinate.zos");
// Run tool Conver Global To Local Coordinates to convert surface #1 to surface #57 back to local coordinate.
TheSystem.SaveAs(TheApplication.SamplesDir + "\\API\\CS\\e19_Sample_Prism_Chain_BackTo_LocalCoordinate.zos");
Console.Write("Press any key to continue...");
Console.ReadKey();
// 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
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
bool ChangeApertureTypeSettings(ISurfaceApertureType settings)
Updates the surface aperture with the specified settings. Use CurrentTypeSettings or CreateApertureTy...
ISurfaceApertureType CreateApertureTypeSettings(SurfaceApertureTypes type)
Creates the a settings interface for the specified aperture type. Note that changing these settings w...
int PickupFrom
Gets or sets the surface to pickup settings from.
Definition: InterfacesLDE.cs:4813
double Thickness
Gets or sets the surface thickness.
Definition: InterfacesLDE.cs:1009
string Material
Definition: InterfacesLDE.cs:1023
ISurfaceTypeSettings GetSurfaceTypeSettings(SurfaceType type)
Create the settings for the specified type. Use this method to specify any extra data required to cha...
IEditorCell GetSurfaceCell(SurfaceColumn Col)
Gets the specified surface cell data.
bool ChangeType(ISurfaceTypeSettings settings)
Changes the current surface to the specified type. Use GetSurfaceTypeSettings to get the relevant set...
ILDETiltDecenterData TiltDecenterData
Gets the surface Tile/Decenter data.
Definition: InterfacesLDE.cs:933
ILDEApertureData ApertureData
Gets the surface Aperture data.
Definition: InterfacesLDE.cs:919
TiltDecenterOrderType BeforeSurfaceOrder
Definition: InterfacesLDE.cs:5326
double AfterSurfaceTiltX
Definition: InterfacesLDE.cs:5346
double BeforeSurfaceTiltX
Definition: InterfacesLDE.cs:5329
This interface defines all properties and methods needed to interact with the Lens Data Editor....
Definition: InterfacesLDE.cs:265
int CopySurfaces(int fromSurfaceNumber, int NumberOfSurfaces, int toSurfaceNumber)
Copy and paste the specified number of surfaces from one location to another. Note that it is okay ...
CoordinateConversionResult RunTool_ConvertLocalToGlobalCoordinates(int FirstSurface, int LastSurface, int referenceSurface)
Converts Local to Global coordinates.
ILDERow GetSurfaceAt(int SurfaceNumber)
Gets the data for the specified surface.
CoordinateConversionResult RunTool_ConvertGlobalToLocalCoordinates(int FirstSurface, int LastSurface, ConversionOrder order)
Converts Global to Local coordinates.
ILDERow InsertNewSurfaceAt(int SurfaceNumber)
Inserts a new surface at the specified position.
Base interface for settings specific to a SurfaceApertureTypes. This interface can be retrieved from ...
Definition: InterfacesLDE.cs:4905
This interface is used for selecting any files required by a surface, and for changing surface types....
Definition: InterfacesLDE.cs:746
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
void New(bool saveIfNeeded)
Clears all data in the current system and resets it to a default state.
ILensDataEditor LDE
Gets the lens data editor.
Definition: Interfaces.cs:865
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
IWavelengths Wavelengths
Definition: InterfacesSE.cs:277
double Wavelength
Definition: InterfacesSE.cs:768
IWavelength GetWavelength(int position)
Gets the specified wavelength."/>.
Definition: InterfacesLDE.cs:8
SurfaceType
All available surface types.
Definition: InterfacesLDE.cs:27
SurfaceApertureTypes
Definition: InterfacesLDE.cs:4773
TiltDecenterOrderType
Definition: InterfacesLDE.cs:5282
SurfaceColumn
Definition: InterfacesLDE.cs:136
Definition: InterfacesEditors.cs:12
SolveType
All solve types available. Note that only a portion of the types are available for any given IEditorC...
Definition: InterfacesEditors.cs:483
Definition: InterfacesSE.cs:11
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