    

 ## On this page

  

 

 # Example 05 - C++ 

 Last update: 17.07.2025 

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

 

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

 

 // Open file and set Analysis Ryas to only 10

 _bstr_t sampleDir = TheApplication-&gt;SamplesDir;

 _bstr_t testFile = sampleDir + "\\\\Non-sequential\\\\Miscellaneous\\\\Digital\_projector\_flys\_eye\_homogenizer.zos";

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

 TheSystem-&gt;NCE-&gt;GetObjectAt(1)-&gt;GetObjectCell(ObjectColumn_Par2)-&gt;IntegerValue = 10;

 

 // Trace and save a ZRD file for test later

 INSCRayTracePtr NSCRayTrace = TheSystem-&gt;Tools-&gt;OpenNSCRayTrace();

 NSCRayTrace-&gt;SplitNSCRays = true;

 NSCRayTrace-&gt;ScatterNSCRays = false;

 NSCRayTrace-&gt;UsePolarization = true;

 NSCRayTrace-&gt;IgnoreErrors = true;

 NSCRayTrace-&gt;SaveRays = true;

 NSCRayTrace-&gt;SaveRaysFile = "Digital\_projector\_flys\_eye\_homogenizer.ZRD";

 NSCRayTrace-&gt;ClearDetectors(0);

 ISystemToolPtr NSCRayTrace_base = NSCRayTrace;

 NSCRayTrace_base-&gt;RunAndWaitForCompletion();

 NSCRayTrace_base-&gt;Close();

 

 // Open ZRD Reader and read data

 IZRDReaderPtr ZRDReader = TheSystem-&gt;Tools-&gt;OpenRayDatabaseReader();

 ZRDReader-&gt;ZRDFile = TheApplication-&gt;SamplesDir +

 "\\\\Non-sequential\\\\Miscellaneous\\\\Digital\_projector\_flys\_eye\_homogenizer.ZRD";

 ISystemToolPtr ZRDReader_base = ZRDReader;

 ZRDReader_base-&gt;RunAndWaitForCompletion();

 if (ZRDReader_base-&gt;Succeeded == false)

 {

 cout &lt;&lt; "Raytracingfailed!\\n" &lt;&lt; endl;

 cout &lt;&lt; ZRDReader_base-&gt;ErrorMessage &lt;&lt; endl;

 }

 else

 {

 cout &lt;&lt; "Raytracing completed!\\n" &lt;&lt; endl;

 }

 

 IZRDReaderResultsPtr ZRDResult = ZRDReader-&gt;GetResults();

 

 VARIANT_BOOL success_NextSegmentFull, nsuccess_NextResult;

 long rayNumber, waveIndex, numSegments;

 double wlUM;

 

 long segmentLevel, segmentParent, hitObj, hitFace, insideOf, xybin, lmbin;

 double x, y, z, l, m, n, exr, exi, eyr, eyi, ezr, ezi, intensity, pathLength, xNorm, yNorm, zNorm, index, startingPhase, phaseOf, phaseAt;

 [RayStatus](namespace_z_o_s_a_p_i_1_1_tools_1_1_ray_trace.xhtml#a6c300f6f57a84075c1f63585133762eb) status;

 

 //ReadNExtResult() returns data ray by ray

 nsuccess_NextResult = ZRDResult-&gt;ReadNextResult(&amp;rayNumber, &amp;waveIndex, &amp;wlUM, &amp;numSegments);

 while (nsuccess_NextResult)

 {

 cout &lt;&lt; "\\nsuccess\_NextResult: " &lt;&lt; (nsuccess_NextResult ? "True" : "False") &lt;&lt;

 ", rayNumber: " &lt;&lt; rayNumber &lt;&lt;

 ", waveIndex: " &lt;&lt; waveIndex &lt;&lt;

 ", wlUM: " &lt;&lt; wlUM &lt;&lt;

 ", numSegments: " &lt;&lt; numSegments &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;

 success_NextSegmentFull = ZRDResult-&gt;ReadNextSegmentFull(&amp;segmentLevel, &amp;segmentParent, &amp;hitObj, &amp;hitFace, &amp;insideOf,

 &amp;status, &amp;x, &amp;y, &amp;z, &amp;l, &amp;m, &amp;n, &amp;exr, &amp;exi, &amp;eyr, &amp;eyi, &amp;ezr, &amp;ezi,

 &amp;intensity, &amp;pathLength, &amp;xybin, &amp;lmbin, &amp;xNorm, &amp;yNorm, &amp;zNorm, &amp;index, &amp;startingPhase, &amp;phaseOf,

 &amp;phaseAt);

 while (success_NextSegmentFull)

 {

 cout &lt;&lt; "success\_NextSegmentFull:" &lt;&lt; (success_NextSegmentFull ? "True" : "False") &lt;&lt;

 ", segmentLevel:" &lt;&lt; segmentLevel &lt;&lt;

 ", segmentParent:" &lt;&lt; segmentParent &lt;&lt;

 ", \\nhitObj:" &lt;&lt; hitObj &lt;&lt; ", hitFace:" &lt;&lt; hitFace &lt;&lt; ", insideOf:" &lt;&lt; insideOf &lt;&lt; ", status:" &lt;&lt; status &lt;&lt;

 ", \\nx:" &lt;&lt; x &lt;&lt; ", y:" &lt;&lt; y &lt;&lt; ", z:" &lt;&lt; z &lt;&lt; 

 ", l:" &lt;&lt; l &lt;&lt; ", m:" &lt;&lt; m &lt;&lt; ", n:" &lt;&lt; n &lt;&lt;

 ", \\nexr:" &lt;&lt; exr &lt;&lt; ", exi:" &lt;&lt; exi &lt;&lt;

 ", eyr:" &lt;&lt; eyr &lt;&lt; ", eyi:" &lt;&lt; eyi &lt;&lt;

 ", ezr:" &lt;&lt; ezr &lt;&lt; ", ezi:" &lt;&lt; ezi &lt;&lt;

 ", \\nintensity:" &lt;&lt; intensity &lt;&lt; ", pathLength:" &lt;&lt; pathLength &lt;&lt;

 ", xybin:" &lt;&lt; xybin &lt;&lt; ", lmbin:" &lt;&lt; lmbin &lt;&lt;

 ", \\nxNorm:" &lt;&lt; xNorm &lt;&lt; ", yNorm:" &lt;&lt; yNorm &lt;&lt; ", zNorm:" &lt;&lt; zNorm &lt;&lt;

 ", \\nindex:" &lt;&lt; index &lt;&lt; ", startingPhase:" &lt;&lt; startingPhase &lt;&lt;

 ", phaseOf:" &lt;&lt; phaseOf &lt;&lt; ", phaseAt:" &lt;&lt; phaseAt &lt;&lt; endl &lt;&lt; endl;

 success_NextSegmentFull = ZRDResult-&gt;ReadNextSegmentFull(&amp;segmentLevel, &amp;segmentParent, &amp;hitObj, &amp;hitFace, &amp;insideOf,

 &amp;status, &amp;x, &amp;y, &amp;z, &amp;l, &amp;m, &amp;n, &amp;exr, &amp;exi, &amp;eyr, &amp;eyi, &amp;ezr, &amp;ezi,

 &amp;intensity, &amp;pathLength, &amp;xybin, &amp;lmbin, &amp;xNorm, &amp;yNorm, &amp;zNorm, &amp;index, &amp;startingPhase, &amp;phaseOf,

 &amp;phaseAt);

 }

 nsuccess_NextResult = ZRDResult-&gt;ReadNextResult(&amp;rayNumber, &amp;waveIndex, &amp;wlUM, &amp;numSegments);

 }

 ZRDReader_base-&gt;Close();

 

\#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.Tools.RayTrace.RayStatus](namespace_z_o_s_a_p_i_1_1_tools_1_1_ray_trace.xhtml#a6c300f6f57a84075c1f63585133762eb)

RayStatus

**Definition:** RayTrace.cs:1153



[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