Skip to content

Commit 67a6a28

Browse files
authored
Merge pull request #633 from ansys/update-zemax-opticstudio-zosapi-20271r1
Add OpticStudio ZOS-API docs for 27 R1
2 parents 2a481d6 + 1c6ca7b commit 67a6a28

1,013 files changed

Lines changed: 408979 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Example 01: New File and QuickFocus
2+
Ex 1: New lens file is created and saved in samples directory. Changes are made to aperture value, field point is added, and wavelength preset for FdC is selected. Comments, thicknesses, and radii are changed in the LDE; an F# solve is placed on the last surface's radius. Finally, the quick focus tool is opened, executed, and closed.
3+
4+
Input: None
5+
6+
Output: API\e01_new_file_and_quickfocus.zos
7+
8+
9+
[C#](example_01_cs.md)
10+
11+
[C++](example_01_cpp.md)
12+
13+
[Mathematica](example_01_nb.md)
14+
15+
[Matlab](example_01_m.md)
16+
17+
[Python](example_01_py.md)
18+
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Example 01 - C++
2+
3+
```cpp
4+
// CppStandaloneApplication.cpp : Defines the entry point for the console application.
5+
6+
#include "stdafx.h"
7+
#include <stdlib.h>
8+
#include <stdio.h>
9+
#include <iostream>
10+
#include <string>
11+
#include <ctime>
12+
#include <functional>
13+
#include <assert.h>
14+
15+
// Note - .tlh files will be generated from the .tlb files (above) once the project is compiled.
16+
// Visual Studio will incorrectly continue to report IntelliSense error messages however until it is restarted.
17+
#include "zosapi.h"
18+
19+
using namespace std;
20+
using namespace ZOSAPI;
21+
using namespace ZOSAPI_Interfaces;
22+
23+
void handleError(std::string msg);
24+
void logInfo(std::string msg);
25+
void finishStandaloneApplication(IZOSAPI_ApplicationPtr TheApplication);
26+
27+
int RunApplication()
28+
{
29+
CoInitialize(NULL);
30+
31+
// Create the initial connection class
32+
IZOSAPI_ConnectionPtr TheConnection(__uuidof(ZOSAPI_Connection));
33+
34+
35+
// Attempt to create a Standalone connection
36+
IZOSAPI_ApplicationPtr TheApplication = TheConnection->CreateNewApplication();
37+
if (TheApplication == nullptr)
38+
{
39+
handleError("An unknown error occurred!");
40+
return -1;
41+
}
42+
43+
// Check the connection status
44+
if (!TheApplication->IsValidLicenseForAPI)
45+
{
46+
handleError("License check failed!");
47+
return -1;
48+
}
49+
if (TheApplication->Mode != ZOSAPI_Mode::ZOSAPI_Mode_Server)
50+
{
51+
handleError("Standlone application was started in the incorrect mode!");
52+
return -1;
53+
}
54+
55+
IOpticalSystemPtr TheSystem = TheApplication->PrimarySystem;
56+
57+
// Add your custom code here...
58+
59+
// creates a new API directory
60+
CreateDirectory(_bstr_t(TheApplication->SamplesDir + "\\API"), NULL);
61+
CreateDirectory(_bstr_t(TheApplication->SamplesDir + "\\API\\CPP"), NULL);
62+
63+
// Set up primary optical system
64+
_bstr_t sampleDir = TheApplication->SamplesDir;
65+
_bstr_t testFile = sampleDir + (_bstr_t)"\\API\\CPP\\e01_new_file_and_quickfocus.zos";
66+
67+
//! [e01s01_cp]
68+
// Make new file
69+
TheSystem->New(false);
70+
TheSystem->SaveAs(testFile);
71+
//! [e01s01_cp]
72+
73+
ISDMaterialCatalogDataPtr(TheSystem->SystemData->MaterialCatalogs)->AddCatalog("SCHOTT");
74+
75+
//! [e01s02_cp]
76+
// Aperture
77+
ISystemDataPtr TheSystemData = TheSystem->SystemData;
78+
TheSystemData->Aperture->ApertureValue = 40;
79+
//! [e01s02_cp]
80+
81+
//! [e01s03_cp]
82+
// Fields
83+
IFieldPtr Field_1 = TheSystemData->Fields->GetField(1);
84+
IFieldPtr NewField_2 = TheSystemData->Fields->AddField(0, 5.0, 1.0);
85+
//! [e01s03_cp]
86+
87+
//! [e01s04_cp]
88+
// Wavelength preset
89+
bool slPreset = TheSystemData->Wavelengths->SelectWavelengthPreset(WavelengthPreset_d_0p587);
90+
//! [e01s04_cp]
91+
92+
//! [e01s05_cp]
93+
// Lens data
94+
ILensDataEditorPtr TheLDE = TheSystem->LDE;
95+
TheLDE->InsertNewSurfaceAt(2);
96+
TheLDE->InsertNewSurfaceAt(2);
97+
ILDERowPtr Surface_1 = TheLDE->GetSurfaceAt(1);
98+
ILDERowPtr Surface_2 = TheLDE->GetSurfaceAt(2);
99+
ILDERowPtr Surface_3 = TheLDE->GetSurfaceAt(3);
100+
//! [e01s05_cp]
101+
102+
//! [e01s06_cp]
103+
// Changes surface cells in LDE
104+
Surface_1->Thickness = 50.0;
105+
Surface_1->Comment = "Stop is free to move";
106+
Surface_2->Radius = 100.0;
107+
Surface_2->Thickness = 10.0;
108+
Surface_2->Comment = "front of lens";
109+
Surface_2->Material = "N-BK7";
110+
Surface_3->Comment = "rear of lens";
111+
//! [e01s06_cp]
112+
113+
//! [e01s07_cp]
114+
// Solver
115+
ISolveDataPtr Solver = Surface_3->RadiusCell->CreateSolveType(SolveType_FNumber);
116+
Solver->_S_FNumber->FNumber = 10;
117+
Surface_3->RadiusCell->SetSolveData(Solver);
118+
//! [e01s07_cp]
119+
120+
//! [e01s08_cp]
121+
// QuickFocus
122+
IQuickFocusPtr quickFocus = TheSystem->Tools->OpenQuickFocus();
123+
quickFocus->Criterion = QuickFocusCriterion_SpotSizeRadial;
124+
quickFocus->UseCentroid = true;
125+
ISystemToolPtr baseTool = quickFocus;
126+
baseTool->RunAndWaitForCompletion();
127+
baseTool->Close();
128+
//! [e01s08_cp]
129+
130+
//! [e01s09_cp]
131+
// Save and close
132+
TheSystem->Save();
133+
//! [e01s09_cp]
134+
135+
// Clean up
136+
finishStandaloneApplication(TheApplication);
137+
138+
return 0;
139+
}
140+
141+
void handleError(std::string msg)
142+
{
143+
throw new exception(msg.c_str());
144+
}
145+
146+
void logInfo(std::string msg)
147+
{
148+
printf("%s", msg.c_str());
149+
}
150+
151+
void finishStandaloneApplication(IZOSAPI_ApplicationPtr TheApplication)
152+
{
153+
// Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases
154+
if (TheApplication != nullptr)
155+
{
156+
TheApplication->CloseApplication();
157+
}
158+
}
159+
160+
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
161+
{
162+
return RunApplication();
163+
}
164+
165+
int _tmain(int argc, _TCHAR* argv[])
166+
{
167+
return RunApplication();
168+
}
169+
```
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Example 01 - C#
2+
3+
```csharp
4+
using System;
5+
using ZOSAPI;
6+
using ZOSAPI.Editors;
7+
using ZOSAPI.Editors.LDE;
8+
using ZOSAPI.SystemData;
9+
using ZOSAPI.Tools.General;
10+
11+
namespace CSharpStandaloneApplication
12+
{
13+
class Program
14+
{
15+
static void Main(string[] args)
16+
{
17+
// Find the installed version of OpticStudio
18+
bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize();
19+
// Note -- uncomment the following line to use a custom initialization path
20+
//bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize(@"C:\Program Files\OpticStudio\");
21+
if (isInitialized)
22+
{
23+
LogInfo("Found OpticStudio at: " + ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory());
24+
}
25+
else
26+
{
27+
HandleError("Failed to locate OpticStudio!");
28+
return;
29+
}
30+
31+
BeginStandaloneApplication();
32+
}
33+
34+
static void BeginStandaloneApplication()
35+
{
36+
// Create the initial connection class
37+
ZOSAPI_Connection TheConnection = new ZOSAPI_Connection();
38+
39+
// Attempt to create a Standalone connection
40+
IZOSAPI_Application TheApplication = TheConnection.CreateNewApplication();
41+
if (TheApplication == null)
42+
{
43+
HandleError("An unknown connection error occurred!");
44+
return;
45+
}
46+
47+
// Check the connection status
48+
if (!TheApplication.IsValidLicenseForAPI)
49+
{
50+
HandleError("Failed to connect to OpticStudio: " + TheApplication.LicenseStatus);
51+
return;
52+
}
53+
if (TheApplication.Mode != ZOSAPI_Mode.Server)
54+
{
55+
HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.Mode.ToString());
56+
return;
57+
}
58+
59+
IOpticalSystem TheSystem = TheApplication.PrimarySystem;
60+
61+
// Add your custom code here...
62+
63+
// creates a new API directory
64+
string strPath = System.IO.Path.Combine(TheApplication.SamplesDir, @"API\\CS");
65+
System.IO.Directory.CreateDirectory(strPath);
66+
67+
// Set up primary optical system
68+
string sampleDir = TheApplication.SamplesDir;
69+
string testFile = sampleDir + "\\API\\CS\\e01_new_file_and_quickfocus.zos";
70+
71+
//! [e01s01_cs]
72+
// Make new file
73+
TheSystem.New(false);
74+
TheSystem.SaveAs(testFile);
75+
//! [e01s01_cs]
76+
77+
TheSystem.SystemData.MaterialCatalogs.AddCatalog("SCHOTT");
78+
79+
//! [e01s02_cs]
80+
// Aperture
81+
ISystemData TheSystemData = TheSystem.SystemData;
82+
TheSystemData.Aperture.ApertureValue = 40;
83+
//! [e01s02_cs]
84+
85+
//! [e01s03_cs]
86+
// Fields
87+
IField Field_1 = TheSystemData.Fields.GetField(1);
88+
IField NewField_2 = TheSystemData.Fields.AddField(0, 5.0, 1.0);
89+
//! [e01s03_cs]
90+
91+
//! [e01s04_cs]
92+
// Wavelength preset
93+
bool slPreset = TheSystemData.Wavelengths.SelectWavelengthPreset(WavelengthPreset.d_0p587);
94+
//! [e01s04_cs]
95+
96+
//! [e01s05_cs]
97+
// Lens data
98+
ILensDataEditor TheLDE = TheSystem.LDE;
99+
TheLDE.InsertNewSurfaceAt(2);
100+
TheLDE.InsertNewSurfaceAt(2);
101+
ILDERow Surface_1 = TheLDE.GetSurfaceAt(1);
102+
ILDERow Surface_2 = TheLDE.GetSurfaceAt(2);
103+
ILDERow Surface_3 = TheLDE.GetSurfaceAt(3);
104+
//! [e01s05_cs]
105+
106+
//! [e01s06_cs]
107+
// Changes surface cells in LDE
108+
Surface_1.Thickness = 50.0;
109+
Surface_1.Comment = "Stop is free to move";
110+
Surface_2.Radius = 100.0;
111+
Surface_2.Thickness = 10.0;
112+
Surface_2.Comment = "front of lens";
113+
Surface_2.Material = "N-BK7";
114+
Surface_3.Comment = "rear of lens";
115+
//! [e01s06_cs]
116+
117+
//! [e01s07_cs]
118+
// Solver
119+
ISolveData Solver = Surface_3.RadiusCell.CreateSolveType(SolveType.FNumber);
120+
Solver._S_FNumber.FNumber = 10;
121+
Surface_3.RadiusCell.SetSolveData(Solver);
122+
//! [e01s07_cs]
123+
124+
//! [e01s08_cs]
125+
// QuickFocus
126+
IQuickFocus quickFocus = TheSystem.Tools.OpenQuickFocus();
127+
quickFocus.Criterion = QuickFocusCriterion.SpotSizeRadial;
128+
quickFocus.UseCentroid = true;
129+
quickFocus.RunAndWaitForCompletion();
130+
quickFocus.Close();
131+
//! [e01s08_cs]
132+
133+
//! [e01s09_cs]
134+
// Save and close
135+
TheSystem.Save();
136+
//! [e01s09_cs]
137+
138+
// Clean up
139+
FinishStandaloneApplication(TheApplication);
140+
}
141+
142+
static void FinishStandaloneApplication(IZOSAPI_Application TheApplication)
143+
{
144+
// Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases
145+
if (TheApplication != null)
146+
{
147+
TheApplication.CloseApplication();
148+
}
149+
}
150+
151+
static void LogInfo(string message)
152+
{
153+
// TODO - add custom logging
154+
Console.WriteLine(message);
155+
}
156+
157+
static void HandleError(string errorMessage)
158+
{
159+
// TODO - add custom error handling
160+
throw new Exception(errorMessage);
161+
}
162+
163+
}
164+
}
165+
166+
```

0 commit comments

Comments
 (0)