Skip to main content
Solved

Variable type conversion in ZPL


Kris
  • Infrared
  • 19 replies

Good day everyone,

since ZPL is based on BASIC, I thought some of it generic functions could be used for converting variable types such as VAL(), INT() etc. Only $STR works, the other conversions are not recognized by ZPL.

In particular, I want to define a single variable in my script (a particular detector number) and use it for both console output (with PRINT), non-sequential raytrace (with NSTR) and changing Detector Viewer settings (with MODIFYSETTINGS).

I could define either a numerical variable and change it to string, but doing so outputs a floating point number (such as 5.000 for instance). Unfortunately, NSTR is having trouble with "h5.000” filter string. Then I thought I can define a string variable "5” and change it to numerical but no commands that I know seem to work. Is there a solution to my issue that would allow me to stay within ZPL script, not switching to ZOS-API?

Best answer by MichaelH

Hi Kris,

The ZPL doesn’t have strong variable types so there is no difference between an integer or a float; ZPL only has string variables and float variables.  So, if you want to treat a float as an integer, you will need to use FORMAT and $STR() when you pass it as an argument.  For example, if you want to pass in the filter string, you could use the following code:

FORMAT .0
filter$ = "H" + $STR(5)
NSTR 1, 1, 1, 1, 0, 1, 0, 1, "saverays.ZRD", filter$

There are some basic file system functions (all listed under KEYWORDS or STRING FUNCTIONS in the Help Files):

  • MAKEFOLDER: creates a new directory
  • $PATHNAME(): gets the directory the current file is in

For other more complex commands that are not part of the ZPL, you can use the COMMAND keyword to call an external program (i.e., to use a batch file):

https://www.linkedin.com/pulse/advanced-zpl-using-bat-files-michael-humphreys

View original
Did this topic help you find an answer to your question?

13 replies

Sean Turner
Ultraviolet
Forum|alt.badge.img+1
  • Ultraviolet
  • 91 replies
  • April 1, 2025

Hi Kris,

FORMAT 1 INT will turn your 5.000 into 5 when printed

SVAL(A$) returns a floating point value of the string A$

I hope these help!

Sean


Kris
  • Author
  • Infrared
  • 19 replies
  • April 2, 2025

Thanks for your reply ​@Sean Turner.

However, it only helps me partially.

I would need to actually convert, not only print, variable types because I want to pass them as arguments for very different functions.

For instance, either define the detector number as a number and pass it as a filter string argument; or in another approach, define it as a string, and pass it to NSTR, and so forth. I am not sure which approach would be the most convenient so far.

I am also interested if primitive file system manipulations are possible via ZPL, such as creating a new folder, or checking the folder of the current project, and so on.

Does functionality of this sort exists for ZPL?


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • Answer
  • April 2, 2025

Hi Kris,

The ZPL doesn’t have strong variable types so there is no difference between an integer or a float; ZPL only has string variables and float variables.  So, if you want to treat a float as an integer, you will need to use FORMAT and $STR() when you pass it as an argument.  For example, if you want to pass in the filter string, you could use the following code:

FORMAT .0
filter$ = "H" + $STR(5)
NSTR 1, 1, 1, 1, 0, 1, 0, 1, "saverays.ZRD", filter$

There are some basic file system functions (all listed under KEYWORDS or STRING FUNCTIONS in the Help Files):

  • MAKEFOLDER: creates a new directory
  • $PATHNAME(): gets the directory the current file is in

For other more complex commands that are not part of the ZPL, you can use the COMMAND keyword to call an external program (i.e., to use a batch file):

https://www.linkedin.com/pulse/advanced-zpl-using-bat-files-michael-humphreys


Kris
  • Author
  • Infrared
  • 19 replies
  • April 3, 2025

Thanks for an extensive answer ​@MichaelH!

The command $PATHNAME() did the job, however, it is not documented in OpticStudio. I bet more similar commands are available (for instance, now I wish to check if a specific file already exists in a specified folder), still without using COMMAND. Does a summary of all permitted in-built commands exist somewhere? This would be super useful to know.


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • April 3, 2025

Checking if a file already exists is a little bit more difficult with built-in ZPL commands.  You will need to use the FINDFILE keyword and check the length of the filename with the filter:

dir$ = $PATHNAME()
filter$ = dir$ + "\lens.zmx"

FINDFILE tempfile$, filter$
IF SLEN(tempfile$) > 0 
	PRINT "file exists"
ELSE
	PRINT "file does not exist"
ENDIF

The Help Files do list all the commands, but the ZPL section of the PDF version of the Help Files is over 200 pages long, so sometimes it’s a little difficult to find the desired functionality.  I would suggest to search the Help Files and if you can’t figure it out, to simply as the forum.


Sean Turner
Ultraviolet
Forum|alt.badge.img+1
  • Ultraviolet
  • 91 replies
  • April 3, 2025

$PATHNAME is found in The Programming Tab > About the ZPL > String Functions

It would be nice if all numeric functions, string functions, and keywords were in one section.


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • April 3, 2025

In order to better search the ZPL section of the help, I like to do the following:

  • Open the Help PDF in a browser (Chrome or Edge)
  • Determine the first and last page of the ZPL section.  For 2024R2, it starts on page 1478 and ends on 1627
  • Click on the Print icon in the browser toolbar
  • For the Printer, select Save as PDF and enter the page ranges from above
  • Click Save

Now you can search this smaller PDF quicker and more easily.  It’s not a perfect solution because you’ll still need to know what you’re looking for but I find this very helpful when I need to create complex ZPL macros.


Kris
  • Author
  • Infrared
  • 19 replies
  • April 4, 2025

