88
99cwd = os .path .dirname (os .path .realpath (__file__ ))
1010
11+
12+ def is_a (entity , ifc_class ):
13+ ifc_class = ifc_class .lower ()
14+ if entity .name_lc () == ifc_class :
15+ return True
16+ if entity .supertype ():
17+ return is_a (entity .supertype (), ifc_class )
18+ return False
19+
20+
1121class Migrator :
1222 def __init__ (self ):
1323 self .migrated_ids = {}
@@ -70,13 +80,13 @@ def migrate(self, element, new_file):
7080 return new_file .by_id (self .migrated_ids [element .id ()])
7181 except :
7282 pass
73- #print("Migrating", element)
83+ # print("Migrating", element)
7484 schema = ifcopenshell .ifcopenshell_wrapper .schema_by_name (new_file .schema )
7585 new_element = self .migrate_class (element , new_file )
76- #print("Migrated class from {} to {}".format(element, new_element))
86+ # print("Migrated class from {} to {}".format(element, new_element))
7787 new_element_schema = schema .declaration_by_name (new_element .is_a ())
7888 if not hasattr (new_element_schema , "all_attributes" ):
79- return element # The element has no attributes, so migration is done
89+ return element # The element has no attributes, so migration is done
8090 new_element = self .migrate_attributes (element , new_file , new_element , new_element_schema )
8191 self .migrated_ids [element .id ()] = new_element .id ()
8292 return new_element
@@ -102,39 +112,47 @@ def migrate_attributes(self, element, new_file, new_element, new_element_schema)
102112 return new_element
103113
104114 def migrate_attribute (self , attribute , element , new_file , new_element , new_element_schema ):
105- #print("Migrating attribute", element, new_element, attribute.name())
115+ # print("Migrating attribute", element, new_element, attribute.name())
106116 if hasattr (element , attribute .name ()):
107117 value = getattr (element , attribute .name ())
108- #print("Attribute names matched", value)
118+ # print("Attribute names matched", value)
109119 elif new_file .schema == "IFC2X3" :
110120 # IFC4 to IFC2X3: We know the IFC2X3 attribute name, but not its IFC4 equivalent
111- #print("Searching for an equivalent", new_element, attribute.name())
121+ # print("Searching for an equivalent", new_element, attribute.name())
112122 try :
113123 equivalent_map = self .attribute_4_to_2x3 [new_element .is_a ()]
114124 equivalent = list (equivalent_map .keys ())[list (equivalent_map .values ()).index (attribute .name ())]
115125 if hasattr (element , equivalent ):
116- #print("Equivalent found", equivalent)
126+ # print("Equivalent found", equivalent)
117127 value = getattr (element , equivalent )
118128 else :
119129 return
120130 except :
121- print ("Unable to find equivalent attribute of {} to migrate from {} to {}" .format (attribute .name (), element , new_element ))
122- return # We tried our best
131+ print (
132+ "Unable to find equivalent attribute of {} to migrate from {} to {}" .format (
133+ attribute .name (), element , new_element
134+ )
135+ )
136+ return # We tried our best
123137 elif new_file .schema == "IFC4" :
124138 # IFC2X3 to IFC4: We know the IFC4 attribute name, but not its IFC2X3 equivalent
125- #print("Searching for an equivalent", element, new_element, attribute.name())
139+ # print("Searching for an equivalent", element, new_element, attribute.name())
126140 try :
127141 equivalent = self .attribute_4_to_2x3 [new_element .is_a ()][attribute .name ()]
128- #print("Searching for equivalent", equivalent)
142+ # print("Searching for equivalent", equivalent)
129143 if hasattr (element , equivalent ):
130144 value = getattr (element , equivalent )
131145 else :
132146 return
133147 except :
134- print ("Unable to find equivalent attribute of {} to migrate from {} to {}" .format (attribute .name (), element , new_element ))
135- return # We tried our best
148+ print (
149+ "Unable to find equivalent attribute of {} to migrate from {} to {}" .format (
150+ attribute .name (), element , new_element
151+ )
152+ )
153+ return # We tried our best
136154
137- #print("Continuing migration of {} to migrate from {} to {}".format(attribute.name(), element, new_element))
155+ # print("Continuing migration of {} to migrate from {} to {}".format(attribute.name(), element, new_element))
138156 if value is None and not attribute .optional ():
139157 value = self .generate_default_value (attribute , new_file )
140158 if value is None :
@@ -155,24 +173,33 @@ def generate_default_value(self, attribute, new_file):
155173 elif self .default_entities [attribute .name ()]:
156174 return self .default_entities [attribute .name ()]
157175 elif attribute .name () == "OwnerHistory" :
158- self .default_entities [attribute .name ()] = new_file .create_entity ("IfcOwnerHistory" , ** {
159- "OwningUser" : new_file .create_entity ("IfcPersonAndOrganization" , ** {
160- "ThePerson" : new_file .create_entity ("IfcPerson" ),
161- "TheOrganization" : new_file .create_entity ("IfcOrganization" , ** {
162- "Name" : "IfcOpenShell Migrator"
163- })
164- }),
165- "OwningApplication" : new_file .create_entity ("IfcApplication" , ** {
166- "ApplicationDeveloper" : new_file .create_entity ("IfcOrganization" , ** {
167- "Name" : "IfcOpenShell Migrator"
168- }),
169- "Version" : "Works for me" ,
170- "ApplicationFullName" : "IfcOpenShell Migrator" ,
171- "ApplicationIdentifier" : "IfcOpenShell Migrator" ,
172- }),
173- "ChangeAction" : "NOCHANGE" ,
174- "CreationDate" : int (time .time ())
175- })
176+ self .default_entities [attribute .name ()] = new_file .create_entity (
177+ "IfcOwnerHistory" ,
178+ ** {
179+ "OwningUser" : new_file .create_entity (
180+ "IfcPersonAndOrganization" ,
181+ ** {
182+ "ThePerson" : new_file .create_entity ("IfcPerson" ),
183+ "TheOrganization" : new_file .create_entity (
184+ "IfcOrganization" , ** {"Name" : "IfcOpenShell Migrator" }
185+ ),
186+ }
187+ ),
188+ "OwningApplication" : new_file .create_entity (
189+ "IfcApplication" ,
190+ ** {
191+ "ApplicationDeveloper" : new_file .create_entity (
192+ "IfcOrganization" , ** {"Name" : "IfcOpenShell Migrator" }
193+ ),
194+ "Version" : "Works for me" ,
195+ "ApplicationFullName" : "IfcOpenShell Migrator" ,
196+ "ApplicationIdentifier" : "IfcOpenShell Migrator" ,
197+ }
198+ ),
199+ "ChangeAction" : "NOCHANGE" ,
200+ "CreationDate" : int (time .time ()),
201+ }
202+ )
176203 return self .default_entities [attribute .name ()]
177204
178205
0 commit comments