Hey Everyone,
I think there are 2 separate topics in this thread.
Comparing Surfaces
For comparing surfaces, @Luc.van Vught is right about the difficulty of checking 2 surfaces because not only do you need to consider the actual cells in the LDE, you’d also need to check everything in the ILDERow
interface. This includes:
- TypeData
- DrawData
- ApertureData
- ScatteringData
- TiltDecenterData
- PhysicalOpticsData
- CoatingData
- ImportData
- CompositeData
Normally, you could write a few line recursive algorithm that would “crawl” through all the properties and populate a dictionary with all the values (similar to how you would list all the files in a folder & sub-folders). In C# (and therefore in Python with pythonnet), you can use System.Reflection
to get all the properties. However, when I quickly tried to mock something up, I realized that you get into an infinite loop real quick because LDE.GetSurfaceAt(n)
returns ILDERow but then when you get 1 level down with LDE.GetSurfaceAt(n).SurfaceData
, you also get a property that return ILDERow. So more logic would need to be applied to filter out existing entries to prevent an infinite loop. I have added my first draft of Python code in case this helps in your effort (note this is failing at Line 21)
import clr
import System.Reflection
def GetPropValue(src, propName):
return src.GetType().GetProperty(propName).GetValue(src, None)
def GetSurfaceData(obj, properties, output):
for property in properties:
value = GetPropValue(obj, property.Name)
if isinstance(value, (int, str, bool, float)):
output.update({property.Name: value})
else:
child_properties = list(value.GetType().GetProperties())
if len(child_properties) > 0:
GetSurfaceData(value, child_properties, output)
return output
obj = TheSystem.LDE.GetSurfaceAt(1)
properties = list(obj.GetType().GetProperties())
output = {}
GetSurfaceData(obj, properties, output)
Copying Surfaces
For copying surfaces, you can use the TheSystem.LDE.CopySurfacesFrom()
method. This actually allows you to do a deep copy of a surface, even from another file loaded into a second IOpticalSystem. The first parameter asks for the ILensDataEditor you want to copy from so if you have 2 IOpticalSystem loaded into the script, you can do something like the following to copy the surface:
TheSystem.LoadFile(r'myfile.zmx', False)
TheSystem2 = TheSystem.TheApplication.CreateNewSystem(ZOSAPI.SystemType.Sequential)
TheSystem2.LoadFile(r'myfile2.zmx', False)
TheSystem.LDE.CopySurfaces(TheSystem2.LDE, 1, 1, 2)
I believe this deep copy implements the same idea of using System.Reflection, but I think the ZOS-API code is a lot more robust than a quick 30 line algorithm.