- Home
- Programming ZEMAX
- ZPL
- What is ZPL?
Introducing ZPL
This article is also available in Japanese.
The ZEMAX Programming Language (ZPL) allows users to write their own programs within ZEMAX. These programs can:
- Automate repetitive keyboard and mouse actions
- Perform calculations based on ZEMAX data
- Export data in specific formats
- Produce graphs and text listings of data
and much, much more! Creating ZPL macros is easy. Our goal is to give optical engineers, who may not have much programming experience, a powerful programming tool which executes quickly and is easy to learn (ZEMAX also supports Extensions, which is an API-level interface which allows programmers access to ZEMAX's features from external programs, such as Matlab).
NOTE: This article is not intended to be a replacement for some of the main points covered in the ZEMAX Programming Language chapter. If you are new to ZPL, it is highly recommended that you also read through Chapter 22 in the User’s Guide in addition to this article.
ZPL consists of five basic concepts: variables, operations, functions, keywords and comments. Variables may be either numeric or string, and array variables may also be defined. For example, here is a classic "hello world" type of program which may be written in ZPL:
Variable a is assigned the value of 1, variable b is assigned the value of 2, and we use the + operator to add them together. ZPL supports all mathematical, string and logical operations.
A function is a call to a pre-compiled ZEMAX calculation, and is used to return a value. For example: 
will call the pre-compiled Fiber Coupling Efficiency calculation, and store the results in the variable coupling. FICL() calls the same calculation you would access from the graphical user interface via Analysis > Calculations > Fiber Coupling. This function executes at the same speed as the native ZEMAX function, because it is a call to the native ZEMAX function. ZEMAX supports a wide range of functions, and any optimization operand can be called directly from ZPL.
Keywords are pre-compiled routines that provide the capability to direct program flow, or perform crucial tasks such as ray tracing and modifying the lens prescription. For example, to tell ZEMAX to optimize a lens file for 10 cycles, you may send the command:
Again, this executes exactly the same code as is called when you press the Optimize > 10 cycles button in the graphical user interface. Keywords span the whole range of ray-tracing and physical optics calculations in ZEMAX, plus provide program flow control like FOR...NEXT and IF...THEN...ELSE...ENDIF statements.
Finally, comments allow you to document how your ZPL program works. Any line that starts with !, and any text that follows #, is treated as a comment and ignored (see example below).
ZPL is a script that wraps these components together. To demonstrate, imagine that you need to optimize a lens, compute the merit function value and multiply this value by the log of 5. To do this, the commands within ZPL are simply:

The syntax and details of each function and keyword available are described in the ZPL functions and ZPL keywords section of the ZEMAX User’s Guide, respectively. It is important to understand that these are pre-compiled functions. Any time a function or keyword is called within the script, it is calling pre-compiled commands. ZPL calls the same functions used when you press the appropriate buttons or select the appropriate menu options in the graphical user interface; there is no reduction in computation time whatsoever.
The execution of a ZEMAX macro is very fast, and ZPL is a simple language to learn and use. To help get you started, there are several articles within the ZEMAX Knowledgebase which demonstrate some of the capabilities of the ZEMAX Programming Language, and many samples are supplied with ZEMAX.
