Changing UpdateMode with python

  • 28 June 2020
  • 3 replies
  • 141 views

I am writing an Interactive Extension with python (following this KB article) and want to have the 3D Layout window update. Apparently, with these extensions, the default UpdateMode is set to Editors only.


I searched the KnowledgeBase and found two relevant articles:


https://my.zemax.com/en-US/Knowledge-Base/kb-article/?ka=KA-01828#EditorsOnly


https://my.zemax.com/en-US/forum/threads/4e706d89-37a3-e911-b083-00155de9288f#143314bd-39a3-e911-a978-000d3a378dd3


Unfortunately, these explain how to do this for MATLAB only, and I am very confused about “ZOSAPI' vs. “TheSystem' vs. “constants' etc. I “translated' both options in the first KnowledgeBase article from MATLAB to python and tried them:


Option 1:  


    TheSystem.UpdateMode=constants.LensUpdateMode.AllWindows


                  This produces “AttributeError: LensUpdateMode”. I also tried replacing 'constants' with 'TheSystem', 'TheApplication', etc.


Option 2:  


    for i in range(1,TheSystem.Analyses.NumberOfAnalyses):


       TheSystem.Analyses.Get_AnalysisAtIndex(i).ApplyAndWaitForCompletion();


             This does not throw an error, but it also does not update the 3D Layout (which I had previously saved with the file)


Can anyone provide an example of setting “Update All Windows” for python? Or for manually updating with ApplyAndWaitForCompletion?


Thanks and best wishes,


Tom


 


3 replies

Userlevel 5
Badge +2

Hello Tom,


Thanks for your question here!


Regarding Option 1, the “constants” library is unique to the old Python setup that connected Python to the API through a COM link. This link required all enumerated variables be placed in a single library with the title “constants”. This connection type is discussed in the following knowledge base article: ZOS-API using Python


If you are using the COM link, then the correct syntax to change the update mode is:



TheSystem.UpdateMode = constants.LensUpdateMode_AllWindows
TheSystem.UpdateStatus()

However, in OpticStudio 20.2, we moved to a .NET connection. Now, Python’s syntax should be nearly identical to what you see in the Matlab examples we provide. You may learn more about this transition and its requirements here: ZOS-API using Python .NET


If you are using the .NET connection, then the correct syntax is as follows:



TheSystem.UpdateMode = ZOSAPI.LensUpdateMode.AllWindows
TheSystem.UpdateStatus()

 


Regarding Option 2, you do not need to use a loop if you are only updating one window. For example, if you know that the layout view is located at index 1, then you can use this line to update it:



TheSystem.Analyses.Get_AnalysisAtIndex(1).ApplyAndWaitForCompletion()

 


Best,


Csilla


 

Hi Csilla,


Thanks!  The 'constants.LensuUpdateMode_AllWindows' approach worked!


Your reply raises a couple of related questions:


1. Is there a place where such information is listed, organized, and explained? Before posting here, I spent a long time searching for such keywords in the ZOS-API Syntax Help tool. And in fact, if you type 'LensUpdateMode_AllWindows' into the Search box (either Index or Topic), you get nothing.


2. I am still running OpticStudio 20.1. Your response ('in OpticStudio 20.2, we moved to a .NET connection') seems to imply that the old-fashioned COM link approach will no longer be supported. Is this true? I code my modest python programs in a standard text editor (and in fact on a Mac - I am a unix/Mac person and use the PC with Remote Desktop). Again, searching through the KnowledgeBase, it seems that I would have to download the many many gigabytes of Visual Studio and learn a whole new environment to do .NET programming (perhaps I am wrong about this...the KB article you referenced - 'ZOS-API using Python .NET' - makes no mention of Visual Studio). Also, I have depended heavily on the example code supplied with Zemax, and it seems to use COM exclusively. Have the examples been updated to .NET in version 20.2?


Thanks again and best wishes,


Tom


 

Userlevel 6
Badge +2

Hi Tom



  1. Yes the ZOS-API Syntax help is the right place where to search information. It is not super intuitive at first (at least for us optical engineers) because the help files are built using the concept of Object-Oriented Programming. Each class has functions and properties, that leads to other classes (hierarchy). We created a learning path on ZOS-API that I would recommend having a look: https://my.zemax.com/en-US/learning-paths/getting-started-with-zos-api/.

    The section called Fundamentals of structure and syntax will hopefully clear up things. I would recommend watching the videos that explain how to navigate through the help files.

  2. Yes in 20.2 we are fully supporting the .NET connection. The COM approach will still be supported as C++ is using the .COM connection and we keep supporting it. To use the .NET, you don't need to install Visual Studio. All you need to do is to download the Python.NET module. We have updated the Getting Started with Python article. Have a look at it.

    I would recommend starting using Python.NET for all your new projects. As you will see, there are a good number of advantages in doing so: ZOS-API using Python.NET


 


Sandrine


 

Reply