Implement ability to add SI units

This commit is contained in:
Dion Moult
2021-08-13 16:18:05 +10:00
parent 671f1ab9cf
commit fd163a3d1e
6 changed files with 99 additions and 19 deletions
@@ -8,6 +8,7 @@ classes = (
operator.DisableUnitEditingUI,
operator.RemoveUnit,
operator.AddMonetaryUnit,
operator.AddSIUnit,
operator.EnableEditingUnit,
operator.DisableEditingUnit,
operator.EditUnit,
@@ -1,5 +1,6 @@
import bpy
import ifcopenshell.api
import ifcopenshell.util.unit
import blenderbim.bim.helper
from blenderbim.bim.ifc import IfcStore
from ifcopenshell.api.unit.data import Data
@@ -156,8 +157,29 @@ class AddMonetaryUnit(bpy.types.Operator):
def _execute(self, context):
props = context.scene.BIMUnitProperties
self.file = IfcStore.get_file()
unit = ifcopenshell.api.run("unit.add_monetary_unit", self.file)
ifcopenshell.api.run("unit.assign_unit", self.file, units=[unit])
ifcopenshell.api.run("unit.add_monetary_unit", self.file)
Data.load(self.file)
bpy.ops.bim.load_units()
return {"FINISHED"}
class AddSIUnit(bpy.types.Operator):
bl_idname = "bim.add_si_unit"
bl_label = "Add SI Unit"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
return IfcStore.execute_ifc_operator(self, context)
def _execute(self, context):
props = context.scene.BIMUnitProperties
self.file = IfcStore.get_file()
unit = ifcopenshell.api.run(
"unit.add_si_unit",
self.file,
unit_type=props.named_unit_types,
name=ifcopenshell.util.unit.si_type_names[props.named_unit_types],
)
Data.load(self.file)
bpy.ops.bim.load_units()
return {"FINISHED"}
@@ -1,6 +1,7 @@
import bpy
import ifcopenshell
import ifcopenshell.util.schema
import ifcopenshell.util.attribute
from blenderbim.bim.ifc import IfcStore
from blenderbim.bim.prop import StrProperty, Attribute
from bpy.types import PropertyGroup
@@ -17,11 +18,14 @@ from bpy.props import (
unitclasses_enum = []
namedunittypes_enum = []
def purge():
global unitclasses_enum
global namedunittypes_enum
unitclasses_enum = []
namedunittypes_enum = []
def getUnitClasses(self, context):
@@ -33,6 +37,16 @@ def getUnitClasses(self, context):
return unitclasses_enum
def getNamedUnitTypes(self, context):
global namedunittypes_enum
if not len(namedunittypes_enum) and IfcStore.get_file():
values = ifcopenshell.util.attribute.get_enum_items(
IfcStore.get_schema().declaration_by_name("IfcNamedUnit").all_attributes()[1]
)
namedunittypes_enum.extend([(c, c, "") for c in sorted(values)])
return namedunittypes_enum
class Unit(PropertyGroup):
name: StringProperty(name="Name")
unit_type: StringProperty(name="Unit Type")
@@ -47,4 +61,5 @@ class BIMUnitProperties(PropertyGroup):
active_unit_index: IntProperty(name="Active Unit Index")
active_unit_id: IntProperty(name="Active Unit Id")
unit_classes: EnumProperty(items=getUnitClasses, name="Unit Classes")
named_unit_types: EnumProperty(items=getNamedUnitTypes, name="Named Unit Types")
unit_attributes: CollectionProperty(name="Unit Attributes", type=Attribute)
@@ -40,8 +40,11 @@ class BIM_PT_units(Panel):
row.operator("bim.add_monetary_unit", text="", icon="ADD")
elif self.props.unit_classes == "IfcDerivedUnit":
pass # TODO
elif self.props.unit_classes == "IfcSIUnit":
row.prop(self.props, "named_unit_types", text="")
row.operator("bim.add_si_unit", text="", icon="ADD")
else:
pass # TODO
row.prop(self.props, "named_unit_types", text="")
self.layout.template_list(
"BIM_UL_units",