    

 ## On this page

  

 

 # Example 20 - C++ 

 Last update: 16.07.2025 

# <a class="anchor" id="ex20_s2"></a>C++

// CppStandaloneApplication.cpp : Defines the entry point for the console application.

 

\#include "stdafx.h"

\#include &lt;stdlib.h&gt;

\#include &lt;stdio.h&gt;

\#include &lt;iostream&gt;

\#include &lt;string&gt;

\#include &lt;ctime&gt;

\#include &lt;functional&gt;

\#include &lt;assert.h&gt;

 

// Note - .tlh files will be generated from the .tlb files (above) once the project is compiled.

// Visual Studio will incorrectly continue to report IntelliSense error messages however until it is restarted.

\#include "zosapi.h"

 

using namespace std;

using namespace [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

using namespace ZOSAPI_Interfaces;

 

void handleError(std::string msg);

void logInfo(std::string msg);

void finishStandaloneApplication(IZOSAPI_ApplicationPtr TheApplication);

 

int RunApplication()

{

 CoInitialize(NULL);

 

 // Create the initial connection class

 IZOSAPI_ConnectionPtr TheConnection(__uuidof([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

 IZOSAPI_ApplicationPtr TheApplication = TheConnection-&gt;CreateNewApplication();

 if (TheApplication == nullptr)

 {

 handleError("An unknown error occurred!");

 return -1;

 }

 

 // Check the connection status

 if (!TheApplication-&gt;IsValidLicenseForAPI)

 {

 handleError("License check failed!");

 return -1;

 }

 if (TheApplication-&gt;Mode != ZOSAPI_Mode::ZOSAPI_Mode_Server)

 {

 handleError("Standlone application was started in the incorrect mode!");

 return -1;

 }

 

 IOpticalSystemPtr TheSystem = TheApplication-&gt;PrimarySystem;

 

 // Add your custom code here...

 

 // Load a non-sequential file

 TheSystem-&gt;LoadFile(TheApplication-&gt;SamplesDir + "\\\\Non-Sequential\\\\Miscellaneous\\\\Digital\_projector\_flys\_eye\_homogenizer.zos", false);

 

 

 // Get interface of IExportCAD

 IExportCADPtr ToolExportCAD = TheSystem-&gt;Tools-&gt;OpenExportCAD();

 

 // default option settings

 ToolExportCAD-&gt;FirstObject = 1;

 ToolExportCAD-&gt;LastObject = 8;

 ToolExportCAD-&gt;RayLayer = 1;

 ToolExportCAD-&gt;LensLayer = 0;

 ToolExportCAD-&gt;DummyThickness = 1;

 ToolExportCAD-&gt;SplineSegments = SplineSegmentsType_N_032;

 ToolExportCAD-&gt;FileType = CADFileType_STEP;

 ToolExportCAD-&gt;Tolerance = CADToleranceType_N_TenEMinus4;

 ToolExportCAD-&gt;SetCurrentConfiguration();

 // For other configuration choices, use following methods.

 //ToolExportCAD-&gt;SetConfigurationAllAtOnce();

 //ToolExportCAD-&gt;SetConfigurationAllByFile();

 //ToolExportCAD-&gt;SetConfigurationAllByLayer();

 //ToolExportCAD-&gt;SetSingleConfiguration(1);

 

 // default check boxes settings

 ToolExportCAD-&gt;SurfacesAsSolids = true;

 ToolExportCAD-&gt;ScatterNSCRays = false;

 ToolExportCAD-&gt;ExportDummySurfaces = false;

 ToolExportCAD-&gt;SplitNSCRays = false;

 ToolExportCAD-&gt;UsePolarization = false;

 

 // set output file name

 ToolExportCAD-&gt;OutputFileName = TheApplication-&gt;ObjectsDir + "\\\\CAD Files\\\\API\_CADexport\_sample.step";

 

 // Starting exporting

 // Run with a 3 minites timeout

 // Run() and WaitWithTimeout() is inherited from interface ISystemTool.

 // In C++ we need to cast before using these methods.

 cout &lt;&lt; "Starting exporting..." &lt;&lt; endl;

 ISystemToolPtr baseTool = ToolExportCAD;

 baseTool-&gt;Run();

 [RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11) runstatus = baseTool-&gt;WaitWithTimeout(3 * 60);

 

 // Report the status

 switch (runstatus)

 {

 case RunStatus_Completed:

 cout &lt;&lt; "Completed!" &lt;&lt; endl;

 break;

 case RunStatus_FailedToStart:

 cout &lt;&lt; "Failed To Start!" &lt;&lt; endl;

 break;

 case RunStatus_InvalidTimeout:

 cout &lt;&lt; "Invalid Timeout!" &lt;&lt; endl;

 break;

 case RunStatus_TimedOut:

 cout &lt;&lt; "Timed Out!" &lt;&lt; endl;

 break;

 }

 cout &lt;&lt; "Progress: " &lt;&lt; baseTool-&gt;Progress &lt;&lt; endl;

 

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

 if (runstatus != RunStatus_Completed &amp; baseTool-&gt;CanCancel)

 {

 baseTool-&gt;Cancel();

 }

 

 // Close the tool

 baseTool-&gt;Close();

 

\#if defined(\_DEBUG)

 // keeps console open when in debug mode

 system("pause");

\#endif

 

 // Clean up

 finishStandaloneApplication(TheApplication);

 

 

 return 0;

}

 

void handleError(std::string msg)

{

 throw new exception(msg.c_str());

}

 

void logInfo(std::string msg)

{

 printf("%s", msg.c_str());

}

 

void finishStandaloneApplication(IZOSAPI_ApplicationPtr TheApplication)

{

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

 if (TheApplication != nullptr)

 {

 TheApplication-&gt;CloseApplication();

 }

}

 

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)

{

 return RunApplication();

}

 

int _tmain(int argc, _TCHAR* argv[])

{

 return RunApplication();

}

[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.Tools.RunStatus](namespace_z_o_s_a_p_i_1_1_tools.xhtml#a3a955211d48653a7f845f495982c2e11)

RunStatus

**Definition:** Tools.cs:516



[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