zosapi-stubs 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- zosapi_stubs-1.1.0/.gitignore +16 -0
- zosapi_stubs-1.1.0/LICENSE +21 -0
- zosapi_stubs-1.1.0/PKG-INFO +360 -0
- zosapi_stubs-1.1.0/README.md +334 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Data/__init__.pyi +658 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/PhysicalOptics/__init__.pyi +245 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/RayTracing/__init__.pyi +50 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Aberrations/__init__.pyi +441 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/DiffractionEfficiency/__init__.pyi +80 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/EncircledEnergy/__init__.pyi +216 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/ExtendedScene/__init__.pyi +254 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Fans/__init__.pyi +55 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Materials/__init__.pyi +117 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Mtf/__init__.pyi +605 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Psf/__init__.pyi +221 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/RMS/RMSField/__init__.pyi +16 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/RMS/RMSFieldMap/__init__.pyi +17 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/RMS/__init__.pyi +322 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/RayTracing/__init__.pyi +102 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Spot/__init__.pyi +102 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Surface/__init__.pyi +284 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/Wavefront/__init__.pyi +104 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Settings/__init__.pyi +429 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Tolerancing/QuickYield/__init__.pyi +115 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/Tolerancing/__init__.pyi +127 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Analysis/__init__.pyi +1338 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Common/__init__.pyi +283 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Editors/LDE/__init__.pyi +7632 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Editors/MCE/__init__.pyi +342 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Editors/MFE/__init__.pyi +679 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Editors/NCE/__init__.pyi +10941 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Editors/TDE/__init__.pyi +287 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Editors/__init__.pyi +688 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Preferences/__init__.pyi +152 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/SystemData/__init__.pyi +749 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Tools/General/__init__.pyi +736 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Tools/LMx/__init__.pyi +607 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Tools/Optimization/__init__.pyi +173 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Tools/RayTrace/__init__.pyi +602 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Tools/Tolerancing/__init__.pyi +626 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Tools/__init__.pyi +689 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/Wizards/__init__.pyi +808 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/__init__.pyi +649 -0
- zosapi_stubs-1.1.0/ZOSAPI-stubs/py.typed +0 -0
- zosapi_stubs-1.1.0/pyproject.toml +64 -0
- zosapi_stubs-1.1.0/src/zosapi_stubs/__init__.py +37 -0
- zosapi_stubs-1.1.0/src/zosapi_stubs/connection.py +307 -0
- zosapi_stubs-1.1.0/src/zosapi_stubs/stubgen/__init__.py +5 -0
- zosapi_stubs-1.1.0/src/zosapi_stubs/stubgen/cli.py +78 -0
- zosapi_stubs-1.1.0/src/zosapi_stubs/stubgen/generator.py +570 -0
- zosapi_stubs-1.1.0/src/zosapi_stubs/stubgen/xmldocs.py +166 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Michael Humphreys
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: zosapi-stubs
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Type stubs (PEP 561), a connection helper, and a rebuild tool for the Zemax OpticStudio ZOS-API
|
|
5
|
+
Project-URL: Homepage, https://github.com/x68507/zosapi-stubs
|
|
6
|
+
Project-URL: Repository, https://github.com/x68507/zosapi-stubs
|
|
7
|
+
Project-URL: Issues, https://github.com/x68507/zosapi-stubs/issues
|
|
8
|
+
Author: Michael Humphreys
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: opticstudio,pythonnet,stubs,type-hints,zemax,zos-api
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Typing :: Stubs Only
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Provides-Extra: connect
|
|
22
|
+
Requires-Dist: pythonnet>=3.0; extra == 'connect'
|
|
23
|
+
Provides-Extra: rebuild
|
|
24
|
+
Requires-Dist: pythonnet>=3.0; extra == 'rebuild'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# zosapi-stubs
|
|
28
|
+
|
|
29
|
+
**PEP 561 type stubs for the Zemax OpticStudio ZOS-API** — full autocomplete,
|
|
30
|
+
inline type hints, signature help, go-to-definition, and **hover documentation**
|
|
31
|
+
in VS Code (Pylance/Pyright) and PyCharm, for an API that ships no stubs of its
|
|
32
|
+
own.
|
|
33
|
+
|
|
34
|
+
The ZOS-API is a .NET Framework 4.8 assembly accessed from Python through
|
|
35
|
+
[pythonnet](https://pythonnet.github.io/). Because the types only exist as a
|
|
36
|
+
dynamically loaded CLR module at runtime, editors have nothing to introspect and
|
|
37
|
+
give you no completion. This package solves that by **reflecting over
|
|
38
|
+
`ZOSAPI.dll` and `ZOSAPI_Interfaces.dll`** and emitting `.pyi` stub files, with
|
|
39
|
+
**docstrings lifted from the API's `.xml` documentation** so you also get hover
|
|
40
|
+
help.
|
|
41
|
+
|
|
42
|
+
It ships three things in one wheel:
|
|
43
|
+
|
|
44
|
+
1. **`ZOSAPI-stubs/`** — the prebuilt stub package your editor reads.
|
|
45
|
+
2. **`zosapi_stubs.Connect()`** — a one-line, registry-discovered connection
|
|
46
|
+
helper (no hard-coded path, no `ZOSAPI_NetHelper.dll`).
|
|
47
|
+
3. **`zosapi-stubgen`** — a console command that *rebuilds* the stubs against a
|
|
48
|
+
newer OpticStudio install, in place, with no source checkout.
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Table of contents
|
|
53
|
+
|
|
54
|
+
- [What's included](#whats-included)
|
|
55
|
+
- [Installation](#installation)
|
|
56
|
+
- [Connecting to the ZOS-API](#connecting-to-the-zos-api)
|
|
57
|
+
- [Using the stubs](#using-the-stubs)
|
|
58
|
+
- [Rebuilding for a new OpticStudio version](#rebuilding-for-a-new-opticstudio-version)
|
|
59
|
+
- [How it works](#how-it-works)
|
|
60
|
+
- [Limitations & caveats](#limitations--caveats)
|
|
61
|
+
- [Project layout](#project-layout)
|
|
62
|
+
- [License](#license)
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## What's included
|
|
67
|
+
|
|
68
|
+
The wheel covers the **complete public surface** of the two ZOS-API assemblies:
|
|
69
|
+
|
|
70
|
+
| | |
|
|
71
|
+
|---|---|
|
|
72
|
+
| Top-level types | **831** (interfaces, classes, enums) |
|
|
73
|
+
| Namespaces | **39** (one `__init__.pyi` per namespace) |
|
|
74
|
+
| Docstrings | **1,432** injected, drawn from **1,161** documented members in the `.xml` docs |
|
|
75
|
+
| Top-level packages | `ZOSAPI`, plus `Analysis`, `Common`, `Editors`, `Preferences`, `SystemData`, `Tools`, `Wizards` and their sub-namespaces |
|
|
76
|
+
|
|
77
|
+
For **every type** the stubs describe:
|
|
78
|
+
|
|
79
|
+
- **Properties** — as `@property` (with `.setter` when the .NET property is
|
|
80
|
+
writable), correctly typed.
|
|
81
|
+
- **Methods** — full signatures with typed parameters and return types;
|
|
82
|
+
multiple .NET overloads become `@overload` blocks.
|
|
83
|
+
- **`out` / `ref` parameters** — folded into the return type as a `Tuple[...]`,
|
|
84
|
+
matching how pythonnet actually returns them.
|
|
85
|
+
- **Indexers** — emitted as `__getitem__` / `__setitem__`.
|
|
86
|
+
- **Enums** — as `IntEnum` (or `IntFlag` for `[Flags]` enums), with every member
|
|
87
|
+
and its integer value.
|
|
88
|
+
- **Inheritance** — base classes and implemented interfaces are preserved, so
|
|
89
|
+
inherited members resolve.
|
|
90
|
+
- **Generics** — generic interfaces/containers map to `typing` equivalents
|
|
91
|
+
(`IList<T>` → `List[T]`, `IEnumerable<T>` → `Iterable[T]`, etc.).
|
|
92
|
+
- **Cross-namespace references** — e.g. `IOpticalSystem.LDE` is correctly typed
|
|
93
|
+
as `ZOSAPI.Editors.LDE.ILensDataEditor`.
|
|
94
|
+
|
|
95
|
+
### Docstrings (`__doc__`)
|
|
96
|
+
|
|
97
|
+
Docstrings are taken from `ZOSAPI_Interfaces.xml` / `ZOSAPI.xml` (shipped next to
|
|
98
|
+
the DLLs by OpticStudio) and rendered as proper Python docstrings — summary,
|
|
99
|
+
`Args:`, and `Returns:` sections, with `<see cref="...">` references resolved to
|
|
100
|
+
plain names. Example of a generated stub entry:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
def RemoveSurfaceAt(self, SurfaceNumber: int) -> bool:
|
|
104
|
+
"""Removes the surface at the specified location.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
SurfaceNumber: The surface number to remove (1 to NumberOfSurfaces-1).
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
true if the surface was successfully remove; otherwise, false.
|
|
111
|
+
"""
|
|
112
|
+
...
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
These appear on hover and in signature-help popups in any editor that reads
|
|
116
|
+
docstrings from stubs (Pylance, Pyright, PyCharm).
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Installation
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pip install zosapi-stubs
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
That is the whole setup. **No `settings.json` edits, no `extraPaths`, no
|
|
127
|
+
`stubPath`.** Because this is a PEP 561 `*-stubs` package, any standards-
|
|
128
|
+
compliant type checker discovers it automatically as long as it's installed in
|
|
129
|
+
the same environment your editor uses for the project.
|
|
130
|
+
|
|
131
|
+
> **Install it into the same interpreter/venv your project uses** — that's how
|
|
132
|
+
> the editor finds it. If VS Code/PyCharm is pointed at a venv, `pip install`
|
|
133
|
+
> into that venv.
|
|
134
|
+
|
|
135
|
+
To install from the locally built wheel instead of an index:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
pip install dist/zosapi_stubs-1.1.0-py3-none-any.whl
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
To also use the one-line connection helper (or to rebuild the stubs), include
|
|
142
|
+
the `connect` extra, which pulls in `pythonnet`:
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
pip install "zosapi-stubs[connect]"
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
## Connecting to the ZOS-API
|
|
151
|
+
|
|
152
|
+
The wheel includes a small runtime package, `zosapi_stubs`, that opens a robust
|
|
153
|
+
connection in one line — no hard-coded install path, no `ZOSAPI_NetHelper.dll`:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
import zosapi_stubs
|
|
157
|
+
|
|
158
|
+
ZOSAPI, TheSystem = zosapi_stubs.Connect() # launch a new standalone instance
|
|
159
|
+
ZOSAPI, TheSystem = zosapi_stubs.Connect(extension=True) # attach to a running OpticStudio
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Both returned objects are fully typed by the bundled stubs: `ZOSAPI` is the CLR
|
|
163
|
+
namespace module and `TheSystem` is the primary `IOpticalSystem`, so completion
|
|
164
|
+
and hover docs work immediately.
|
|
165
|
+
|
|
166
|
+
The install directory is discovered automatically from the Windows registry — a
|
|
167
|
+
pure-Python port of Zemax's `Helper.cs` (`ZOSAPI_Initializer`): it checks the
|
|
168
|
+
COM `CodeBase` entry and the `HKCU\Software\Zemax\ZemaxRoot` key, validates that
|
|
169
|
+
a candidate holds `ZOSAPI.dll`, `ZOSAPI_Interfaces.dll` and
|
|
170
|
+
`OpticStudio.exe`/`ZemaxServer.exe`, and registers a .NET `AssemblyResolve` hook
|
|
171
|
+
so dependent assemblies load from that directory. Pass an explicit path to
|
|
172
|
+
override discovery: `Connect(path=r"D:\Zemax\OpticStudio")`.
|
|
173
|
+
|
|
174
|
+
For finer control, use the `Connection` class directly:
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
from zosapi_stubs import Connection
|
|
178
|
+
|
|
179
|
+
conn = Connection() # connects on construction (extension=, path= optional)
|
|
180
|
+
conn.directory # resolved install dir
|
|
181
|
+
conn.edition # 'Premium' | 'Professional' | 'Standard' | 'Invalid'
|
|
182
|
+
conn.TheApplication # IZOSAPI_Application
|
|
183
|
+
conn.TheSystem # IOpticalSystem
|
|
184
|
+
conn.open_file(r"C:\lens.zmx", False)
|
|
185
|
+
conn.close() # also works as a context manager
|
|
186
|
+
|
|
187
|
+
with Connection() as conn:
|
|
188
|
+
print(conn.TheSystem.SystemName)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
Connection problems raise specific exceptions you can catch:
|
|
192
|
+
`InitializationException` (OpticStudio not found / app won't start),
|
|
193
|
+
`ConnectionException`, `LicenseException`, `SystemNotPresentException` — all
|
|
194
|
+
subclasses of `ZOSAPIError`.
|
|
195
|
+
|
|
196
|
+
> **`Connect()` needs `pythonnet` and a local OpticStudio install** — it runs the
|
|
197
|
+
> API for real. Install with the `connect` extra. The stubs alone (for pure
|
|
198
|
+
> autocomplete) need neither.
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## Using the stubs
|
|
203
|
+
|
|
204
|
+
You can also connect manually; the stubs type everything either way. Nothing
|
|
205
|
+
about the runtime changes — the stubs are invisible to Python at execution time
|
|
206
|
+
and only inform the editor.
|
|
207
|
+
|
|
208
|
+
Write your ZOS-API code exactly as you already do. Nothing about the runtime
|
|
209
|
+
changes — the stubs are invisible to Python at execution time and only inform
|
|
210
|
+
the editor.
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
import clr
|
|
214
|
+
from pathlib import Path
|
|
215
|
+
|
|
216
|
+
zos_path = r"C:\Program Files\Zemax OpticStudio"
|
|
217
|
+
clr.AddReference(str(Path(zos_path) / "ZOSAPI.dll"))
|
|
218
|
+
clr.AddReference(str(Path(zos_path) / "ZOSAPI_Interfaces.dll"))
|
|
219
|
+
import ZOSAPI # runtime: the real CLR module
|
|
220
|
+
|
|
221
|
+
conn = ZOSAPI.ZOSAPI_Connection() # ← autocompletes
|
|
222
|
+
app = conn.CreateNewApplication() # ← ConnectAsExtension(0) for an extension
|
|
223
|
+
system = app.PrimarySystem # ← inferred as IOpticalSystem
|
|
224
|
+
lde = system.LDE # ← inferred as ILensDataEditor (hover docs)
|
|
225
|
+
row = lde.AddSurface() # ← inferred as ILDERow
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
What your editor now knows (verified with Pyright against the installed wheel):
|
|
229
|
+
|
|
230
|
+
| Expression | Inferred type |
|
|
231
|
+
|---|---|
|
|
232
|
+
| `system` | `IOpticalSystem` |
|
|
233
|
+
| `system.LDE` | `ILensDataEditor` |
|
|
234
|
+
| `system.LDE.AddSurface()` | `ILDERow` |
|
|
235
|
+
|
|
236
|
+
You get completion on `system.`, `lde.`, etc., parameter hints while typing a
|
|
237
|
+
call, and docstrings on hover.
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
## Rebuilding for a new OpticStudio version
|
|
242
|
+
|
|
243
|
+
When you upgrade OpticStudio, regenerate the stubs so they match the new API.
|
|
244
|
+
The wheel installs a console command that rewrites the stubs **in place** (right
|
|
245
|
+
where `pip` installed them), so you never need this repo:
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
pip install "zosapi-stubs[rebuild]" # adds pythonnet, used only to read the DLLs
|
|
249
|
+
zosapi-stubgen # reflects over the installed DLLs, rewrites the stubs
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
Options:
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
# non-default OpticStudio install location
|
|
256
|
+
zosapi-stubgen --dll-dir "D:\Zemax\OpticStudio"
|
|
257
|
+
|
|
258
|
+
# write to a folder instead of rebuilding in place (e.g. a project-local typings dir)
|
|
259
|
+
zosapi-stubgen -o ./typings
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
`zosapi-stubgen` reads `ZOSAPI.dll`, `ZOSAPI_Interfaces.dll` **and** their
|
|
263
|
+
`ZOSAPI*.xml` doc files from `--dll-dir` (default
|
|
264
|
+
`C:\Program Files\Zemax OpticStudio`). Restart your editor's language server (or
|
|
265
|
+
the editor) afterward so it re-reads the refreshed stubs.
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## How it works
|
|
270
|
+
|
|
271
|
+
- **`zosapi_stubs/connection.py`** is the runtime helper: it ports `Helper.cs`
|
|
272
|
+
to Python (registry discovery + `AssemblyResolve` hook) and exposes `Connect()`
|
|
273
|
+
/ `Connection`.
|
|
274
|
+
- **`zosapi_stubs/stubgen/generator.py`** loads the assemblies through pythonnet
|
|
275
|
+
and walks every public type with .NET reflection (`System.Reflection`), mapping
|
|
276
|
+
.NET types to Python annotations and writing one `__init__.pyi` per namespace.
|
|
277
|
+
- **`zosapi_stubs/stubgen/xmldocs.py`** parses the `.xml` documentation. To attach
|
|
278
|
+
a docstring to the right member it **reconstructs each member's .NET XML
|
|
279
|
+
documentation-comment ID** (e.g.
|
|
280
|
+
`M:ZOSAPI.Editors.LDE.ILensDataEditor.RemoveSurfaceAt(System.Int32)`) from the
|
|
281
|
+
reflected metadata and looks it up — handling byref/array/generic parameter
|
|
282
|
+
encodings — so signatures and docs line up exactly.
|
|
283
|
+
- **`zosapi_stubs/stubgen/cli.py`** is the `zosapi-stubgen` entry point; with no
|
|
284
|
+
`-o` it locates the installed `ZOSAPI-stubs` directory and rebuilds it there.
|
|
285
|
+
|
|
286
|
+
### Why it's runtime-safe
|
|
287
|
+
|
|
288
|
+
The stub directory is named **`ZOSAPI-stubs`** (the PEP 561 stub-package suffix)
|
|
289
|
+
and contains **only `.pyi` files — no `__init__.py`**. This is deliberate:
|
|
290
|
+
|
|
291
|
+
- A directory literally named `ZOSAPI` (or any dir with an `__init__.py`/just
|
|
292
|
+
`.pyi` files on `sys.path`) would **shadow** `import ZOSAPI` and hide
|
|
293
|
+
pythonnet's dynamically generated CLR module, breaking
|
|
294
|
+
`ZOSAPI.ZOSAPI_Connection()` at runtime.
|
|
295
|
+
- The `-stubs` suffix is recognized by type checkers as "stubs *for* the
|
|
296
|
+
`ZOSAPI` module" while being impossible to import at runtime. Your code keeps
|
|
297
|
+
importing and running against the real DLL; the stubs are consulted only by
|
|
298
|
+
the editor.
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Limitations & caveats
|
|
303
|
+
|
|
304
|
+
- **Editing the stubs is pointless** — they're regenerated wholesale. Don't
|
|
305
|
+
hand-edit; change the generator and re-run `zosapi-stubgen`.
|
|
306
|
+
- **Stubs describe shape, not runtime behavior.** They reflect the API surface
|
|
307
|
+
of the OpticStudio version they were generated from. After an upgrade,
|
|
308
|
+
[rebuild](#rebuilding-for-a-new-opticstudio-version) to stay accurate.
|
|
309
|
+
- **`pythonnet` is required only to *connect* or *rebuild*, not for autocomplete.**
|
|
310
|
+
It's an optional extra (`[connect]` / `[rebuild]`, same dependency). Using the
|
|
311
|
+
stubs for autocomplete needs nothing but the wheel; `Connect()` and stub
|
|
312
|
+
generation both need pythonnet **and** a local OpticStudio install (the DLLs
|
|
313
|
+
are read at runtime / generation time, not bundled).
|
|
314
|
+
- **Python version for connecting/rebuilding:** `Connect()` and `zosapi-stubgen`
|
|
315
|
+
must run on an interpreter where pythonnet works — currently **CPython
|
|
316
|
+
3.9–3.13**. pythonnet does not yet support 3.14; once it does, no change to
|
|
317
|
+
this package is needed. The *stubs themselves* are plain text and impose no
|
|
318
|
+
Python-version limit on your project.
|
|
319
|
+
- **`reportMissingModuleSource` warning is expected.** Pyright/Pylance may emit a
|
|
320
|
+
*warning* (not an error) like `Import "ZOSAPI" could not be resolved from
|
|
321
|
+
source` — it found the stubs but no `.py` source, which is correct: the source
|
|
322
|
+
is a compiled DLL. Type checking and completion work fully; you can silence it
|
|
323
|
+
in your project's Pyright config if desired.
|
|
324
|
+
- **`out`/`ref` returns are modeled as tuples.** Where a .NET method has `out`
|
|
325
|
+
parameters, the stub return type is a `Tuple[...]` of the return value followed
|
|
326
|
+
by the out values — matching pythonnet's behavior. Confirm ordering against
|
|
327
|
+
the API help when a method has several.
|
|
328
|
+
- **`System.Object` and unmapped types fall back to `Any`.** A small number of
|
|
329
|
+
exotic or pointer types are annotated `Any` rather than guessing.
|
|
330
|
+
- **Windows / OpticStudio required for generation.** The DLLs and their `.xml`
|
|
331
|
+
docs come from a local OpticStudio installation; the prebuilt stubs in the
|
|
332
|
+
wheel were generated from one specific install and version.
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
336
|
+
## Project layout
|
|
337
|
+
|
|
338
|
+
```
|
|
339
|
+
zosapi-stubs/
|
|
340
|
+
├── ZOSAPI-stubs/ # prebuilt PEP 561 stub package (ships in the wheel)
|
|
341
|
+
│ ├── __init__.pyi # top-level ZOSAPI namespace
|
|
342
|
+
│ ├── py.typed # PEP 561 marker
|
|
343
|
+
│ └── Analysis/ Editors/ … # one __init__.pyi per sub-namespace
|
|
344
|
+
├── src/zosapi_stubs/ # runtime package (ships in the wheel)
|
|
345
|
+
│ ├── __init__.py # exposes Connect / Connection
|
|
346
|
+
│ ├── connection.py # Helper.cs port + Connect()
|
|
347
|
+
│ └── stubgen/ # the rebuild tool
|
|
348
|
+
│ ├── generator.py # reflects over the DLLs → .pyi
|
|
349
|
+
│ ├── xmldocs.py # parses .xml docs → docstrings
|
|
350
|
+
│ └── cli.py # `zosapi-stubgen` entry point
|
|
351
|
+
├── pyproject.toml # hatchling build; force-includes ZOSAPI-stubs
|
|
352
|
+
├── README.md
|
|
353
|
+
└── dist/ # built wheel
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
---
|
|
357
|
+
|
|
358
|
+
## License
|
|
359
|
+
|
|
360
|
+
MIT
|