In the Twyman-Green Interferometer example, a select few functions and keywords are used to automate the process of tilting the test plate and saving the images of the fringe patterns to separate files.  Type in the following text into your text editor (or you may use the already generated macro at the end of this article).  Though the ZEMAX macro language is not case sensitive, it is good practice to capitalize functions and keywords to help distinguish the macro logic.  EditPlus 2 can be used to color code functions (in blue below), keywords (in red below), and strings…which make it even easier to read and create macros.

Writing the macro

To get a better idea of the functionality of this marco, let's review the macro logic line by line (the line numbers are indicated in gray at the very left hand side of each line):

LINE DESCRIPTION 
10 This line of the macro defines a FOR loop, which marks the beginning of a group of statements to be executed a multiple number of times.  The start_value is the initial Tilt About X (zero degrees) for the test surface, and the stop_value is the desired end value for the loop (0.1 degrees).  For simplicity, we will only execute five different tilts of the test surface, hence the increment of 0.025 degrees.
11 This starts the first statement to be executed within the FOR loop.  The SETNSCPOSITION keyword in this case is used to set the x tilt (the Tilt About X parameter) of the test object.  Note that the value is set to the value carried by the variable, x.
 12 Prior to performing each new ray trace, it is important to clear the information from the detectors first.  Line twelve of the macro calls the NSDD(surf, object, pixel, data) function.  If the object number is zero, then all detectors are cleared and the function returns zero.
 13 The non-sequential ray trace is performed by the keyword, NSTR.  Note that split rays, polarization, and ignore errors are all flagged by the integer 1.  This is equivalent to checking the respective boxes in the Ray Trace/Detector Control dialog.
 14 UPDATE 3 updates window number 3, which is the Detector Viewer in the Twyman-Green_2.ZMX file.  This window will need to be updated to reflect the new ray trace results upon each execution of each loop.
 15, 16, & 17 Each of these lines are used to define the path and name of the file to be saved upon each execution of the loop.  Strings can be added together (as in Line 17) to generate a single string entity.  The $STR(x) string function is used to convert the value of x into a string.  This way, each file will be saved under a different name which is indicative of the tilt value.  Note that the path (indicated by prefix$) may be changed to indicate any desired path.  The above path was specified simply for demonstration purposes.
 18 The EXPORTWMF keyword exports any graphic window as a Windows Metafiile.  This keyword requires the filename to have the desired extension (.WMF) included in the filename argument.  The filename argument, is indicated by the filename$ string, which was defined in lines 15 through 17.
 19 The NEXT keyword marks the end of the group of statements.
 20 Once the FOR loop has been executed the defined number of times, the PRINT statement is used to indicate that the macro is complete.
 21 The END keyword terminates the macro.