    

 ## On this page

  

 

 # Example 06 - C++ 

 Last update: 16.07.2025 

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

\#include &lt;fstream&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);

 

 // Create new non- sequential file

 // Set up primary optical system

 TheSystem = TheApplication-&gt;CreateNewSystem(SystemType_NonSequential);

 INonSeqEditorPtr TheNCE = TheSystem-&gt;NCE;

 

 // inserts objects and changes type

 INCERowPtr o1 = TheNCE-&gt;GetObjectAt(1);

 INCERowPtr o2 = TheNCE-&gt;InsertNewObjectAt(2);

 o1-&gt;ChangeType(o1-&gt;GetObjectTypeSettings(ObjectType_SourcePoint));

 o2-&gt;ChangeType(o2-&gt;GetObjectTypeSettings(ObjectType_DetectorRectangle));

 

 // modify object's cell values in the NCE

 ((IObjectSourcesPtr)o1-&gt;ObjectData)-&gt;NumberOfAnalysisRays = (int)1e6;

 ((IObjectSourcesPtr)o1-&gt;ObjectData)-&gt;NumberOfLayoutRays = 10;

 ((IObjectSourcePointPtr)o1-&gt;ObjectData)-&gt;ConeAngle = 2.5;

 

 o2-&gt;ZPosition = 1;

 ((IObjectDetectorRectanglePtr)o2-&gt;ObjectData)-&gt;XHalfWidth = 0.1;

 ((IObjectDetectorRectanglePtr)o2-&gt;ObjectData)-&gt;YHalfWidth = 0.1;

 ((IObjectDetectorRectanglePtr)o2-&gt;ObjectData)-&gt;NumberXPixels = 100;

 ((IObjectDetectorRectanglePtr)o2-&gt;ObjectData)-&gt;NumberYPixels = 100;

 

 // Setup and run the ray trace

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

 NSCRayTrace-&gt;SplitNSCRays = false;

 NSCRayTrace-&gt;ScatterNSCRays = true;

 NSCRayTrace-&gt;UsePolarization = false;

 NSCRayTrace-&gt;IgnoreErrors = true;

 NSCRayTrace-&gt;SaveRays = false;

 NSCRayTrace-&gt;ClearDetectors(0);

 

 ((ISystemToolPtr)NSCRayTrace)-&gt;RunAndWaitForCompletion();

 ((ISystemToolPtr)NSCRayTrace)-&gt;Close();

 

 int det = 2;

 // Export the data series to a text file

 fstream textfile;

 string filepath = TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e06\_nsc\_phase.txt";

 textfile.open(filepath, fstream::trunc | ios::out);

 

 // constants for extracting data from detector methods

 double pi = 3.1415926535897932;

 int index;

 int xpix = ((IObjectDetectorRectanglePtr)o2-&gt;ObjectData)-&gt;NumberXPixels;

 int ypix = ((IObjectDetectorRectanglePtr)o2-&gt;ObjectData)-&gt;NumberYPixels;

 

 // extracts the irradiance data from detector

 double *irradiance = new double[xpix * ypix];

 TheNCE-&gt;GetAllDetectorData(det, 1, TheNCE-&gt;GetDetectorSize(det), irradiance);

 

 for (int i = 0; i &lt; xpix; i++) {

 for (int j = 0; j &lt; ypix; j++) {

 index = ((ypix - (i + 1)) * xpix + (j + 1));

 textfile &lt;&lt; irradiance[index] &lt;&lt; "\\t";

 }

 textfile &lt;&lt; "\\n";

 }

 

 textfile &lt;&lt; "\\n";

 

 // Calculates phase data from Er &amp; Ei

 // Create the array. Note that if the array is too small, the application will crash when ReadData is called

 double *real = new double[xpix * ypix];

 double *imag = new double[xpix * ypix];

 double *phase = new double[xpix, ypix];

 

 TheNCE-&gt;GetAllCoherentData(det, DetectorDataType_Real, xpix * ypix, real);

 TheNCE-&gt;GetAllCoherentData(det, DetectorDataType_Imaginary, xpix * ypix, imag);

 

 for (int i = 0; i &lt; xpix; i++) {

 for (int j = 0; j &lt; ypix; j++) {

 index = ((ypix - (i + 1)) * xpix + (j + 1));

 phase[i, j] = atan2(imag[index], real[index]) * 180 / pi;

 textfile &lt;&lt; phase[i, j] &lt;&lt; "\\t";

 }

 textfile &lt;&lt; "\\n";

 }

 delete[] real;

 delete[] imag;

 

\#if defined(\_DEBUG)

 // keeps console open when in debug mode

 system("pause");

\#endif

 

 textfile.close();

 

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e06\_nsc\_phase.zos");

 

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