Example 01 - C#
Last update: 17.07.2025C#
using System;
using ZOSAPI;
using ZOSAPI.Editors;
using ZOSAPI.Editors.LDE;
using ZOSAPI.SystemData;
using ZOSAPI.Tools.General;
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
// 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;
}
{
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 a new API directory
System.IO.Directory.CreateDirectory(strPath);
// Set up primary optical system
string sampleDir = TheApplication.SamplesDir;
string testFile = sampleDir + "\\API\\CS\\e01_new_file_and_quickfocus.zos";
// Make new file
TheSystem.New(false);
TheSystem.SaveAs(testFile);
TheSystem.SystemData.MaterialCatalogs.AddCatalog("SCHOTT");
// Aperture
ISystemData TheSystemData = TheSystem.SystemData;
TheSystemData.Aperture.ApertureValue = 40;
// Fields
// Wavelength preset
// Lens data
ILensDataEditor TheLDE = TheSystem.LDE;
TheLDE.InsertNewSurfaceAt(2);
TheLDE.InsertNewSurfaceAt(2);
ILDERow Surface_1 = TheLDE.GetSurfaceAt(1);
ILDERow Surface_2 = TheLDE.GetSurfaceAt(2);
ILDERow Surface_3 = TheLDE.GetSurfaceAt(3);
// Changes surface cells in LDE
Surface_1.Thickness = 50.0;
Surface_1.Comment = "Stop is free to move";
Surface_2.Radius = 100.0;
Surface_2.Thickness = 10.0;
Surface_2.Comment = "front of lens";
Surface_2.Material = "N-BK7";
Surface_3.Comment = "rear of lens";
// Solver
Solver._S_FNumber.FNumber = 10;
Surface_3.RadiusCell.SetSolveData(Solver);
// QuickFocus
IQuickFocus quickFocus = TheSystem.Tools.OpenQuickFocus();
quickFocus.Criterion = QuickFocusCriterion.SpotSizeRadial;
quickFocus.UseCentroid = true;
quickFocus.RunAndWaitForCompletion();
quickFocus.Close();
// Save and close
TheSystem.Save();
// Clean up
FinishStandaloneApplication(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:863
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 Lens Data Editor surface. This interface can be accessed via the ILensDataEditor inter...
Definition: InterfacesLDE.cs:791
This interface defines all properties and methods needed to interact with the Lens Data Editor....
Definition: InterfacesLDE.cs:265
ILDERow GetSurfaceAt(int SurfaceNumber)
Gets the data for the specified surface.
ILDERow InsertNewSurfaceAt(int SurfaceNumber)
Inserts a new surface at the specified position.
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:690
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:848
void New(bool saveIfNeeded)
Clears all data in the current system and resets it to a default state.
IOpticalSystemTools Tools
Gets an interface used to run various tools on the optical system.
Definition: Interfaces.cs:935
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:264
bool IsValidLicenseForAPI
Gets a value indicating whether this the API is currently useable.
Definition: Interfaces.cs:290
ZOSAPI_Mode Mode
Gets the current connetion mode. Use this to check if Optic Studio is expecting a user operand / anal...
Definition: Interfaces.cs:309
LicenseStatusType LicenseStatus
Gets the license status. Note that this displays the license edition if successful,...
Definition: Interfaces.cs:283
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:331
string SamplesDir
Gets the full path for the current user's samples directory (in the My Documents\Zemax\Samples\ folde...
Definition: Interfaces.cs:508
System field data. This interface can be accessed via the IFields interface.
Definition: InterfacesSE.cs:975
IField AddField(double X, double Y, double Weight)
Add a new field, after all the current fields.
Interfaces and methods for changing all System Explorer data. This interface can be accessed via the ...
Definition: InterfacesSE.cs:275
bool SelectWavelengthPreset(WavelengthPreset preset)
Replaces all system wavelengths with a preset definition.
Interfaces and methods for running the Quick Focus tool. This interface can be accessed via the IOpti...
Definition: Tools.cs:792
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
Definition: InterfacesLDE.cs:8
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
Definition: Tools.cs:696
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5