Solved

Change object (lens, detector) settings in Python and ZOS-API and saving it

  • 22 January 2024
  • 1 reply
  • 145 views

Hello,

I have tried to use sample code from Python Samples [e17s06_py] for changing settings of Detector Rectangle object loaded in predefined system. But when I saved the new system with a new name, settings were still the same as in the predefined system.

 

How to load predefined system (in file A.zos), change some objects parameters and save the result as a new system (B.zos)?

 

def load_system(zos, lensdir=lensdir, lensfile=lensfile):
    # load local variables
    ZOSAPI = zos.ZOSAPI
    TheApplication = zos.TheApplication
    TheSystem = zos.TheSystem
    
    # Open file
    lensname = os.path.join(os.sep, lensdir, lensfile)
    TheSystem.LoadFile(lensname, False)
    
    return ZOSAPI, TheApplication, TheSystem

 

zos = PythonStandaloneApplication()

ZOSAPI, TheApplication, TheSystem = load_system(zos, lensfile='A.zos')

TheNCE = TheSystem.NCE

 

d = TheNCE.GetObjectAt(18) #Detector Rectangle

o = d.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorRectangle)

d.ChangeType( o)

d.ObjectData.XHalfWidth = 5

d.ObjectData.YHalfWidth = 5

TheSystem.SaveAs('B.zos')

 

I also tried to remove that object, insert new one and define the new one with new values of parameters. ObjectData are not the same as I defined. In GUI I see values X Half Width = 1 and Y Half Width = 1, but in the code is: “d.ObjectData.XHalfWidth = 5”.

Thanks!

 

icon

Best answer by Stavros Sklavenitis 13 February 2024, 12:52

View original

1 reply

Hello Jan,

The problem you are facing is related to the updated PythonNET 3.x version which works slightly different than the previous versions. There are three possible ways to solve or bypass this issue.

The two of them can be found in the following forum post:

Also, more in-depth information on how the PythonNET 3.x version causes this issue can be found here:

According to these, the first way is to simply downgrade your PythonNET version to 2.5.2, but this is not recommended.

The second way is to replace this part of your script:

d.ObjectData.XHalfWidth = 5
d.ObjectData.YHalfWidth = 5

with:

d.get_ObjectData().__implementation__.XHalfWidth = 5
d.get_ObjectData().__implementation__.YHalfWidth = 5

or, more similarly to your original syntax, with:

d.ObjectData.__implementation__.XHalfWidth = 5
d.ObjectData.__implementation__.YHalfWidth = 5

However, this method is unintuitive, and decreases code readability.

The third way is to install and use the ZOSPy v1.2.0 open-source library, which, apart from serving other functionalities, is a definitive and the recommended solution for problems related to PythonNET 3.x. Extended information on how it tackles these issues can be found on this forum post:

In your case, the code after installing ZOSPy, should be functional in the following way, without needing the default “PythonStandaloneApplication” script:

import os
import zospy as zp

zos = zp.ZOS()
oss = zos.connect("standalone")
ZOSAPI = zos.ZOSAPI

lensname = os.path.join(os.sep, lensdir, lensfile)
oss.load(lensName, False)

TheNCE = oss.NCE

d = TheNCE.GetObjectAt(2) # Detector Rectangle

o = d.GetObjectTypeSettings(ZOSAPI.Editors.NCE.ObjectType.DetectorRectangle)

d.ChangeType(o)

d.ObjectData.XHalfWidth = 5

d.ObjectData.YHalfWidth = 5

oss.save_as('B.zos')

del zos
zos = None

I recommend you devote a few minutes on the the ZOSPy documentation; I hope you will find it a useful and intuitive tool.

Kind regards,

Stavros Sklavenitis
Application Engineer

Reply