    

 ## On this page

  

 

 # Example 09 - C++ 

 Last update: 17.07.2025 

# <a class="anchor" id="ex09_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;

\#include &lt;fstream&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...

 

 // creates a new API directory

 CreateDirectory(_bstr_t(TheApplication-&gt;SamplesDir + "\\\\API"), NULL);

 CreateDirectory(_bstr_t(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP"), NULL);

 

 // Open new Non-Sequential system and save

 TheSystem = TheApplication-&gt;CreateNewSystem(SystemType_NonSequential);

 // Define file path and name

 _bstr_t filename = TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e09\_NSC\_CAD.zos";

 // Save New NSC File

 TheSystem-&gt;SaveAs(filename);

 

 // Insert CAD object

 INonSeqEditorPtr NSCE = TheSystem-&gt;NCE;

 INCERowPtr Obj1 = NSCE-&gt;GetObjectAt(1);

 Obj1-&gt;ZPosition = -5;

 // Define the STP file to search for.

 std::ifstream ifile((string)TheApplication-&gt;ObjectsDir + "\\\\CAD Files\\\\ExtPoly.stp");

 if (ifile) // Check if the CAD part exists in correct directory. Directory is defined under OpticStudio Preferences...Folders...Objects.

 {

 IObjectTypeSettingsPtr Obj1_Type = Obj1-&gt;GetObjectTypeSettings(ObjectType_CADPartSTEPIGESSAT); // create CAD object type

 Obj1_Type-&gt;FileName1 = "ExtPoly.stp"; // set CAD file to be used (file must be in valid directory)

 Obj1-&gt;ChangeType(Obj1_Type); // Set Object 1 as the previously specified CAD file

 }

 else

 cout &lt;&lt; "CAD file not found" &lt;&lt; endl;

 

 TheSystem-&gt;Save();

 

 // Access the CAD settings for object 1

 INCECADDataPtr Obj1_CAD = Obj1-&gt;CADData; // Retrieve CAD data

 

 // Determine if surface-to-face mapping is available for this object. If yes, update the mapping.

 if (Obj1_CAD-&gt;HasFaceData)

 {

 if (Obj1_CAD-&gt;NumberOfSurfaces &gt; 1) 

 {

 // Assign last face to surface 0. Now, the first and last surfaces will be defined with the same scatter and coating properties

 Obj1_CAD-&gt;SetSurfaceFace(0, Obj1_CAD-&gt;NumberOfSurfaces - 1);

 }

 }

 

 fstream POBfile;

 string filepath = TheApplication-&gt;ObjectsDir + "\\\\Polygon Objects\\\\API\_cube\_demo.POB";

 POBfile.open(filepath, fstream::trunc | ios::out);

 

 POBfile &lt;&lt; "! A cube" &lt;&lt; "\\n";

 POBfile &lt;&lt; "! front face vertices" &lt;&lt; "\\n" &lt;&lt; "V 1 -1 -1 0" &lt;&lt; "\\n" &lt;&lt; "V 2 1 -1 0" &lt;&lt; "\\n" &lt;&lt; "V 3 1 1 0" &lt;&lt; "\\n" &lt;&lt; "V 4 -1 1 0" &lt;&lt; "\\n";

 POBfile &lt;&lt; "! back face vertices" &lt;&lt; "\\n" &lt;&lt; "V 5 -1 -1 2" &lt;&lt; "\\n" &lt;&lt; "V 6 1 -1 2" &lt;&lt; "\\n" &lt;&lt; "V 7 1 1 2" &lt;&lt; "\\n" &lt;&lt; "V 8 -1 1 2" &lt;&lt; "\\n";

 POBfile &lt;&lt; "! Front" &lt;&lt; "\\n" &lt;&lt; "R 1 2 3 4 0 0" &lt;&lt; "\\n" &lt;&lt; "! Back" &lt;&lt; "\\n" &lt;&lt; "R 5 6 7 8 0 0" &lt;&lt; "\\n";

 POBfile &lt;&lt; "! Top" &lt;&lt; "\\n" &lt;&lt; "R 4 3 7 8 0 0" &lt;&lt; "\\n" &lt;&lt; "! Bottom" &lt;&lt; "\\n" &lt;&lt; "R 1 2 6 5 0 0" &lt;&lt; "\\n";

 POBfile &lt;&lt; "! Left Side" &lt;&lt; "\\n" &lt;&lt; "R 1 4 8 5 0 0" &lt;&lt; "\\n" &lt;&lt; "! Right Side" &lt;&lt; "\\n" &lt;&lt; "R 2 3 7 6 0 0";

 INCERowPtr Obj2 = NSCE-&gt;InsertNewObjectAt(2); // Add new line to NSCE

 IObjectTypeSettingsPtr Obj2_Type = Obj2-&gt;GetObjectTypeSettings(ObjectType_PolygonObject);

 Obj2_Type-&gt;FileName1 = "API\_cube\_demo.POB";

 Obj2-&gt;ChangeType(Obj2_Type);

 

 // opens 3d layout

 TheSystem-&gt;Analyses-&gt;New_Analysis(AnalysisIDM_NSC3DLayout);

 TheSystem-&gt;Save(); // Save the File

 

\#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](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