    

 ## On this page

  

 

 # Example 03 - C++ 

 Last update: 16.07.2025 

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

 

bool fileExists(const wchar\_t* name) {

 FILE *file;

 errno_t err = _wfopen_s(&amp;file, name, L"r");

 if (err == 0) {

 fclose(file);

 return true;

 } else {

 return false;

 }

}

 

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

 

 // Set up primary optical system

 TheSystem = TheApplication-&gt;PrimarySystem;

 _bstr_t sampleDir = TheApplication-&gt;SamplesDir;

 

 // Open file

 _bstr_t testFile = sampleDir + (_bstr_t)"\\\\API\\\\CPP\\\\e01\_new\_file\_and\_quickfocus.zos";

 if (!fileExists(testFile)) {

 cout &lt;&lt; "You need to run Example 01 before running this example" &lt;&lt; endl;

\#if defined(\_DEBUG)

 // keeps console open when in debug mode

 system("pause");

\#endif

 return false;

 }

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

 _bstr_t testFile2 = sampleDir + (_bstr_t)"\\\\API\\\\CPP\\\\e03\_open\_file\_and\_optimise.zos";

 TheSystem-&gt;SaveAs(testFile2);

 

 // Get Surfaces

 ILensDataEditorPtr TheLDE = TheSystem-&gt;LDE;

 ILDERowPtr Surface_1 = TheLDE-&gt;GetSurfaceAt(1);

 ILDERowPtr Surface_2 = TheLDE-&gt;GetSurfaceAt(2);

 ILDERowPtr Surface_3 = TheLDE-&gt;GetSurfaceAt(3);

 

 // Make thicknesses and radii variable

 Surface_1-&gt;ThicknessCell-&gt;MakeSolveVariable();

 Surface_2-&gt;ThicknessCell-&gt;MakeSolveVariable();

 Surface_2-&gt;RadiusCell-&gt;MakeSolveVariable();

 Surface_3-&gt;ThicknessCell-&gt;MakeSolveVariable();

 

 // Merit functions

 IMeritFunctionEditorPtr TheMFE = TheSystem-&gt;MFE;

 IMFERowPtr Operand_1 = TheMFE-&gt;GetOperandAt(1);

 Operand_1-&gt;ChangeType(MeritOperandType_ASTI);

 Operand_1-&gt;Target = 0.0;

 Operand_1-&gt;Weight = 10.0;

 

 IMFERowPtr Operand_2 = TheMFE-&gt;InsertNewOperandAt(2);

 Operand_2-&gt;ChangeType(MeritOperandType_COMA);

 Operand_2-&gt;Target = 0.0;

 Operand_2-&gt;Weight = 1.0;

 // Air min/max

 IMFERowPtr Operand_3 = TheMFE-&gt;AddOperand();

 Operand_3-&gt;ChangeType(MeritOperandType_MNCA);

 Operand_3-&gt;Target = 0.5;

 Operand_3-&gt;Weight = 1.0;

 IEditorRowPtr castRow = Operand_3;

 castRow-&gt;GetCellAt(2)-&gt;IntegerValue = 1;

 castRow-&gt;GetCellAt(3)-&gt;IntegerValue = 3;

 IMFERowPtr Operand_4 = TheMFE-&gt;AddOperand();

 Operand_4-&gt;ChangeType(MeritOperandType_MXCA);

 Operand_4-&gt;Target = 1000;

 Operand_4-&gt;Weight = 1.0;

 castRow = Operand_4;

 castRow-&gt;GetCellAt(2)-&gt;IntegerValue = 1;

 castRow-&gt;GetCellAt(3)-&gt;IntegerValue = 3;

 IMFERowPtr Operand_5 = TheMFE-&gt;AddOperand();

 Operand_5-&gt;ChangeType(MeritOperandType_MNEA);

 Operand_5-&gt;Target = 0.5;

 Operand_5-&gt;Weight = 1.0;

 castRow = Operand_5;

 castRow-&gt;GetCellAt(2)-&gt;IntegerValue = 1;

 castRow-&gt;GetCellAt(3)-&gt;IntegerValue = 3;

 // Material min/max

 IMFERowPtr Operand_6 = TheMFE-&gt;AddOperand();

 Operand_6-&gt;ChangeType(MeritOperandType_MNCG);

 Operand_6-&gt;Target = 3.0;

 Operand_6-&gt;Weight = 1.0;

 castRow = Operand_6;

 castRow-&gt;GetCellAt(2)-&gt;IntegerValue = 1;

 castRow-&gt;GetCellAt(3)-&gt;IntegerValue = 3;

 IMFERowPtr Operand_7 = TheMFE-&gt;AddOperand();

 Operand_7-&gt;ChangeType(MeritOperandType_MXCG);

 Operand_7-&gt;Target = 15.0;

 Operand_7-&gt;Weight = 1.0;

 castRow = Operand_7;

 castRow-&gt;GetCellAt(2)-&gt;IntegerValue = 1;

 castRow-&gt;GetCellAt(3)-&gt;IntegerValue = 3;

 IMFERowPtr Operand_8 = TheMFE-&gt;AddOperand();

 Operand_8-&gt;ChangeType(MeritOperandType_MNEG);

 Operand_8-&gt;Target = 3.0;

 Operand_8-&gt;Weight = 1.0;

 castRow = Operand_8;

 castRow-&gt;GetCellAt(2)-&gt;IntegerValue = 1;

 castRow-&gt;GetCellAt(3)-&gt;IntegerValue = 3;

 

 // Local optimisation till completion

 ILocalOptimizationPtr LocalOpt = TheSystem-&gt;Tools-&gt;OpenLocalOptimization();

 LocalOpt-&gt;Algorithm = OptimizationAlgorithm_DampedLeastSquares;

 LocalOpt-&gt;Cycles = OptimizationCycles_Automatic;

 LocalOpt-&gt;NumberOfCores = 8;

 ISystemToolPtr baseTool = LocalOpt;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 // Hammer for 10 seconds

 IHammerOptimizationPtr HammerOpt = TheSystem-&gt;Tools-&gt;OpenHammerOptimization();

 baseTool = HammerOpt;

 baseTool-&gt;RunAndWaitWithTimeout(10); 

 baseTool-&gt;Cancel();

 baseTool-&gt;WaitForCompletion();

 baseTool-&gt;Close();

 

 // Save and close

 TheSystem-&gt;Save();

 

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