Skip to main content
Solved

Image simulation using ZOS-API overwriting existing images when used in a loop


Hi all,

I am facing an issue when using the Image Simulation tool via the ZOS-API. The goal is to vary a certain variable in a loop (in this case the object distance), perform each time an image simulation, and save the image with a specific name to a file. However, I notice that the previous images are always overwritten when a new image is saved.

This is the simple MATLAB code that I am using in the main body:

sampleDir = 'D:\Zemax OpticStudio\';

testFile = System.String.Concat(sampleDir, '\Test_ImageTelecentric_Issue.zos');

TheSystem.LoadFile(testFile,false);

TheSystem = TheApplication.PrimarySystem;

TheLDE = TheSystem.LDE;

zline = 0;

surface = TheLDE.GetSurfaceAt(zline);

zvec = [1100,1500]; %variable object distance

for z = zvec

surface.Thickness = z;

index = 3; %existing Image Analysis window with correct settings

im_analysis = TheSystem.Analyses.Get_AnalysisAtIndex(index);

im_settings = im_analysis.GetSettings;

im_settings.OutputFile = ['z',num2str(z),'.png']; %change file name according to variable

im_analysis.ApplyAndWaitForCompletion(); %save image

TheSystem.Save();

end

 

Although I seem to change the OutputFile before running the analysis, the previous image is always being overwritten:

The ZEMAX archive file is found in attachment.

 

I would be very glad if someone could point out what the issue could be.

Thanks,

Indy

Best answer by MichaelH

Hi Indy,

The method TheSystem.Analyses.Get_AnalysisAtIndex(index) actually causes a refresh of the analysis window, the same as clicking the 2 blue circular arrows in the Image Simulation GUI.  So calling surface.Thickness = z; will change the object distance in the LDE, the Get_AnalsysiAtIndex will force an update with the old OutputFile name, then you run the Image Simulation (actually for the 2nd time) and then save the new file.

If you want to keep your current logic, then I suggest after the ApplyAndWaitForCompletion(), you use Matlab to rename/move your PNG file.  Actually, you can simply run the ApplyAndWaitForCompletion() and then move the file to a new folder so you’re not running IS two times.

View original
Did this topic help you find an answer to your question?

2 replies

MichaelH
Luminary
Forum|alt.badge.img+2
  • Luminary
  • 336 replies
  • Answer
  • July 15, 2024

Hi Indy,

The method TheSystem.Analyses.Get_AnalysisAtIndex(index) actually causes a refresh of the analysis window, the same as clicking the 2 blue circular arrows in the Image Simulation GUI.  So calling surface.Thickness = z; will change the object distance in the LDE, the Get_AnalsysiAtIndex will force an update with the old OutputFile name, then you run the Image Simulation (actually for the 2nd time) and then save the new file.

If you want to keep your current logic, then I suggest after the ApplyAndWaitForCompletion(), you use Matlab to rename/move your PNG file.  Actually, you can simply run the ApplyAndWaitForCompletion() and then move the file to a new folder so you’re not running IS two times.


  • Author
  • Monochrome
  • 2 replies
  • July 16, 2024

Hi Michael,

Thanks a lot for the fast and clear reply!

Indeed, changing the code with a file rename as follows solved the problem:

surface.Thickness = z;

index = 3;

im_analysis = TheSystem.Analyses.Get_AnalysisAtIndex(index);

im_analysis.ApplyAndWaitForCompletion();

name_folder = 'C:\Users\imagnus\Documents\Zemax\IMAFiles\';

name_old = 'z.png';

name_new = ['z',num2str(z),'.png'];

system("rename " + [name_folder,name_old] + " " + name_new);

 

Best regards,

Indy


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings