    

 ## On this page

  

 

 # Example 16 - C++ 

 Last update: 17.07.2025 

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

// CppUserOperandApplication.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;vector&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 finishUserOperand(IZOSAPI_ApplicationPtr TheApplication, std::vector&lt;double&gt; &amp;resultData);

 

int RunOperand()

{

 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 connect to the existing OpticStudio instance

 IZOSAPI_ApplicationPtr TheApplication = nullptr;

 try

 {

 TheApplication = TheConnection-&gt;ConnectToApplication(); // this will throw an exception if not launched from OpticStudio

 }

 catch (exception &amp;ex)

 {

 handleError(ex.what());

 return -1;

 }

 if (TheApplication == nullptr)

 {

 handleError("An unknown connection error occurred!");

 return -1;

 }

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

 {

 handleError("User Operand started in wrong mode!");

 return -1;

 }

 

 // Check the connection status

 if (!TheApplication-&gt;IsValidLicenseForAPI)

 {

 handleError("License check failed!");

 return -1;

 }

 

 

 // Read the operand arguments

 double Hx = TheApplication-&gt;OperandArgument1;

 double Hy = TheApplication-&gt;OperandArgument2;

 double Px = TheApplication-&gt;OperandArgument3;

 double Py = TheApplication-&gt;OperandArgument4;

 

 // Initialize the output array

 int maxResultLength = TheApplication-&gt;OperandResults-&gt;Length;

 std::vector&lt;double&gt; operandResults(maxResultLength);

 

 IOpticalSystemPtr TheSystem = TheApplication-&gt;PrimarySystem;

 // Add your custom code here...

 

 // This is an example that calculates the sum of absolute distance between surfaces. 

 // Hx -&gt; Surf1

 // Hy -&gt; Surf2

 

 // Read the operand arguments (Note that we aren't using operand 3 and 4)

 int surf1 = (int)TheApplication-&gt;OperandArgument1;

 int surf2 = (int)TheApplication-&gt;OperandArgument2;

 //double x = (double)TheApplication-&gt;OperandArgument3;

 //double y = (double)TheApplication-&gt;OperandArgument4; 

 

 //Make the sum of the absolute value of each surface thickness

 ILDERowPtr surf;

 double thic = 0;

 

 if (surf1 &lt;= surf2)

 {

 for (int i = surf1; i &lt;= surf2; i++)

 {

 surf = TheSystem-&gt;LDE-&gt;GetSurfaceAt(i);

 thic = thic + abs(surf-&gt;Thickness);

 }

 operandResults[0] = thic;

 }

 else

 {

 operandResults[0] = 1e9; // invalid input

 }

 

 // Clean up

 finishUserOperand(TheApplication, operandResults);

 

 return 0;

}

 

void handleError(std::string msg)

{

 throw new exception(msg.c_str());

}

 

void logInfo(std::string msg)

{

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

}

 

void finishUserOperand(IZOSAPI_ApplicationPtr TheApplication, std::vector&lt;double&gt; &amp;resultData)

{

 // Note - OpticStudio will wait for the operand to complete until this application exits 

 if (TheApplication != nullptr)

 {

 TheApplication-&gt;OperandResults-&gt;WriteData(

 (long)resultData.size(),

 &amp;resultData[0]);

 }

}

 

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

{

 return RunOperand();

}

 

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

{

 return RunOperand();

}

[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