    

 ## On this page

  

 

 # Example 25 - C++ 

 Last update: 17.07.2025 

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

\#include &lt;iomanip&gt;

\#include &lt;vector&gt;

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

 }

 

 

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

 

 // Set up primary optical system

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

 

 // Initializes NCE and loads surfaces

 // Inserts objects

 INonSeqEditorPtr TheNCE = TheSystem-&gt;NCE;

 TheNCE-&gt;InsertNewObjectAt(1);

 TheNCE-&gt;InsertNewObjectAt(1);

 TheNCE-&gt;InsertNewObjectAt(1);

 TheNCE-&gt;InsertNewObjectAt(1);

 

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

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

 INCERowPtr o3 = TheNCE-&gt;GetObjectAt(3);

 INCERowPtr o4 = TheNCE-&gt;GetObjectAt(4);

 INCERowPtr o5 = TheNCE-&gt;GetObjectAt(5);

 

 // Changes Object Type

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

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

 o3-&gt;ChangeType(o3-&gt;GetObjectTypeSettings(ObjectType_NullObject));

 o4-&gt;ChangeType(o4-&gt;GetObjectTypeSettings(ObjectType_DiffractionGrating));

 o5-&gt;ChangeType(o5-&gt;GetObjectTypeSettings(ObjectType_DetectorColor));

 

 // Sets positions &amp; materials

 o3-&gt;ZPosition = 10;

 o3-&gt;TiltAboutX = 10;

 o4-&gt;RefObject = 3;

 o4-&gt;Material = "MIRROR";

 o5-&gt;YPosition = 8.45;

 o5-&gt;TiltAboutX = 40;

 

 // Sets parameters

 o1-&gt;GetObjectCell(ObjectColumn_Par7)-&gt;DoubleValue = 5;

 o2-&gt;GetObjectCell(ObjectColumn_Par7)-&gt;DoubleValue = 5;

 o4-&gt;GetObjectCell(ObjectColumn_Par10)-&gt;DoubleValue = 0.6;

 o4-&gt;GetObjectCell(ObjectColumn_Par11)-&gt;DoubleValue = 1;

 

 // Changes sourcecolor to Blackbody, sets temperature, min / max wavelength

 o1-&gt;SourcesData-&gt;SourceColor = SourceColorMode_BlackBodySpectrum;

 o1-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;TemperatureK = 6000;

 o1-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;WavelengthFrom = 0.45;

 o1-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;WavelengthTo = 0.65;

 

 o2-&gt;SourcesData-&gt;SourceColor = SourceColorMode_BlackBodySpectrum;

 o2-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;TemperatureK = 6000;

 o2-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;SpectrumCount = 100;

 o2-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;WavelengthFrom = 0.4;

 o2-&gt;SourcesData-&gt;SourceColorSettings-&gt;_S_BlackBodySpectrum-&gt;WavelengthTo = 0.7;

 

 // Sets up the MCE, adds configuration &amp; operands

 IMultiConfigEditorPtr TheMCE = TheSystem-&gt;MCE;

 TheMCE-&gt;AddConfiguration(false);

 TheMCE-&gt;AddOperand();

 TheMCE-&gt;AddOperand();

 TheMCE-&gt;AddOperand();

 

 // change MCE to NPAR, modifies the number of Layout Rays for a Source

 for (int a = 1; a &lt; 5; a++)

 TheMCE-&gt;GetOperandAt(a)-&gt;ChangeType(MultiConfigOperandType_NPAR);

 

 TheMCE-&gt;GetOperandAt(1)-&gt;Param2 = 1;

 TheMCE-&gt;GetOperandAt(1)-&gt;Param3 = 1;

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

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

 

 

 TheMCE-&gt;GetOperandAt(2)-&gt;Param2 = 1;

 TheMCE-&gt;GetOperandAt(2)-&gt;Param3 = 2;

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

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

 

 TheMCE-&gt;GetOperandAt(3)-&gt;Param2 = 2;

 TheMCE-&gt;GetOperandAt(3)-&gt;Param3 = 1;

 TheMCE-&gt;GetOperandAt(3)-&gt;GetOperandCell(1)-&gt;DoubleValue = 0;

 TheMCE-&gt;GetOperandAt(3)-&gt;GetOperandCell(2)-&gt;DoubleValue = 200;

 

 TheMCE-&gt;GetOperandAt(4)-&gt;Param2 = 2;

 TheMCE-&gt;GetOperandAt(4)-&gt;Param3 = 2;

 TheMCE-&gt;GetOperandAt(4)-&gt;GetOperandCell(1)-&gt;DoubleValue = 0;

 TheMCE-&gt;GetOperandAt(4)-&gt;GetOperandCell(2)-&gt;DoubleValue = 1000000;

 

 

 // Setup detector color

 double x_width = 1.5;

 double y_width = 1.5;

 int x_pixel = 500;

 int y_pixel = 500;

 

 

 o5-&gt;GetObjectCell(ObjectColumn_Par1)-&gt;DoubleValue = x_width;

 o5-&gt;GetObjectCell(ObjectColumn_Par2)-&gt;DoubleValue = y_width;

 o5-&gt;GetObjectCell(ObjectColumn_Par3)-&gt;IntegerValue = x_pixel;

 o5-&gt;GetObjectCell(ObjectColumn_Par4)-&gt;IntegerValue = y_pixel;

 

 for (int a = 1; a &lt; TheMCE-&gt;NumberOfConfigurations + 1; a++)

 TheMCE-&gt;SetCurrentConfiguration(a);

 

 // Setup and run the ray trace

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

 NSCRayTrace-&gt;ClearDetectors(0);

 NSCRayTrace-&gt;SplitNSCRays = false;

 NSCRayTrace-&gt;ScatterNSCRays = false;

 NSCRayTrace-&gt;UsePolarization = false;

 NSCRayTrace-&gt;IgnoreErrors = true;

 NSCRayTrace-&gt;SaveRays = false;

 ISystemToolPtr baseTool = NSCRayTrace;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 cout &lt;&lt; "Finished ray trace\\n";

 

 // Creates a new detector viewer window, changes to true color

 IA_Ptr det = TheSystem-&gt;Analyses-&gt;New_Analysis(AnalysisIDM_DetectorViewer);

 IAS_DetectorViewerPtr det_settings = det-&gt;GetSettings();

 det_settings-&gt;ShowAs = DetectorViewerShowAsTypes_TrueColor;

 det-&gt;ApplyAndWaitForCompletion();

 

 // Pre-create the array to retrieve RGB pixels data

 float *rData = new float[(x_pixel * y_pixel)];

 float *gData = new float[(x_pixel * y_pixel)];

 float *bData = new float[(x_pixel * y_pixel)];

 

 

 // Loads RGB data into arrays

 det-&gt;GetResults()-&gt;GetDataGridRgb(0)-&gt;FillValues((x_pixel * y_pixel), rData, gData, bData);

 

 

 // expoerts RGB array to text file.

 int Nx = det-&gt;GetResults()-&gt;GetDataGridRgb(0)-&gt;Nx;

 int Ny = det-&gt;GetResults()-&gt;GetDataGridRgb(0)-&gt;Ny;

 

 fstream textfile;

 string filepath = TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e25\_source\_spectrum\_diffraction\_grating.txt";

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

 

 textfile &lt;&lt; "First column = smallest y coordinate.\\n";

 textfile &lt;&lt; "First row = smallest x coordinate.\\n\\n";

 

 textfile &lt;&lt; "R Channel Data:\\n";

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

 {

 for (int j = 0; j &lt; Nx; j++)

 {

 textfile &lt;&lt; setw(8) &lt;&lt; left &lt;&lt; rData[j + Nx*i] &lt;&lt; "\\t";

 }

 textfile &lt;&lt; endl;

 }

 textfile &lt;&lt; "\\nG Channel Data:\\n";

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

 {

 for (int j = 0; j &lt; Nx; j++)

 {

 textfile &lt;&lt; setw(8) &lt;&lt; left &lt;&lt; gData[j + Nx*i] &lt;&lt; "\\t";

 }

 textfile &lt;&lt; endl;

 }

 textfile &lt;&lt; "\\nB Channel Data:\\n";

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

 {

 for (int j = 0; j &lt; Nx; j++)

 {

 textfile &lt;&lt; setw(8) &lt;&lt; left &lt;&lt; bData[j + Nx*i] &lt;&lt; "\\t";

 }

 textfile &lt;&lt; endl;

 }

 

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