Skip to main content

ZOS-API interface 2024 R1

Example 20 - 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...
// Load a non-sequential file
TheSystem.LoadFile(TheApplication.SamplesDir + "\\Non-Sequential\\Miscellaneous\\Digital_projector_flys_eye_homogenizer.zos", false);
// Get interface of IExportCAD
IExportCAD ToolExportCAD = TheSystem.Tools.OpenExportCAD();
// default option settings
ToolExportCAD.FirstObject = 1;
ToolExportCAD.LastObject = 8;
ToolExportCAD.RayLayer = 1;
ToolExportCAD.LensLayer = 0;
ToolExportCAD.DummyThickness = 1;
ToolExportCAD.SplineSegments = SplineSegmentsType.N_032;
ToolExportCAD.FileType = CADFileType.STEP;
ToolExportCAD.Tolerance = CADToleranceType.N_TenEMinus4;
ToolExportCAD.SetCurrentConfiguration();
// For other configuration choices, use following methods.
//ToolExportCAD.SetConfigurationAllAtOnce();
//ToolExportCAD.SetConfigurationAllByFile();
//ToolExportCAD.SetConfigurationAllByLayer();
//ToolExportCAD.SetSingleConfiguration(1);
// default check boxes settings
ToolExportCAD.SurfacesAsSolids = true;
ToolExportCAD.ScatterNSCRays = false;
ToolExportCAD.ExportDummySurfaces = false;
ToolExportCAD.SplitNSCRays = false;
ToolExportCAD.UsePolarization = false;
// set output file name
ToolExportCAD.OutputFileName = TheApplication.ObjectsDir + "\\CAD Files\\e20_export_CAD_File.step";
// Starting exporting
// Run with a 3 minites timeout
Console.Write("Starting exporting...");
ToolExportCAD.Run();
RunStatus runstatus = ToolExportCAD.WaitWithTimeout(3 * 60);
// Report the status
switch (runstatus)
{
case RunStatus.Completed:
Console.WriteLine("Completed!");
break;
case RunStatus.FailedToStart:
Console.WriteLine("Failed To Start!");
break;
case RunStatus.InvalidTimeout:
Console.WriteLine("Invalid Timeout!");
break;
case RunStatus.TimedOut:
Console.WriteLine("Timed Out!");
break;
}
Console.WriteLine("Progress: " + ToolExportCAD.Progress.ToString("000") + "%");
// If the exporting is not completed and can be cancelled, cancel the work
if (runstatus != RunStatus.Completed & ToolExportCAD.CanCancel)
{
ToolExportCAD.Cancel();
}
// Close the tool
ToolExportCAD.Close();
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:863
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:690
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: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
Interfaces and methods for exporting the current system to various CAD formats. This interface can be...
Definition: Tools.cs:1103
bool Run()
Start the tool. Note that for synchronous tools (see IsAsynchronous), this call will block until comp...
bool Close()
Closes this tool and frees up and associated resources.
bool Cancel()
Cancels the currently running asynchronous tool.
bool CanCancel
Gets a value indicating whether the tool can be cancelled.
Definition: Tools.cs:563
RunStatus WaitWithTimeout(double timeOutSeconds)
Waits for the currently running tool to complete, with a specified time out value....
int Progress
Gets the progress of the current tool, if supported.
Definition: Tools.cs:541
Definition: Tools.cs:696
CADFileType
Definition: Tools.cs:1021
CADToleranceType
Definition: Tools.cs:1054
SplineSegmentsType
Definition: Tools.cs:1009
Definition: FileSource.cs:4
RunStatus
Definition: Tools.cs:508
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5
ZOSAPI_Mode
Definition: Interfaces.cs:80

Connect with Ansys