Solved

GetTextFile returns True, but does not create a file in ZOS-API via Python

  • 25 October 2022
  • 2 replies
  • 204 views

Hi,

 

Having started with the default python Interactive Extension template generated by OpticStudio, I added the following code:

# Insert Code Here
huygens_psf = TheSystem.Analyses.New_HuygensPsf()

huygens_psf.ApplyAndWaitForCompletion()
success = huygens_psf.GetResults().GetTextFile("textdump.txt")
print(success)

When running this as an interactive extension, the following gets printed to my terminal emulator:

Found OpticStudio at:   %sc:\program files\zemax opticstudio                                                                                                                                           Connected to OpticStudio                                                                                                                                                                               Serial #:  L118546                                                                                                                                                                                     True                                                                                                                                                                                                                   

That last “True” implies that the GetTextFile() supposedly was successful in writing the PSF data as a text file. But, looking at the directory from which I was running the python file and in which it was located, I can’t see the textdump.txt which should have been generated. I repeated the same thing with the more explicit complete path (C:/… etc, which is a reflection of my personal system) but again to no avail. This is the smallest reproducible example of this problem, but I initially saw it in the context of a larger program which could successfully load the PSF and read the values by huygens_psf.GetResults().GetDataGrid().Values, for example. This leads me to believe that the PSF analysis is done correctly and successfully, and that the correct thing is returned by GetResults(). Has this kind of problem been seen before? Could it be some sort of internal OpticStudio settings or operating system security settings? Where could I start to look for the cause of this behavior?

My python version is 3.8.13, my pythonnet version is 2.5.2, and my OpticStudio version is 22.2.1. 

Thanks for your help,

Daniel

icon

Best answer by MichaelH 25 October 2022, 19:27

View original

2 replies

Userlevel 6
Badge +2

Hi Daniel,

For GetTextTile(), you need to pass in the full file path.  The True that is returned is not saying that the file was successfully written (the ZOS-API does not perform syntax/error checking such as a valid file path) but rather than there were no internal errors in the calculation for Huygens PSF & generating the results.  The ZOS-API will also not verify if you have the correct path separator nor will it create a directory if it doesn’t already exist.

You should use the os module in Python and something like the following code to save “local” files:

import os
fullfile = os.path.join(os.path.sep, os.path.dirname(TheSystem.SystemFile), r"textdump.txt");

 

Hi Michael,

 

Makes sense, specifying the whole path worked for me.

 

Regards,
Daniel

Reply