Zemax API (Application Programming Interface) is a set of programming interfaces that allow external control and automation of Zemax OpticStudio. The API enables developers to interact with OpticStudio programmatically using languages like Python, MATLAB, and C++ and C#.
To create a standalone application using the Zemax API with Python, you need to interface with Zemax through its ZOS-API. The ZOS-API allows you to control Zemax from an external Python environment, enabling automated tasks like design optimization, analysis, and report generation. This is especially useful when integrating Zemax into larger workflows or automating repetitive tasks.
Here presents an instance of using Zemax API with Python to generate and edit an optical system. The Zemax OpticStudio used in this application is 2024 R1.00. The python interpreter is 3.12.4, downloaded from https://www.python.org/downloads/. The python compiler in this instance is spyder 5.5.1, integrated in Anaconda 3, downloaded from https://www.anaconda.com/download/.
Zemax provides multiple python example code located at Documents\Zemax\ZOS-API Sample Code\Python. To run those codes, the compiler requires Pythonnet module. Here in the anaconda, the Pythonnet is already installed. Otherwise, an instruction of how to install Pythonnet is provided in example code.
The python standalone application code starts from header codes of about 160 lines. This code chunk locates the installed version of OpticStudio, determines root directory, creates namespace and class, load local variables. In most cases, these code chunks are not required to be edited.
Below these code chunks, the user application code can be inserted to generate ZOS file. Here I use an example (#1) for demonstration of how to use the code. This code creates a simple lens with system definitions. With the code below, they are saved at ZOS file directory: /Documents\Zemax\Samples\API\Python.
The code chunk below defines the system setting, i.e. aperture, field and wavelength. One additional field of Y height = 5 mm is added above the default field Y height = 0.
In the generated Zemax file, these properties are verified below:
The code chunk below defines the lens parameters, including radius, thickness and material. We can see how the code definition relates to the lens data editor in generated ZOS file.
The lens layout is as below:
If some special edit is needed for the defined lens, such as reversed, a quick method is to find corresponding command from the ZOS-API syntax help located at Programming tab.
It can be found from the help document that the syntax of reversing lens is below:
In the code, we can add the command on the surface 2 and 3, as below:
Rerun the whole code chunk, the lens direction is changed.
The next code chunk below is to define an F number solve at surface 3. Then based on such solve type, a quick focus command is executed to find the best focus position. F number solve forces the curvature of the surface such that the marginal ray angle exiting the surface is -1/2F, where F is the paraxial F/#.
The completed system is shown below:
This example demonstrated using Python to create a lens and define properties. Beyond basic format definition of optical properties, how to integrate special command is very helpful in system definition and revision. It is recommended programmers seek useful information from ZOS-API syntax help.
October 22, 2024