diff --git a/src/blenderbim/blenderbim/bim/__init__.py b/src/blenderbim/blenderbim/bim/__init__.py index b7d1db49ac..65618371a3 100644 --- a/src/blenderbim/blenderbim/bim/__init__.py +++ b/src/blenderbim/blenderbim/bim/__init__.py @@ -24,8 +24,9 @@ if bpy is not None: "aggregate": None, "geometry": None, "cobie": None, - "sequence": None, + "resource": None, "cost": None, + "sequence": None, "group": None, "structural": None, "material": None, diff --git a/src/blenderbim/blenderbim/bim/module/resource/__init__.py b/src/blenderbim/blenderbim/bim/module/resource/__init__.py new file mode 100644 index 0000000000..86dee57ace --- /dev/null +++ b/src/blenderbim/blenderbim/bim/module/resource/__init__.py @@ -0,0 +1,24 @@ +import bpy +from . import ui, prop, operator + +classes = ( + operator.DisableResourceEditingUI, + operator.DisableEditingResource, + operator.EnableEditingResource, + operator.LoadResources, + operator.AddCrewResource, + operator.AddSubcontractResource, + operator.EditResource, + operator.RemoveResource, + prop.Resource, + prop.BIMResourceProperties, + ui.BIM_PT_resources, + ui.BIM_UL_resources, +) + + +def register(): + bpy.types.Scene.BIMResourceProperties = bpy.props.PointerProperty(type=prop.BIMResourceProperties) + +def unregister(): + del bpy.types.Scene.BIMResourceProperties diff --git a/src/blenderbim/blenderbim/bim/module/resource/operator.py b/src/blenderbim/blenderbim/bim/module/resource/operator.py new file mode 100644 index 0000000000..72fc3971b6 --- /dev/null +++ b/src/blenderbim/blenderbim/bim/module/resource/operator.py @@ -0,0 +1,128 @@ +import bpy +import json +import ifcopenshell.api +from blenderbim.bim.ifc import IfcStore +from ifcopenshell.api.resource.data import Data + + +class LoadResources(bpy.types.Operator): + bl_idname = "bim.load_resources" + bl_label = "Load Work Plans" + + def execute(self, context): + props = context.scene.BIMResourceProperties + while len(props.resources) > 0: + props.resources.remove(0) + for ifc_definition_id, resource in Data.resources.items(): + new = props.resources.add() + new.ifc_definition_id = ifc_definition_id + new.name = resource["Name"] or "Unnamed" + props.is_editing = True + bpy.ops.bim.disable_editing_resource() + return {"FINISHED"} + +class EnableEditingResource(bpy.types.Operator): + bl_idname = "bim.enable_editing_resource" + bl_label = "Enable Editing Resource" + resource: bpy.props.IntProperty() + + def execute(self, context): + props = context.scene.BIMResourceProperties + while len(props.resource_attributes) > 0: + props.resource_attributes.remove(0) + + data = Data.resources[self.resource] + for attribute in IfcStore.get_schema().declaration_by_name("IfcConstructionResource").all_attributes(): + data_type = ifcopenshell.util.attribute.get_primitive_type(attribute) + if data_type == "entity": + continue + new = props.resource_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_resource_id = self.resource + return {"FINISHED"} + +class DisableEditingResource(bpy.types.Operator): + bl_idname = "bim.disable_editing_resource" + bl_label = "Disable Editing Workplan" + + def execute(self, context): + context.scene.BIMWorkPlanProperties.active_resource_id = 0 + return {"FINISHED"} + +class DisableResourceEditingUI(bpy.types.Operator): + bl_idname = "bim.disable_resource_editing_ui" + bl_label = "Disable Resources Editing UI" + + def execute(self, context): + context.scene.BIMResourceProperties.is_editing = False + return {"FINISHED"} + + +class AddSubcontractResource(bpy.types.Operator): + bl_idname = "bim.add_subcontract_resource" + bl_label = "Add Subcontract Resource" + + def execute(self, context): + ifcopenshell.api.run("resource.add_subcontract_resource", IfcStore.get_file()) + Data.load(IfcStore.get_file()) + return {"FINISHED"} + + +class AddCrewResource(bpy.types.Operator): + bl_idname = "bim.add_crew_resource" + bl_label = "Add Crew Resource" + + def execute(self, context): + ifcopenshell.api.run("resource.add_crew_resource", IfcStore.get_file()) + Data.load(IfcStore.get_file()) + return {"FINISHED"} + + +class EditResource(bpy.types.Operator): + bl_idname = "bim.edit_resource" + bl_label = "Edit Resource" + + def execute(self, context): + props = context.scene.BIMResourceProperties + attributes = {} + for attribute in props.cost_resource_attributes: + if attribute.is_null: + attributes[attribute.name] = None + else: + if attribute.data_type == "string": + attributes[attribute.name] = attribute.string_value + elif attribute.data_type == "enum": + attributes[attribute.name] = attribute.enum_value + self.file = IfcStore.get_file() + ifcopenshell.api.run( + "resource.edit_resource", + self.file, + **{"resource": self.file.by_id(props.active_resource_id), "attributes": attributes}, + ) + Data.load(IfcStore.get_file()) + bpy.ops.bim.disable_editing_resource() + return {"FINISHED"} + + +class RemoveResource(bpy.types.Operator): + bl_idname = "bim.remove_resource" + bl_label = "Remove Resource" + resource: bpy.props.IntProperty() + + def execute(self, context): + ifcopenshell.api.run( + "resource.remove_resource", + IfcStore.get_file(), + resource=IfcStore.get_file().by_id(self.resource), + ) + Data.load(IfcStore.get_file()) + return {"FINISHED"} diff --git a/src/blenderbim/blenderbim/bim/module/resource/prop.py b/src/blenderbim/blenderbim/bim/module/resource/prop.py new file mode 100644 index 0000000000..2f94fe2939 --- /dev/null +++ b/src/blenderbim/blenderbim/bim/module/resource/prop.py @@ -0,0 +1,37 @@ +import bpy +import ifcopenshell.api +from blenderbim.bim.ifc import IfcStore +from ifcopenshell.api.resource.data import Data +from blenderbim.bim.prop import StrProperty, Attribute +from bpy.types import PropertyGroup +from bpy.props import ( + PointerProperty, + StringProperty, + EnumProperty, + BoolProperty, + IntProperty, + FloatProperty, + FloatVectorProperty, + CollectionProperty, +) + + +class Resource(PropertyGroup): + name: StringProperty(name="Name") + ifc_definition_id: IntProperty(name="IFC Definition ID") + has_children: BoolProperty(name="Has Children") + is_expanded: BoolProperty(name="Is Expanded") + level_index: IntProperty(name="Level Index") + + +class BIMResourceProperties(PropertyGroup): + resource_attributes: CollectionProperty(name="Resource Attributes", type=Attribute) + is_editing: BoolProperty(name="Is Editing") + is_a:StringProperty(name="Contracted Cost Items", default="[]") + active_resource_id: IntProperty(name="Active Resource Id") + active_resource_index: IntProperty(name="Active Resource Id") + resources: CollectionProperty(name="Resource", type=Resource) + active_nested_resource_id: IntProperty(name="Active Nested Ressource Id") + active_nested_resource_index: IntProperty(name="Active Nested Resource Index") + nested_resource_attributes: CollectionProperty(name="Nested Resource Attributes", type=Attribute) + contracted_nested_resources: StringProperty(name="Contracted Cost Items", default="[]") diff --git a/src/blenderbim/blenderbim/bim/module/resource/ui.py b/src/blenderbim/blenderbim/bim/module/resource/ui.py new file mode 100644 index 0000000000..81aa6d445a --- /dev/null +++ b/src/blenderbim/blenderbim/bim/module/resource/ui.py @@ -0,0 +1,73 @@ +from bpy.types import Panel, UIList +from blenderbim.bim.ifc import IfcStore +from ifcopenshell.api.resource.data import Data + +class BIM_PT_resources(Panel): + bl_label = "IFC Resources" + bl_idname = "BIM_PT_resources" + bl_options = {"DEFAULT_CLOSED"} + bl_space_type = "PROPERTIES" + bl_region_type = "WINDOW" + bl_context = "scene" + + @classmethod + def poll(cls, context): + return IfcStore.get_file() + + def draw(self, context): + if not Data.is_loaded: + Data.load(IfcStore.get_file()) + self.props = context.scene.BIMResourceProperties + + row = self.layout.row(align=True) + if self.props.is_editing: + row.operator("bim.add_subcontract_resource",emboss=False, text="", icon="FILE_TICK") + row.operator("bim.add_crew_resource",emboss=False, text="", icon="COMMUNITY") + row.operator("bim.disable_resource_editing_ui", text="", icon="CANCEL") + else: + row.operator("bim.load_resources", text="", icon="GREASEPENCIL") + + if self.props.is_editing: + self.layout.template_list( + "BIM_UL_resources", + "", + self.props, + "resources", + self.props, + "active_resource_index", + ) + + if self.props.active_resource_id: + self.draw_editable_ui(context) + + def draw_editable_ui(self, context): + for attribute in self.props.resource_attributes: + row = self.layout.row(align=True) + if attribute.data_type == "string": + row.prop(attribute, "string_value", text=attribute.name) + elif attribute.data_type == "boolean": + row.prop(attribute, "bool_value", text=attribute.name) + elif attribute.data_type == "integer": + row.prop(attribute, "int_value", text=attribute.name) + elif attribute.data_type == "float": + row.prop(attribute, "float_value", text=attribute.name) + elif attribute.data_type == "enum": + row.prop(attribute, "enum_value", text=attribute.name) + if attribute.is_optional: + row.prop(attribute, "is_null", icon="RADIOBUT_OFF" if attribute.is_null else "RADIOBUT_ON", text="") + + +class BIM_UL_resources(UIList): + def draw_item(self, context, layout, data, item, icon, active_data, active_propname): + if item: + row = layout.row(align=True) + row.label(text=item.name) + if context.scene.BIMResourceProperties.active_resource_id == item.ifc_definition_id: + row.operator("bim.edit_resource", text="", icon="CHECKMARK") + row.operator("bim.disable_editing_resource", text="", icon="X") + elif context.scene.BIMResourceProperties.active_resource_id: + row.operator("bim.remove_resource", text="", icon="X").resource = item.ifc_definition_id + else: + op = row.operator("bim.enable_editing_resource", text="", icon="GREASEPENCIL") + op.resource = item.ifc_definition_id + row.operator("bim.remove_resource", text="", icon="X").resource = item.ifc_definition_id diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/add_crew_resource.py b/src/ifcopenshell-python/ifcopenshell/api/resource/add_crew_resource.py new file mode 100644 index 0000000000..1937dfe26a --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/add_crew_resource.py @@ -0,0 +1,30 @@ +import ifcopenshell.api +import ifcopenshell.util.date +from datetime import datetime + + +class Usecase: + def __init__(self, file, **settings): + self.file = file + self.settings = { + "name": "Unnamed", + "predefined_type": "NOTDEFINED", + } + for key, value in settings.items(): + self.settings[key] = value + + def execute(self): + crew_resource = ifcopenshell.api.run( + "root.create_entity", + self.file, + ifc_class="IfcCrewResource", + predefined_type=self.settings["predefined_type"], + name=self.settings["name"], + ) + # TODO: this is an ambiguity by buildingSMART: Can we nest and IfcCrewResource under an ifcCrewResource ? + # See https://forums.buildingsmart.org/t/is-the-ifcCrewResource-project-declaration-mutually-exclusive-to-aggregation-within-a-relating-ifcworkplan/3510 + context = self.file.by_type("IfcContext")[0] + ifcopenshell.api.run( + "project.assign_declaration", self.file, definition=crew_resource, relating_context=context + ) + return crew_resource diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/add_subcontract_resource.py b/src/ifcopenshell-python/ifcopenshell/api/resource/add_subcontract_resource.py new file mode 100644 index 0000000000..87516a4219 --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/add_subcontract_resource.py @@ -0,0 +1,30 @@ +import ifcopenshell.api +import ifcopenshell.util.date +from datetime import datetime + + +class Usecase: + def __init__(self, file, **settings): + self.file = file + self.settings = { + "name": "Unnamed", + "predefined_type": "NOTDEFINED", + } + for key, value in settings.items(): + self.settings[key] = value + + def execute(self): + subcontract_resource = ifcopenshell.api.run( + "root.create_entity", + self.file, + ifc_class="IfcSubContractResource", + predefined_type=self.settings["predefined_type"], + name=self.settings["name"], + ) + # TODO: this is an ambiguity by buildingSMART: Can we nest and IfcCrewResource under an ifcCrewResource ? + # See https://forums.buildingsmart.org/t/is-the-ifcCrewResource-project-declaration-mutually-exclusive-to-aggregation-within-a-relating-ifcworkplan/3510 + context = self.file.by_type("IfcContext")[0] + ifcopenshell.api.run( + "project.assign_declaration", self.file, definition=subcontract_resource, relating_context=context + ) + return subcontract_resource diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py b/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py new file mode 100644 index 0000000000..f8eef97224 --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/assign_resource.py @@ -0,0 +1,43 @@ +import ifcopenshell +import ifcopenshell.api + + +class Usecase: + def __init__(self, file, **settings): + self.file = file + self.settings = { + "relating_resource": None, + "related_object": None, + } + for key, value in settings.items(): + self.settings[key] = value + + def execute(self): + if self.settings["related_object"].HasAssignments: + for assignment in self.settings["related_object"].HasAssignments: + if ( + assignment.is_a("IfclRelAssignsToResource") + and assignment.RelatingResource == self.settings["relating_resource"] + ): + return + + referenced_by = None + if self.settings["relating_resource"].ResourceOf: + referenced_by = self.settings["relating_resource"].ResourceOf[0] + + if referenced_by: + related_objects = list(referenced_by.RelatedObjects) + related_objects.append(self.settings["related_object"]) + referenced_by.RelatedObjects = related_objects + ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": referenced_by}) + else: + referenced_by = self.file.create_entity( + "IfcRelAssignsToResource", + **{ + "GlobalId": ifcopenshell.guid.new(), + "OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file), + "RelatedObjects": [self.settings["related_object"]], + "RelatingProduct": self.settings["relating_resource"], + } + ) + return referenced_by diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/data.py b/src/ifcopenshell-python/ifcopenshell/api/resource/data.py new file mode 100644 index 0000000000..5fdc7c5a68 --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/data.py @@ -0,0 +1,23 @@ +import ifcopenshell.api + + +class Data: + is_loaded = False + resources = {} + + @classmethod + def purge(cls): + cls.resources = {} + + @classmethod + def load(cls, file): + cls.resources = {} + for resource in file.by_type("IfcResource"): + data = resource.get_info() + del data["OwnerHistory"] + data["RelatedObjects"] = [] + for rel in resource.IsNestedBy: + [data["RelatedObjects"].append(o.id()) for o in rel.RelatedObjects if o.is_a("IfcCrewResource") or o.is_a("IfcsubcontractResource")] + cls.resources[resource.id()] = data + + cls.is_loaded=True diff --git a/src/ifcopenshell-python/ifcopenshell/api/resource/remove_resource.py b/src/ifcopenshell-python/ifcopenshell/api/resource/remove_resource.py new file mode 100644 index 0000000000..50fa0376ce --- /dev/null +++ b/src/ifcopenshell-python/ifcopenshell/api/resource/remove_resource.py @@ -0,0 +1,10 @@ +class Usecase: + def __init__(self, file, **settings): + self.file = file + self.settings = {"resource": None} + for key, value in settings.items(): + self.settings[key] = value + + def execute(self): + # TODO: do a deep purge + self.file.remove(self.settings["resource"])