Solved

How do I reload all surfaces by using Matlab API?

  • 24 January 2023
  • 4 replies
  • 97 views

I notice these are “Reload Surface” and “Reload All Surfaces” tools on the top of the LDE window. Does anyone know how do we access this tool in Matlab API? I did not find such a function in ZOS-API. One similar function is  “Reload Object” but it doesn’t work for “Reload Surface”.

The reason why I need this function is I am simulating gradient index lenses whose property is read from an external file named “GRADIENT_9.DAT”. After I change the material property in this external “.DAT” file, I need to Reload All Surfaces to update the material.

If anyone has suggestions on how to do this, I would appreciate it a lot.

icon

Best answer by David.Nguyen 27 January 2023, 10:54

View original

4 replies

Userlevel 7
Badge +2

Hi Dekang,

 

That’s a tricky question. I didn’t find the possibility to Reload Surface with the ZOS-API.

However, I may have found a workaround if you are running a Standalone Application. The idea is to close TheApplication, modify the file, and then re-open a new application from within the same script. Closing TheApplication happens automatically after those default lines of code (I think):

# This will clean up the connection to OpticStudio.
# Note that it closes down the server instance of OpticStudio, so you for maximum performance do not do
# this until you need to.
del zos
zos = None

I made a dummy example in Python (should be similar to MATLAB):

if __name__ == '__main__':
# Modify GRADIENT_9.DAT
with open(r'C:\Users\David Nguyen\Documents\Zemax\Glasscat\GRADIENT_9.DAT', 'r') as file:
data = file.readlines()

data[0] = 'DUMMY\n'

with open(r'C:\Users\David Nguyen\Documents\Zemax\Glasscat\GRADIENT_9.DAT', 'w') as file:
file.writelines(data)

# Start first application
zos = PythonStandaloneApplication1()

# load local variables
ZOSAPI = zos.ZOSAPI
TheApplication = zos.TheApplication
TheSystem = zos.TheSystem

# Gradient 9 type setting
gradient_9 = TheSystem.LDE.GetSurfaceAt(1).GetSurfaceTypeSettings(ZOSAPI.Editors.LDE.SurfaceType.Gradient9)

# Change Surface 2 to Gradient 9
TheSystem.LDE.GetSurfaceAt(1).ChangeType(gradient_9)

# Save this file
test_file_path = r'C:\Users\David Nguyen\Desktop\test_0.zos'
TheSystem.SaveAs(test_file_path)

# This will clean up the connection to OpticStudio.
# Note that it closes down the server instance of OpticStudio, so you for maximum performance do not do
# this until you need to.
del zos
zos = None

# Modify GRADIENT_9.DAT
with open(r'C:\Users\David Nguyen\Documents\Zemax\Glasscat\GRADIENT_9.DAT', 'r') as file:
data = file.readlines()

data[0] = 'MODIFIED_DUMMY\n'

with open(r'C:\Users\David Nguyen\Documents\Zemax\Glasscat\GRADIENT_9.DAT', 'w') as file:
file.writelines(data)

# Open a new application
zos = PythonStandaloneApplication1()

# load local variables
ZOSAPI = zos.ZOSAPI
TheApplication = zos.TheApplication
TheSystem = zos.TheSystem

# Change Surface 2 to Gradient 9
TheSystem.LDE.GetSurfaceAt(1).ChangeType(gradient_9)

# Save this other file
test_file_path = r'C:\Users\David Nguyen\Desktop\test_1.zos'
TheSystem.SaveAs(test_file_path)

# This will clean up the connection to OpticStudio.
# Note that it closes down the server instance of OpticStudio, so you for maximum performance do not do
# this until you need to.
del zos
zos = None

I only modified this part of the template. On my desktop, it creates two files: test_0, and test_1. Test_0 has a Material called “DUMMY” while test_1 has “MODIFIED_DUMMY”.

I hope this can help you a little bit.

Take care,


David

Hi David,

Thanks very much for your detailed reply. It is very helpful! I would like to say it is indeed unfortunate there is no “Reload Surface” command in the ZOS-API. I notice that all the other tools in the LDE window has been equipped with a API command in the Tools section just except “Reload Surface”. I don’t know why this one is missing. Your idea is a really good workaround, and I am going to try it on my side. However, I would imagine it is challenging for my application. This is because I want to do a Monte-Carlo simulation by including the variation of GRIN lenses index, which needs ~ 10000 runs of simulation in a Monte-Carlo cycle. Therefore, re-opening file each time may result in a very long time.

I am wondering do you have any idea of how to improve the performance of re-opening file in standalone mode? such as parallel calculation? Any suggestions will be much appreciated.

Best regards,

Dekang

Userlevel 7
Badge +2

Hi Dekang,

 

No worries. To be clear, I don’t know if there really is no “Reload Surface”, I just couldn’t find it. I see the problem with your application indeed. Another solution would be to program your own Gradient 9 user-defined surface. It’ll still be slower than the built-in one, but probably faster than opening and closing the application everytime. For that, I suggest reading about this article, and start with the us_grin1.c example you will find in your {Documents}\Zemax\DLL\Surfaces folder.

Take care,

 

David

Hi David,

Your suggestion sounds great. Thanks very much for all your help!

Best regards,

Dekang

Reply