xzos / PyZDDE

Zemax/ OpticStudio Extension using Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No function to export jpg to file PyZDDE

opened this issue · comments

Hello,

I've come across the ipzCaptureWindow function, but this only seems to work inside the IPython environment. Since I'm working on a bigger project I cannot use IPython. I'd like to export a screenshot of a window to a designated .jpg (or .png or .bmp) file.

I've seen the W01_PyZDDE_ExportJPEGWIN01.ZPL macro, but I cannot dynamically specify the filename, causing it to write it to the same file when I execute the macro multiple times.

I've edited the macro to tell the filename to use the date:

CLOSEWINDOW

DATAPATH$ = $DATAPATH()
date$ = $DATE() 
PRINT date$
filename$ = DATAPATH$ + date$
EXPORTJPG 1, filename$, 500

I use the following to execute the macro within python
ln.zExecuteZPLMacro('SAVEIM.ZPL')

This would be a good solution if $DATE() were to return both date ánd time, however, it only returns the date time format from windows, which outputs only Month-Day-Year (and no time) and I need a smaller timescale.

Preferably, I'd like to be able to assign my own filename. Is this possible within PyZDDE?

PS. I need to save a large number of these windows while changing parameters of the design using PyZDDE. So using macro's as a solution is not an option.

Kind regards

I've solved the issue by continuously renaming the saved .jpg file via python itself, it's not pretty but it works. Maybe there's a better way to do this, but I'm happy for now:)

Hi @Toniostado,

I am sorry that I couldn't respond to you earlier. The ipzCaptureWindow() function was designed to work in an IPython environment only. The ipzCaptureWindow() function internally uses GetMetaFile DDE command, which Zemax has removed in recent versions of OpticStudio. So, going forward, I imagine that the only way to dump images automatically from Zemax is by using macros (which I don't really like myself exactly because of the limitations that you have mentioned).
Also, you are right that one cannot change the macro to specify a filename, etc. That is a limitation imposed by Zemax, unfortunately, and so we probably cannot do much about it from PyZDDE.
I am not sure if there is any other more efficient way to circumvent this problem other than what you have done.
Since, you seem to have resolved your problem for now, I will go ahead and close this issue.

I am trying to do the same thing, however when I try to ExecuteZPLMacro('xyz') I get either -999 or -1 return code.

I am able to use the 'CALL MACRO' operand in Zemax and run the secondary macro, but unable to do so in PyZDDE

@shbhuk The errors you are seeing could be due to a couple of reasons. Where does the zpl macro file which you are trying to execute reside? Is it in the default location (i.e. in C:\Users\xxx\Documents\Zemax\Macros)? If not then you got to do two things:

  1. Change the folder location in Zemax (Setup >> Project Preferences >> Folders >> ZPL) to the appropriate location, and also
  2. Use the PyZDDE function zSetMacroPath() to set the macro path to the same folder as 1.

See the example below (I just executed in Jupyter Console and OpticStudio 16.5):

The .zpl file is in the default Macro location.