WIP implement presentation layers. Nicer UI. See #1222.

This commit is contained in:
Dion Moult
2021-01-25 18:10:18 +11:00
parent 911ff4484f
commit 20ab036016
17 changed files with 403 additions and 332 deletions
@@ -16,13 +16,17 @@ class LoadInformation(bpy.types.Operator):
bl_label = "Load Information"
def execute(self, context):
self.file = IfcStore.get_file()
props = context.scene.BIMDocumentProperties
while len(props.documents) > 0:
props.documents.remove(0)
for information_id, information in Data.information.items():
new = props.documents.add()
new.name = information["Name"] or "Unnamed"
new.identification = information["Identification"] or "*"
if self.file.schema == "IFC2X3":
new.identification = information["DocumentId"] or "*"
else:
new.identification = information["Identification"] or "*"
new.ifc_definition_id = information_id
props.is_editing = "information"
bpy.ops.bim.disable_editing_document()
@@ -34,13 +38,17 @@ class LoadDocumentReferences(bpy.types.Operator):
bl_label = "Load Document References"
def execute(self, context):
self.file = IfcStore.get_file()
props = context.scene.BIMDocumentProperties
while len(props.documents) > 0:
props.documents.remove(0)
for reference_id, reference in Data.references.items():
new = props.documents.add()
new.name = reference["Name"] or "Unnamed"
new.identification = reference["Identification"] or "*"
if self.file.schema == "IFC2X3":
new.identification = reference["ItemReference"] or "*"
else:
new.identification = reference["Identification"] or "*"
new.ifc_definition_id = reference_id
props.is_editing = "reference"
bpy.ops.bim.disable_editing_document()
@@ -25,19 +25,19 @@ class BIM_PT_documents(Panel):
row = self.layout.row(align=True)
row.label(text="{} Documents Found".format(len(Data.information)), icon="FILE")
if self.props.is_editing == "information":
row.operator("bim.disable_document_editing_ui", text="", icon="CHECKMARK")
row.operator("bim.add_information", text="", icon="ADD")
row.operator("bim.disable_document_editing_ui", text="", icon="X")
else:
row.operator("bim.load_information", text="", icon="GREASEPENCIL")
row.operator("bim.load_information", text="", icon="IMPORT")
if not self.props.is_editing or self.props.is_editing == "reference":
row = self.layout.row(align=True)
row.label(text="{} References Found".format(len(Data.references)), icon="FILE_HIDDEN")
if self.props.is_editing == "reference":
row.operator("bim.disable_document_editing_ui", text="", icon="CHECKMARK")
row.operator("bim.add_document_reference", text="", icon="ADD")
row.operator("bim.disable_document_editing_ui", text="", icon="X")
else:
row.operator("bim.load_document_references", text="", icon="GREASEPENCIL")
row.operator("bim.load_document_references", text="", icon="IMPORT")
if self.props.is_editing:
self.layout.template_list(
@@ -51,7 +51,6 @@ class BIM_PT_documents(Panel):
if self.props.active_document_id:
self.draw_editable_ui(context)
return
def draw_editable_ui(self, context):
for attribute in self.props.document_attributes:
@@ -0,0 +1,26 @@
import bpy
from . import ui, prop, operator
classes = (
operator.LoadLayers,
operator.DisableLayerEditingUI,
operator.EnableEditingLayer,
operator.DisableEditingLayer,
operator.AddPresentationLayer,
operator.EditPresentationLayer,
operator.RemovePresentationLayer,
operator.AssignPresentationLayer,
operator.UnassignPresentationLayer,
prop.Layer,
prop.BIMLayerProperties,
ui.BIM_PT_layers,
ui.BIM_UL_layers,
)
def register():
bpy.types.Scene.BIMLayerProperties = bpy.props.PointerProperty(type=prop.BIMLayerProperties)
def unregister():
del bpy.types.Scene.BIMLayerProperties
@@ -0,0 +1,13 @@
import ifcopenshell
class Usecase:
def __init__(self, file, settings={}):
self.file = file
self.settings = {}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
return self.file.create_entity("IfcPresentationLayerAssignment", **{
"Name": "Unnamed"
})
@@ -0,0 +1,17 @@
import ifcopenshell
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"item": None,
"layer": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
assigned_items = set(self.settings["layer"].AssignedItems) or set()
assigned_items.add(self.settings["item"])
self.settings["layer"].AssignedItems = list(assigned_items)
@@ -0,0 +1,30 @@
import ifcopenshell
import ifcopenshell.util.date
from blenderbim.bim.ifc import IfcStore
from datetime import datetime
class Data:
is_loaded = False
items = {}
layers = {}
@classmethod
def load(cls, item_id=None):
cls._file = IfcStore.get_file()
if not cls._file:
return
cls.load_layers()
cls.is_loaded = True
@classmethod
def load_layers(cls):
cls.layers = {}
cls.items = {}
for layer in cls._file.by_type("IfcPresentationLayerAssignment"):
data = layer.get_info()
if layer.AssignedItems:
for item in layer.AssignedItems:
cls.items.setdefault(item.id(), []).append(layer.id())
del data["AssignedItems"]
cls.layers[layer.id()] = data
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"layer": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["layer"], name, value)
@@ -0,0 +1,151 @@
import bpy
import json
import blenderbim.bim.module.layer.add_layer as add_layer
import blenderbim.bim.module.layer.edit_layer as edit_layer
import blenderbim.bim.module.layer.remove_layer as remove_layer
import blenderbim.bim.module.layer.assign_layer as assign_layer
import blenderbim.bim.module.layer.unassign_layer as unassign_layer
from blenderbim.bim.ifc import IfcStore
from blenderbim.bim.module.layer.data import Data
class LoadLayers(bpy.types.Operator):
bl_idname = "bim.load_layers"
bl_label = "Load Layers"
def execute(self, context):
self.file = IfcStore.get_file()
props = context.scene.BIMLayerProperties
while len(props.layers) > 0:
props.layers.remove(0)
for layer_id, layer in Data.layers.items():
new = props.layers.add()
new.name = layer["Name"] or "Unnamed"
new.ifc_definition_id = layer_id
props.is_editing = True
bpy.ops.bim.disable_editing_layer()
return {"FINISHED"}
class DisableLayerEditingUI(bpy.types.Operator):
bl_idname = "bim.disable_layer_editing_ui"
bl_label = "Disable Layer Editing UI"
def execute(self, context):
context.scene.BIMLayerProperties.is_editing = False
return {"FINISHED"}
class EnableEditingLayer(bpy.types.Operator):
bl_idname = "bim.enable_editing_layer"
bl_label = "Enable Editing Layer"
layer: bpy.props.IntProperty()
def execute(self, context):
props = context.scene.BIMLayerProperties
while len(props.layer_attributes) > 0:
props.layer_attributes.remove(0)
data = Data.layers[self.layer]
for attribute in IfcStore.get_schema().declaration_by_name("IfcPresentationLayerAssignment").all_attributes():
data_type = str(attribute.type_of_attribute)
if "<entity" in data_type:
continue
new = props.layer_attributes.add()
new.name = attribute.name()
new.is_null = data[attribute.name()] is None
new.is_optional = attribute.optional()
new.string_value = "" if new.is_null else data[attribute.name()]
props.active_layer_id = self.layer
return {"FINISHED"}
class DisableEditingLayer(bpy.types.Operator):
bl_idname = "bim.disable_editing_layer"
bl_label = "Disable Editing Layer"
def execute(self, context):
context.scene.BIMLayerProperties.active_layer_id = 0
return {"FINISHED"}
class AddPresentationLayer(bpy.types.Operator):
bl_idname = "bim.add_presentation_layer"
bl_label = "Add Layer"
def execute(self, context):
result = add_layer.Usecase(IfcStore.get_file()).execute()
Data.load()
bpy.ops.bim.load_layers()
bpy.ops.bim.enable_editing_layer(layer=result.id())
return {"FINISHED"}
class EditPresentationLayer(bpy.types.Operator):
bl_idname = "bim.edit_presentation_layer"
bl_label = "Edit Layer"
def execute(self, context):
props = context.scene.BIMLayerProperties
attributes = {}
for attribute in props.layer_attributes:
if attribute.is_null:
attributes[attribute.name] = None
else:
attributes[attribute.name] = attribute.string_value
self.file = IfcStore.get_file()
edit_layer.Usecase(
self.file, {"layer": self.file.by_id(props.active_layer_id), "attributes": attributes}
).execute()
Data.load()
bpy.ops.bim.load_layers()
return {"FINISHED"}
class RemovePresentationLayer(bpy.types.Operator):
bl_idname = "bim.remove_presentation_layer"
bl_label = "Remove Presentation Layer"
layer: bpy.props.IntProperty()
def execute(self, context):
props = context.scene.BIMLayerProperties
self.file = IfcStore.get_file()
remove_layer.Usecase(self.file, {"layer": self.file.by_id(self.layer)}).execute()
Data.load()
bpy.ops.bim.load_layers()
return {"FINISHED"}
class AssignPresentationLayer(bpy.types.Operator):
bl_idname = "bim.assign_presentation_layer"
bl_label = "Assign Presentation Layer"
item: bpy.props.StringProperty()
layer: bpy.props.IntProperty()
def execute(self, context):
item = bpy.data.meshes.get(self.item) if self.item else context.active_object.data
self.file = IfcStore.get_file()
assign_layer.Usecase(self.file, {
"item": self.file.by_id(item.BIMMeshProperties.ifc_definition_id),
"layer": self.file.by_id(self.layer)
}).execute()
Data.load()
return {"FINISHED"}
class UnassignPresentationLayer(bpy.types.Operator):
bl_idname = "bim.unassign_presentation_layer"
bl_label = "Unassign Presentation Layer"
item: bpy.props.StringProperty()
layer: bpy.props.IntProperty()
def execute(self, context):
item = bpy.data.meshes.get(self.item) if self.item else context.active_object.data
self.file = IfcStore.get_file()
unassign_layer.Usecase(self.file, {
"item": self.file.by_id(item.BIMMeshProperties.ifc_definition_id),
"layer": self.file.by_id(self.layer)
}).execute()
Data.load()
return {"FINISHED"}
@@ -0,0 +1,26 @@
import bpy
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 Layer(PropertyGroup):
name: StringProperty(name="Name")
ifc_definition_id: IntProperty(name="IFC Definition ID")
class BIMLayerProperties(PropertyGroup):
layer_attributes: CollectionProperty(name="Layer Attributes", type=Attribute)
active_layer_id: IntProperty(name="Active Layer Id")
layers: CollectionProperty(name="Layers", type=Layer)
active_layer_index: IntProperty(name="Active Layer Index")
is_editing: BoolProperty(name="Is Editing", default=False)
@@ -0,0 +1,9 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"layer": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["layer"])
@@ -0,0 +1,86 @@
from bpy.types import Panel, UIList, Mesh
from blenderbim.bim.ifc import IfcStore
from blenderbim.bim.module.layer.data import Data
class BIM_PT_layers(Panel):
bl_label = "IFC Presentation Layers"
bl_idname = "BIM_PT_layers"
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()
self.props = context.scene.BIMLayerProperties
if Data.layers:
row = self.layout.row(align=True)
row.label(text="{} Layers Found".format(len(Data.layers.keys())))
if self.props.is_editing:
row.operator("bim.add_presentation_layer", text="", icon="ADD")
row.operator("bim.disable_layer_editing_ui", text="", icon="X")
else:
row.operator("bim.load_layers", text="", icon="IMPORT")
else:
row = self.layout.row(align=True)
row.label(text="No Layers")
if self.props.is_editing:
self.layout.template_list(
"BIM_UL_layers",
"",
self.props,
"layers",
self.props,
"active_layer_index",
)
if self.props.active_layer_id:
self.draw_editable_ui(context)
def draw_editable_ui(self, context):
for attribute in self.props.layer_attributes:
row = self.layout.row(align=True)
row.prop(attribute, "string_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_layers(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.active_object and isinstance(context.active_object.data, Mesh):
mprops = context.active_object.data.BIMMeshProperties
if (
mprops.ifc_definition_id in Data.items
and item.ifc_definition_id in Data.items[mprops.ifc_definition_id]
):
op = row.operator("bim.unassign_presentation_layer", text="", icon="KEYFRAME_HLT", emboss=False)
op.layer = item.ifc_definition_id
else:
op = row.operator("bim.assign_presentation_layer", text="", icon="KEYFRAME", emboss=False)
op.layer = item.ifc_definition_id
row.operator("bim.disable_editing_layer", text="", icon="HIDE_OFF", emboss=False)
row.operator("bim.disable_editing_layer", text="", icon="FREEZE", emboss=False)
if context.scene.BIMLayerProperties.active_layer_id == item.ifc_definition_id:
row.operator("bim.edit_presentation_layer", text="", icon="CHECKMARK")
row.operator("bim.disable_editing_layer", text="", icon="X")
elif context.scene.BIMLayerProperties.active_layer_id:
row.operator("bim.remove_presentation_layer", text="", icon="X").layer = item.ifc_definition_id
else:
op = row.operator("bim.enable_editing_layer", text="", icon="GREASEPENCIL")
op.layer = item.ifc_definition_id
row.operator("bim.remove_presentation_layer", text="", icon="X").layer = item.ifc_definition_id
@@ -0,0 +1,17 @@
import ifcopenshell
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"item": None,
"layer": None,
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
assigned_items = set(self.settings["layer"].AssignedItems) or set()
assigned_items.remove(self.settings["item"])
self.settings["layer"].AssignedItems = list(assigned_items)