Skip to main content

ZOS-API interface 2025 R1

Example 14 - 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\e15_Seq_Optimization");
System.IO.Directory.CreateDirectory(strPath);
// Open Double Gauss sample file
string samplesFolder = TheApplication.SamplesDir;
string DGfile = System.IO.Path.Combine(samplesFolder, "Sequential\\Objectives\\Double Gauss 28 degree field.zos");
TheSystem.LoadFile(DGfile, false);
// Set up the Tolerance Wizard and run it
ZOSAPI.Wizards.ISEQToleranceWizard tWiz = TheSystem.TDE.SEQToleranceWizard;
// Specify surface tolerances
tWiz.SurfaceRadius = 0.1;
tWiz.SurfaceThickness = 0.1;
tWiz.SurfaceDecenterX = 0.1;
tWiz.SurfaceDecenterY = 0.1;
tWiz.SurfaceTiltX = 0.2;
tWiz.SurfaceTiltY = 0.2;
// Specify element tolerances
tWiz.ElementDecenterX = 0.1;
tWiz.ElementDecenterY = 0.1;
tWiz.ElementTiltXDegrees = 0.2;
tWiz.ElementTiltYDegrees = 0.2;
// Specify tolerances not to be used
tWiz.IsSurfaceSandAIrregularityUsed = false;
tWiz.IsIndexUsed = false;
tWiz.IsIndexAbbePercentageUsed = false;
// Select OK
tWiz.OK();
// Create a "Double Gauss" folder in the Samples folder
string dirLoc = System.IO.Path.Combine(samplesFolder, "API\\CS\\e14_seq_tolerance");
System.IO.Directory.CreateDirectory(dirLoc);
// Save a copy of the file in the Double Gauss folder
string fileNameSeq = System.IO.Path.Combine(dirLoc, "Double Gauss (seq).zos");
TheSystem.SaveAs(fileNameSeq);
// Set up the Tolerancing analysis and run it
ZOSAPI.Tools.Tolerancing.ITolerancing tol = TheSystem.Tools.OpenTolerancing();
// Select Sensitivity mode
tol.SetupMode = ZOSAPI.Tools.Tolerancing.SetupModes.Sensitivity;
// Select Criterion and related settings
tol.Criterion = ZOSAPI.Tools.Tolerancing.Criterions.RMSSpotRadius;
tol.CriterionSampling = 3;
tol.CriterionComp = ZOSAPI.Tools.Tolerancing.CriterionComps.OptimizeAll_DLS;
tol.CriterionCycle = 2;
tol.CriterionField = ZOSAPI.Tools.Tolerancing.CriterionFields.UserDefined;
// Select the number or MC runs and files to save
tol.NumberOfRuns = 20;
tol.NumberToSave = 20;
// Run the Tolerancing analysis
tol.Close();
// Convert file to Non-sequential mode
ZOSAPI.Tools.IConvertToNSCGroup convertNSmode = TheSystem.Tools.OpenConvertToNSCGroup();
convertNSmode.ConvertFileToNSC = true;
convertNSmode.RunAndWaitForCompletion();
convertNSmode.Close();
// Save the Non-sequential file to the Double Gauss folder
string fileNameNS = System.IO.Path.Combine(dirLoc, "Double Gauss (NS).zos");
TheSystem.SaveAs(fileNameNS);
// 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
Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....
Definition: Interfaces.cs:687
IToleranceDataEditor TDE
Gets the tolerance data editor.
Definition: Interfaces.cs:891
void SaveAs(string fileName)
Saves the current system to the specified file. All future calls to Save will use the same file.
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
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 converting the sequential surfaces to a non-sequential group....
Definition: IConvertToNSCGroup.cs:29
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
bool Close()
Closes this tool and frees up and associated resources.
Interfaces and methods for running Tolerancing. This interface can be accessed via the IOpticalSystem...
Definition: ITolerancing.cs:137
Interface for the Tolerance Editor, Sequential Wizard These settings can be retrieved from the ZOSAPI...
Definition: InterfacesWizards.cs:566
void OK()
Settings are set, perform the wizardry.
Definition: ITolerancing.cs:5
Definition: FileSource.cs:4
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