- Home
- Programming ZEMAX
- ZPL
- What is the Best Way to Reference a Surface or Object in ZPL?
- Home
- Frequently Asked Questions
- What is the Best Way to Reference a Surface or Object in ZPL?
What is the Best Way to Reference a Surface or Object in ZPL?
- By Mark Nicholson
- Published 26 June 2008
- ZPL , Frequently Asked Questions
-
Rating:




Surface/Object Referencing
Question: What is the best way to reference a surface or object in ZPL, so that the macro still works when surfaces/objects are inserted or deleted, and the numbering changes?
Many macros are 'quick and dirty', and so coding like this is perfectly reasonable:
a = THIC(10)
b = RADI(11)
In this case, the variable a is assigned the value of the thickness of surface 10, and b is assigned the value of the radius of curvature of the following surface. But what if you add or delete a surface before surface 10? The numbering in the editor will update, and the new surface 10 will not be the one you wanted. One solution is to ask every time the macro runs:
INPUT "What surface do you want to use", my_surface
a = THIC(my_surface)
b = RADI(my_surface + 1)
But this gets tedious if you run the macro several times without changing the surface numbering. Or, you could just use a variable:
my_surface = 10
a = THIC(my_surface)
b = RADI(my_surface + 1)
So you only have one line to edit in your macro when surfaces change. But that's still a bit tedious.
The best thing to do is to give the surface a unique comment, like "target" for example, and then use the SURC() function:
my_surface = SURC("target")
a = THIC(my_surface)
b = RADI(my_surface + 1)
SURC(A$) finds the first surface where the comment string is the same as A$ and returns its surface number. In the non-sequential component editor use OBJC() instead:
my_object = OBJC("target")
This simple structure ensures that your macro always finds the intended surface or object. Note that if no surface/object has the target string set as a comment, the functions will return the value -1. It's simple to add a test to bullet-proof your code:
my_object = OBJC("target")
IF (my_object == -1)
PRINT "Target object not found"
END
ENDIF
2 Responses to "What is the Best Way to Reference a Surface or Object in ZPL?" 
|
said this on 02 Jul 2008 8:16:08 AM PST
Sweet and simple article.
|
|
said this on 19 Sep 2008 10:05:02 AM PST
"SURC" didn't jump out of the manual at me, so finding this post was gave me eaxctly the command I needed.
|
Author)