    

 ## On this page

  

 

 # Example 12 - C++ 

 Last update: 16.07.2025 

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

 

 ZOSAPI_Interfaces::IWavelengthsPtr sysWave = TheSystem-&gt;SystemData-&gt;Wavelengths;

 sysWave-&gt;GaussianQuadrature(0.45, 0.65, ZOSAPI_Interfaces::QuadratureSteps_S6);

 

 // Define fields using Paraxial Image Height

 ZOSAPI_Interfaces::IFieldsPtr sysField = TheSystem-&gt;SystemData-&gt;Fields;

 sysField-&gt;SetFieldType(ZOSAPI_Interfaces::FieldType_ParaxialImageHeight);

 

 // Change field 1 to be X=1.0 and Y=2.0

 ZOSAPI_Interfaces::IFieldPtr field1 = sysField-&gt;GetField(1);

 field1-&gt;X = 1.0;

 field1-&gt;Y = 2.0;

 

 // Inserts paraxial lens so there will not be an error when using ParaxialImageHeight

 TheSystem-&gt;SystemData-&gt;Aperture-&gt;ApertureValue = 10;

 auto s1 = TheSystem-&gt;LDE-&gt;GetSurfaceAt(1);

 ISurfaceTypeSettingsPtr [SurfaceType](namespace_z_o_s_a_p_i_1_1_editors_1_1_l_d_e.xhtml#a1481c47c02108d8faf7204b17fcc25ad) = s1-&gt;GetSurfaceTypeSettings(SurfaceType_Paraxial);

 s1-&gt;ChangeType(SurfaceType);

 s1-&gt;Thickness = 100;

 

 // Change polarization axis reference to be Y

 ZOSAPI_Interfaces::ISDPolarizationDataPtr sysPol = TheSystem-&gt;SystemData-&gt;Polarization;

 sysPol-&gt;Method = ZOSAPI_Interfaces::PolarizationMethod_YAxisMethod;

 

 // Add Corning catalog and remove Schott catalog

 ZOSAPI_Interfaces::ISDMaterialCatalogDataPtr sysCat = TheSystem-&gt;SystemData-&gt;MaterialCatalogs;

 sysCat-&gt;AddCatalog("Corning");

 sysCat-&gt;RemoveCatalog("Schott");

 

 // Add Title and Notes

 ZOSAPI_Interfaces::ISDTitleNotesPtr sysTitleNotes = TheSystem-&gt;SystemData-&gt;TitleNotes;

 sysTitleNotes-&gt;Title = "Add here the title";

 sysTitleNotes-&gt;Notes = "Add here the notes";

 

 // As default Files choose: COATING.DAT, SCATTER\_PROFILE.DAT, ABG\_DATA.DAT 

 ZOSAPI_Interfaces::ISDFilesPtr sysFiles = TheSystem-&gt;SystemData-&gt;Files;

 sysFiles-&gt;CoatingFile = "COATING.DAT";

 sysFiles-&gt;ScatterProfile = "SCATTER\_PROFILE.DAT";

 sysFiles-&gt;ABgDataFile = "ABG\_DATA.DAT";

 sysFiles-&gt;ReloadFiles();

 

 // Change Lens Units to Inches

 ZOSAPI_Interfaces::ISDUnitsDataPtr sysUnits = TheSystem-&gt;SystemData-&gt;Units;

 sysUnits-&gt;LensUnits = ZOSAPI_Interfaces::ZemaxSystemUnits_Inches;

 

 TheSystem-&gt;SaveAs(_bstr_t(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e12\_system\_explorer.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.Editors.LDE.SurfaceType](namespace_z_o_s_a_p_i_1_1_editors_1_1_l_d_e.xhtml#a1481c47c02108d8faf7204b17fcc25ad)

SurfaceType

All available surface types.

**Definition:** InterfacesLDE.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