TestBuildVolume.py 21 KB

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