def import_attribute(attribute, props, data, callback=None): data_type = ifcopenshell.util.attribute.get_primitive_type(attribute) if isinstance(data_type, tuple) or data_type == "entity": callback(attribute.name(), None, data) if callback else None return new = props.add() new.name = attribute.name() new.is_null = data[attribute.name()] is None new.is_optional = attribute.optional() new.data_type = data_type if isinstance(data_type, str) else "" is_handled_by_callback = callback(attribute.name(), new, data) if callback else None if is_handled_by_callback: pass # Our job is done elif is_handled_by_callback is False: props.remove(len(props) - 1) elif data_type == "string": new.string_value = "" if new.is_null else data[attribute.name()] elif data_type == "boolean": new.bool_value = False if new.is_null else data[attribute.name()] elif data_type == "integer": new.int_value = 0 if new.is_null else data[attribute.name()] elif data_type == "float": new.float_value = 0.0 if new.is_null else data[attribute.name()] elif data_type == "enum": new.enum_items = json.dumps( ifcopenshell.util.attribute.get_enum_items(attribute)) if data[attribute.name()]: new.enum_value = data[attribute.name()]
def execute(self, context): props = context.scene.BIMDocumentProperties props.document_attributes.clear() if props.is_editing == "information": data = Data.information[self.document] ifc_class = "IfcDocumentInformation" elif props.is_editing == "reference": data = Data.references[self.document] ifc_class = "IfcDocumentReference" for attribute in IfcStore.get_schema().declaration_by_name( ifc_class).all_attributes(): data_type = ifcopenshell.util.attribute.get_primitive_type( attribute) if data_type == "entity": continue new = props.document_attributes.add() new.name = attribute.name() new.is_null = data[attribute.name()] is None new.is_optional = attribute.optional() new.data_type = data_type if data_type == "string": new.string_value = "" if new.is_null else data[ attribute.name()] elif data_type == "enum": new.enum_items = json.dumps( ifcopenshell.util.attribute.get_enum_items(attribute)) if data[attribute.name()]: new.enum_value = data[attribute.name()] props.active_document_id = self.document return {"FINISHED"}
def load(cls, file, product_id): if not file: return product = file.by_id(product_id) schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(file.schema) cls.products[product_id] = [] declaration = schema.declaration_by_name(product.is_a()) for attribute in declaration.all_attributes(): data_type = ifcopenshell.util.attribute.get_primitive_type( attribute) value = getattr(product, attribute.name()) list_type = None enum_items = () if isinstance(data_type, tuple): list_type = data_type[1] data_type = data_type[0] if data_type in ["entity", "list", "string", "enum"]: value = None if value is None else str(value) elif data_type == "float": value = None if value is None else float(value) elif data_type == "integer": value = None if value is None else int(value) if data_type == "enum": enum_items = ifcopenshell.util.attribute.get_enum_items( attribute) cls.products[product_id].append({ "name": attribute.name(), "value": value, "type": data_type, "enum_items": enum_items, "list_type": list_type, "is_optional": attribute.optional(), "is_null": getattr(product, attribute.name()) is None, })
def load_set_item_attributes(self, material_set_item, material_set_item_data): self.props.material_set_item_attributes.clear() for attribute in IfcStore.get_schema().declaration_by_name( material_set_item.is_a()).all_attributes(): data_type = ifcopenshell.util.attribute.get_primitive_type( attribute) if data_type == "entity": continue if attribute.name() in material_set_item_data: new = self.props.material_set_item_attributes.add() new.name = attribute.name() new.is_null = material_set_item_data[attribute.name()] is None new.data_type = data_type if data_type == "string": new.string_value = "" if new.is_null else material_set_item_data[ attribute.name()] elif data_type == "float": new.float_value = 0.0 if new.is_null else material_set_item_data[ attribute.name()] elif data_type == "integer": new.int_value = 0 if new.is_null else material_set_item_data[ attribute.name()] elif data_type == "boolean": new.bool_value = False if new.is_null else material_set_item_data[ attribute.name()]
def load_profile_attributes(self, material_set_item, material_set_item_data): self.props.material_set_item_profile_attributes.clear() if not material_set_item_data["Profile"]: return profile = self.file.by_id(material_set_item_data["Profile"]) profile_data = ProfileData.profiles[material_set_item_data["Profile"]] for attribute in IfcStore.get_schema().declaration_by_name( profile.is_a()).all_attributes(): data_type = ifcopenshell.util.attribute.get_primitive_type( attribute) if data_type == "entity": continue if attribute.name() in profile_data: new = self.props.material_set_item_profile_attributes.add() new.name = attribute.name() new.is_null = profile_data[attribute.name()] is None new.is_optional = attribute.optional() new.data_type = data_type if data_type == "string": new.string_value = "" if new.is_null else profile_data[ attribute.name()] elif data_type == "float": new.float_value = 0.0 if new.is_null else profile_data[ attribute.name()] elif data_type == "integer": new.int_value = 0 if new.is_null else profile_data[ attribute.name()] elif data_type == "boolean": new.bool_value = False if new.is_null else profile_data[ attribute.name()] elif data_type == "enum": new.enum_items = json.dumps( ifcopenshell.util.attribute.get_enum_items(attribute)) if profile_data[attribute.name()]: new.enum_value = profile_data[attribute.name()] # Force null to be false if the attribute is mandatory because when we first assign a profile, all of # its fields are null (which is illegal). # TODO: find a better solution. if not new.is_optional: new.is_null = False
def print_element(declaration, should_print_subtypes=True): if declaration.name() in ifcopenshell.util.fm.fmhem_excluded_classes: pass elif declaration.is_abstract(): pass else: types = [] for attribute in declaration.all_attributes(): if attribute.name() == "PredefinedType": types = list( ifcopenshell.util.attribute.get_enum_items(attribute)) if "NOTDEFINED" in types: types.remove("NOTDEFINED") print("{}".format(declaration.name())) if types: for line in textwrap.wrap(", ".join(types), width=70): print("\t\t{}".format(line)) if should_print_subtypes: for subtype in declaration.subtypes(): print_element(subtype)