    

 ## On this page

  

 

 # Example 11 - C++ 

 Last update: 17.07.2025 

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

 

 // Create New Sequential File

 TheSystem-&gt;New(false);

 // Name File

 _bstr_t fileName = TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e11\_basic\_seq.zos";

 TheSystem-&gt;SaveAs(fileName);

 

 // Changing System Explorer Settings

 // Set Aperture

 ISystemDataPtr TheSystemData = TheSystem-&gt;SystemData;

 TheSystemData-&gt;Aperture-&gt;ApertureValue = 20;

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

 

 

 // Set Apodization Type to Gaussian, and set apodization factor to 1

 TheSystemData-&gt;Aperture-&gt;ApodizationType = ZemaxApodizationType_Gaussian;

 TheSystemData-&gt;Aperture-&gt;ApodizationFactor = 1;

 // Set Wavelength

 TheSystemData-&gt;Wavelengths-&gt;SelectWavelengthPreset(WavelengthPreset_FdC_Visible);

 

 

 // Insert and Track New Surfaces, set STOP to surface 1

 ILensDataEditorPtr TheLDE = TheSystem-&gt;LDE;

 TheLDE-&gt;InsertNewSurfaceAt(1);

 TheLDE-&gt;InsertNewSurfaceAt(1);

 TheLDE-&gt;InsertNewSurfaceAt(1);

 ILDERowPtr Surf_1 = TheLDE-&gt;GetSurfaceAt(1);

 ILDERowPtr Surf_2 = TheLDE-&gt;GetSurfaceAt(2);

 ILDERowPtr Surf_3 = TheLDE-&gt;GetSurfaceAt(3);

 ILDERowPtr Surf_4 = TheLDE-&gt;GetSurfaceAt(4);

 Surf_1-&gt;IsStop = true;

 // Set some baseline parameters

 Surf_1-&gt;Thickness = 5;

 Surf_2-&gt;Thickness = 5;

 Surf_2-&gt;Radius = 100;

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

 Surf_3-&gt;Thickness = 3;

 Surf_3-&gt;Radius = -30;

 Surf_3-&gt;Material = "F2";

 Surf_4-&gt;Radius = -80;

 

 // Set system lens units to inches, scale all values with Scale Lens tool

 // For demonstration only. This file is new, so it has default units mm.

 [ZemaxSystemUnits](namespace_z_o_s_a_p_i_1_1_system_data.xhtml#aee6855a5e7c0b638c250e8f0214ca6aa) unit = TheSystemData-&gt;Units-&gt;LensUnits;

 // Open Scale Lens tool

 IScalePtr ScaleLens = TheSystem-&gt;Tools-&gt;OpenScale();

 // Apply Tool Settings

 ScaleLens-&gt;ScaleByUnits = true;

 ScaleLens-&gt;ScaleToUnit = ScaleToUnits_Inches;

 ISystemToolPtr baseTool = ScaleLens;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 // Add Rectangular Aperture to Surface 1

 // Get surface 1, create aperture settings

 Surf_1 = TheSystem-&gt;LDE-&gt;GetSurfaceAt(1);

 ISurfaceApertureTypePtr rAperture = Surf_1-&gt;ApertureData-&gt;CreateApertureTypeSettings(SurfaceApertureTypes_RectangularAperture);

 // Set aperture size

 rAperture-&gt;_S_RectangularAperture-&gt;XHalfWidth = 0.1;

 rAperture-&gt;_S_RectangularAperture-&gt;YHalfWidth = 0.1;

 // Apply aperture settings to surface 1

 Surf_1-&gt;ApertureData-&gt;ChangeApertureTypeSettings(rAperture);

 

 // Run Quick Focus

 IQuickFocusPtr QuickFocus = TheSystem-&gt;Tools-&gt;OpenQuickFocus();

 baseTool = QuickFocus;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 // Open Universal Plot of RMS Spot Size vs Surface3 Thickness

 IA_Ptr UnivPlot = TheSystem-&gt;Analyses-&gt;New_Analysis(AnalysisIDM_UniversalPlot1D);

 IAS_Ptr UnivPlot_Settings = UnivPlot-&gt;GetSettings();

 cout &lt;&lt; "Universal Plot has analysis specific settings? " &lt;&lt; UnivPlot-&gt;HasAnalysisSpecificSettings &lt;&lt; endl;

 // Above is False; Universal Plot Settings must be changed via ModifySettings (changing a config (.cfg) file)

 _bstr_t cfg = TheApplication-&gt;ZemaxDataDir + "\\\\Configs\\\\UNI.CFG";

 UnivPlot_Settings-&gt;Save(); // Create new .cfg file, named "UNI.CFG" in \\Configs\\ folder

 UnivPlot_Settings-&gt;ModifySettings(cfg, "UN1\_SURFACE", _bstr_t(TheSystem-&gt;LDE-&gt;NumberOfSurfaces - 2));

 UnivPlot_Settings-&gt;ModifySettings(cfg, "UN1\_STARTVAL", _bstr_t(Surf_4-&gt;Thickness - 0.4 / 25.4)); // Change universal plot settings

 UnivPlot_Settings-&gt;ModifySettings(cfg, "UN1\_STOPVAL", _bstr_t(Surf_4-&gt;Thickness + 0.1 / 25.4));

 UnivPlot_Settings-&gt;ModifySettings(cfg, "UN1\_STEPS", _bstr_t(20));

 UnivPlot_Settings-&gt;ModifySettings(cfg, "UN1\_PAR1", _bstr_t(10));

 UnivPlot_Settings-&gt;ModifySettings(cfg, "UN1\_OPERAND", "RSRE");

 // For ModifySettings keycodes (UN1\_STARTVAL, UN1\_STEPS, etc.), see MODIFYSETTINGS page in ZPL&gt;keywords help files

 // LoadFrom allows you to load any CFG file, not just default; not available via GUI

 UnivPlot_Settings-&gt;LoadFrom(cfg);

 

 // Open Spot Diagram to See Result!

 IA_Ptr newSpot = TheSystem-&gt;Analyses-&gt;New_StandardSpot();

 cout &lt;&lt; "Spot has analysis specific settings? " &lt;&lt; newSpot-&gt;HasAnalysisSpecificSettings &lt;&lt; endl; // True; no ModifySettings

 IAS_SpotPtr spotSet = newSpot-&gt;GetSettings();

 spotSet-&gt;RayDensity = 15;

 newSpot-&gt;ApplyAndWaitForCompletion();

 

 // 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.SystemData.ZemaxSystemUnits](namespace_z_o_s_a_p_i_1_1_system_data.xhtml#aee6855a5e7c0b638c250e8f0214ca6aa)

ZemaxSystemUnits

**Definition:** InterfacesSE.cs:27



[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