TestBuildVolume.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. from unittest.mock import MagicMock, patch
  2. from UM.Math.AxisAlignedBox import AxisAlignedBox
  3. import pytest
  4. from UM.Math.Polygon import Polygon
  5. from UM.Math.Vector import Vector
  6. from cura.BuildVolume import BuildVolume, PRIME_CLEARANCE
  7. import numpy
  8. @pytest.fixture
  9. def build_volume() -> BuildVolume:
  10. mocked_application = MagicMock()
  11. mocked_platform = MagicMock(name="platform")
  12. with patch("cura.BuildVolume.Platform", mocked_platform):
  13. return BuildVolume(mocked_application)
  14. def test_buildVolumeSetSizes(build_volume):
  15. build_volume.setWidth(10)
  16. assert build_volume.getDiagonalSize() == 10
  17. build_volume.setWidth(0)
  18. build_volume.setHeight(100)
  19. assert build_volume.getDiagonalSize() == 100
  20. build_volume.setHeight(0)
  21. build_volume.setDepth(200)
  22. assert build_volume.getDiagonalSize() == 200
  23. def test_buildMesh(build_volume):
  24. mesh = build_volume._buildMesh(0, 100, 0, 100, 0, 100, 1)
  25. result_vertices = numpy.array([[0., 0., 0.], [100., 0., 0.], [0., 0., 0.], [0., 100., 0.], [0., 100., 0.], [100., 100., 0.], [100., 0., 0.], [100., 100., 0.], [0., 0., 100.], [100., 0., 100.], [0., 0., 100.], [0., 100., 100.], [0., 100., 100.], [100., 100., 100.], [100., 0., 100.], [100., 100., 100.], [0., 0., 0.], [0., 0., 100.], [100., 0., 0.], [100., 0., 100.], [0., 100., 0.], [0., 100., 100.], [100., 100., 0.], [100., 100., 100.]], dtype=numpy.float32)
  26. assert numpy.array_equal(result_vertices, mesh.getVertices())
  27. def test_buildGridMesh(build_volume):
  28. mesh = build_volume._buildGridMesh(0, 100, 0, 100, 0, 100, 1)
  29. result_vertices = numpy.array([[0., -1., 0.], [100., -1., 100.], [100., -1., 0.], [0., -1., 0.], [0., -1., 100.], [100., -1., 100.]])
  30. assert numpy.array_equal(result_vertices, mesh.getVertices())
  31. def test_clamp(build_volume):
  32. assert build_volume._clamp(0, 0, 200) == 0
  33. assert build_volume._clamp(0, -200, 200) == 0
  34. assert build_volume._clamp(300, -200, 200) == 200
  35. class TestCalculateBedAdhesionSize:
  36. setting_property_dict = {"adhesion_type": {"value": "brim"},
  37. "skirt_brim_line_width": {"value": 0},
  38. "initial_layer_line_width_factor": {"value": 0},
  39. "brim_line_count": {"value": 0},
  40. "machine_width": {"value": 200},
  41. "machine_depth": {"value": 200},
  42. "skirt_line_count": {"value": 0},
  43. "skirt_gap": {"value": 0},
  44. "raft_margin": {"value": 0}
  45. }
  46. def getPropertySideEffect(*args, **kwargs):
  47. properties = TestCalculateBedAdhesionSize.setting_property_dict.get(args[1])
  48. if properties:
  49. return properties.get(args[2])
  50. def createAndSetGlobalStack(self, build_volume):
  51. mocked_stack = MagicMock()
  52. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  53. build_volume._global_container_stack = mocked_stack
  54. def test_noGlobalStack(self, build_volume: BuildVolume):
  55. assert build_volume._calculateBedAdhesionSize([]) is None
  56. @pytest.mark.parametrize("setting_dict, result", [
  57. ({}, 0),
  58. ({"adhesion_type": {"value": "skirt"}}, 0),
  59. ({"adhesion_type": {"value": "raft"}}, 0),
  60. ({"adhesion_type": {"value": "none"}}, 0),
  61. ({"adhesion_type": {"value": "skirt"}, "skirt_line_count": {"value": 2}, "initial_layer_line_width_factor": {"value": 1}, "skirt_brim_line_width": {"value": 2}}, 0.02),
  62. # Even though it's marked as skirt, it should behave as a brim as the prime tower has a brim (skirt line count is still at 0!)
  63. ({"adhesion_type": {"value": "skirt"}, "prime_tower_brim_enable": {"value": True}, "skirt_brim_line_width": {"value": 2}, "initial_layer_line_width_factor": {"value": 3}}, -0.06),
  64. ({"brim_line_count": {"value": 1}, "skirt_brim_line_width": {"value": 2}, "initial_layer_line_width_factor": {"value": 3}}, 0),
  65. ({"brim_line_count": {"value": 2}, "skirt_brim_line_width": {"value": 2}, "initial_layer_line_width_factor": {"value": 3}}, 0.06),
  66. ({"brim_line_count": {"value": 9000000}, "skirt_brim_line_width": {"value": 90000}, "initial_layer_line_width_factor": {"value": 9000}}, 100), # Clamped at half the max size of buildplate
  67. ])
  68. def test_singleExtruder(self, build_volume: BuildVolume, setting_dict, result):
  69. self.createAndSetGlobalStack(build_volume)
  70. patched_dictionary = self.setting_property_dict.copy()
  71. patched_dictionary.update(setting_dict)
  72. with patch.dict(self.setting_property_dict, patched_dictionary):
  73. assert build_volume._calculateBedAdhesionSize([]) == result
  74. def test_unknownBedAdhesion(self, build_volume: BuildVolume):
  75. self.createAndSetGlobalStack(build_volume)
  76. patched_dictionary = self.setting_property_dict.copy()
  77. patched_dictionary.update({"adhesion_type": {"value": "OMGZOMGBBQ"}})
  78. with patch.dict(self.setting_property_dict, patched_dictionary):
  79. with pytest.raises(Exception):
  80. build_volume._calculateBedAdhesionSize([])
  81. class TestComputeDisallowedAreasStatic:
  82. setting_property_dict = {"machine_disallowed_areas": {"value": [[[-200, 112.5], [ -82, 112.5], [ -84, 102.5], [-115, 102.5]]]},
  83. "machine_width": {"value": 200},
  84. "machine_depth": {"value": 200},
  85. }
  86. def getPropertySideEffect(*args, **kwargs):
  87. properties = TestComputeDisallowedAreasStatic.setting_property_dict.get(args[1])
  88. if properties:
  89. return properties.get(args[2])
  90. def test_computeDisallowedAreasStaticNoExtruder(self, build_volume: BuildVolume):
  91. mocked_stack = MagicMock()
  92. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  93. build_volume._global_container_stack = mocked_stack
  94. assert build_volume._computeDisallowedAreasStatic(0, []) == {}
  95. def test_computeDisalowedAreasStaticSingleExtruder(self, build_volume: BuildVolume):
  96. mocked_stack = MagicMock()
  97. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  98. mocked_extruder = MagicMock()
  99. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  100. mocked_extruder.getId = MagicMock(return_value = "zomg")
  101. build_volume._global_container_stack = mocked_stack
  102. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  103. result = build_volume._computeDisallowedAreasStatic(0, [mocked_extruder])
  104. assert result == {"zomg": [Polygon([[-84.0, 102.5], [-115.0, 102.5], [-200.0, 112.5], [-82.0, 112.5]])]}
  105. def test_computeDisalowedAreasMutliExtruder(self, build_volume):
  106. mocked_stack = MagicMock()
  107. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  108. mocked_extruder = MagicMock()
  109. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  110. mocked_extruder.getId = MagicMock(return_value="zomg")
  111. extruder_manager = MagicMock()
  112. extruder_manager.getActiveExtruderStacks = MagicMock(return_value = [mocked_stack])
  113. build_volume._global_container_stack = mocked_stack
  114. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance", MagicMock(return_value = extruder_manager)):
  115. result = build_volume._computeDisallowedAreasStatic(0, [mocked_extruder])
  116. assert result == {"zomg": [Polygon([[-84.0, 102.5], [-115.0, 102.5], [-200.0, 112.5], [-82.0, 112.5]])]}
  117. class TestUpdateRaftThickness:
  118. setting_property_dict = {"raft_base_thickness": {"value": 1},
  119. "raft_interface_thickness": {"value": 1},
  120. "raft_surface_layers": {"value": 1},
  121. "raft_surface_thickness": {"value": 1},
  122. "raft_airgap": {"value": 1},
  123. "layer_0_z_overlap": {"value": 1},
  124. "adhesion_type": {"value": "raft"}}
  125. def getPropertySideEffect(*args, **kwargs):
  126. properties = TestUpdateRaftThickness.setting_property_dict.get(args[1])
  127. if properties:
  128. return properties.get(args[2])
  129. def createMockedStack(self):
  130. mocked_global_stack = MagicMock(name="mocked_global_stack")
  131. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  132. extruder_stack = MagicMock()
  133. mocked_global_stack.extruders = {"0": extruder_stack}
  134. return mocked_global_stack
  135. def test_simple(self, build_volume: BuildVolume):
  136. build_volume.raftThicknessChanged = MagicMock()
  137. mocked_global_stack = self.createMockedStack()
  138. build_volume._global_container_stack = mocked_global_stack
  139. assert build_volume.getRaftThickness() == 0
  140. build_volume._updateRaftThickness()
  141. assert build_volume.getRaftThickness() == 3
  142. assert build_volume.raftThicknessChanged.emit.call_count == 1
  143. def test_adhesionIsNotRaft(self, build_volume: BuildVolume):
  144. patched_dictionary = self.setting_property_dict.copy()
  145. patched_dictionary["adhesion_type"] = {"value": "not_raft"}
  146. mocked_global_stack = self.createMockedStack()
  147. build_volume._global_container_stack = mocked_global_stack
  148. assert build_volume.getRaftThickness() == 0
  149. with patch.dict(self.setting_property_dict, patched_dictionary):
  150. build_volume._updateRaftThickness()
  151. assert build_volume.getRaftThickness() == 0
  152. def test_noGlobalStack(self, build_volume: BuildVolume):
  153. build_volume.raftThicknessChanged = MagicMock()
  154. assert build_volume.getRaftThickness() == 0
  155. build_volume._updateRaftThickness()
  156. assert build_volume.getRaftThickness() == 0
  157. assert build_volume.raftThicknessChanged.emit.call_count == 0
  158. class TestComputeDisallowedAreasPrimeBlob:
  159. setting_property_dict = {"machine_width": {"value": 50},
  160. "machine_depth": {"value": 100},
  161. "prime_blob_enable": {"value": True},
  162. "extruder_prime_pos_x": {"value": 25},
  163. "extruder_prime_pos_y": {"value": 50},
  164. "machine_center_is_zero": {"value": True},
  165. }
  166. def getPropertySideEffect(*args, **kwargs):
  167. properties = TestComputeDisallowedAreasPrimeBlob.setting_property_dict.get(args[1])
  168. if properties:
  169. return properties.get(args[2])
  170. def test_noGlobalContainer(self, build_volume: BuildVolume):
  171. # No global container and no extruders, so we expect no blob areas
  172. assert build_volume._computeDisallowedAreasPrimeBlob(12, []) == {}
  173. def test_noExtruders(self, build_volume: BuildVolume):
  174. mocked_stack = MagicMock()
  175. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  176. build_volume._global_container_stack = mocked_stack
  177. # No extruders, so still expect that we get no area
  178. assert build_volume._computeDisallowedAreasPrimeBlob(12, []) == {}
  179. def test_singleExtruder(self, build_volume: BuildVolume):
  180. mocked_global_stack = MagicMock(name = "mocked_global_stack")
  181. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  182. mocked_extruder_stack = MagicMock(name = "mocked_extruder_stack")
  183. mocked_extruder_stack.getId = MagicMock(return_value = "0")
  184. mocked_extruder_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  185. build_volume._global_container_stack = mocked_global_stack
  186. # Create a polygon that should be the result
  187. resulting_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  188. # Since we want a blob of size 12;
  189. resulting_polygon = resulting_polygon.getMinkowskiHull(Polygon.approximatedCircle(12))
  190. # In the The translation result is 25, -50 (due to the settings used)
  191. resulting_polygon = resulting_polygon.translate(25, -50)
  192. assert build_volume._computeDisallowedAreasPrimeBlob(12, [mocked_extruder_stack]) == {"0": [resulting_polygon]}
  193. class TestCalculateExtraZClearance:
  194. setting_property_dict = {"retraction_hop": {"value": 12},
  195. "retraction_hop_enabled": {"value": True}}
  196. def getPropertySideEffect(*args, **kwargs):
  197. properties = TestCalculateExtraZClearance.setting_property_dict.get(args[1])
  198. if properties:
  199. return properties.get(args[2])
  200. def test_noContainerStack(self, build_volume: BuildVolume):
  201. assert build_volume._calculateExtraZClearance([]) is 0
  202. def test_withRetractionHop(self, build_volume: BuildVolume):
  203. mocked_global_stack = MagicMock(name="mocked_global_stack")
  204. mocked_extruder = MagicMock()
  205. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  206. build_volume._global_container_stack = mocked_global_stack
  207. # It should be 12 because we have the hop enabled and the hop distance is set to 12
  208. assert build_volume._calculateExtraZClearance([mocked_extruder]) == 12
  209. def test_withoutRetractionHop(self, build_volume: BuildVolume):
  210. mocked_global_stack = MagicMock(name="mocked_global_stack")
  211. mocked_extruder = MagicMock()
  212. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  213. build_volume._global_container_stack = mocked_global_stack
  214. patched_dictionary = self.setting_property_dict.copy()
  215. patched_dictionary["retraction_hop_enabled"] = {"value": False}
  216. with patch.dict(self.setting_property_dict, patched_dictionary):
  217. # It should be 12 because we have the hop enabled and the hop distance is set to 12
  218. assert build_volume._calculateExtraZClearance([mocked_extruder]) == 0
  219. class TestRebuild:
  220. def test_zeroWidthHeightDepth(self, build_volume: BuildVolume):
  221. build_volume.rebuild()
  222. assert build_volume.getMeshData() is None
  223. def test_engineIsNotRead(self, build_volume: BuildVolume):
  224. build_volume.setWidth(10)
  225. build_volume.setHeight(10)
  226. build_volume.setDepth(10)
  227. build_volume.rebuild()
  228. assert build_volume.getMeshData() is None
  229. def test_noGlobalStack(self, build_volume: BuildVolume):
  230. build_volume.setWidth(10)
  231. build_volume.setHeight(10)
  232. build_volume.setDepth(10)
  233. # Fake the the "engine is created callback"
  234. build_volume._onEngineCreated()
  235. build_volume.rebuild()
  236. assert build_volume.getMeshData() is None
  237. def test_updateBoundingBox(self, build_volume: BuildVolume):
  238. build_volume.setWidth(10)
  239. build_volume.setHeight(10)
  240. build_volume.setDepth(10)
  241. mocked_global_stack = MagicMock()
  242. build_volume._global_container_stack = mocked_global_stack
  243. build_volume.getEdgeDisallowedSize = MagicMock(return_value = 0)
  244. build_volume.updateNodeBoundaryCheck = MagicMock()
  245. # Fake the the "engine is created callback"
  246. build_volume._onEngineCreated()
  247. build_volume.rebuild()
  248. bounding_box = build_volume.getBoundingBox()
  249. assert bounding_box.minimum == Vector(-5.0, -1.0, -5.0)
  250. assert bounding_box.maximum == Vector(5.0, 10.0, 5.0)
  251. class TestUpdateMachineSizeProperties:
  252. setting_property_dict = {"machine_width": {"value": 50},
  253. "machine_depth": {"value": 100},
  254. "machine_height": {"value": 200},
  255. "machine_shape": {"value": "DERP!"}}
  256. def getPropertySideEffect(*args, **kwargs):
  257. properties = TestUpdateMachineSizeProperties.setting_property_dict.get(args[1])
  258. if properties:
  259. return properties.get(args[2])
  260. def test_noGlobalStack(self, build_volume: BuildVolume):
  261. build_volume._updateMachineSizeProperties()
  262. assert build_volume._width == 0
  263. assert build_volume._height == 0
  264. assert build_volume._depth == 0
  265. assert build_volume._shape == ""
  266. def test_happy(self, build_volume: BuildVolume):
  267. mocked_global_stack = MagicMock(name="mocked_global_stack")
  268. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  269. build_volume._global_container_stack = mocked_global_stack
  270. build_volume._updateMachineSizeProperties()
  271. assert build_volume._width == 50
  272. assert build_volume._height == 200
  273. assert build_volume._depth == 100
  274. assert build_volume._shape == "DERP!"
  275. class TestGetEdgeDisallowedSize:
  276. setting_property_dict = {}
  277. bed_adhesion_size = 1
  278. @pytest.fixture()
  279. def build_volume(self, build_volume):
  280. build_volume._calculateBedAdhesionSize = MagicMock(return_value = 1)
  281. return build_volume
  282. def getPropertySideEffect(*args, **kwargs):
  283. properties = TestGetEdgeDisallowedSize.setting_property_dict.get(args[1])
  284. if properties:
  285. return properties.get(args[2])
  286. def createMockedStack(self):
  287. mocked_global_stack = MagicMock(name="mocked_global_stack")
  288. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  289. return mocked_global_stack
  290. def test_noGlobalContainer(self, build_volume: BuildVolume):
  291. assert build_volume.getEdgeDisallowedSize() == 0
  292. def test_unknownAdhesion(self, build_volume: BuildVolume):
  293. build_volume._global_container_stack = self.createMockedStack()
  294. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  295. #with pytest.raises(Exception):
  296. # Since we don't have any adhesion set, this should break.
  297. build_volume.getEdgeDisallowedSize()
  298. def test_oneAtATime(self, build_volume: BuildVolume):
  299. build_volume._global_container_stack = self.createMockedStack()
  300. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  301. with patch.dict(self.setting_property_dict, {"print_sequence": {"value": "one_at_a_time"}}):
  302. assert build_volume.getEdgeDisallowedSize() == 0.1