    

 ## On this page

  

 

 # Example 01 - C++ 

 Last update: 16.07.2025 

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

 

 // Set up primary optical system

 _bstr_t sampleDir = TheApplication-&gt;SamplesDir;

 _bstr_t testFile = sampleDir + (_bstr_t)"\\\\API\\\\CPP\\\\e01\_new\_file\_and\_quickfocus.zos";

 

 // Make new file

 TheSystem-&gt;New(false);

 TheSystem-&gt;SaveAs(testFile);

 

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

 

 // Aperture

 ISystemDataPtr TheSystemData = TheSystem-&gt;SystemData;

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

 

 // Fields

 IFieldPtr Field_1 = TheSystemData-&gt;Fields-&gt;GetField(1);

 IFieldPtr NewField_2 = TheSystemData-&gt;Fields-&gt;AddField(0, 5.0, 1.0);

 

 // Wavelength preset

 bool slPreset = TheSystemData-&gt;Wavelengths-&gt;SelectWavelengthPreset(WavelengthPreset_d_0p587);

 

 // Lens data 

 ILensDataEditorPtr TheLDE = TheSystem-&gt;LDE;

 TheLDE-&gt;InsertNewSurfaceAt(2);

 TheLDE-&gt;InsertNewSurfaceAt(2);

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

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

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

 

 // Changes surface cells in LDE

 Surface_1-&gt;Thickness = 50.0;

 Surface_1-&gt;Comment = "Stop is free to move";

 Surface_2-&gt;Radius = 100.0;

 Surface_2-&gt;Thickness = 10.0;

 Surface_2-&gt;Comment = "front of lens";

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

 Surface_3-&gt;Comment = "rear of lens";

 

 // Solver

 ISolveDataPtr Solver = Surface_3-&gt;RadiusCell-&gt;CreateSolveType(SolveType_FNumber);

 Solver-&gt;_S_FNumber-&gt;FNumber = 10;

 Surface_3-&gt;RadiusCell-&gt;SetSolveData(Solver);

 

 // QuickFocus

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

 quickFocus-&gt;Criterion = QuickFocusCriterion_SpotSizeRadial;

 quickFocus-&gt;UseCentroid = true;

 ISystemToolPtr baseTool = quickFocus;

 baseTool-&gt;RunAndWaitForCompletion();

 baseTool-&gt;Close();

 

 // Save and close

 TheSystem-&gt;Save();

 

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