    

 ## On this page

  

 

 # Example 18 - C++ 

 Last update: 16.07.2025 

# <a class="anchor" id="ex18_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...

 

 // creates new directory

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

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

 

 TheSystem-&gt;LoadFile(TheApplication-&gt;SamplesDir + "\\\\Sequential\\\\Objectives\\\\Double Gauss 28 degree field.zos", false);

 

 // Get interface of the Multi-Configuration Editor

 IMultiConfigEditorPtr TheMCE = TheSystem-&gt;MCE;

 // Add two configurations (totally 3)

 TheMCE-&gt;AddConfiguration(false);

 TheMCE-&gt;AddConfiguration(false);

 

 // Add one operand (totally 2)

 TheMCE-&gt;AddOperand();

 

 // Get interface of each operand

 IMCERowPtr MCOperand1 = TheMCE-&gt;GetOperandAt(1);

 IMCERowPtr MCOperand2 = TheMCE-&gt;GetOperandAt(2);

 // Change both operands' type to THIC

 MCOperand1-&gt;ChangeType(MultiConfigOperandType_THIC);

 MCOperand2-&gt;ChangeType(MultiConfigOperandType_THIC);

 

 // Set parameters of operands

 // If the type of operand is THIC, the first parameter here means surface number

 MCOperand1-&gt;Param1 = 0;

 MCOperand2-&gt;Param1 = 11;

 

 // Set values of opeand for each configurations

 MCOperand1-&gt;GetOperandCell(1)-&gt;DoubleValue = 10000.0;

 MCOperand1-&gt;GetOperandCell(2)-&gt;DoubleValue = 5000.0;

 MCOperand1-&gt;GetOperandCell(3)-&gt;DoubleValue = 1000.0;

 

 // Refocus for each configuration

 IQuickFocusPtr quickfocus = TheSystem-&gt;Tools-&gt;OpenQuickFocus();

 ISystemToolPtr baseTool = quickfocus;

 TheMCE-&gt;SetCurrentConfiguration(1);

 baseTool-&gt;RunAndWaitForCompletion();

 TheMCE-&gt;SetCurrentConfiguration(2);

 baseTool-&gt;RunAndWaitForCompletion();

 TheMCE-&gt;SetCurrentConfiguration(3);

 baseTool-&gt;RunAndWaitForCompletion();

 

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e18\_Double\_Gauss\_28\_degree\_field\_MultiConfig.zos");

 

 // An exmple of manually "Make Thermal"

 TheSystem-&gt;LoadFile(TheApplication-&gt;SamplesDir + "\\\\Sequential\\\\Objectives\\\\Doublet.zos", false);

 // Add 1 configuration (totally 2)

 TheMCE-&gt;AddConfiguration(false);

 // Add 12 operand (totally 13)

 for (int i = 0; i &lt; 12; i++)

 TheMCE-&gt;AddOperand();

 

 // Create an operand type array and later we will input the array into MCE by a for loop

 [MultiConfigOperandType](namespace_z_o_s_a_p_i_1_1_editors_1_1_m_c_e.xhtml#a92bcb7af31a91fbaa508ee44e5abfdd1) *operandtype = new [MultiConfigOperandType](namespace_z_o_s_a_p_i_1_1_editors_1_1_m_c_e.xhtml#a92bcb7af31a91fbaa508ee44e5abfdd1)[13]{

 MultiConfigOperandType_TEMP,

 MultiConfigOperandType_PRES,

 MultiConfigOperandType_CRVT,

 MultiConfigOperandType_THIC,

 MultiConfigOperandType_GLSS,

 MultiConfigOperandType_SDIA,

 MultiConfigOperandType_CRVT,

 MultiConfigOperandType_THIC,

 MultiConfigOperandType_GLSS,

 MultiConfigOperandType_SDIA,

 MultiConfigOperandType_CRVT,

 MultiConfigOperandType_THIC,

 MultiConfigOperandType_SDIA };

 

 // Set parameter 1 for each config operand

 int *param1value = new int[11]{ 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3 };

 for (int i = 0; i &lt; 11; i++)

 TheMCE-&gt;GetOperandAt(i + 3)-&gt;Param1 = param1value[i];

 

 // Set type for each config operand

 for (int i = 0; i &lt; 13; i++)

 TheMCE-&gt;GetOperandAt(i + 1)-&gt;ChangeType(operandtype[i]);

 

 // Set Thermal Pickup solves

 ISolveDataPtr thermalpickupsolve = TheMCE-&gt;GetOperandAt(1)-&gt;GetOperandCell(1)-&gt;CreateSolveType(SolveType_ThermalPickup);

 thermalpickupsolve-&gt;_S_ThermalPickup-&gt;Configuration = 1;

 int *thermalpickup_num = new int[9]{ 3, 4, 6, 7, 8, 10, 11, 12, 13 };

 for (int i = 0; i &lt; 9; i++)

 TheMCE-&gt;GetOperandAt(thermalpickup_num[i])-&gt;GetOperandCell(2)-&gt;SetSolveData(thermalpickupsolve);

 

 // Set Pickup solve

 ISolveDataPtr configpickupsolve = TheMCE-&gt;GetOperandAt(1)-&gt;GetOperandCell(1)-&gt;CreateSolveType(SolveType_ConfigPickup);

 configpickupsolve-&gt;_S_ConfigPickup-&gt;Configuration = 1;

 TheMCE-&gt;GetOperandAt(5)-&gt;GetOperandCell(2)-&gt;SetSolveData(configpickupsolve);

 TheMCE-&gt;GetOperandAt(9)-&gt;GetOperandCell(2)-&gt;SetSolveData(configpickupsolve);

 

 // Set temperature of configuration 2 to 100 degrees

 TheMCE-&gt;GetOperandAt(1)-&gt;GetOperandCell(2)-&gt;DoubleValue = 100;

 ISystemDataPtr TheSystemData = TheSystem-&gt;SystemData;

 TheSystemData-&gt;Environment-&gt;AdjustIndexToEnvironment = true;

 

 // Save the system

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e18\_Doublet\_MakeTermal.zos");

\#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.Editors.MCE.MultiConfigOperandType](namespace_z_o_s_a_p_i_1_1_editors_1_1_m_c_e.xhtml#a92bcb7af31a91fbaa508ee44e5abfdd1)

MultiConfigOperandType

All supported Multi Configuration operand types.

**Definition:** InterfacesMCE.cs:30



[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