Example 09 - C#
Last update: 17.07.2025C#
using System;
using ZOSAPI;
using ZOSAPI.Editors.NCE;
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;
// creates new directory
System.IO.Directory.CreateDirectory(strPath);
// Add your custom code here...
// Open new NS system and save
String filename = TheApplication.SamplesDir + "\\API\\CS\\e09_NSC_CAD.zos"; // Define file path and name
// Save New NSC File
TheSystem.SaveAs(filename);
// Insert CAD object
INonSeqEditor NSCE = TheSystem.NCE;
INCERow Obj1 = NSCE.GetObjectAt(1);
Obj1.ZPosition = -5;
// Set object 1 as CAD object if CAD file exists in proper directory
if (System.IO.File.Exists(TheApplication.ObjectsDir + "\\CAD Files\\ExtPoly.stp")) // Check if the CAD part exists in correct directory
{
IObjectTypeSettings Obj1_Type = Obj1.GetObjectTypeSettings(ObjectType.CADPartSTEPIGESSAT); // create CAD object type
Obj1_Type.FileName1 = "ExtPoly.stp"; // set CAD file to be used (file must be in directory specified under OpticStudio Preferences...Folders...Objects)
Obj1.ChangeType(Obj1_Type); // Set Object 1 as the previously specified CAD file
}
else
Console.WriteLine("CAD file not found");
TheSystem.Save();
// Access the CAD settings for object 1
// Determine if the surface-to-face mappint is available for this object.
// If available, update the mapping based on the total number of surfaces in the system
if (Obj1_CAD.HasFaceData)
{
if (Obj1_CAD.NumberOfSurfaces > 1)
{
// Assign the last face to surface 0. Now, the first and last surfaces will be defined with the same scatter and coating properties
Obj1_CAD.SetSurfaceFace(0, Obj1_CAD.NumberOfSurfaces - 1);
}
}
using (System.IO.StreamWriter POBfile = new System.IO.StreamWriter(TheApplication.ObjectsDir + "\\Polygon Objects\\API_cube_demo.POB", false)) // Open new POB file
{
// Append new POB file with polygon definitions (see help files for syntax information)
POBfile.WriteLine("! A cube" + "\n");
POBfile.WriteLine("! front face vertices" + "\n" + "V 1 -1 -1 0" + "\n" + "V 2 1 -1 0" + "\n" + "V 3 1 1 0" + "\n" + "V 4 -1 1 0" + "\n");
POBfile.WriteLine("! back face vertices" + "\n" + "V 5 -1 -1 2" + "\n" + "V 6 1 -1 2" + "\n" + "V 7 1 1 2" + "\n" + "V 8 -1 1 2" + "\n");
POBfile.WriteLine("! Front" + "\n" + "R 1 2 3 4 0 0" + "\n" + "! Back" + "\n" + "R 5 6 7 8 0 0" + "\n");
POBfile.WriteLine("! Top" + "\n" + "R 4 3 7 8 0 0" + "\n" + "! Bottom" + "\n" + "R 1 2 6 5 0 0" + "\n");
POBfile.WriteLine("! Left Side" + "\n" + "R 1 4 8 5 0 0" + "\n" + "! Right Side" + "\n" + "R 2 3 7 6 0 0");
Obj2_Type.FileName1 = "API_cube_demo.POB";
Obj2.ChangeType(Obj2_Type);
}
// opens 3d layout
TheSystem.Save(); // Save the file
Console.WriteLine("Press any key 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:863
Object properties CAD tab. This interface can be accessed via the INCERow interface.
Definition: InterfacesNCE.cs:6972
int NumberOfSurfaces
Definition: InterfacesNCE.cs:7010
bool SetSurfaceFace(int surfaceIdx, int face)
Sets the face assigned to the specified surface.
All data for a Non-Sequential Component Editor object. This interface can be accessed via the INonSeq...
Definition: InterfacesNCE.cs:1198
INCECADData CADData
Gets the object CAD data.
Definition: InterfacesNCE.cs:1416
bool ChangeType(IObjectTypeSettings settings)
Changes the current object to the specified type. Use GetObjectTypeSettings to get the relevant setti...
IObjectTypeSettings GetObjectTypeSettings(ObjectType type)
Create the settings for the specified type. Use this method to specify any extra data required to cha...
This interface defines all properties and methods needed to interact with the Non-Sequential Componen...
Definition: InterfacesNCE.cs:363
INCERow GetObjectAt(int ObjectNumber)
Gets the object at the specified position.
INCERow InsertNewObjectAt(int ObjectNumber)
Inserts a new object at the specified position.
This interface is used for selecting any files required by a object, and for changing object types....
Definition: InterfacesNCE.cs:1110
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.
I_Analyses Analyses
Gets the analyses for the current system.
Definition: Interfaces.cs:928
INonSeqEditor NCE
Gets the non-sequential component editor.
Definition: Interfaces.cs:887
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:264
IOpticalSystem CreateNewSystem(SystemType mode)
Creates a new empty optical system, in the specified mode.
string ObjectsDir
Gets the Objects directory (e.g. C:\Users\John.Doe\My Documents\Zemax\OBJECTS).
Definition: Interfaces.cs:411
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
Definition: IAS_FieldCurvatureAndDistortion.cs:5
Definition: InterfacesNCE.cs:19
ObjectType
All supported non-sequential object types.
Definition: InterfacesNCE.cs:38
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5