Skip to main content

Hi all,

I have been using ZOS-API Pythonnet to perform a ray trace and save the ray trace data as text files. The following program snippet worked perfectly fine up until a few months ago. Now, when I run the same code, it does the ray trace and saves the text file without changing any of the settings.

import ZOSAPI;
TheSystem = TheApplication.PrimarySystem;
teleFile=('T_file.zmx')
TheSystem.LoadFile(teleFile,False);
Systemdata=TheSystem.SystemData

TheAnalyses=TheSystem.Analyses;
X, Y = np.round(np.mgrid[(-1):(1):100j, (-1):(1):100j],3)
positions = np.vstack([X.ravel(), Y.ravel()])
pos=np.transpose(positions)
px=pos[:,0]
py=pos[:,1]
max_rays=len(px)
newWin=TheAnalyses.New_Analysis(ZOSAPI.Analysis.AnalysisIDM.RayTrace);
newWin_Settings=newWin.GetSettings();
Field_1 = Systemdata.Fields.GetField(1)
Field_1.X=0
Field_1.Y=0
path='\RT_trial'
for i in range(max_rays):
    newWin_Settings.Px=pxpi];
    newWin_Settings.Py=pyyi];
    #newWin_Settings.Wavelength.SetWavelengthNumber(1);
    #newWin_Settings.Field.SetFieldNumber(1);
    newWin_Settings.Type=ZOSAPI.Analysis.Settings.Aberrations.RayTraceType.DirectionCosines;
    newWin.ApplyAndWaitForCompletion();
    newWin_Results=newWin.GetResults();
    file_name=path+'ray_'+str(i)+'.txt'
    newWin_Results.GetTextFile(file_name)

The Px and Py =0 for all the rays in the text files. Does anyone know what the issue could be? 

Hi Ramya,

 

Assuming your code doesn’t have a bug, what’s your Python and Pythonnet version? There have been issues recently with version of Pythonnet > 2.5.2. If you are in this case, can you install Pythonnet 2.5.2 and try again?

Take care,

 

David


Hi Ramya,

If the script is actually able to run to the end but you just don’t see anything change, then I think pythonnet is correct, however please take David’s advice and ensure you’re not using pythonnet 3.0.

You might not have copied the entire script, but I don’t see where you initialize the type of connection from the zosapi module (zosapi.App() or zosapi.Interactive()).  So assume you have the proper connection to OpticStudio in another part of the script, I think you might be failing around lines 3-4; the LoadFile is looking for a full file path.  The default LENS.ZMX file which is loaded in OpticStudio has 0 aperture size, so I always like to add the following code immediately after a TheSystemLoadFile() command:

import zosapi

zos = zosapi.App()
TheSystem = zos.TheSystem
TheApplication = zos.TheApplication
ZOSAPI = zos.ZOSAPI

fullfilename = r'c:\user\Documents\Zemax\file.zmx'
TheSystem.LoadFile(fullfilename, False)

# this checks to make sure a valid file was loaded
if TheSystem.SystemData.Aperture.ApertureValue == 0:
raise SystemError('The Aperture Value is "0". Please ensure the filename is valid')

# rest of code goes below here

Also, unless it’s absolutely necessary, I would suggest simplifying how you generate your px/py values; there is no reason to use mgrid, vstack, revel, or transpose.  Since the pythonic way is to keep it simple, I would simply use 2 for loops (you can do this with a simple range but numpy already has the ability to normalize the coordinates with linspace):

n = 100
x = np.linspace(-1, 1, n)
y = np.linspace(-1, 1, n)
for px in x:
for py in y:
# make sure the ray is inside the pupil
if px*px + py*py <= 1:
# run code

 


Reply