    

 ## On this page

  

 

 # CSharpStandalone\_20\_export\_CAD\_File

 Last update: 17.07.2025 

using System;

using [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

using [ZOSAPI.Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml);

using [ZOSAPI.Tools.General](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml);

 

 

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

 <a id="_a0" name="_a0"></a>[ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml) TheConnection = new [ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)();

 

 // Attempt to create a Standalone connection

 <a id="_a1" name="_a1"></a>[IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication = TheConnection.<a id="a2" name="a2"></a>[CreateNewApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aadc54af7d7415a765592acf23d3a08ef)();

 if (TheApplication == null)

 {

 HandleError("An unknown connection error occurred!");

 return;

 }

 

 // Check the connection status

 if (!TheApplication.<a id="a3" name="a3"></a>[IsValidLicenseForAPI](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a5b8d6c216c668d13d6dcb870698c247f))

 {

 HandleError("Failed to connect to OpticStudio: " + TheApplication.<a id="a4" name="a4"></a>[LicenseStatus](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a95179eb8038b629caa12397723aacb3a));

 return;

 }

 if (TheApplication.<a id="a5" name="a5"></a>[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19) != [ZOSAPI\_Mode](namespace_z_o_s_a_p_i.xhtml#a254f5f6cb4ff37c2245b456b9bb79d2a).Server)

 {

 HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19).ToString());

 return;

 }

 

 <a id="_a6" name="_a6"></a>[IOpticalSystem](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml) TheSystem = TheApplication.<a id="a7" name="a7"></a>[PrimarySystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#aca8045fe84351622e0ee58814a2a3364);

 

 // Add your custom code here...

 // Load a non-sequential file

 TheSystem.<a id="a8" name="a8"></a>[LoadFile](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a6eb1455ac53555ae6f30032d85b63735)(TheApplication.<a id="a9" name="a9"></a>[SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb) + "\\\\Non-Sequential\\\\Miscellaneous\\\\Digital\_projector\_flys\_eye\_homogenizer.zos", false);

 

 // Get interface of IExportCAD

 <a id="_a10" name="_a10"></a>[IExportCAD](interface_z_o_s_a_p_i_1_1_tools_1_1_general_1_1_i_export_c_a_d.xhtml) ToolExportCAD = TheSystem.<a id="a11" name="a11"></a>[Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8).OpenExportCAD();

 

 // default option settings

 ToolExportCAD.FirstObject = 1;

 ToolExportCAD.LastObject = 8;

 ToolExportCAD.RayLayer = 1;

 ToolExportCAD.LensLayer = 0;

 ToolExportCAD.DummyThickness = 1;

 ToolExportCAD.SplineSegments = [SplineSegmentsType](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml#ac7bc92335d84b37013503e1b92a28e22).N_032;

 ToolExportCAD.FileType = [CADFileType](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml#aa8038711502dd1a7ebe3b181d7f7d2a1).STEP;

 ToolExportCAD.Tolerance = [CADToleranceType](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml#aa99cc7ff7bdef0cc7700d4da164b2668).N_TenEMinus4;

 ToolExportCAD.<a id="a12" name="a12"></a>[SetCurrentConfiguration](interface_z_o_s_a_p_i_1_1_tools_1_1_general_1_1_i_export_c_a_d.xhtml#a26fd31e24262a5ff040a5e170fedfc28)();

 // For other configuration choices, use following methods.

 //ToolExportCAD.SetConfigurationAllAtOnce();

 //ToolExportCAD.SetConfigurationAllByFile();

 //ToolExportCAD.SetConfigurationAllByLayer();

 //ToolExportCAD.SetSingleConfiguration(1);

 

 // default check boxes settings

 ToolExportCAD.SurfacesAsSolids = true;

 ToolExportCAD.ScatterNSCRays = false;

 ToolExportCAD.ExportDummySurfaces = false;

 ToolExportCAD.SplitNSCRays = false;

 ToolExportCAD.UsePolarization = false;

 

 // set output file name

 ToolExportCAD.OutputFileName = TheApplication.ObjectsDir + "\\\\CAD Files\\\\e20\_export\_CAD\_File.step";

 

 // Starting exporting

 // Run with a 3 minites timeout

 Console.Write("Starting exporting...");

 ToolExportCAD.<a id="a13" name="a13"></a>[Run](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a4cf991fab30856f8aeb987f4ad19582b)();

 [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11) runstatus = ToolExportCAD.<a id="a14" name="a14"></a>[WaitWithTimeout](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#aed6c681640c06ec6fa7dc4fd8d947394)(3 * 60);

 

 // Report the status

 switch (runstatus)

 {

 case [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11).Completed:

 Console.WriteLine("Completed!");

 break;

 case [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11).FailedToStart:

 Console.WriteLine("Failed To Start!");

 break;

 case [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11).InvalidTimeout:

 Console.WriteLine("Invalid Timeout!");

 break;

 case [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11).TimedOut:

 Console.WriteLine("Timed Out!");

 break;

 }

 Console.WriteLine("Progress: " + ToolExportCAD.<a id="a15" name="a15"></a>[Progress](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#af8f3521e7190d895d9774c38ca51276b).ToString("000") + "%");

 

 // If the exporting is not completed and can be cancelled, cancel the work

 if (runstatus != [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11).Completed &amp; ToolExportCAD.<a id="a16" name="a16"></a>[CanCancel](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#ac4f45510d803f3b4d3cff327ca410395))

 {

 ToolExportCAD.<a id="a17" name="a17"></a>[Cancel](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a7802878e38f79b8d8c42a22c83a174b6)();

 }

 

 // Close the tool

 ToolExportCAD.<a id="a18" name="a18"></a>[Close](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a5bfa126385b9d0c5619b1aff9a354d31)();

 

 Console.Write("Press any key to continue...");

 Console.ReadKey();

 

 // Clean up

 FinishStandaloneApplication(TheApplication);

 }

 

 static void FinishStandaloneApplication([IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication)

 {

 // Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases

 if (TheApplication != null)

 {

 TheApplication.<a id="a19" name="a19"></a>[CloseApplication](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ab662a3cc1b97d8632a324b1137fa943b)();

 }

 }

 

 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);

 }

 

 }

}

[ZOSAPI.ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)

**Definition:** ZemaxService.cs:198



[ZOSAPI.ZOSAPI\_Connection.CreateNewApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aadc54af7d7415a765592acf23d3a08ef)

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



[ZOSAPI.IOpticalSystem](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml)

Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....

**Definition:** Interfaces.cs:687



[ZOSAPI.IOpticalSystem.LoadFile](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#a6eb1455ac53555ae6f30032d85b63735)

bool LoadFile(string LensFile, bool saveIfNeeded)

Replaces the current system with data from the specified file.



[ZOSAPI.IOpticalSystem.Tools](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml#add3e3631eadf7955b7b341179ff1eff8)

IOpticalSystemTools Tools

Gets an interface used to run various tools on the optical system.

**Definition:** Interfaces.cs:932



[ZOSAPI.IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml)

This interface contains all information about the current ZOS-API connection, as well as methods for ...

**Definition:** Interfaces.cs:261



[ZOSAPI.IZOSAPI\_Application.IsValidLicenseForAPI](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a5b8d6c216c668d13d6dcb870698c247f)

bool IsValidLicenseForAPI

Gets a value indicating whether this the API is currently useable.

**Definition:** Interfaces.cs:287



[ZOSAPI.IZOSAPI\_Application.Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19)

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



[ZOSAPI.IZOSAPI\_Application.LicenseStatus](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a95179eb8038b629caa12397723aacb3a)

LicenseStatusType LicenseStatus

Gets the license status. Note that this displays the license edition if successful,...

**Definition:** Interfaces.cs:280



[ZOSAPI.IZOSAPI\_Application.CloseApplication](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ab662a3cc1b97d8632a324b1137fa943b)

void CloseApplication()

Shut down the Optic Studio process.



[ZOSAPI.IZOSAPI\_Application.PrimarySystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#aca8045fe84351622e0ee58814a2a3364)

IOpticalSystem PrimarySystem

Gets the primary system. When Mode is ZOSAPI_Mode.Server, this will initially be an empty sequential ...

**Definition:** Interfaces.cs:328



[ZOSAPI.IZOSAPI\_Application.SamplesDir](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ae8fc2a9409927955cc362f9262b052bb)

string SamplesDir

Gets the full path for the current user's samples directory (in the My Documents\Zemax\Samples\ folde...

**Definition:** Interfaces.cs:505



[ZOSAPI.Tools.General.IExportCAD](interface_z_o_s_a_p_i_1_1_tools_1_1_general_1_1_i_export_c_a_d.xhtml)

Interfaces and methods for exporting the current system to various CAD formats. This interface can be...

**Definition:** Tools.cs:1104



[ZOSAPI.Tools.General.IExportCAD.SetCurrentConfiguration](interface_z_o_s_a_p_i_1_1_tools_1_1_general_1_1_i_export_c_a_d.xhtml#a26fd31e24262a5ff040a5e170fedfc28)

void SetCurrentConfiguration()



[ZOSAPI.Tools.ISystemTool.Run](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a4cf991fab30856f8aeb987f4ad19582b)

bool Run()

Start the tool. Note that for synchronous tools (see IsAsynchronous), this call will block until comp...



[ZOSAPI.Tools.ISystemTool.Close](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a5bfa126385b9d0c5619b1aff9a354d31)

bool Close()

Closes this tool and frees up and associated resources.



[ZOSAPI.Tools.ISystemTool.Cancel](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#a7802878e38f79b8d8c42a22c83a174b6)

bool Cancel()

Cancels the currently running asynchronous tool.



[ZOSAPI.Tools.ISystemTool.CanCancel](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#ac4f45510d803f3b4d3cff327ca410395)

bool CanCancel

Gets a value indicating whether the tool can be cancelled.

**Definition:** Tools.cs:564



[ZOSAPI.Tools.ISystemTool.WaitWithTimeout](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#aed6c681640c06ec6fa7dc4fd8d947394)

RunStatus WaitWithTimeout(double timeOutSeconds)

Waits for the currently running tool to complete, with a specified time out value....



[ZOSAPI.Tools.ISystemTool.Progress](interface_z_o_s_a_p_i_1_1_tools_1_1_i_system_tool.xhtml#af8f3521e7190d895d9774c38ca51276b)

int Progress

Gets the progress of the current tool, if supported.

**Definition:** Tools.cs:542



[ZOSAPI.Tools.General](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml)

**Definition:** Tools.cs:697



[ZOSAPI.Tools.General.CADFileType](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml#aa8038711502dd1a7ebe3b181d7f7d2a1)

CADFileType

**Definition:** Tools.cs:1022



[ZOSAPI.Tools.General.CADToleranceType](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml#aa99cc7ff7bdef0cc7700d4da164b2668)

CADToleranceType

**Definition:** Tools.cs:1055



[ZOSAPI.Tools.General.SplineSegmentsType](namespace_z_o_s_a_p_i_1_1_tools_1_1_general.xhtml#ac7bc92335d84b37013503e1b92a28e22)

SplineSegmentsType

**Definition:** Tools.cs:1010



[ZOSAPI.Tools](namespace_z_o_s_a_p_i_1_1_tools.xhtml)

**Definition:** FileSource.cs:4



[ZOSAPI.Tools.RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11)

RunStatus

**Definition:** Tools.cs:509



[ZOSAPI](namespace_z_o_s_a_p_i.xhtml)

The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...

**Definition:** IAS_FieldCurvatureAndDistortion.cs:5



[ZOSAPI.ZOSAPI\_Mode](namespace_z_o_s_a_p_i.xhtml#a254f5f6cb4ff37c2245b456b9bb79d2a)

ZOSAPI_Mode

**Definition:** Interfaces.cs:77