ZPL Nuances & Best Practices

  • 21 September 2022
  • 2 replies

Userlevel 6
Badge +2

Hey Community,

I’m starting this thread as a place to document some of the “pitfalls” of the ZPL language.  The ZPL is extremely powerful but since OpticStudio is trying to create a new language, the parsing is sometimes problematic.

Variable Names

You can actually assign a value to a number (the ZPL treats the number as a variable name).  So while OpticStudio won’t throw an error, you should be very careful when picking variable names:

PRINT 1 + 2		# answer is 3
1 = 2
PRINT 1 + 2 # answer is 4

FOR Loops

There is always round-off when dealing with decimals and this becomes especially true when trying to use a decimal step size in a FOR loop.  Rather than using decimals as either the startend or step size, you should keep these as integers and convert to decimals inside the FOR loop:


FOR i = 0, 10, 1
px = i / 10

 Not Recommended

FOR i = 0, 1, 0.1
px = i

Speed Up Calculations

All calculations via the ZPL force an update to both the GUI and the core code.  Pushing data to the GUI can be extremely expensive.  To get around this, I always recommend using SUSPENDUPDATES and RESUMEUPDATES:


# some really long running calculations


# all subsequent code will force a core & GUI update

SWITCH Statement

The ZPL doesn’t have a built in SWITCH statement but you can mimic one by using a series of GOTO and LABEL keywords:

code$ = "grape"

GOTO code$
! default calculations go here
fruit$ = "not recognized"
LABEL apple ! note: no double quotes
fruit$ = "apples are red"
LABEL banana ! note: labels are NOT case sensitive
fruit$ = "bananas are yellow"
LABEL grape
fruit$ = "grapes are purple"

PRINT fruit$


By default the PRINT command will always end in a new line.  If you want to continue printing on the same line, simply end the PRINT command with a comma.  You can print 2D data with 2 FOR loops:

FOR i = 1, 5, 1
FOR j = 1, 5, 1
PRINT $STR(i * j), $TAB(),

# output
# 1 2 3 4 5
# 2 4 6 8 10
# 3 6 9 12 15
# 4 8 12 16 20
# 5 10 15 20 25

If you have any other recommendations for the ZPL, please leave them in the comments.  It always nice to learn something new :D

2 replies


how do I write a ZPL code that will tile all the plots? I realize I can do that using the GUIs by going to windows → tile all windows. But trying to learn the language. If you can post it, it will  be great. 


Userlevel 6
Badge +2

Hi AC,

The ZPL only gives limited functionality for interacting with built-in windows.  You can change some settings with MODIFYSETTINGS, you can open a new window with OPENANALYSISWINDOW, you can save an image with EXPORTBMP/EXPORTJPG, and you can extract the text with GETTEXTFILE. 

Outside of these basic functions, you cannot interact with the native windows, including tiling all the plots.