TestBuildVolume.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. "material_shrinkage_percentage": {"value": 100.0},
  47. "material_shrinkage_percentage_xy": {"value": 100.0},
  48. "material_shrinkage_percentage_z": {"value": 100.0},
  49. }
  50. def getPropertySideEffect(*args, **kwargs):
  51. properties = TestCalculateBedAdhesionSize.setting_property_dict.get(args[1])
  52. if properties:
  53. return properties.get(args[2])
  54. def createAndSetGlobalStack(self, build_volume):
  55. mocked_stack = MagicMock(name = "mocked_stack")
  56. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  57. mocked_extruder = MagicMock(name = "mocked_extruder")
  58. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  59. mocked_stack.extruderList = [mocked_extruder]
  60. build_volume._global_container_stack = mocked_stack
  61. def test_noGlobalStack(self, build_volume: BuildVolume):
  62. assert build_volume._calculateBedAdhesionSize([]) is None
  63. @pytest.mark.parametrize("setting_dict, result", [
  64. ({}, 0),
  65. ({"adhesion_type": {"value": "skirt"}}, 0),
  66. ({"adhesion_type": {"value": "raft"}}, 0),
  67. ({"adhesion_type": {"value": "none"}}, 0),
  68. ({"adhesion_type": {"value": "skirt"}, "skirt_line_count": {"value": 2}, "initial_layer_line_width_factor": {"value": 1}, "skirt_brim_line_width": {"value": 2}}, 0.02),
  69. # 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!)
  70. ({"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),
  71. ({"brim_line_count": {"value": 1}, "skirt_brim_line_width": {"value": 2}, "initial_layer_line_width_factor": {"value": 3}}, 0),
  72. ({"brim_line_count": {"value": 2}, "skirt_brim_line_width": {"value": 2}, "initial_layer_line_width_factor": {"value": 3}}, 0.06),
  73. ({"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
  74. ])
  75. def test_singleExtruder(self, build_volume: BuildVolume, setting_dict, result):
  76. self.createAndSetGlobalStack(build_volume)
  77. patched_dictionary = self.setting_property_dict.copy()
  78. patched_dictionary.update(setting_dict)
  79. patched_dictionary.update({
  80. "skirt_brim_extruder_nr": {"value": 0},
  81. "raft_base_extruder_nr": {"value": 0},
  82. "raft_interface_extruder_nr": {"value": 0},
  83. "raft_surface_extruder_nr": {"value": 0}
  84. })
  85. with patch.dict(self.setting_property_dict, patched_dictionary):
  86. assert build_volume._calculateBedAdhesionSize([]) == result
  87. def test_unknownBedAdhesion(self, build_volume: BuildVolume):
  88. self.createAndSetGlobalStack(build_volume)
  89. patched_dictionary = self.setting_property_dict.copy()
  90. patched_dictionary.update({"adhesion_type": {"value": "OMGZOMGBBQ"}})
  91. with patch.dict(self.setting_property_dict, patched_dictionary):
  92. with pytest.raises(Exception):
  93. build_volume._calculateBedAdhesionSize([])
  94. class TestComputeDisallowedAreasStatic:
  95. setting_property_dict = {"machine_disallowed_areas": {"value": [[[-200, 112.5], [ -82, 112.5], [ -84, 102.5], [-115, 102.5]]]},
  96. "machine_width": {"value": 200},
  97. "machine_depth": {"value": 200},
  98. "material_shrinkage_percentage": {"value": 100.0},
  99. "material_shrinkage_percentage_xy": {"value": 100.0},
  100. "material_shrinkage_percentage_z": {"value": 100.0},
  101. }
  102. def getPropertySideEffect(*args, **kwargs):
  103. properties = TestComputeDisallowedAreasStatic.setting_property_dict.get(args[1])
  104. if properties:
  105. return properties.get(args[2])
  106. def test_computeDisallowedAreasStaticNoExtruder(self, build_volume: BuildVolume):
  107. mocked_stack = MagicMock()
  108. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  109. build_volume._global_container_stack = mocked_stack
  110. assert build_volume._computeDisallowedAreasStatic(0, []) == {}
  111. def test_computeDisalowedAreasStaticSingleExtruder(self, build_volume: BuildVolume):
  112. mocked_stack = MagicMock()
  113. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  114. mocked_extruder = MagicMock()
  115. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  116. mocked_extruder.getId = MagicMock(return_value = "zomg")
  117. build_volume._global_container_stack = mocked_stack
  118. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  119. result = build_volume._computeDisallowedAreasStatic(0, [mocked_extruder])
  120. assert result == {"zomg": [Polygon([[-84.0, 102.5], [-115.0, 102.5], [-200.0, 112.5], [-82.0, 112.5]])]}
  121. def test_computeDisalowedAreasMutliExtruder(self, build_volume):
  122. mocked_stack = MagicMock()
  123. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  124. mocked_extruder = MagicMock()
  125. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  126. mocked_extruder.getId = MagicMock(return_value="zomg")
  127. extruder_manager = MagicMock()
  128. extruder_manager.getActiveExtruderStacks = MagicMock(return_value = [mocked_stack])
  129. build_volume._global_container_stack = mocked_stack
  130. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance", MagicMock(return_value = extruder_manager)):
  131. result = build_volume._computeDisallowedAreasStatic(0, [mocked_extruder])
  132. assert result == {"zomg": [Polygon([[-84.0, 102.5], [-115.0, 102.5], [-200.0, 112.5], [-82.0, 112.5]])]}
  133. class TestUpdateRaftThickness:
  134. setting_property_dict = {"raft_base_thickness": {"value": 1},
  135. "raft_interface_thickness": {"value": 1},
  136. "raft_surface_layers": {"value": 1},
  137. "raft_surface_thickness": {"value": 1},
  138. "raft_airgap": {"value": 1},
  139. "layer_0_z_overlap": {"value": 1},
  140. "adhesion_type": {"value": "raft"},
  141. "material_shrinkage_percentage": {"value": 100.0},
  142. "material_shrinkage_percentage_xy": {"value": 100.0},
  143. "material_shrinkage_percentage_z": {"value": 100.0},
  144. }
  145. def getPropertySideEffect(*args, **kwargs):
  146. properties = TestUpdateRaftThickness.setting_property_dict.get(args[1])
  147. if properties:
  148. return properties.get(args[2])
  149. def createMockedStack(self):
  150. mocked_global_stack = MagicMock(name = "mocked_global_stack")
  151. mocked_global_stack.getProperty = MagicMock(side_effect = self.getPropertySideEffect)
  152. extruder_stack = MagicMock()
  153. return mocked_global_stack
  154. def test_simple(self, build_volume: BuildVolume):
  155. build_volume.raftThicknessChanged = MagicMock()
  156. mocked_global_stack = self.createMockedStack()
  157. build_volume._global_container_stack = mocked_global_stack
  158. assert build_volume.getRaftThickness() == 0
  159. build_volume._updateRaftThickness()
  160. assert build_volume.getRaftThickness() == 3
  161. assert build_volume.raftThicknessChanged.emit.call_count == 1
  162. def test_adhesionIsNotRaft(self, build_volume: BuildVolume):
  163. patched_dictionary = self.setting_property_dict.copy()
  164. patched_dictionary["adhesion_type"] = {"value": "not_raft"}
  165. mocked_global_stack = self.createMockedStack()
  166. build_volume._global_container_stack = mocked_global_stack
  167. assert build_volume.getRaftThickness() == 0
  168. with patch.dict(self.setting_property_dict, patched_dictionary):
  169. build_volume._updateRaftThickness()
  170. assert build_volume.getRaftThickness() == 0
  171. def test_noGlobalStack(self, build_volume: BuildVolume):
  172. build_volume.raftThicknessChanged = MagicMock()
  173. assert build_volume.getRaftThickness() == 0
  174. build_volume._updateRaftThickness()
  175. assert build_volume.getRaftThickness() == 0
  176. assert build_volume.raftThicknessChanged.emit.call_count == 0
  177. class TestComputeDisallowedAreasPrimeBlob:
  178. setting_property_dict = {"machine_width": {"value": 50},
  179. "machine_depth": {"value": 100},
  180. "prime_blob_enable": {"value": True},
  181. "extruder_prime_pos_x": {"value": 25},
  182. "extruder_prime_pos_y": {"value": 50},
  183. "machine_center_is_zero": {"value": True},
  184. "material_shrinkage_percentage": {"value": 100.0},
  185. "material_shrinkage_percentage_xy": {"value": 100.0},
  186. "material_shrinkage_percentage_z": {"value": 100.0},
  187. }
  188. def getPropertySideEffect(*args, **kwargs):
  189. properties = TestComputeDisallowedAreasPrimeBlob.setting_property_dict.get(args[1])
  190. if properties:
  191. return properties.get(args[2])
  192. def test_noGlobalContainer(self, build_volume: BuildVolume):
  193. # No global container and no extruders, so we expect no blob areas
  194. assert build_volume._computeDisallowedAreasPrimeBlob(12, []) == {}
  195. def test_noExtruders(self, build_volume: BuildVolume):
  196. mocked_stack = MagicMock()
  197. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  198. build_volume._global_container_stack = mocked_stack
  199. # No extruders, so still expect that we get no area
  200. assert build_volume._computeDisallowedAreasPrimeBlob(12, []) == {}
  201. def test_singleExtruder(self, build_volume: BuildVolume):
  202. mocked_global_stack = MagicMock(name = "mocked_global_stack")
  203. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  204. mocked_extruder_stack = MagicMock(name = "mocked_extruder_stack")
  205. mocked_extruder_stack.getId = MagicMock(return_value = "0")
  206. mocked_extruder_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  207. build_volume._global_container_stack = mocked_global_stack
  208. # Create a polygon that should be the result
  209. resulting_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  210. # Since we want a blob of size 12;
  211. resulting_polygon = resulting_polygon.getMinkowskiHull(Polygon.approximatedCircle(12))
  212. # In the The translation result is 25, -50 (due to the settings used)
  213. resulting_polygon = resulting_polygon.translate(25, -50)
  214. assert build_volume._computeDisallowedAreasPrimeBlob(12, [mocked_extruder_stack]) == {"0": [resulting_polygon]}
  215. class TestCalculateExtraZClearance:
  216. setting_property_dict = {"retraction_hop": {"value": 12},
  217. "retraction_hop_enabled": {"value": True},
  218. "material_shrinkage_percentage": {"value": 100.0},
  219. "material_shrinkage_percentage_xy": {"value": 100.0},
  220. "material_shrinkage_percentage_z": {"value": 100.0},
  221. }
  222. def getPropertySideEffect(*args, **kwargs):
  223. properties = TestCalculateExtraZClearance.setting_property_dict.get(args[1])
  224. if properties:
  225. return properties.get(args[2])
  226. def test_noContainerStack(self, build_volume: BuildVolume):
  227. assert build_volume._calculateExtraZClearance([]) == 0
  228. def test_withRetractionHop(self, build_volume: BuildVolume):
  229. mocked_global_stack = MagicMock(name="mocked_global_stack")
  230. mocked_extruder = MagicMock()
  231. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  232. build_volume._global_container_stack = mocked_global_stack
  233. # It should be 12 because we have the hop enabled and the hop distance is set to 12
  234. assert build_volume._calculateExtraZClearance([mocked_extruder]) == 12
  235. def test_withoutRetractionHop(self, build_volume: BuildVolume):
  236. mocked_global_stack = MagicMock(name="mocked_global_stack")
  237. mocked_extruder = MagicMock()
  238. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  239. build_volume._global_container_stack = mocked_global_stack
  240. patched_dictionary = self.setting_property_dict.copy()
  241. patched_dictionary["retraction_hop_enabled"] = {"value": False}
  242. with patch.dict(self.setting_property_dict, patched_dictionary):
  243. # It should be 12 because we have the hop enabled and the hop distance is set to 12
  244. assert build_volume._calculateExtraZClearance([mocked_extruder]) == 0
  245. class TestRebuild:
  246. setting_property_dict = {
  247. "material_shrinkage_percentage": {"value": 100.0},
  248. "material_shrinkage_percentage_xy": {"value": 100.0},
  249. "material_shrinkage_percentage_z": {"value": 100.0},
  250. }
  251. def getPropertySideEffect(*args, **kwargs):
  252. properties = TestCalculateExtraZClearance.setting_property_dict.get(args[1])
  253. if properties:
  254. return properties.get(args[2])
  255. def test_zeroWidthHeightDepth(self, build_volume: BuildVolume):
  256. build_volume.rebuild()
  257. assert build_volume.getMeshData() is None
  258. def test_engineIsNotRead(self, build_volume: BuildVolume):
  259. build_volume.setWidth(10)
  260. build_volume.setHeight(10)
  261. build_volume.setDepth(10)
  262. build_volume.rebuild()
  263. assert build_volume.getMeshData() is None
  264. def test_noGlobalStack(self, build_volume: BuildVolume):
  265. build_volume.setWidth(10)
  266. build_volume.setHeight(10)
  267. build_volume.setDepth(10)
  268. # Fake the the "engine is created callback"
  269. build_volume._onEngineCreated()
  270. build_volume.rebuild()
  271. assert build_volume.getMeshData() is None
  272. def test_updateBoundingBox(self, build_volume: BuildVolume):
  273. build_volume.setWidth(10)
  274. build_volume.setHeight(10)
  275. build_volume.setDepth(10)
  276. mocked_global_stack = MagicMock()
  277. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  278. build_volume._global_container_stack = mocked_global_stack
  279. build_volume.getEdgeDisallowedSize = MagicMock(return_value = 0)
  280. build_volume.updateNodeBoundaryCheck = MagicMock()
  281. # Fake the the "engine is created callback"
  282. build_volume._onEngineCreated()
  283. build_volume.rebuild()
  284. bounding_box = build_volume.getBoundingBox()
  285. assert bounding_box.minimum == Vector(-5.0, -1.0, -5.0)
  286. assert bounding_box.maximum == Vector(5.0, 10.0, 5.0)
  287. class TestUpdateMachineSizeProperties:
  288. setting_property_dict = {"machine_width": {"value": 50},
  289. "machine_depth": {"value": 100},
  290. "machine_height": {"value": 200},
  291. "machine_shape": {"value": "DERP!"},
  292. "material_shrinkage_percentage": {"value": 100.0},
  293. "material_shrinkage_percentage_xy": {"value": 100.0},
  294. "material_shrinkage_percentage_z": {"value": 100.0},
  295. }
  296. def getPropertySideEffect(*args, **kwargs):
  297. properties = TestUpdateMachineSizeProperties.setting_property_dict.get(args[1])
  298. if properties:
  299. return properties.get(args[2])
  300. def test_noGlobalStack(self, build_volume: BuildVolume):
  301. build_volume._updateMachineSizeProperties()
  302. assert build_volume._width == 0
  303. assert build_volume._height == 0
  304. assert build_volume._depth == 0
  305. assert build_volume._shape == ""
  306. def test_happy(self, build_volume: BuildVolume):
  307. mocked_global_stack = MagicMock(name="mocked_global_stack")
  308. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  309. build_volume._global_container_stack = mocked_global_stack
  310. build_volume._updateMachineSizeProperties()
  311. assert build_volume._width == 50
  312. assert build_volume._height == 200
  313. assert build_volume._depth == 100
  314. assert build_volume._shape == "DERP!"
  315. class TestGetEdgeDisallowedSize:
  316. setting_property_dict = {}
  317. bed_adhesion_size = 1
  318. @pytest.fixture()
  319. def build_volume(self, build_volume):
  320. build_volume._calculateBedAdhesionSize = MagicMock(return_value = 1)
  321. return build_volume
  322. def getPropertySideEffect(*args, **kwargs):
  323. properties = TestGetEdgeDisallowedSize.setting_property_dict.get(args[1])
  324. if properties:
  325. return properties.get(args[2])
  326. def createMockedStack(self):
  327. mocked_global_stack = MagicMock(name="mocked_global_stack")
  328. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  329. return mocked_global_stack
  330. def test_noGlobalContainer(self, build_volume: BuildVolume):
  331. assert build_volume.getEdgeDisallowedSize() == 0
  332. def test_unknownAdhesion(self, build_volume: BuildVolume):
  333. build_volume._global_container_stack = self.createMockedStack()
  334. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  335. #with pytest.raises(Exception):
  336. # Since we don't have any adhesion set, this should break.
  337. build_volume.getEdgeDisallowedSize()
  338. def test_oneAtATime(self, build_volume: BuildVolume):
  339. build_volume._global_container_stack = self.createMockedStack()
  340. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  341. with patch.dict(self.setting_property_dict, {"print_sequence": {"value": "one_at_a_time"}}):
  342. assert build_volume.getEdgeDisallowedSize() == 0.1