    

 ## On this page

  

 

 # Example 17 - C++ 

 Last update: 17.07.2025 

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

 

 // NSC (bulk\_volume)

 // This example recreates Non-sequential\\Scattering\\Bulk Scatter.zos

 

 //Create a New File

 TheSystem-&gt;New(false);

 TheSystem-&gt;MakeNonSequential();

 

 ISDMaterialCatalogDataPtr(TheSystem-&gt;SystemData-&gt;MaterialCatalogs)-&gt;AddCatalog("SCHOTT");

 

 //Define path locations

 _bstr_t SamplesFolder = TheApplication-&gt;SamplesDir;

 _bstr_t SampleFile = TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e17\_BulkScatter.zos";

 //TheSystem.LoadFile(SampleFile, false);

 TheSystem-&gt;SaveAs(SampleFile);

 

 //Non-sequential component editor

 INonSeqEditorPtr TheNCE = TheSystem-&gt;NCE;

 TheNCE = TheSystem-&gt;NCE;

 INCERowPtr Object_1 = TheNCE-&gt;InsertNewObjectAt(1);

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

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

 

 //Source point

 IObjectTypeSettingsPtr oType_1 = Object_1-&gt;GetObjectTypeSettings(ObjectType_SourcePoint);

 Object_1-&gt;ChangeType(oType_1);

 IObjectSourcePointPtr Source1_data = Object_1-&gt;ObjectData;

 // Proterties NumberOfLayoutRays and NumberOfAnalysisRays belong to base interface IObjectSources. To use these properties, cast to IObjectSources first.

 IObjectSourcesPtr caseIObjSources = Source1_data;

 caseIObjSources-&gt;NumberOfLayoutRays = 3;

 caseIObjSources-&gt;NumberOfAnalysisRays = 1000000;

 

 //Rectangular Volume

 //Scattering properties

 //Draw:opacity set to 50%

 IObjectTypeSettingsPtr oType_2 = Object_2-&gt;GetObjectTypeSettings(ObjectType_RectangularVolume);

 Object_2-&gt;ChangeType(oType_2);

 Object_2-&gt;ZPosition = 2;

 Object_2-&gt;Material = "N-BK7";

 IObjectRectangularVolumePtr RectVolume2_data = Object_2-&gt;ObjectData;

 RectVolume2_data-&gt;X1HalfWidth = 12;

 RectVolume2_data-&gt;Y1HalfWidth = 12;

 RectVolume2_data-&gt;ZLength = 40;

 RectVolume2_data-&gt;X2HalfWidth = 12;

 RectVolume2_data-&gt;Y2HalfWidth = 12;

 INCEVolumePhysicsDataPtr RectVolume2_volphysdata = Object_2-&gt;VolumePhysicsData;

 RectVolume2_volphysdata-&gt;Model = VolumePhysicsModelType_AngleScattering;

 RectVolume2_volphysdata-&gt;ModelSettings-&gt;_S_AngleScattering-&gt;MeanPath = 5;

 RectVolume2_volphysdata-&gt;ModelSettings-&gt;_S_AngleScattering-&gt;Angle = 30;

 INCEDrawDataPtr RectVolume2_Drawdata = Object_2-&gt;DrawData;

 RectVolume2_Drawdata-&gt;Opacity = ZemaxOpacity_P50;

 

 //Detector Rectangle

 IObjectTypeSettingsPtr oType_3 = Object_3-&gt;GetObjectTypeSettings(ObjectType_DetectorRectangle);

 Object_3-&gt;RefObject = 2;

 Object_3-&gt;ZPosition = 42;

 Object_3-&gt;Material = "ABSORB";

 Object_3-&gt;ChangeType(oType_3);

 IObjectDetectorRectanglePtr DetRect3_data = Object_3-&gt;ObjectData;

 DetRect3_data-&gt;XHalfWidth = 15;

 DetRect3_data-&gt;YHalfWidth = 15;

 DetRect3_data-&gt;NumberXPixels = 25;

 DetRect3_data-&gt;NumberYPixels = 25;

 DetRect3_data-&gt;DataType = 0;

 DetRect3_data-&gt;Color = 2;

 DetRect3_data-&gt;Smoothing = 1;

 

 TheSystem-&gt;SaveAs(SampleFile);

 

 //Open a shaded model

 IA_Ptr analysis = TheSystem-&gt;Analyses-&gt;New_Analysis(AnalysisIDM_NSCShadedModel);

 analysis-&gt;Terminate();

 analysis-&gt;WaitForCompletion();

 IAS_Ptr analysisSettings = analysis-&gt;GetSettings();

 char cfgFile[L_tmpnam_s];

 tmpnam_s(cfgFile, L_tmpnam_s);

 // Save the current settings to the temp file

 analysisSettings-&gt;SaveTo(cfgFile);

 // make your modifications to it

 // MODIFYSETTINGS are defined in the ZPL help files: The Programming Tab &gt; About the ZPL &gt; Keywords

 analysisSettings-&gt;ModifySettings(cfgFile, "SHA\_ROTX", "20");

 analysisSettings-&gt;ModifySettings(cfgFile, "SHA\_ROTY", "-20");

 analysisSettings-&gt;ModifySettings(cfgFile, "SHA\_ROTZ", "30");

 // now load in the modified settings

 analysisSettings-&gt;LoadFrom(cfgFile);

 // We don't need the temp file any more, so delete it

 remove(cfgFile);

 // Run the analysis with the new settings

 analysis-&gt;ApplyAndWaitForCompletion();

 

 //Open a detector viewer

 analysis = TheSystem-&gt;Analyses-&gt;New_Analysis(AnalysisIDM_DetectorViewer);

 analysisSettings = analysis-&gt;GetSettings();

 tmpnam_s(cfgFile, L_tmpnam_s);

 analysisSettings-&gt;SaveTo(cfgFile);

 analysisSettings-&gt;ModifySettings(cfgFile, "DVW\_SHOW", "2");

 analysisSettings-&gt;ModifySettings(cfgFile, "DVW\_SMOOTHING", "1");

 analysisSettings-&gt;LoadFrom(cfgFile);

 remove(cfgFile);

 analysis-&gt;ApplyAndWaitForCompletion();

 

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

 // The method RunAndWaitForCompletion() and Close() are inehrited from base interface ISystemToolPtr. To use these methods, first cast to ISystemTool.

 ISystemToolPtr baseTool = NSCRayTrace;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 

 TheSystem-&gt;SaveAs(SampleFile);

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