Feature to Add Crew Resources and SubContract Resources with the ifcopenshell.api and blenderB

This commit is contained in:
bosonprojets
2021-04-23 06:38:11 +00:00
parent 18d7193efc
commit c7aa42bfd5
10 changed files with 400 additions and 1 deletions
+2 -1
View File
@@ -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,
@@ -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
@@ -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"}
@@ -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="[]")
@@ -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