Solved

How to reload user defined bulk scattering dll in non-sequential mode


Hi,

I wrote a user defined bulk scattering dll (Non-sequential mode) that reads data from a txt file.

I create/edit the txt file with Python which I connect to ZEMAX via ZOS-API. What I need to do is as follows (all lines using ZOSAPI and python):

(1) Ray trace and Read detector data with Volume Physics bulk scattering set to None and save to a txt file (to be read by dll in step 3).

(2) Change some variables for various objects

(3) change bulk scattering to user defined dll

(4) Ray trace and Read detector data

(5) repeat from step 1 

I’m having two issues (a small one and a huge one):

  • the smaller issue - In step (1), how do I change Volume Physics bulk scattering to None? I tried the following:

    Obj = TheNCE.GetObjectAt(1)

    volphysdata = Obj.VolumePhysicsData

    volphysdata.Model = ZOSAPI.Editors.NCE.VolumePhysicsModelType.None


    However, Python doesn't allow this (as None is a protected variable). So, I found a different method which works , by changing to AngleScattering type and then set meanpath to zero (this acts exactly like the None option):

    volphysdata.Model = ZOSAPI.Editors.NCE.VolumePhysicsModelType.AngleScattering

    volphysdata.ModelSettings._S_AngleScattering.MeanPath = 0


    I can probably live with that but if anyone know how to change to None, that would be better.
     
  • The larger issue. In step 3 when loading the dll again (doing it manually at the moment),  and reading the data in step 4 I notice nothing changed, and detector data showed same numbers (output power)
    so I read about this in the following example: https://support.zemax.com/hc/en-us/articles/1500005576142-How-to-read-a-static-data-file-into-a-user-defined-surface
    and it seems that the dll is only loaded once when opticstudio is first opened. I tried it and indeed I see the changes after restarting Opticstudio. 
    Any ideas on how to reload the dll through Python and retrieve the new data in the txt file?

Thanks in advance!

Eyal

icon

Best answer by MichaelH 23 May 2022, 22:42

View original

4 replies

Userlevel 6
Badge +2

Hi Eyal,

To use the “None” enum in Python, you’ll need to import the Enum .NET namespace and use Enum.Parse:

import Enum

volphysdata.Model = Enum.Parse(ZOSAPI.Editors.NCE.VolumePhysicsModelType, “None”)

In order to speed up optimization, when a DLL is needed in OpticStudio, it is only read from disk/loaded once in memory (as you’ve discovered) and any subsequent calls to the DLL is from memory.  When you use the GUI to change from DLL Defined Scattering to Angle Scattering, you can see that the DLL dropdown is still present, just grayed out; so I'm wondering if the DLL is never really unloaded from memory and therefore when you switch back to DLL Defined Scattering, the DLL is not reloaded.

I would suggest a few things to try:

  • Use the Enum.Parse mentioned above to try to remove the DLL completely from memory
  • If the above doesn’t work, create a “dummy” DLL that doesn’t do anything.  In Step 3, load the dummy DLL, then load the real DLL
  • If neither of the two ideas above works, try to change the object to a Null Object and then change back to the Object Type you’re using for volume physics 

You should not have to restart OpticStudio to re-read the data; you should simply have to unload and reload the DLL.

Hi Michael,

Thanks for the answers. 

Actually for the first issue, I tried something simpler which worked:

volphysdata.Model = ZOSAPI.Editors.NCE.VolumePhysicsModelType(0)

For the 2nd issue, you suggested using Enum.Parse. I’m using enum.Enum which doesn't have a .Parse method. Any suggestion on how to remove the dll from memory (and than reload)?

I also tried the 2nd suggestion. It didn't work.

Trying the third is a bit more work so trying it now.

 

Userlevel 6
Badge +2

Hmm, using an enumeration as a method isn’t typically valid Python or .NET code and when I try ZOSAPI.Editors.NCE.VolumePhysicsModelType(0) I get TypeError: cannot instantiate enumeration.  So I’m not quite sure how this is changing the Volume Physics type:

 

The Enum.Parse is shown in Sample Code examples 22, 23, & 26 (the module is the .NET System.Enum, not the Python enum.Enum).

The other thing I could think of why the reading the data a second time is failing would be if you have a file lock for the TXT file inside the DLL; make sure to close the TXT file after you read the contents.  

No idea why ZOSAPI.Editors.NCE.VolumePhysicsModelType(0) doesn't work for you, I think I saw an example somewhere that uses it.

I rechecked, and I’m closing the file in the DLL.

So, last option didnt work also (at first I though it did, posted it and edited it later to this reply). 

By the way, I also checked what ReloadObject() is doing and it doesn't work (saw that in another topic). Somehow I think this should have been the best location for reloading DLLs.

Any other suggestions?

Reply