Optimizing with ZPL Macros
If the ZPL macro language (see "OPTICSTUDIO PROGRAMMING LANGUAGE") is sufficient to perform the required computations, then the operand ZPLM may be used to call a ZPL macro from within the merit function. The macro performs the required computations, then returns the result using the ZPL OPTRETURN keyword.
ZPLM is simple to use. The Mac# and Data values are used to specify the macro number and data field number, respectively. The macro number is used to indicate which ZPL macro should be executed, while the data field number indicates which value computed by the macro should be optimized.
The macro number must be an integer between 0 and 99. If the Mac# value is 17, for example, then the macro to be executed must be named ZPL17.ZPL. The macro name must always use a two digit representation of the macro number. If the macro number was 6, then the macro to be executed would be ZPL06.ZPL. The ZPL macro file must reside in the folder for ZPL macros; see "Folders".
The data field number may be any number between 0 and 50, inclusive. This number refers to a position in a global array associated with the lens in memory. During execution of the macro, the macro keyword OPTRETURN specifies which data field number stores the results of the macro calculation. There are 51 different data fields, so that a single macro call can be used to optimize up to 51 different values simultaneously. For example, suppose you needed a macro which computed the total length of the lens from surface 1 to the image surface (this is in effect a user-defined version of the TOTR operand). The macro might look like this:
n = NSUR() x = 0 FOR i = 1, n, 1 x = x + THIC(i) NEXT OPTRETURN 0, x
Note the use of the OPTRETURN keyword. This keyword stores the resulting value for "x" in the global array position 0. Suppose this macro was named ZPL15.ZPL. To optimize the resulting value for x, the ZPLM merit function operand would be added to the Merit Function Editor, with Mac# = 15 and Data = 0. After updating the merit function, the "value" would be the same as that returned by TOTR, and it can be optimized in the same way.
ZPLM also permits the use of the data in the Merit Function Editor columns. These data fields can be read by the ZPL macro using the PVHX, PVHY, PVPX, PVPY, PVEX, and PVEY ZPL functions. "PV" is a mnemonic for "Pass Value".
There is one very important thing to know about the data field number. If it is zero, then the macro is executed and the value from OPTRETURN 0 is returned. However, if the data field number is not zero, then the macro is not executed, but any previous value stored from an earlier call to the macro is used instead. The advantage to this convention is substantial. If the macro computes many values, all of which need to be optimized, the macro only needs to be called once, yet multiple ZPLM operands can access the data. This is much more efficient than calling the macro multiple times.
For example, suppose a macro named ZPL11.ZPL computes three values, all of which require optimization. In the macro, the values are stored using OPTRETURN:
OPTRETURN 0, x OPTRETURN 1, y OPTRETURN 2, z
Then three ZPLM operands in the merit function can extract the data and perform the optimization with a single call to the macro:
ZPLM 11 0 ZPLM 11 1 ZPLM 11 2
The macro ZPL11.ZPL is only called during the evaluation of the ZPLM 11 0 operand. Note the data columns can only be used if the Int2 value is zero, since only in this case is the macro evaluated.
Next: