1+ import sys
2+ import xml .etree .ElementTree as ET
3+
4+ input_file = sys .argv [1 ]
5+
6+ file = input_file .split ("/" )[- 1 ]
7+ outputname = input_file .split ("/" )[0 ] + "/" + file .split ("." )[0 ] + "_Coatings.rml"
8+
9+ print (f"Processing file: { input_file } " )
10+ print (f"Output file: { outputname } " )
11+
12+ tree = ET .parse (input_file )
13+ root = tree .getroot ()
14+
15+ for obj in root .findall (".//object" ):
16+ reflectivity = obj .find ("param[@id='reflectivityType']" )
17+ if reflectivity is None or "Derived" not in (reflectivity .get ("comment" ) or "" ):
18+ continue
19+
20+ # Alte Coating-Parameter sammeln
21+ coatings = []
22+ i = 1
23+ while True :
24+ mat = obj .find (f"param[@id='materialCoating{ i } ']" )
25+ thick = obj .find (f"param[@id='thicknessCoating{ i } ']" )
26+ rough = obj .find (f"param[@id='roughnessCoating{ i } ']" )
27+ if mat is None or thick is None or rough is None :
28+ break
29+ coatings .append ({
30+ "material" : mat .text ,
31+ "thickness" : thick .text ,
32+ "roughness" : rough .text
33+ })
34+ i += 1
35+
36+ # Toplayer prüfen und anhängen
37+ mat_top = obj .find ("param[@id='materialTopLayer']" )
38+ thick_top = obj .find ("param[@id='thicknessTopLayer']" )
39+ rough_top = obj .find ("param[@id='roughnessTopLayer']" )
40+
41+ if mat_top is not None and thick_top is not None :
42+ if rough_top is None :
43+ rough_top .text = "0.0"
44+ coatings .append ({
45+ "material" : mat_top .text ,
46+ "thickness" : thick_top .text ,
47+ "roughness" : rough_top .text
48+ })
49+
50+ if coatings :
51+ # Neue Coating-Struktur einfügen
52+ coating_param = ET .Element ("param" , {"id" : "Coating" , "enabled" : "T" })
53+ for idx , c in enumerate (coatings ):
54+ layer = ET .SubElement (coating_param , "layer" , {
55+ "material" : c ["material" ],
56+ "thickness" : c ["thickness" ],
57+ "roughness" : c ["roughness" ]
58+ })
59+ layer .text = f"layer{ idx } "
60+ obj .append (coating_param )
61+
62+ num_layers = str (len (coatings ))
63+
64+ surface_param = ET .Element ("param" , {
65+ "id" : "surfaceCoating" ,
66+ "comment" : "Multilayer Coating" ,
67+ "enabled" : "T"
68+ })
69+ surface_param .text = "2"
70+
71+ num_param = ET .Element ("param" , {
72+ "id" : "NumberOfLayer" ,
73+ "enabled" : "T"
74+ })
75+ num_param .text = num_layers
76+
77+ obj .append (surface_param )
78+ obj .append (num_param )
79+
80+ def indent (elem , level = 0 ):
81+ i = "\n " + level * " "
82+ if len (elem ):
83+ if not elem .text or not elem .text .strip ():
84+ elem .text = i + " "
85+ for e in elem :
86+ indent (e , level + 1 )
87+ if not e .tail or not e .tail .strip ():
88+ e .tail = i + " "
89+ if not elem .tail or not elem .tail .strip ():
90+ elem .tail = i
91+ else :
92+ if level and (not elem .tail or not elem .tail .strip ()):
93+ elem .tail = i
94+ indent (root )
95+ tree .write (outputname , encoding = "UTF-8" , xml_declaration = True , short_empty_elements = False )
0 commit comments