New usecase for adding conversion based units

This commit is contained in:
Dion Moult
2021-10-12 18:31:18 +11:00
parent e455f87305
commit fc2e12bff6
20 changed files with 974 additions and 207 deletions
@@ -0,0 +1,71 @@
@unit
Feature: Unit
Scenario: Assign scene units
Given an empty IFC project
When I press "bim.assign_scene_units"
Then nothing happens
Scenario: Assign unit
Given an empty IFC project
And the variable "unit" is "{ifc}.by_type('IfcSIUnit')[0].id()"
When I press "bim.assign_unit(unit={unit})"
Then nothing happens
Scenario: Unassign unit
Given an empty IFC project
And the variable "unit" is "{ifc}.by_type('IfcSIUnit')[0].id()"
When I press "bim.unassign_unit(unit={unit})"
Then nothing happens
Scenario: Load units
Given an empty IFC project
When I press "bim.load_units"
Then nothing happens
Scenario: Disable unit editing UI
Given an empty IFC project
And I press "bim.load_units"
When I press "bim.disable_unit_editing_ui"
Then nothing happens
Scenario: Remove unit
Given an empty IFC project
And the variable "unit" is "{ifc}.by_type('IfcSIUnit')[0].id()"
When I press "bim.remove_unit(unit={unit})"
Then nothing happens
Scenario: Add monetary unit
Given an empty IFC project
When I press "bim.add_monetary_unit"
Then nothing happens
Scenario: Add SI unit
Given an empty IFC project
When I press "bim.add_si_unit(unit_type='LENGTHUNIT')"
Then nothing happens
Scenario: Add context dependent unit
Given an empty IFC project
When I press "bim.add_context_dependent_unit(name='THINGAMAJIGS', unit_type='LENGTHUNIT')"
Then nothing happens
Scenario: Enable editing unit
Given an empty IFC project
And the variable "unit" is "{ifc}.by_type('IfcSIUnit')[0].id()"
When I press "bim.enable_editing_unit(unit={unit})"
Then nothing happens
Scenario: Disable editing unit
Given an empty IFC project
And the variable "unit" is "{ifc}.by_type('IfcSIUnit')[0].id()"
And I press "bim.enable_editing_unit(unit={unit})"
When I press "bim.disable_editing_unit"
Then nothing happens
Scenario: Edit unit
Given an empty IFC project
And the variable "unit" is "{ifc}.by_type('IfcSIUnit')[0].id()"
And I press "bim.enable_editing_unit(unit={unit})"
When I press "bim.edit_unit(unit={unit})"
Then nothing happens
+7
View File
@@ -98,6 +98,13 @@ def container():
prophet.verify()
@pytest.fixture
def unit():
prophet = Prophecy(blenderbim.core.tool.Unit)
yield prophet
prophet.verify()
class Prophecy:
def __init__(self, cls):
self.subject = cls
+154
View File
@@ -0,0 +1,154 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BlenderBIM Add-on is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import blenderbim.core.unit as subject
from test.core.bootstrap import ifc, unit
class TestAssignSceneUnits:
def test_creating_and_assigning_metric_units(self, ifc, unit):
unit.is_scene_unit_metric().should_be_called().will_return(True)
unit.get_scene_unit_name("length").should_be_called().will_return("name")
unit.get_scene_unit_si_prefix().should_be_called().will_return("prefix")
ifc.run(
"unit.add_si_unit", unit_type="LENGTHUNIT", name="name", prefix="prefix"
).should_be_called().will_return("lengthunit")
unit.get_scene_unit_name("area").should_be_called().will_return("name")
ifc.run("unit.add_si_unit", unit_type="AREAUNIT", name="name", prefix="prefix").should_be_called().will_return(
"areaunit"
)
unit.get_scene_unit_name("volume").should_be_called().will_return("name")
ifc.run(
"unit.add_si_unit", unit_type="VOLUMEUNIT", name="name", prefix="prefix"
).should_be_called().will_return("volumeunit")
ifc.run("unit.assign_unit", units=["lengthunit", "areaunit", "volumeunit"]).should_be_called()
subject.assign_scene_units(ifc, unit)
def test_creating_and_assigning_imperial_units(self, ifc, unit):
unit.is_scene_unit_metric().should_be_called().will_return(False)
unit.get_scene_unit_name("length").should_be_called().will_return("lengthname")
ifc.run("unit.add_conversion_based_unit", name="lengthname").should_be_called().will_return("lengthunit")
unit.get_scene_unit_name("area").should_be_called().will_return("areaname")
ifc.run("unit.add_conversion_based_unit", name="areaname").should_be_called().will_return("areaunit")
unit.get_scene_unit_name("volume").should_be_called().will_return("volumename")
ifc.run("unit.add_conversion_based_unit", name="volumename").should_be_called().will_return("volumeunit")
ifc.run("unit.assign_unit", units=["lengthunit", "areaunit", "volumeunit"]).should_be_called()
subject.assign_scene_units(ifc, unit)
class TestAssignUnit:
def test_run(self, ifc, unit):
ifc.run("unit.assign_unit", units=["unit"]).should_be_called()
unit.import_units().should_be_called()
subject.assign_unit(ifc, unit, unit="unit")
class TestUnassignUnit:
def test_run(self, ifc, unit):
ifc.run("unit.unassign_unit", units=["unit"]).should_be_called()
unit.import_units().should_be_called()
subject.unassign_unit(ifc, unit, unit="unit")
class TestLoadUnits:
def test_run(self, unit):
unit.import_units().should_be_called()
unit.enable_editing_units().should_be_called()
subject.load_units(unit)
class TestDisableUnitEditingUI:
def test_run(self, unit):
unit.disable_editing_units().should_be_called()
subject.disable_unit_editing_ui(unit)
class TestRemoveUnit:
def test_run(self, ifc, unit):
ifc.run("unit.remove_unit", unit="unit").should_be_called()
unit.import_units().should_be_called()
subject.remove_unit(ifc, unit, unit="unit")
class TestAddMonetaryUnit:
def test_run(self, ifc, unit):
ifc.run("unit.add_monetary_unit").should_be_called().will_return("unit")
unit.import_units().should_be_called()
assert subject.add_monetary_unit(ifc, unit) == "unit"
class TestAddSIUnit:
def test_run(self, ifc, unit):
unit.get_si_name_from_unit_type("unit_type").should_be_called().will_return("name")
ifc.run("unit.add_si_unit", unit_type="unit_type", name="name").should_be_called().will_return("unit")
unit.import_units().should_be_called()
assert subject.add_si_unit(ifc, unit, unit_type="unit_type") == "unit"
class TestAddContextDependentUnit:
def test_run(self, ifc, unit):
ifc.run("unit.add_context_dependent_unit", unit_type="unit_type", name="name").should_be_called().will_return(
"unit"
)
unit.import_units().should_be_called()
assert subject.add_context_dependent_unit(ifc, unit, unit_type="unit_type", name="name") == "unit"
class TestEnableEditingUnit:
def test_run(self, unit):
unit.set_active_unit("unit").should_be_called()
unit.import_unit_attributes("unit").should_be_called()
subject.enable_editing_unit(unit, unit="unit")
class TestDisableEditingUnit:
def test_run(self, unit):
unit.clear_active_unit().should_be_called()
subject.disable_editing_unit(unit)
class TestEditUnit:
def test_editing_monetary_units(self, ifc, unit):
unit.export_unit_attributes().should_be_called().will_return("attributes")
unit.get_unit_class("unit").should_be_called().will_return("IfcMonetaryUnit")
ifc.run("unit.edit_monetary_unit", unit="unit", attributes="attributes").should_be_called()
unit.import_units().should_be_called()
unit.clear_active_unit().should_be_called()
subject.edit_unit(ifc, unit, unit="unit")
def test_editing_derived_units(self, ifc, unit):
unit.export_unit_attributes().should_be_called().will_return("attributes")
unit.get_unit_class("unit").should_be_called().will_return("IfcDerivedUnit")
ifc.run("unit.edit_derived_unit", unit="unit", attributes="attributes").should_be_called()
unit.import_units().should_be_called()
unit.clear_active_unit().should_be_called()
subject.edit_unit(ifc, unit, unit="unit")
def test_editing_named_units(self, ifc, unit):
unit.export_unit_attributes().should_be_called().will_return("attributes")
unit.get_unit_class("unit").should_be_called().will_return("IfcNamedUnit")
ifc.run("unit.edit_named_unit", unit="unit", attributes="attributes").should_be_called()
unit.import_units().should_be_called()
unit.clear_active_unit().should_be_called()
subject.edit_unit(ifc, unit, unit="unit")
+315
View File
@@ -0,0 +1,315 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2021 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BlenderBIM Add-on is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import bpy
import ifcopenshell
import blenderbim.core.tool
import blenderbim.tool as tool
from test.bim.bootstrap import NewFile
from blenderbim.tool.unit import Unit as subject
class TestImplementsTool(NewFile):
def test_run(self):
assert isinstance(subject(), blenderbim.core.tool.Unit)
class TestClearActiveUnit(NewFile):
def test_run(self):
bpy.context.scene.BIMUnitProperties.active_unit_id = 1
subject.clear_active_unit()
assert bpy.context.scene.BIMUnitProperties.active_unit_id == 0
class TestDisableEditingUnits(NewFile):
def test_run(self):
bpy.context.scene.BIMUnitProperties.is_editing = True
subject.disable_editing_units()
assert bpy.context.scene.BIMUnitProperties.is_editing == False
class TestEnableEditingUnits(NewFile):
def test_run(self):
bpy.context.scene.BIMUnitProperties.is_editing = False
subject.enable_editing_units()
assert bpy.context.scene.BIMUnitProperties.is_editing == True
class TestExportUnitAttributes(NewFile):
def test_exporting_derived_units(self):
TestImportUnitAttributes().test_importing_derived_units()
assert subject.export_unit_attributes() == {
"UnitType": "ANGULARVELOCITYUNIT",
"UserDefinedType": "UserDefinedType",
}
def test_exporting_monetary_units(self):
TestImportUnitAttributes().test_importing_monetary_units()
assert subject.export_unit_attributes() == {"Currency": "Currency"}
def test_exporting_monetary_units_ifc2x3(self):
TestImportUnitAttributes().test_importing_monetary_units_ifc2x3()
assert subject.export_unit_attributes() == {"Currency": "USD"}
def test_exporting_context_dependent_units(self):
TestImportUnitAttributes().test_importing_context_dependent_units()
assert subject.export_unit_attributes() == {
"UnitType": "ABSORBEDDOSEUNIT",
"Name": "Name",
"Dimensions": [1, 2, 3, 4, 5, 6, 7],
}
def test_exporting_conversion_based_units(self):
TestImportUnitAttributes().test_importing_conversion_based_units()
assert subject.export_unit_attributes() == {
"UnitType": "ABSORBEDDOSEUNIT",
"Name": "Name",
"Dimensions": [1, 2, 3, 4, 5, 6, 7],
}
def test_exporting_conversion_based_with_offset_units(self):
TestImportUnitAttributes().test_importing_conversion_based_with_offset_units()
assert subject.export_unit_attributes() == {
"UnitType": "ABSORBEDDOSEUNIT",
"Name": "Name",
"Dimensions": [1, 2, 3, 4, 5, 6, 7],
"ConversionOffset": 1,
}
def test_exporting_si_units(self):
TestImportUnitAttributes().test_importing_si_units()
assert subject.export_unit_attributes() == {"UnitType": "ABSORBEDDOSEUNIT", "Prefix": "EXA", "Name": "AMPERE"}
class TestGetSceneUnitName(NewFile):
def test_getting_a_metric_name(self):
bpy.context.scene.unit_settings.system = "METRIC"
assert subject.get_scene_unit_name("length") == "METRE"
assert subject.get_scene_unit_name("area") == "SQUARE_METRE"
assert subject.get_scene_unit_name("volume") == "CUBIC_METRE"
def test_getting_an_imperial_name(self):
bpy.context.scene.unit_settings.system = "IMPERIAL"
bpy.context.scene.unit_settings.length_unit = "MILES"
assert subject.get_scene_unit_name("length") == "mile"
assert subject.get_scene_unit_name("area") == "square mile"
assert subject.get_scene_unit_name("volume") == "cubic mile"
bpy.context.scene.unit_settings.length_unit = "FEET"
assert subject.get_scene_unit_name("length") == "foot"
assert subject.get_scene_unit_name("area") == "square foot"
assert subject.get_scene_unit_name("volume") == "cubic foot"
bpy.context.scene.unit_settings.length_unit = "INCHES"
assert subject.get_scene_unit_name("length") == "inch"
assert subject.get_scene_unit_name("area") == "square inch"
assert subject.get_scene_unit_name("volume") == "cubic inch"
bpy.context.scene.unit_settings.length_unit = "THOU"
assert subject.get_scene_unit_name("length") == "thou"
assert subject.get_scene_unit_name("area") == "square thou"
assert subject.get_scene_unit_name("volume") == "cubic thou"
bpy.context.scene.unit_settings.length_unit = "ADAPTIVE"
assert subject.get_scene_unit_name("length") == "foot"
assert subject.get_scene_unit_name("area") == "square foot"
assert subject.get_scene_unit_name("volume") == "cubic foot"
def test_getting_a_name_with_no_unit_system(self):
bpy.context.scene.unit_settings.system = "NONE"
assert subject.get_scene_unit_name("length") == "METRE"
class TestGetSceneUnitSIPrefix:
def test_run(self):
bpy.context.scene.unit_settings.system = "METRIC"
bpy.context.scene.unit_settings.length_unit = "METERS"
assert subject.get_scene_unit_si_prefix() is None
bpy.context.scene.unit_settings.length_unit = "MICROMETERS"
assert subject.get_scene_unit_si_prefix() == "MICRO"
bpy.context.scene.unit_settings.length_unit = "MILLIMETERS"
assert subject.get_scene_unit_si_prefix() == "MILLI"
bpy.context.scene.unit_settings.length_unit = "CENTIMETERS"
assert subject.get_scene_unit_si_prefix() == "CENTI"
bpy.context.scene.unit_settings.length_unit = "KILOMETERS"
assert subject.get_scene_unit_si_prefix() == "KILO"
bpy.context.scene.unit_settings.length_unit = "ADAPTIVE"
assert subject.get_scene_unit_si_prefix() is None
class TestGetSINameFromUnitType:
def test_run(self):
for key, value in ifcopenshell.util.unit.si_type_names.items():
assert subject.get_si_name_from_unit_type(key) == value
class TestGetUnitClass:
def test_run(self):
ifc = ifcopenshell.file()
assert subject.get_unit_class(ifc.createIfcSIUnit()) == "IfcSIUnit"
assert subject.get_unit_class(ifc.createIfcNamedUnit()) == "IfcNamedUnit"
class TestImportUnitAttributes(NewFile):
def test_importing_derived_units(self):
ifc = ifcopenshell.file()
unit = ifc.createIfcDerivedUnit()
unit.UnitType = "ANGULARVELOCITYUNIT"
unit.UserDefinedType = "UserDefinedType"
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("UnitType").enum_value == "ANGULARVELOCITYUNIT"
assert props.unit_attributes.get("UserDefinedType").string_value == "UserDefinedType"
def test_importing_monetary_units(self):
ifc = ifcopenshell.file()
unit = ifc.createIfcMonetaryUnit()
unit.Currency = "Currency"
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("Currency").string_value == "Currency"
def test_importing_monetary_units_ifc2x3(self):
ifc = ifcopenshell.file(schema="IFC2X3")
unit = ifc.createIfcMonetaryUnit()
unit.Currency = "USD"
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("Currency").enum_value == "USD"
def test_importing_context_dependent_units(self):
ifc = ifcopenshell.file()
tool.Ifc.set(ifc)
unit = ifc.createIfcContextDependentUnit()
unit.UnitType = "ABSORBEDDOSEUNIT"
unit.Name = "Name"
unit.Dimensions = ifc.createIfcDimensionalExponents(1, 2, 3, 4, 5, 6, 7)
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("UnitType").enum_value == "ABSORBEDDOSEUNIT"
assert props.unit_attributes.get("Name").string_value == "Name"
assert props.unit_attributes.get("Dimensions").string_value == "[1, 2, 3, 4, 5, 6, 7]"
def test_importing_conversion_based_units(self):
ifc = ifcopenshell.file()
tool.Ifc.set(ifc)
unit = ifc.createIfcConversionBasedUnit()
unit.UnitType = "ABSORBEDDOSEUNIT"
unit.Name = "Name"
unit.Dimensions = ifc.createIfcDimensionalExponents(1, 2, 3, 4, 5, 6, 7)
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("UnitType").enum_value == "ABSORBEDDOSEUNIT"
assert props.unit_attributes.get("Name").string_value == "Name"
assert props.unit_attributes.get("Dimensions").string_value == "[1, 2, 3, 4, 5, 6, 7]"
def test_importing_conversion_based_with_offset_units(self):
ifc = ifcopenshell.file()
tool.Ifc.set(ifc)
unit = ifc.createIfcConversionBasedUnitWithOffset()
unit.UnitType = "ABSORBEDDOSEUNIT"
unit.Name = "Name"
unit.Dimensions = ifc.createIfcDimensionalExponents(1, 2, 3, 4, 5, 6, 7)
unit.ConversionOffset = 1
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("UnitType").enum_value == "ABSORBEDDOSEUNIT"
assert props.unit_attributes.get("Name").string_value == "Name"
assert props.unit_attributes.get("Dimensions").string_value == "[1, 2, 3, 4, 5, 6, 7]"
assert props.unit_attributes.get("ConversionOffset").float_value == 1
def test_importing_si_units(self):
ifc = ifcopenshell.file()
tool.Ifc.set(ifc)
unit = ifc.createIfcSIUnit()
unit.UnitType = "ABSORBEDDOSEUNIT"
unit.Prefix = "EXA"
unit.Name = "AMPERE"
subject.import_unit_attributes(unit)
props = bpy.context.scene.BIMUnitProperties
assert props.unit_attributes.get("UnitType").enum_value == "ABSORBEDDOSEUNIT"
assert props.unit_attributes.get("Prefix").enum_value == "EXA"
assert props.unit_attributes.get("Name").enum_value == "AMPERE"
assert props.unit_attributes.get("Dimensions") is None
class TestImportUnits(NewFile):
def test_importing_multiple_units(self):
ifc = ifcopenshell.file()
tool.Ifc.set(ifc)
unit1 = ifc.createIfcDerivedUnit(UnitType="ANGULARVELOCITYUNIT")
unit2 = ifc.createIfcMonetaryUnit(Currency="Currency")
unit3 = ifc.createIfcContextDependentUnit(Name="Name", UnitType="ABSORBEDDOSEUNIT")
unit4 = ifc.createIfcConversionBasedUnit(Name="Name", UnitType="ABSORBEDDOSEUNIT")
unit5 = ifc.createIfcSIUnit(Name="AMPERE", Prefix="MILLI", UnitType="ABSORBEDDOSEUNIT")
unit6 = ifc.createIfcSIUnit(Name="CUBIC_METRE", Prefix="CENTI", UnitType="ABSORBEDDOSEUNIT")
ifc.createIfcUnitAssignment(Units=[unit2])
subject.import_units()
props = bpy.context.scene.BIMUnitProperties
assert len(props.units) == 6
assert props.units[0].ifc_definition_id == unit1.id()
assert props.units[0].name == ""
assert props.units[0].is_assigned is False
assert props.units[0].unit_type == unit1.UnitType
assert props.units[0].ifc_class == unit1.is_a()
assert props.units[1].ifc_definition_id == unit2.id()
assert props.units[1].name == "Currency"
assert props.units[1].is_assigned is True
assert props.units[1].unit_type == "CURRENCY"
assert props.units[1].ifc_class == unit2.is_a()
assert props.units[2].ifc_definition_id == unit3.id()
assert props.units[2].name == "Name"
assert props.units[2].is_assigned is False
assert props.units[2].unit_type == unit3.UnitType
assert props.units[2].ifc_class == unit3.is_a()
assert props.units[3].ifc_definition_id == unit4.id()
assert props.units[3].name == "Name"
assert props.units[3].is_assigned is False
assert props.units[3].unit_type == unit4.UnitType
assert props.units[3].ifc_class == unit4.is_a()
assert props.units[4].ifc_definition_id == unit5.id()
assert props.units[4].name == "MILLIAMPERE"
assert props.units[4].is_assigned is False
assert props.units[4].unit_type == unit5.UnitType
assert props.units[4].ifc_class == unit5.is_a()
assert props.units[5].ifc_definition_id == unit6.id()
assert props.units[5].name == "CUBIC CENTIMETRE"
assert props.units[5].is_assigned is False
assert props.units[5].unit_type == unit6.UnitType
assert props.units[5].ifc_class == unit6.is_a()
class TestIsSceneUnitMetric(NewFile):
def test_run(self):
props = bpy.context.scene.unit_settings
props.system = "METRIC"
assert subject.is_scene_unit_metric() is True
props.system = "IMPERIAL"
assert subject.is_scene_unit_metric() is False
props.system = "NONE"
assert subject.is_scene_unit_metric() is True
class TestSetActiveUnit(NewFile):
def test_run(self):
ifc = ifcopenshell.file()
unit = ifc.createIfcSIUnit()
subject.set_active_unit(unit)
assert bpy.context.scene.BIMUnitProperties.active_unit_id == unit.id()