ZPL Macro for Ray Trace in Mixed mode

  • 27 January 2022
  • 2 replies


I  am facing an issue when I try to extract ray trace data for a non sequential component in mixed mode of Zemax. 

In the above data, I want to get the direction cosines of normal and incident ray for NSC object 7 and 2 using a ZPL macro (as I am tracing a 10000 rays). Since for  RANX(surface) (Surface number in the sequential editor has to be given) I don’t know which surface number has to given for this case.  I also tried to save the ray trace data every time I trace the ray, but When I run RAYTRACE hx,hy,px,py,wav, (for different px and py), somehow the data in the Raytrace text is not getting update. Can you please provide me a solution for this. 


Best answer by Jeff.Wilde 3 February 2022, 05:51

View original

2 replies

Userlevel 7
Badge +3

Hi Ramya,

I don’t think it is possible to get the mixed-mode ray data you want using the ZPL RAYTRACE keyword (and subsequent keywords like RAYX), or any merit function operands for that matter, because they only report ray data at a specific sequential surface (not inside an NSC group).  In ZPL it is possible to run a single ray trace analysis, identical to what can be done manually inside the model GUI, and save the results to a temporary file using:  GETTEXTFILE A$, Rtr (where Rtr is the string code for the analysis function and A$ is the temporary file name).  Then, data from a particular line of  interest can be extracted from this file; however, it does not appear possible to change the analysis parameters from within ZPL (e.g., Hx, Hy, Px, Py) because the single ray trace analysis is not supported by the MODIFYSETTINGS keyword. 

However, you can change the source ray parameters and access the desired ray data using ZOS-API.  Here is some sample Matlab code using the Interactive Extension mode of operation:

TheApplication = MATLABZOSConnection(10);

% define an instance for the currently loaded zmx file
TheSystem = TheApplication.PrimarySystem;

% Establish connection to existing ray trace analysis window
RayTrace = TheSystem.Analyses.Get_AnalysisAtIndex(4);

RayTrace.Settings.Hx = 0.0;
RayTrace.Settings.Hy = 0.0;
RayTrace.Settings.Px = 0.0;
RayTrace.Settings.Py = 0.5;
Rays = RayTrace.GetResults();
filename = 'D:\data\ray_trace.txt';
Rays.GetTextFile(filename); % write results to file

% open the file
% set linenum to the desired line number that you want to import
linenum = 26;
% read first nine values
C = textscan(fid,'%f',9,'headerlines',linenum-1); % C is cell array

% convert to standard array of doubles
raydata = C{:}';

We can then display the results:


and compare them to the values in the Single Ray Trace analysis window:


Of course this only traces one ray at a time, but with a couple of nested loops, rays spanning the area of the entrance pupil (Px, Py) can be sequentially traced, and the ray parameter values inside the NSC group can be extracted.



Hi Jeff,

Thanks a lot for the solution. I was able to get the ray trace data for many rays through the ZOS-API (Matlab) using a simple loop :slight_smile: . I now realize ZOS-API can do many things which ZPL macros cannot!!