Skip to main content

ZOS-API interface 2024 R1

Example 26 - C#

Last update: 17.07.2025

C#

using System;
using ZOSAPI;
namespace CSharpStandaloneApplication4
{
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();
// Define a Preferences File.
// Preferences file is defined on the IZOSAPIConnection interface (prior to connecting to the API)
// If no PreferencesFile is defined it will use the default OpticStudio.CFG file however changes will not persist between sessions.
// If a PreferencesFile is defined, then any changes will save automatically.
Console.WriteLine("===PreferencesFile===");
string cfgFile = @"C:\Users\Documents\Zemax\Configs\OpticStudio.CFG";
if (System.IO.File.Exists(cfgFile))
{
TheConnection.PreferencesFile = cfgFile;
Console.WriteLine("PreferencesFile: " + TheConnection.PreferencesFile);
}
else
{
Console.WriteLine("Default OpticStudio.CFG prefernces used");
}
// 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...
// Define variables for OpticStudio Preferences
IPreferences Preference = TheApplication.Preferences;
IPreferencesGeneral PrefG = Preference.General;
// Read and print the initial settings
Console.WriteLine("\n===Check Settings===");
Console.WriteLine("DateTimeFormat: " + PrefG.DateTimeFormat);
Console.WriteLine("Language: " + PrefG.Language);
Console.WriteLine("ZMXFileEncoding: " + PrefG.ZMXFileEncoding);
Console.WriteLine("TXTFileEncoding: " + PrefG.TXTFileEncoding);
Console.WriteLine("UseSessionFiles: " + PrefG.UseSessionFiles);
Console.WriteLine("IncludeCalculatedDataInsession: " + PrefG.IncludeCalculatedDataInSession);
Console.WriteLine("UpdateMostRecentlyUsedList: " + PrefG.UpdateMostRecentlyUsedList);
Console.WriteLine("UserPreferences: " + PrefG.UserPreferences);
// Reset the settings to default
Preference.ResetToDefaults();
// Set the settings
PrefG.DateTimeFormat = ZOSAPI.Preferences.DateTimeType.DateTime;
PrefG.Language = ZOSAPI.Preferences.LanguageType.English;
PrefG.ZMXFileEncoding = ZOSAPI.Preferences.EncodingType.ANSI;
PrefG.TXTFileEncoding = ZOSAPI.Preferences.EncodingType.ANSI;
PrefG.UseSessionFiles = false;
PrefG.IncludeCalculatedDataInSession = false;
PrefG.UpdateMostRecentlyUsedList = false;
PrefG.UserPreferences = "Never gonna run around and desert you";
Console.WriteLine("\n===Final Settings===");
Console.WriteLine("DateTimeFormat: " + PrefG.DateTimeFormat);
Console.WriteLine("Language: " + PrefG.Language);
Console.WriteLine("ZMXFileEncoding: " + PrefG.ZMXFileEncoding);
Console.WriteLine("TXTFileEncoding: " + PrefG.TXTFileEncoding);
Console.WriteLine("UseSessionFiles: " + PrefG.UseSessionFiles);
Console.WriteLine("IncludeCalculatedDataInsession: " + PrefG.IncludeCalculatedDataInSession);
Console.WriteLine("UpdateMostRecentlyUsedList: " + PrefG.UpdateMostRecentlyUsedList);
Console.WriteLine("UserPreferences: " + PrefG.UserPreferences);
Console.WriteLine("\nPress 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
string PreferencesFile
For standalone ZOS-API applications, this controls the file that will be used to specify all OpticStu...
Definition: ZemaxService.cs:1339
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
Definition: InterfacesPreferences.cs:16
IPreferencesGeneral General
Definition: InterfacesPreferences.cs:18
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:264
IPreferences Preferences
Definition: Interfaces.cs:645
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
Allows configuration of settings from the OpticStudio Preferences -> General tab in OpticStudio....
Definition: InterfacesPreferences.cs:89
EncodingType ZMXFileEncoding
Definition: InterfacesPreferences.cs:92
bool UseSessionFiles
Definition: InterfacesPreferences.cs:94
DateTimeType DateTimeFormat
Definition: InterfacesPreferences.cs:90
bool UpdateMostRecentlyUsedList
This setting can be used to turn updating of the most recently used lens files in File > Open on or o...
Definition: InterfacesPreferences.cs:100
EncodingType TXTFileEncoding
Definition: InterfacesPreferences.cs:93
bool IncludeCalculatedDataInSession
Definition: InterfacesPreferences.cs:95
string UserPreferences
This is a single string in which you can store anything you want. It is saved in your Preferences con...
Definition: InterfacesPreferences.cs:104
LanguageType Language
Definition: InterfacesPreferences.cs:91
Definition: InterfacesPreferences.cs:30
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