CSharpStandalone_17_NSC_BulkScatter
Last update: 17.07.2025using System;
using ZOSAPI;
using ZOSAPI.Analysis;
using ZOSAPI.Analysis.Settings;
using ZOSAPI.Editors.NCE;
using ZOSAPI.Tools.RayTrace;
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...
// NSC (bulk_volume)
// This example recreates Non-sequential\Scattering\Bulk Scatter.zos
// creates new directory
System.IO.Directory.CreateDirectory(strPath);
//Create a New File
TheSystem.MakeNonSequential();
//Define path locations
string SamplesFolder = TheApplication.SamplesDir;
string SampleFile = System.IO.Path.Combine(TheApplication.SamplesDir, "API\\CS\\e17_NSC_BulkdScatter.zos");
TheSystem.SaveAs(SampleFile);
//Non-sequential component editor
TheNCE = TheSystem.NCE;
INCERow Object_2 = TheNCE.InsertNewObjectAt(2);
//Source point
IObjectTypeSettings oType_1 = Object_1.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.SourcePoint);
Object_1.ChangeType(oType_1);
Source1_data.NumberOfLayoutRays = 3;
Source1_data.NumberOfAnalysisRays = 1000000;
//Rectangular Volume
//Scattering properties
//Draw:opacity set to 50%
IObjectTypeSettings oType_2 = Object_2.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.RectangularVolume);
Object_2.ChangeType(oType_2);
Object_2.ZPosition = 2;
Object_2.Material = "N-BK7";
RectVolume2_data.X1HalfWidth = 12;
RectVolume2_data.Y1HalfWidth = 12;
RectVolume2_data.ZLength = 40;
RectVolume2_data.X2HalfWidth = 12;
RectVolume2_data.Y2HalfWidth = 12;
RectVolume2_volphysdata.Model = VolumePhysicsModelType.AngleScattering;
RectVolume2_volphysdata.ModelSettings._S_AngleScattering.MeanPath = 5;
RectVolume2_volphysdata.ModelSettings._S_AngleScattering.Angle = 30;
//Detector Rectangle
IObjectTypeSettings oType_3 = Object_3.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorRectangle);
Object_3.RefObject = 2;
Object_3.ZPosition = 42;
Object_3.Material = "ABSORB";
Object_3.ChangeType(oType_3);
DetRect3_data.XHalfWidth = 15;
DetRect3_data.YHalfWidth = 15;
DetRect3_data.NumberXPixels = 25;
DetRect3_data.NumberYPixels = 25;
DetRect3_data.DataType = 0;
DetRect3_data.Color = 2;
DetRect3_data.Smoothing = 1;
TheSystem.SaveAs(SampleFile);
//Open a shaded model
analysis.Terminate();
analysis.WaitForCompletion();
string cfgFile = System.IO.Path.GetTempFileName();
// Save the current settings to the temp file
analysisSettings.SaveTo(cfgFile);
// make your modifications to it
//MODIFYSETTINGS are defined in the ZPL help files: The Programming Tab > About the ZPL > Keywords
// now load in the modified settings
analysisSettings.LoadFrom(cfgFile);
// If you want to overwrite your default CFG, copy it after you are done modifying the settings:
//string CFG_fullname = "\\zmedc02\\users\\sandrine.auriol\\Documents\\Zemax\\Configs\\POP.CFG";
//System.IO.File.Copy(cfgFile, CFG_fullname, true);
// We don't need the temp file any more, so delete it
System.IO.File.Delete(cfgFile);
// Run the analysis with the new settings
analysis.ApplyAndWaitForCompletion();
//Open a detector viewer
analysis = TheSystem.Analyses.New_Analysis(AnalysisIDM.DetectorViewer);
analysisSettings = analysis.GetSettings();
cfgFile = System.IO.Path.GetTempFileName();
analysisSettings.SaveTo(cfgFile);
analysisSettings.LoadFrom(cfgFile);
System.IO.File.Delete(cfgFile);
analysis.ApplyAndWaitForCompletion();
//Run a ray trace
NSCRayTrace.SplitNSCRays = false;
NSCRayTrace.ScatterNSCRays = true;
NSCRayTrace.UsePolarization = false;
NSCRayTrace.IgnoreErrors = true;
NSCRayTrace.SaveRays = false;
NSCRayTrace.ClearDetectors(0);
NSCRayTrace.RunAndWaitForCompletion();
NSCRayTrace.Close();
TheSystem.SaveAs(SampleFile);
// 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
Base interface for all analysis windows. This interface can be accessed via the I_Analyses interface.
Definition: IA_Base.cs:29
void WaitForCompletion()
Waits for the current analysis to finish running (if applicable).
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 SaveTo(string settingsFile)
Saves the current settings to the specified file.
bool LoadFrom(string settingsFile)
Replaces the current settings with settings read from the specified file.
Object properties Draw tab. This interface can be accessed via the INCERow interface.
Definition: InterfacesNCE.cs:5789
All data for a Non-Sequential Component Editor object. This interface can be accessed via the INonSeq...
Definition: InterfacesNCE.cs:1198
INCEVolumePhysicsData VolumePhysicsData
Gets the object Volume Physics data.
Definition: InterfacesNCE.cs:1383
INCEDrawData DrawData
Gets the object Draw data.
Definition: InterfacesNCE.cs:1341
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...
Object properties Volume Physics tab. This interface can be accessed via the INCERow interface.
Definition: InterfacesNCE.cs:6551
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.
Definition: InterfacesNCE.cs:3274
Definition: InterfacesNCE.cs:4582
Definition: InterfacesNCE.cs:4937
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.
ISystemData SystemData
Data for configuring everything in the System Explorer.
Definition: Interfaces.cs:848
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:928
INonSeqEditor NCE
Gets the non-sequential component editor.
Definition: Interfaces.cs:887
bool MakeNonSequential()
Makes the system non-sequential if it is not already. Note that any sequential data will be lost.
IOpticalSystemTools Tools
Gets an interface used to run various tools on the optical system.
Definition: Interfaces.cs:935
This interface contains all information about the current ZOS-API connection, as well as methods for ...
Definition: Interfaces.cs:264
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
bool RunAndWaitForCompletion()
Sames as calling Run followed by WaitForCompletion.
Interfaces and methods for running a non-sequential ray trace. This interface can be accessed via the...
Definition: RayTrace.cs:45
ErrorType ClearDetectors(int DetectorNumber)
Clears the specified detectors.
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: InterfacesCommon.cs:9
Definition: InterfacesNCE.cs:19
Definition: InterfacesEditors.cs:12
Definition: RayTrace.cs:12
The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...
Definition: IAS_FieldCurvatureAndDistortion.cs:5