Skip to main content

Hi,

I would like to write a macro that runs the surface slope analysis and outputs the “Text” tab results (X-Coord, Y-Coord, and Slope) to a file. I was thinking of using GetTextFile, but I don’t see a string code for surface slope cross section (or slope either). What’s the best way to do this?

Thank you,

-Jen

@JenR 

 

If you didn’t find the String Code, this means its probably not implemented. By chance, the Analyze..Surface..Slope and Slope Cross Section analyses are available in the ZOSAPI, and you could achieve the same as GetTextFile with this Python code snippet:

surface_slope_analysis = TheSystem.Analyses.New_SurfaceSlope()

## Use New_SurfaceSlopeCross() for the cross-sectional analysis instead

## Optional: change analysis settings here before running analysis

surface_slope_analysis.ApplyAndWaitForCompletion()

surface_slope_analysis.GetResults().GetTextFile(r'C:/.../surface_slope_analysis.txt')

I hope this helps and take care,

 

David


Hi David,

 

Thank you for the reply. Your suggestion looks simple, but unfortunately I do am not familiar with using Python or the ZOSAPI.

 

-Jennifer


@JenR 

 

If you want to get started with Python, I highly suggest Automate The Boring Stuff with Python:

https://automatetheboringstuff.com/

I’m not affiliated in any way with the website above, but I read the book when I transitioned from MATLAB to Python. Then, its good to be aware about the community-driven ZOSPy: https://pypi.org/project/zospy/, which really facilitate the ZOSAPI integration in Python.

That being said, I know not everyone has the bandwidth or willingness to learn a new programing language. If that’s your case, I created a User-Defined Operand (UDOC02.exe), that will run a Surface Slope and save its text output in your ...Documents/Zemax/Samples/ folder under the name surface_slope.txt.

This is the code of the User-Defined operand for your reference (this is already compiled in UDOC02.exe, you don’t need to care about this part):

ZOSAPI.Analysis.IA_ surface_slope_analysis = TheSystem.Analyses.New_SurfaceSlope();
surface_slope_analysis.ApplyAndWaitForCompletion();
surface_slope_analysis.GetResults().GetTextFile(TheApplication.SamplesDir + "\\surface_slope.txt");

If you have this operand in your Merit Function:

Simply calling the ZPL keyword:

UPDATE EDITORS

Will run the Surface Slope analysis and create the text file.

You could also insert this operand when required with ZPL.

To install the User-Defined Operand, just download the ZIP attached to my answer, extract its ZAR archive, and open it in OpticStudio (it will automatically extract UDOC02.exe and place it in the correct folder).

Note that its an inefficient roundabout way of achieving what you want.

Take care,

 

David

 


Hi David,

 

Thank you! I was able to get the user-defined operand to run, but is there any way to choose which surface the calculation is done on? Also, I am really looking for the slope cross-section (3 columns of data: X-coordinate, Y-coordinate, slope), not the slope map of the whole surface.

 

-Jen


Hi Jen,

 

All of this is possible. I’ve made UDOC03.exe for the cross-section slope and it uses the column Hx for the Surface number. This is the modified code:

IA_ surface_slope_cross_analysis = TheSystem.Analyses.New_SurfaceSlopeCross();
IAS_SurfaceSlopeCross surface_slope_cross_settings = surface_slope_cross_analysis.GetSettings() as IAS_SurfaceSlopeCross;
surface_slope_cross_settings.Surface.SetSurfaceNumber((int)Hx);
surface_slope_cross_analysis.ApplyAndWaitForCompletion();
surface_slope_cross_analysis.GetResults().GetTextFile(TheApplication.SamplesDir + "\\surface_slope_cross.txt");

And the result:

Take care,

 

David


Hi David,

Thank you once again! Sorry to keep going back and forth, but now the computation is only done on surface 2 even if I have a different number in the Hx column. I though the surface could be changed based on the value of Hx?

-Jen


@JenR 

 

Strange, its working for me.

Are you using the command:

UPDATE EDITORS

In your ZPL macro to force an update of the Merit Function, and therefore rerun the analysis?

Take care,

 

David


Hi David,

It seems to be working today. Not sure why it wasn’t earlier. Maybe I just needed to close and re-open Zemax. 

Thank you very much!

-Jen


Reply