TestBuildVolume.py 18 KB

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