Skip to main content
Solved

Exchange source file .dat file

  • August 13, 2025
  • 1 reply
  • 56 views

dsotm

I need to exchange the Data File Name of a Source File Object:

 

 

 

I tried to do that by python code:

1import zospy
2
3zos = zospy.ZOS()
4
5oss = zos.connect()
6
7oss.load(‘zmx_file.zmx’)
8
9led = oss.NCE.GetObjectAt(1)
10
11TypeSet_LED = led.GetObjectTypeSettings(zp.constants.Editors.NCE.ObjectType.SourceFile)
12TypeSet_LED.FileName1 = 'filename.dat'

I wrote that code according to section #! [e21s03_py] in example 21 of the ZOS-API Python examples

Apparently, the code does not work.

Did anyone ever manage to exchange source files via python?

Can you provide a code snippet that works?

 

Thanks in advance

Best answer by chaasjes

Hi ​@dsotm,

Thank you for asking this question and nice to see you’re using ZOSPy!
I checked the example code and this works for me:

1import zospy as zp
2
3zos = zp.ZOS()
4oss = zos.connect("extension")
5
6oss.new()
7oss.make_nonsequential()
8
9# Add new catalog MISC
10oss.SystemData.MaterialCatalogs.AddCatalog('MISC')
11# Set Wave #1 to 0.47 micron
12oss.SystemData.Wavelengths.GetWavelength(1).Wavelength = 0.47
13# Use lumens as the source unit
14oss.SystemData.Units.SourceUnits = zp.constants.SystemData.ZemaxSourceUnits.Lumens # ZOSAPI.SystemData.ZemaxSourceUnits.Lumens
15#! [e21s01_py]
16
17#! [e21s02_py]
18# Add 4 more objects
19oss.NCE.AddObject()
20oss.NCE.AddObject()
21oss.NCE.AddObject()
22oss.NCE.AddObject()
23#! [e21s02_py]
24
25#! [e21s03_py]
26# Set 1st object as a Source File
27Object_1 = oss.NCE.GetObjectAt(1)
28Typeset_SourceFile = Object_1.GetObjectTypeSettings(zp.constants.Editors.NCE.ObjectType.SourceFile)
29Typeset_SourceFile.FileName1 = 'RAYFILE_LB_T67C_100K_190608_ZEMAX.DAT'
30Object_1.ChangeType(Typeset_SourceFile)
31Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par1).IntegerValue = 5
32Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par2).IntegerValue = 1000
33Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par3).DoubleValue = 2.485572
34Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par8).DoubleValue = 0.47
35Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par9).DoubleValue = 0.47
36#! [e21s03_py]

Your code seems to be missing this line:

1led.ChangeType(Typeset_LED)

And the specified source file must be located in Zemax/Objects/Sources/Source Files.

Does that help?

View original
Did this topic help you find an answer to your question?

1 reply

chaasjes
Forum|alt.badge.img
  • Visible
  • 38 replies
  • Answer
  • August 13, 2025

Hi ​@dsotm,

Thank you for asking this question and nice to see you’re using ZOSPy!
I checked the example code and this works for me:

1import zospy as zp
2
3zos = zp.ZOS()
4oss = zos.connect("extension")
5
6oss.new()
7oss.make_nonsequential()
8
9# Add new catalog MISC
10oss.SystemData.MaterialCatalogs.AddCatalog('MISC')
11# Set Wave #1 to 0.47 micron
12oss.SystemData.Wavelengths.GetWavelength(1).Wavelength = 0.47
13# Use lumens as the source unit
14oss.SystemData.Units.SourceUnits = zp.constants.SystemData.ZemaxSourceUnits.Lumens # ZOSAPI.SystemData.ZemaxSourceUnits.Lumens
15#! [e21s01_py]
16
17#! [e21s02_py]
18# Add 4 more objects
19oss.NCE.AddObject()
20oss.NCE.AddObject()
21oss.NCE.AddObject()
22oss.NCE.AddObject()
23#! [e21s02_py]
24
25#! [e21s03_py]
26# Set 1st object as a Source File
27Object_1 = oss.NCE.GetObjectAt(1)
28Typeset_SourceFile = Object_1.GetObjectTypeSettings(zp.constants.Editors.NCE.ObjectType.SourceFile)
29Typeset_SourceFile.FileName1 = 'RAYFILE_LB_T67C_100K_190608_ZEMAX.DAT'
30Object_1.ChangeType(Typeset_SourceFile)
31Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par1).IntegerValue = 5
32Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par2).IntegerValue = 1000
33Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par3).DoubleValue = 2.485572
34Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par8).DoubleValue = 0.47
35Object_1.GetObjectCell(zp.constants.Editors.NCE.ObjectColumn.Par9).DoubleValue = 0.47
36#! [e21s03_py]

Your code seems to be missing this line:

1led.ChangeType(Typeset_LED)

And the specified source file must be located in Zemax/Objects/Sources/Source Files.

Does that help?


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings