From 887f50dee456b353e67efcef22b5a657557147e6 Mon Sep 17 00:00:00 2001 From: Jaime van Kessel Date: Fri, 7 Jun 2019 16:30:29 +0200 Subject: [PATCH] Add tests for calculating the extra z clearance --- cura/BuildVolume.py | 21 +++++++-------------- tests/TestBuildVolume.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/cura/BuildVolume.py b/cura/BuildVolume.py index b077d100ba..f04a8c73e5 100755 --- a/cura/BuildVolume.py +++ b/cura/BuildVolume.py @@ -33,6 +33,7 @@ from typing import List, Optional, TYPE_CHECKING, Any, Set, cast, Iterable, Dict if TYPE_CHECKING: from cura.CuraApplication import CuraApplication from cura.Settings.ExtruderStack import ExtruderStack + from UM.Settings.ContainerStack import ContainerStack # Radius of disallowed area in mm around prime. I.e. how much distance to keep from prime position. PRIME_CLEARANCE = 6.5 @@ -565,25 +566,17 @@ class BuildVolume(SceneNode): self.setPosition(Vector(0, -self._raft_thickness, 0), SceneNode.TransformSpace.World) self.raftThicknessChanged.emit() - def _updateExtraZClearance(self) -> None: + def _calculateExtraZClearance(self, extruders: List["ContainerStack"]) -> float: if not self._global_container_stack: - return + return 0 extra_z = 0.0 - extruders = ExtruderManager.getInstance().getUsedExtruderStacks() - use_extruders = False for extruder in extruders: if extruder.getProperty("retraction_hop_enabled", "value"): retraction_hop = extruder.getProperty("retraction_hop", "value") if extra_z is None or retraction_hop > extra_z: extra_z = retraction_hop - use_extruders = True - if not use_extruders: - # If no extruders, take global value. - if self._global_container_stack.getProperty("retraction_hop_enabled", "value"): - extra_z = self._global_container_stack.getProperty("retraction_hop", "value") - if extra_z != self._extra_z_clearance: - self._extra_z_clearance = extra_z + return extra_z def _onStackChanged(self): self._stack_change_timer.start() @@ -620,7 +613,7 @@ class BuildVolume(SceneNode): self._updateDisallowedAreas() self._updateRaftThickness() - self._updateExtraZClearance() + self._extra_z_clearance = self._calculateExtraZClearance(ExtruderManager.getInstance().getUsedExtruderStacks()) if self._engine_ready: self.rebuild() @@ -694,7 +687,7 @@ class BuildVolume(SceneNode): self._updateRaftThickness() if update_extra_z_clearance: - self._updateExtraZClearance() + self._extra_z_clearance = self._calculateExtraZClearance(ExtruderManager.getInstance().getUsedExtruderStacks()) if rebuild_me: self.rebuild() @@ -724,7 +717,7 @@ class BuildVolume(SceneNode): def _updateDisallowedAreasAndRebuild(self): self._updateDisallowedAreas() self._updateRaftThickness() - self._updateExtraZClearance() + self._extra_z_clearance = self._calculateExtraZClearance(ExtruderManager.getInstance().getUsedExtruderStacks()) self.rebuild() def _updateDisallowedAreas(self) -> None: diff --git a/tests/TestBuildVolume.py b/tests/TestBuildVolume.py index 59b94d0c6a..709c481443 100644 --- a/tests/TestBuildVolume.py +++ b/tests/TestBuildVolume.py @@ -142,6 +142,44 @@ class TestComputeDisallowedAreasPrimeBlob: assert build_volume._computeDisallowedAreasPrimeBlob(12, [mocked_extruder_stack]) == {"0": [resulting_polygon]} +class TestCalculateExtraZClearance: + setting_property_dict = {"retraction_hop": {"value": 12}, + "retraction_hop_enabled": {"value": True}} + + def getPropertySideEffect(*args, **kwargs): + properties = TestCalculateExtraZClearance.setting_property_dict.get(args[1]) + if properties: + return properties.get(args[2]) + + def test_noContainerStack(self, build_volume: BuildVolume): + assert build_volume._calculateExtraZClearance([]) is 0 + + def test_withRetractionHop(self, build_volume: BuildVolume): + mocked_global_stack = MagicMock(name="mocked_global_stack") + + mocked_extruder = MagicMock() + mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect) + + build_volume._global_container_stack = mocked_global_stack + + # It should be 12 because we have the hop enabled and the hop distance is set to 12 + assert build_volume._calculateExtraZClearance([mocked_extruder]) == 12 + + def test_withoutRetractionHop(self, build_volume: BuildVolume): + mocked_global_stack = MagicMock(name="mocked_global_stack") + + mocked_extruder = MagicMock() + mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect) + + build_volume._global_container_stack = mocked_global_stack + + patched_dictionary = self.setting_property_dict.copy() + patched_dictionary["retraction_hop_enabled"] = {"value": False} + with patch.dict(self.setting_property_dict, patched_dictionary): + # It should be 12 because we have the hop enabled and the hop distance is set to 12 + assert build_volume._calculateExtraZClearance([mocked_extruder]) == 0 + + class TestRebuild: def test_zeroWidthHeightDepth(self, build_volume: BuildVolume): build_volume.rebuild()