CSharpStandalone_11_Basic_Seq
Last update: 17.07.2025using System;
using ZOSAPI;
using ZOSAPI.SystemData;
using ZOSAPI.Analysis;
using ZOSAPI.Analysis.Settings;
using ZOSAPI.Editors.LDE;
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
if (TheApplication == null)
{
HandleError("An unknown connection error occurred!");
return;
}
// Check the connection status
{
return;
}
{
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
return;
}
// creates new directory
System.IO.Directory.CreateDirectory(strPath);
// Add your custom code here...
// Create New Sequential File
// Name File
String fileName = TheApplication.SamplesDir + "\\API\\CS\\e11_basic_seq.zos";
TheSystem.SaveAs(fileName);
// Changing System Explorer Settings
// Set Aperture
TheSystemData.Aperture.ApertureValue = 20;
TheSystem.SystemData.MaterialCatalogs.AddCatalog("SCHOTT");
// Set Apodization Type to Gaussian, and set apodization factor to 1
TheSystemData.Aperture.ApodizationType = ZemaxApodizationType.Gaussian;
TheSystemData.Aperture.ApodizationFactor = 1;
// Set Wavelength
// Insert and Track New Surfaces, set STOP to surface 1
TheLDE.InsertNewSurfaceAt(1);
TheLDE.InsertNewSurfaceAt(1);
TheLDE.InsertNewSurfaceAt(1);
ILDERow Surf_2 = TheLDE.GetSurfaceAt(2);
ILDERow Surf_3 = TheLDE.GetSurfaceAt(3);
ILDERow Surf_4 = TheLDE.GetSurfaceAt(4);
Surf_1.IsStop = true;
// Set some baseline parameters
Surf_1.Thickness = 5;
Surf_2.Thickness = 5;
Surf_2.Radius = 100;
Surf_2.Material = "N-BK7";
Surf_3.Thickness = 3;
Surf_3.Radius = -30;
Surf_3.Material = "F2";
Surf_4.Radius = -80;
// Set system lens units to inches, scale all values with Scale Lens tool
// For demonstration only. This file is new, so it has default units mm.
// Open Scale Lens tool
ScaleLens = TheSystem.Tools.OpenScale();
// Apply Tool Settings
ScaleLens.ScaleByUnits = true;
ScaleLens.ScaleToUnit = ScaleToUnits.Inches;
ScaleLens.RunAndWaitForCompletion();
ScaleLens.Close();
// Add Rectangular Aperture to Surface 1
// Get surface 1, create aperture settings
Surf_1 = TheSystem.LDE.GetSurfaceAt(1);
ISurfaceApertureType rAperture = Surf_1.ApertureData.CreateApertureTypeSettings(SurfaceApertureTypes.RectangularAperture);
// Set aperture size
rAperture._S_RectangularAperture.XHalfWidth = 0.1;
rAperture._S_RectangularAperture.YHalfWidth = 0.1;
// Apply aperture settings to surface 1
// Run Quick Focus
QuickFocus.RunAndWaitForCompletion();
QuickFocus.Close();
// Open Universal Plot of RMS Spot Size vs Surface3 Thickness
Console.WriteLine("Universal Plot has analysis specific settings? " + UnivPlot.HasAnalysisSpecificSettings.ToString());
// Above is False; Universal Plot Settings must be changed via ModifySettings (changing a config (.cfg) file)
String cfg = TheApplication.ZemaxDataDir + "\\Configs\\UNI.CFG";
UnivPlot_Settings.ModifySettings(cfg, "UN1_SURFACE", (TheSystem.LDE.NumberOfSurfaces - 2).ToString());
UnivPlot_Settings.ModifySettings(cfg, "UN1_STARTVAL", (Surf_4.Thickness - 0.4 / 25.4).ToString()); // Change universal plot settings
UnivPlot_Settings.ModifySettings(cfg, "UN1_STEPS", 20.ToString());
UnivPlot_Settings.ModifySettings(cfg, "UN1_PAR1", 10.ToString());
// For ModifySettings keycodes (UN1_STARTVAL, UN1_STEPS, etc.), see MODIFYSETTINGS page in ZPL>keywords help files
// LoadFrom allows you to load any CFG file, not just default; not available via GUI
UnivPlot_Settings.LoadFrom(cfg);
// Open Spot Diagram to See Result!
Console.WriteLine("Spot has analysis specific settings? " + newSpot.HasAnalysisSpecificSettings.ToString()); // True; no ModifySettings
spotSet.RayDensity = 15;
newSpot.ApplyAndWaitForCompletion();
// save!
TheSystem.Save();
Console.WriteLine("Finished! Press any key to leave...");
Console.ReadKey();
// 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:864
Base interface for all analysis windows. This interface can be accessed via the I_Analyses interface.
Definition: IA_Base.cs:29
bool HasAnalysisSpecificSettings
Gets a value indicating whether this instance has a fully-implemented settings interface available....
Definition: IA_Base.cs:55
IMessage ApplyAndWaitForCompletion()
Re-runs the analysis with the current settings and waits for it to finish calculating.
Base class for all analysis settings interfaces. This class can be accessed via the IA_ interface.
Definition: IAS_Base.cs:25
bool ModifySettings(string settingsFile, string typeCode, string newValue)
Changes a single setting in the specified file. See the MODIFYSETTINGS DDE command help documentation...
bool LoadFrom(string settingsFile)
Replaces the current settings with settings read from the specified file.
Definition: IAS_Spot.cs:50
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...
All data for a Lens Data Editor surface. This interface can be accessed via the ILensDataEditor inter...
Definition: InterfacesLDE.cs:791
double Thickness
Gets or sets the surface thickness.
Definition: InterfacesLDE.cs:1009
ILDEApertureData ApertureData
Gets the surface Aperture data.
Definition: InterfacesLDE.cs:919
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.
Base interface for settings specific to a SurfaceApertureTypes. This interface can be retrieved from ...
Definition: InterfacesLDE.cs:4905
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.
I_Analyses Analyses
Gets the analyses for the current system.
Definition: Interfaces.cs:925
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 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:793
Interfaces and methods for Scaling the Currently active Lens System. This interface can be accessed v...
Definition: Tools.cs:1446
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
Definition: IAS_Spot.cs:5
Definition: IAS_FieldCurvatureAndDistortion.cs:5
Definition: IAS_FieldCurvatureAndDistortion.cs:5
AnalysisIDM
All available analysis types. See the I_Analyses interface for more information.
Definition: I_Analyses.cs:15
Definition: InterfacesLDE.cs:8
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