Good tip ​@MichaelH! I see that the help file is indeed extensive and lists (probably) all possible ZPL commands. I see no harm in adding this extensive list to the help file as well (i.e. the interactive Windows help system) so that both sources of information - the manual and the help system - would be equivalent.


Kris
  • Author
  • Infrared
  • 19 replies
  • April 7, 2025
Kris wrote:

Good tip ​@MichaelH! I see that the help file is indeed extensive and lists (probably) all possible ZPL commands. I see no harm in adding this extensive list to the help file as well (i.e. the interactive Windows help system) so that both sources of information - the manual and the help system - would be equivalent.

One more thing, is there any command to switch a tab of a particular window (to be more precise, I wish to switch a Detector Viewer window to a Text tab)?


MichaelH
Ansys Staff
Forum|alt.badge.img+2
  • Ansys Staff
  • 352 replies
  • April 8, 2025

Hey Kris,

There is no direct way to switch between the Graph and the Text tab for an already opened analysis window.  However, you can use the OPENANALYSISWINDOW and GETTEXTFILE keywords to get the graph & text tab.  The OPENANALYSISWINDOW will actually open the window and then you can either the EXPORTBMP or EXPORTJPG to save the graph as an image.  For the GETTEXTFILE, you will need to provide a file path to save the output (you can use $TEMPFILENAME() to ensure you have a path you can write to).

For both of these keywords, you will need to know the associated 3 letter String Code.  For example, if you want to save both the image and text of the Detector Viewer, you could use the following code:

image$ = "c:\temp\dvr.bmp"
text$ = "c:\temp\dvr.txt"

! turn off updates to speed up the process
SUSPENDUPDATES

! open & save the window
OPENANALYSISWINDOW "dvr"
PAUSE THREADS

! export the last opened window
last_window = WINL()
EXPORTBMP last_window, image$, 500

! save the text file
GETTEXTFILE text$, "dvr"

! immediately show the text file (but do not delete)
SHOWFILE text$, 1

Note the last line of the code will immediately show the text file so you can have both the graph & text side-by-side.  There is no way to change the window layout position or create a new tab group.

The ZOS-API will have a lot more control over the settings & results you can extract from an analysis window, especially for the Detector Viewer.  If you need finer grained control, I’d suggest looking into the ZOS-API (for ease of plotting & saving images, I suggest either using Python+matplotlib or C#+ScottPlot).


Kris
  • Author
  • Infrared
  • 19 replies
  • April 8, 2025

Thanks for an extensive answer ​@MichaelH!

I get the urging to switch to ZOS-API, and plan to do so eventually. However, for now, I wish to finish this one script still in ZPL, to keep the process compatible with what is implemented now on my part. So I understand I am probably asking a bit too much from a scripting language not designed to deal with these kind of fine details, but nevertheless I am trying to get the best of what's possible.

One issue I see is that in case several detector windows are opened, the proposed commands do not specify which one exactly to save to an external file.

Also, GETTEXTFILE exports different data than what would be done in a Text tab. For instance, let's say there is a rectangular detector. In a viewer, one would see this 2D data, and in the Text tab, a big 2D matrix accordingly. With GETTEXTFILE, however, I only see  some 1D representation (attached in a separate pic).

Is there any hope to resolve these issues still within the framework of ZPL?

 


Sean Turner
Ultraviolet
Forum|alt.badge.img+1
  • Ultraviolet
  • 91 replies
  • April 8, 2025

@Kris you need to save a settings file for the GETTEXTFILE keyword to use. When you switch to the text tab in the detector viewer there are a few options for “Show As”. What you are showing in the middle image is the Full Listing, whereas the text shown from your GETTEXTFILE looks to be set to “Cross Section Column”. Save the settings with “Full Listing” chosen and you should be good. 

Unfortunately you can’t save multiple config files for a detector viewer, so the saved settings file will always be named “DVR.CFG”. Therefore there is no reason to specify a settings file name when using GETTEXTFILE with a detector viewer. 

Sean


Kris
  • Author
  • Infrared
  • 19 replies
  • April 9, 2025

The trick worked ​@Sean Turner! So just one manual preparatory action, saving the viewer configuration already in Text tab is needed. This worked.

There is just one last remaining issue and I do not want to give up just yet. After this script, I will switch to ZOS-API, but I still want to finalize the current project.

If I have several Detector Viewers open (with different settings such as different filter strings), both viewers show the correct signal. I then use GETTEXTFILE to independently save all datasets. However, GETTEXTFILE only saves the output of the detector where GETTEXTFILE was first used. It does not switch to other detectors. Could you propose any solution?

I will share the sample code snippet here:

# I prepare the settings for 1st Detector Viewer:
MODIFYSETTINGS full_config_path$, DVW_FILTER, ""
# ...I then open a new viewer for this configuration:
OPENANALYSISWINDOW "Dvr",full_config_path$
# ...and save its data:
last_window = WINL()
file1$="C:\some_address"
GETTEXTFILE file1$, "dvr"

# Now I need another detector, so I use the same base setting file and modify it accordingly:
MODIFYSETTINGS full_config_path$, DVW_FILTER, "h10"
# ...and open the according new viewer:
OPENANALYSISWINDOW "Dvr",full_config_path$

# Now I save the output of the 1st window:
last_window = WINL()
file2$="C:\some_other_address"
GETTEXTFILE file2$, "dvr"

As I explained, although it does create different output files, they all store the same data from the 1st opened Detector Viewer. Maybe I need to change the order of commands, or use some less-known command that I am not aware of? Your help would be much appreciated.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings