    

 ## On this page

  

 

 # Example 19 - C++ 

 Last update: 17.07.2025 

# <a class="anchor" id="ex19_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 new directory

 CreateDirectory(TheApplication-&gt;SamplesDir + "\\\\API", NULL);

 CreateDirectory(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP", NULL);

 

 // This code build a chain of prisms based on the KB article:

 // http://zemax.com/os/resources/learn/knowledgebase/how-to-work-in-global-coordinates-in-a-sequential

 TheSystem-&gt;New(false);

 

 // ISystemData represents the System Explorer in GUI.

 // We access options in System Explorer through ISystemData in ZOS-API

 ISystemDataPtr TheSystemData = TheSystem-&gt;SystemData;

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

 TheSystemData-&gt;Aperture-&gt;AFocalImageSpace = true;

 TheSystemData-&gt;Wavelengths-&gt;GetWavelength(1)-&gt;Wavelength = 0.55;

 

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

 

 // Get interface of Lens Data Editor and add 3 surfaces.

 ILensDataEditorPtr TheLDE = TheSystem-&gt;LDE;

 TheLDE-&gt;InsertNewSurfaceAt(2);

 TheLDE-&gt;InsertNewSurfaceAt(2);

 TheLDE-&gt;InsertNewSurfaceAt(2);

 

 // Set thickness and material for each surface.

 TheLDE-&gt;GetSurfaceAt(1)-&gt;Thickness = 30;

 TheLDE-&gt;GetSurfaceAt(2)-&gt;Thickness = 20;

 TheLDE-&gt;GetSurfaceAt(4)-&gt;Thickness = 30;

 TheLDE-&gt;GetSurfaceAt(2)-&gt;Material = "N-BK7";

 

 // GetSurfaceAt(surface number shown in LDE) will return an interface ILDERow

 // Through property TiltDecenterData of each interface ILDERow, we can modify data in Surface Properties &gt; Tilt/Decenter section

 TheLDE-&gt;GetSurfaceAt(2)-&gt;TiltDecenterData-&gt;BeforeSurfaceOrder = TiltDecenterOrderType_Decenter_Tilt;

 TheLDE-&gt;GetSurfaceAt(2)-&gt;TiltDecenterData-&gt;BeforeSurfaceTiltX = 15;

 TheLDE-&gt;GetSurfaceAt(2)-&gt;TiltDecenterData-&gt;AfterSurfaceTiltX = -15;

 TheLDE-&gt;GetSurfaceAt(3)-&gt;TiltDecenterData-&gt;BeforeSurfaceTiltX = -15;

 TheLDE-&gt;GetSurfaceAt(3)-&gt;TiltDecenterData-&gt;AfterSurfaceTiltX = 15;

 

 // To specify an aperture to a surface, we need to first create an ISurfaceApertureType and then assign it.

 ISurfaceApertureTypePtr Rect_Aper = TheLDE-&gt;GetSurfaceAt(2)-&gt;ApertureData-&gt;CreateApertureTypeSettings(SurfaceApertureTypes_RectangularAperture);

 Rect_Aper-&gt;_S_RectangularAperture-&gt;XHalfWidth = 10;

 Rect_Aper-&gt;_S_RectangularAperture-&gt;YHalfWidth = 10;

 TheLDE-&gt;GetSurfaceAt(2)-&gt;ApertureData-&gt;ChangeApertureTypeSettings(Rect_Aper);

 TheLDE-&gt;GetSurfaceAt(3)-&gt;ApertureData-&gt;PickupFrom = 2;

 

 // To change surface type, we need to first get an ISurfaceTypesettings and then assign it.

 ISurfaceTypeSettingsPtr SurfaceType_CB = TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceTypeSettings(SurfaceType_CoordinateBreak);

 TheLDE-&gt;GetSurfaceAt(4)-&gt;ChangeType(SurfaceType_CB);

 

 // Set Chief Ray solves to surface 4, which is Coordinate Break

 // To set a solve to a cell in editor, we need to first create a ISolveData and then assign it.

 ISolveDataPtr Solve_ChiefNormal = TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceCell(SurfaceColumn_Par1)-&gt;CreateSolveType(SolveType_PickupChiefRay);

 TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceCell(SurfaceColumn_Par1)-&gt;SetSolveData(Solve_ChiefNormal);

 TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceCell(SurfaceColumn_Par2)-&gt;SetSolveData(Solve_ChiefNormal);

 TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceCell(SurfaceColumn_Par3)-&gt;SetSolveData(Solve_ChiefNormal);

 TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceCell(SurfaceColumn_Par4)-&gt;SetSolveData(Solve_ChiefNormal);

 TheLDE-&gt;GetSurfaceAt(4)-&gt;GetSurfaceCell(SurfaceColumn_Par5)-&gt;SetSolveData(Solve_ChiefNormal);

 

 // Copy 3 surfaces starting from surface number 2 in LDE and paste to surface number 5, 

 // which will become surface number 8 after pasting.

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

 TheLDE-&gt;CopySurfaces(2, 3, 5);

 }

 // Save file

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e19\_Sample\_Prism\_Chain.zos");

 

 // Run tool Convert Local To Global Coordinates to convert surface #2 to surface #35 to be globally referenced to surface #1

 TheLDE-&gt;RunTool_ConvertLocalToGlobalCoordinates(2, 35, 1);

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e19\_Sample\_Prism\_Chain\_GlobalCoordinate.zos");

 

 // Run tool Conver Global To Local Coordinates to convert surface #1 to surface #57 back to local coordinate.

 TheLDE-&gt;RunTool_ConvertGlobalToLocalCoordinates(1, 57, ConversionOrder_Forward);

 TheSystem-&gt;SaveAs(TheApplication-&gt;SamplesDir + "\\\\API\\\\CPP\\\\e19\_Sample\_Prism\_Chain\_BackTo\_LocalCoordinate.zos");

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