    

 ## On this page

  

 

 # Example 10 - C++ 

 Last update: 16.07.2025 

# <a class="anchor" id="ex10_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 a new API directory

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

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

 

 // Open File, Save to New Name

 _bstr_t file = "\\\\Non-Sequential\\\\Ray Splitting\\\\Beam splitter.zos";

 TheSystem-&gt;LoadFile(TheApplication-&gt;SamplesDir + file, false);

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e10\_NSC\_ray\_trace.zos");

 

 // Run an NSC Ray Trace, Save .zrd file

 INSCRayTracePtr NSCRayTrace = TheSystem-&gt;Tools-&gt;OpenNSCRayTrace(); // Open NSC RayTrace tool

 // Clear all detectors

 NSCRayTrace-&gt;ClearDetectors(0); 

 // Set up RayTrace tool

 NSCRayTrace-&gt;IgnoreErrors = true;

 NSCRayTrace-&gt;SaveRays = true;

 NSCRayTrace-&gt;SaveRaysFile = "e10\_API\_RayTrace.ZRD"; // Saves to same directory as lens file

 ISystemToolPtr baseTool = NSCRayTrace;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 // Open Detector Viewer, view previously saved .zrd file

 IA_Ptr DetectorView = TheSystem-&gt;Analyses-&gt;New_DetectorViewer();

 IAS_DetectorViewerPtr DetectorView_Settings = DetectorView-&gt;GetSettings();

 DetectorView_Settings-&gt;RayDatabaseFilename = "e10\_API\_Raytrace.ZRD";

 DetectorView_Settings-&gt;ShowAs = DetectorViewerShowAsTypes_FalseColor;

 // Detector will only display rays which hit object 2 exactly 4 times

 DetectorView_Settings-&gt;Filter = "X\_HIT(2, 4)";

 // Apply Settings to Detector Viewer

 DetectorView-&gt;ApplyAndWaitForCompletion(); 

 

 // Retrieve detector data and detector information

 INonSeqEditorPtr TheNCE = TheSystem-&gt;NCE;

 double total_hits, total_flux;

 bool hits_bool_return, flux_bool_return, dims_bool_return;

 unsigned long X_detectorDims, Y_detectorDims;

 hits_bool_return = TheNCE-&gt;GetDetectorData(4, -3, 0, &amp;total_hits); // Object Number=4, Pix -3 &amp; Data=0 (total hits)

 flux_bool_return = TheNCE-&gt;GetDetectorData(4, 0, 0, &amp;total_flux); // Object Number=4, Pix=0 &amp; Data=0 (total flux)

 dims_bool_return = TheNCE-&gt;GetDetectorDimensions(4, &amp;X_detectorDims, &amp;Y_detectorDims); // get number of pixels in X, Y

 cout &lt;&lt; "total hits = " &lt;&lt; total_hits &lt;&lt; "\\n" &lt;&lt; "total flux = " &lt;&lt; total_flux &lt;&lt; endl;

 

 int length = X_detectorDims * Y_detectorDims;

 double *pix = new double[length]; // Create array to store flux data for each pixel

 bool pix_bool;

 for (int i = 0; i &lt; length; i++) // loop through pixels, store value in pix

 {

 pix_bool = TheNCE-&gt;GetDetectorData(4, i, 0, &amp;pix[i]);

 }

 

 // Save Ray Path Analysis to Text File

 if (TheApplication-&gt;LicenseStatus == ZOSAPI_Interfaces::LicenseStatusType::LicenseStatusType_PremiumEdition) {

 IA_Ptr RayPath = TheSystem-&gt;Analyses-&gt;New_Analysis(AnalysisIDM_PathAnalysis);

 ZOSAPI_Interfaces::IAS_PathAnalysisPtr RayPath_settings = RayPath-&gt;GetSettings();

 RayPath_settings-&gt;RayDatabaseFile = "e10\_API\_RayTrace.ZRD";

 RayPath-&gt;ApplyAndWaitForCompletion();

 

 IAR_Ptr Rays = RayPath-&gt;GetResults();

 Rays-&gt;GetTextFile(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e10\_RayPathAnalysis.txt");

 }

 

 // Save!

 TheSystem-&gt;Save();

\#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](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