    

 ## On this page

  

 

 # Example 14 - C++ 

 Last update: 16.07.2025 

# <a class="anchor" id="ex14_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()

{

\#if !\_WIN64

 int msgboxID = MessageBox(

 NULL,

 (LPCWSTR)L"This example needs to be run as a x64 configuration. Wizards are not supported via COM in 32-bit mode.",

 (LPCWSTR)L"ZOS-API Error",

 MB_ICONERROR | MB_OK

 );

 return 0;

\#endif

 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;

 

 // creates a new API directory

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

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

 

 // Open Double Gauss sample file

 _bstr_t samplesFolder = TheApplication-&gt;SamplesDir;

 _bstr_t DGfile = samplesFolder + (_bstr_t)"\\\\Sequential\\\\Objectives\\\\Double Gauss 28 degree field.zos";

 TheSystem-&gt;LoadFile(DGfile, false);

 

 // Set up the Tolerance Wizard and run it

 // Need to make sure you have x64 set to the platform, otherwise COM will throw an error

 ZOSAPI_Interfaces::ISEQToleranceWizardPtr tWiz = TheSystem-&gt;TDE-&gt;SEQToleranceWizard;

 // Specify surface tolerances

 tWiz-&gt;ToleranceSettings-&gt;SurfaceRadius = 0.1;

 tWiz-&gt;ToleranceSettings-&gt;SurfaceThickness = 0.1;

 tWiz-&gt;ToleranceSettings-&gt;SurfaceDecenterX = 0.1;

 tWiz-&gt;ToleranceSettings-&gt;SurfaceDecenterY = 0.1;

 tWiz-&gt;ToleranceSettings-&gt;SurfaceTiltX = 0.2;

 tWiz-&gt;ToleranceSettings-&gt;SurfaceTiltY = 0.2;

 // Specify element tolerances

 tWiz-&gt;ToleranceSettings-&gt;ElementDecenterX = 0.1;

 tWiz-&gt;ToleranceSettings-&gt;ElementDecenterY = 0.1;

 tWiz-&gt;ToleranceSettings-&gt;ElementTiltXDegrees = 0.2;

 tWiz-&gt;ToleranceSettings-&gt;ElementTiltYDegrees = 0.2;

 // Specify tolerances not to be used

 tWiz-&gt;ToleranceSettings-&gt;IsSurfaceSandAIrregularityUsed = false;

 tWiz-&gt;ToleranceSettings-&gt;IsIndexUsed = false;

 tWiz-&gt;ToleranceSettings-&gt;IsIndexAbbePercentageUsed = false;

 // Select OK

 tWiz-&gt;ToleranceSettings-&gt;CommonSettings-&gt;OK();

 

 // Create a "Double Gauss" folder in the Samples folder

 _bstr_t dirLoc = samplesFolder + (_bstr_t)"\\\\API\\\\CPP\\\\e14\_seq\_tolerance";

 CreateDirectory(dirLoc, NULL);

 // Save a copy of the file in the Double Gauss folder

 _bstr_t fileNameSeq = dirLoc + (_bstr_t)"\\\\Double\_Gauss\_(seq).zos";

 TheSystem-&gt;SaveAs(fileNameSeq);

 

 // Set up the Tolerancing analysis and run it

 ZOSAPI_Interfaces::ITolerancingPtr tol = TheSystem-&gt;Tools-&gt;OpenTolerancing();

 // Select Sensitivity mode

 tol-&gt;SetupMode = ZOSAPI_Interfaces::SetupModes_Sensitivity;

 // Select Criterion and related settings 

 tol-&gt;Criterion = ZOSAPI_Interfaces::Criterions_RMSSpotRadius;

 tol-&gt;CriterionSampling = 3;

 tol-&gt;CriterionComp = ZOSAPI_Interfaces::CriterionComps_OptimizeAll_DLS;

 tol-&gt;CriterionCycle = 2;

 tol-&gt;CriterionField = ZOSAPI_Interfaces::CriterionFields_UserDefined;

 // Select the number or MC runs and files to save

 tol-&gt;NumberOfRuns = 20;

 tol-&gt;NumberToSave = 20;

 // Run the Tolerancing analysis

 ZOSAPI_Interfaces::ISystemToolPtr baseTool = tol;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 // Convert file to Non-sequential mode

 ZOSAPI_Interfaces::IConvertToNSCGroupPtr convertNSmode = TheSystem-&gt;Tools-&gt;OpenConvertToNSCGroup();

 convertNSmode-&gt;ConvertFileToNSC = true;

 ZOSAPI_Interfaces::ISystemToolPtr baseTool2 = convertNSmode;

 baseTool2-&gt;RunAndWaitForCompletion();

 baseTool2-&gt;Close();

 // Save the Non-sequential file to the Double Gauss folder

 _bstr_t fileNameNS = dirLoc + (_bstr_t)"\\\\Double\_Gauss\_(NS).zos";

 TheSystem-&gt;SaveAs(fileNameNS);

 

 // 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