CSharpStandalone_03_open_file_and_optimise
Last update: 17.07.2025using System;
using ZOSAPI;
using ZOSAPI.Editors.LDE;
using ZOSAPI.Editors.MFE;
using ZOSAPI.Tools.Optimization;
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;
}
// Add your custom code here...
// creates a new API directory
System.IO.Directory.CreateDirectory(strPath);
// Set up primary optical system
TheSystem = TheApplication.PrimarySystem;
string sampleDir = TheApplication.SamplesDir;
// Open file & save a copy
string testFile = sampleDir + "\\API\\CS\\e01_new_file_and_quickfocus.zos";
if (!System.IO.File.Exists(testFile))
{
Console.WriteLine("You need to run Example 01 before running this example");
Console.ReadKey();
return;
}
string testFile2 = sampleDir + "\\API\\CS\\e03_open_file_and_optimise.zos";
TheSystem.SaveAs(testFile2);
// Get Surfaces
ILDERow Surface_2 = TheLDE.GetSurfaceAt(2);
ILDERow Surface_3 = TheLDE.GetSurfaceAt(3);
// Make thicknesses and radii variable
Surface_2.ThicknessCell.MakeSolveVariable();
Surface_3.ThicknessCell.MakeSolveVariable();
// Merit functions
Operand_1.Target = 0.0;
Operand_1.Weight = 10.0;
Operand_2.ChangeType(MeritOperandType.COMA);
Operand_2.Target = 0.0;
Operand_2.Weight = 1.0;
// Air min/max
Operand_3.ChangeType(MeritOperandType.MNCA);
Operand_3.Target = 0.5;
Operand_3.Weight = 1.0;
Operand_3.GetCellAt(3).IntegerValue = 3;
IMFERow Operand_4 = TheMFE.AddOperand();
Operand_4.ChangeType(MeritOperandType.MXCA);
Operand_4.Target = 1000;
Operand_4.Weight = 1.0;
Operand_4.GetCellAt(2).IntegerValue = 1;
Operand_4.GetCellAt(3).IntegerValue = 3;
IMFERow Operand_5 = TheMFE.AddOperand();
Operand_5.ChangeType(MeritOperandType.MNEA);
Operand_5.Target = 0.5;
Operand_5.Weight = 1.0;
Operand_5.GetCellAt(2).IntegerValue = 1;
Operand_5.GetCellAt(3).IntegerValue = 3;
// Material min/max
IMFERow Operand_6 = TheMFE.AddOperand();
Operand_6.ChangeType(MeritOperandType.MNCG);
Operand_6.Target = 3.0;
Operand_6.Weight = 1.0;
Operand_6.GetCellAt(2).IntegerValue = 1;
Operand_6.GetCellAt(3).IntegerValue = 3;
IMFERow Operand_7 = TheMFE.AddOperand();
Operand_7.ChangeType(MeritOperandType.MXCG);
Operand_7.Target = 15.0;
Operand_7.Weight = 1.0;
Operand_7.GetCellAt(2).IntegerValue = 1;
Operand_7.GetCellAt(3).IntegerValue = 3;
IMFERow Operand_8 = TheMFE.AddOperand();
Operand_8.ChangeType(MeritOperandType.MNEG);
Operand_8.Target = 3.0;
Operand_8.Weight = 1.0;
Operand_8.GetCellAt(2).IntegerValue = 1;
Operand_8.GetCellAt(3).IntegerValue = 3;
// Local optimisation till completion
LocalOpt.Algorithm = OptimizationAlgorithm.DampedLeastSquares;
LocalOpt.Cycles = OptimizationCycles.Automatic;
LocalOpt.NumberOfCores = 8;
LocalOpt.RunAndWaitForCompletion();
LocalOpt.Close();
// Hammer for 10 seconds
Console.WriteLine("Running Hammer for 10 seconds");
HammerOpt.Cancel();
HammerOpt.WaitForCompletion();
HammerOpt.Close();
// Save and close
TheSystem.Save();
Console.WriteLine("Press ENTER to continue");
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
bool MakeSolveVariable()
Sets the solve type to SolveType.Variable.
int IntegerValue
Gets or sets the integer value of this cell. Note that if DataType is not CellDataType....
Definition: InterfacesEditors.cs:319
IEditorCell GetCellAt(int pos)
Gets the cell at the specified index (IEditor.MinColumn to IEditor.MaxColumn).
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.
All data for a Merit Function Editor operand. This interface can be accessed via the IMeritFunctionEd...
Definition: InterfacesMFE.cs:697
bool ChangeType(MeritOperandType type)
Changes to the specified operand type.
This interface defines all properties and methods needed to interact with the MRit Function Editor....
Definition: InterfacesMFE.cs:492
IMFERow AddOperand()
Adds a new operand as the last operand in the system.
IMFERow InsertNewOperandAt(int OperandNumber)
Inserts a new operand at the specified position.
IMFERow GetOperandAt(int OperandNumber)
Gets the data for the specified operand.
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.
bool LoadFile(string LensFile, bool saveIfNeeded)
Replaces the current system with data from the specified file.
IMeritFunctionEditor MFE
Gets the merit function editor.
Definition: Interfaces.cs:910
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
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
bool WaitForCompletion()
Waits for the asynchronous tool to complete.
RunStatus RunAndWaitWithTimeout(double timeOutSeconds)
Starts the tool running, and waits for it to complete with the specified timeout value....
Interfaces and methods for running a Hammer Optimization. This interface can be accessed via the IOpt...
Definition: Optimization.cs:170
Interfaces and methods for running a Local Optimization. This interface can be accessed via the IOpti...
Definition: Optimization.cs:50
Definition: InterfacesLDE.cs:8
Definition: InterfacesMFE.cs:12
MeritOperandType
All supported Merit Function operands.
Definition: InterfacesMFE.cs:19
Definition: Optimization.cs:9
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5