ZPL-Tutorial

前言

  最近需要用Zemax分析一个6片式的镜头组的总体透射率,由于原始数据是使用CodeV建模并且Surface Type为Odd Polynomial类型,利用Zemax自带的Converter宏无法将此面型转化(只可识别有限的面型),为避免重复输入多组数据,只好使用ZPL Macros来解决此问题。

Chapter 1 ZPL Macros Group

  ZPL Macros is a section of the Programming Tab

1.1 Edit/Run

**Edit: ** The edit button invokes OpticStudio’s text editor which can be used to modify or rename the macro. This editor colors the macro text using the following ZPL syntax:

Syntax Category Color
Comment Green
String Violet
Number Light Blue
ControlKeyword Blue
FunctionKeyword Brown
Identifier Black

1.2 Refresh List (zpl macros group)

  This tool updates the current macro list; which may be required if any macros have been added or deleted since OpticStudio was started or the last time the list was refreshed.

1.3 New Macro and Macro Help

  This button opens an OpticStudio text editor to create a new ZPL macro. Macros created with this text editor are automatically saved with the *.ZPL extension and in the Macros user data folder.

  This opens the HTML OpticStudio Help Files to the section About the ZPL. If you are viewing this as an HTML window, please click the “+” next to the section header in the Contents pane to expand the subtopics.

Chapter 2 About the ZPL

2.1 Introduction (about the zpl)

  The Zemax Programming Language (ZPL) is a macro language specifically designed for use with OpticStudio. ZPL offers the power of user-entensibility. This means thar if you need a particular calculation or graphical display which is not “built in”, you can write your own ZPL macro to do the job.

  ZPL is similar to the BASIC programming language, except not all BASIC constructs and keywords are supported, and capabilities and functions unique to ray tracing have been added. ZPL is easy to use, and this section will give you instructions and examples to get you started.

  ZPL is a powerful programming language. While it is easy to use, ZPL has no built-in debugging or variable shadowing capabilities, so the user is responsible for error-checking, debugging code logic and for good programming practice. For this reason, technical support on macro writing is restricted to ensuring that all ZPL functions and keywords work as documented: we cannot advise on how to perform detailed calculations as part of technical support. If you need a OpticStudio macro, and do not possess the desire or ability to write it yourself, please feel free to contact OpticStudio Technical Support for a quote on developing a custom program to meet your requirements. We have considerable experience in developing these types of programs, and can generally write macros at very competitive rates on short notice.

2.2 Creating ZPL Macros

  A ZPL macro consists of a series of commands which are stored in a text file. The commands are either assignments, keywords, or comments. See “OPTICSTUDIO PROGRAMMING LANGUAGE” for more information.

  To create a ZPL macro, it is probably easiest to start with an existing macro that performs a task similar to the one you want to achieve. If you are attempting to write your first ZPL macro, you may want to read the example sections at the end of this help file. Some example ZPL macros can also be found in the <data>\Macros folder installed with the OpticStudio program.

  Use any text editor to create the ZPL file (such as the NOTEPAD editor). The file may have any name but must end in the .ZPL extension. The file must be placed in the ZPL Folder, which by default is <data>\Macros.

  There is a limit to the allowed complexity of any single line in a ZPL macro. If the “line too long” error occurs, try breaking the line into several smaller lines.

2.3 An Overview of ZPL

  A ZPL macro consists of a series of commands which are stored in a text file. The commands are either assignments, keywords, or comments. Assignments may be for either numeric or string (textual) data. Both assignments and keywords may accept expressions as arguments, although the syntax is slightly different as described below.

2.3.1 Assignments

  The general syntax for an assignment is

1
variable = (expression)

  The (expression) may consist of an explicit value, such as 5, or a variable name containing some preassigned value, or a complex mathematical expression involving functions, constants, and variables. In all cases, the expression on the right side of the equal sign is evaluated, and the result is assigned to the variable designated on the left.

  The simplest form of an assignment is where the expression is a fixed value, such as:

1
x = 5

  There are several important things to note in this command. First, no declaration of variables is required. This means “x”, which is called a variable, did not need to exist before the value of 5 was assigned to it. If “x” had already been assigned a value, it would now be reassigned. Second, no special symbol is required to terminate a command, such as “;” in C. Because of this, each ZPL command must be on a line by itself

1
2
3
x = SQRT (5)
y = SINE (x)
z = SQRT (x + 5 * (7-x))

  The functions SQRT (square root) and SINE (sine) are built in to ZPL. There are many of these functions, all of which are defined in “Numeric functions”. Note that ZPL is not case-sensitive; SQRT() and sqrt() are the same function. This documentation will use the convention of capital letters for functions and keywords, and lower case for everything else.

  There are also string assignments as described in “String variables”.

2.3.2 Keywords (an overview of zpl)

  The general syntax for a keyword is

  KEYWORD argument1, argument2, argument3...

  Some keywords have no arguments, others have many. Arguments may be either numeric expressions or string constants or string variables. Some keywords accept a mixture of numeric and string arguments.

  One example of a keyword is PRINT. The PRINT keyword is followed by a list of items, separated by commas, to be printed. For example, the ZPL commands

1
2
x = 3 y = 4
z = SQRT (x * x + y * y) PRINT "The hypotenuse is ", z

  will print the following:

The hypotenuse is 5.0000

  Note that ZPL enforces operator precedence. ZPL uses the following precedence from highest to lowest: Parentheses, functions (such as SQRT), logical operators (such as ==), multiplication and division, and then addition and subtraction.

  There are many keywords, all are described in “Keywords”.

2.3.3 Comments

  There are 3 ways to add comments to a ZPL macro: by starting a line with the REM keyword, by starting a line with the “!” symbol, or by placing the “#” symbol anywhere on the line as long as the # symbol is not inside a string. Blank lines are also allowed anywhere in the macro. Here are 3 examples of comments:

1
2
3
REM this is a remark
! This is also a remark
x = 5 # The # symbol allows comments on the same line as a valid command

  Comments make macros easier to understand and modify, and have no effect on performance.

2.3.3 Creating Graphics

  There are a number of low-level functions for generating graphics, including GRAPHICS, GTEXT, GLEN- SNAME, and others. For details see “GRAPHICS”. An easier method of generating typical OpticStudio style line graphs and 2D graphs graphics is available using the PLOT and PLOT2D keywords; see “PLOT” and “PLOT2D”.

2.4 Numeric Variables

https://support.zemax.com/hc/en-us/articles/1500005576742-Getting-started-with-Python


ZPL-Tutorial
https://blog.alist-ruyi.top/2024/06/ZPL-Tutorial/
Author
Ruyi Chen
Posted on
06/19/2024 16:28
Licensed under