TestBuildVolume.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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]])]}
  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. class TestUpdateRaftThickness:
  135. setting_property_dict = {"raft_base_thickness": {"value": 1},
  136. "raft_interface_layers": {"value": 2},
  137. "raft_interface_thickness": {"value": 1},
  138. "raft_surface_layers": {"value": 3},
  139. "raft_surface_thickness": {"value": 1},
  140. "raft_airgap": {"value": 1},
  141. "layer_0_z_overlap": {"value": 1},
  142. "adhesion_type": {"value": "raft"},
  143. "material_shrinkage_percentage": {"value": 100.0},
  144. "material_shrinkage_percentage_xy": {"value": 100.0},
  145. "material_shrinkage_percentage_z": {"value": 100.0},
  146. }
  147. def getPropertySideEffect(*args, **kwargs):
  148. properties = TestUpdateRaftThickness.setting_property_dict.get(args[1])
  149. if properties:
  150. return properties.get(args[2])
  151. def createMockedStack(self):
  152. mocked_global_stack = MagicMock(name = "mocked_global_stack")
  153. mocked_global_stack.getProperty = MagicMock(side_effect = self.getPropertySideEffect)
  154. extruder_stack = MagicMock()
  155. return mocked_global_stack
  156. def test_simple(self, build_volume: BuildVolume):
  157. build_volume.raftThicknessChanged = MagicMock()
  158. mocked_global_stack = self.createMockedStack()
  159. build_volume._global_container_stack = mocked_global_stack
  160. assert build_volume.getRaftThickness() == 0
  161. build_volume._updateRaftThickness()
  162. assert build_volume.getRaftThickness() == 6 # 1 base layer of 1mm, 2 interface layers of 1mm each, 3 surface layer of 1mm.
  163. assert build_volume.raftThicknessChanged.emit.call_count == 1
  164. def test_adhesionIsNotRaft(self, build_volume: BuildVolume):
  165. patched_dictionary = self.setting_property_dict.copy()
  166. patched_dictionary["adhesion_type"] = {"value": "not_raft"}
  167. mocked_global_stack = self.createMockedStack()
  168. build_volume._global_container_stack = mocked_global_stack
  169. assert build_volume.getRaftThickness() == 0
  170. with patch.dict(self.setting_property_dict, patched_dictionary):
  171. build_volume._updateRaftThickness()
  172. assert build_volume.getRaftThickness() == 0
  173. def test_noGlobalStack(self, build_volume: BuildVolume):
  174. build_volume.raftThicknessChanged = MagicMock()
  175. assert build_volume.getRaftThickness() == 0
  176. build_volume._updateRaftThickness()
  177. assert build_volume.getRaftThickness() == 0
  178. assert build_volume.raftThicknessChanged.emit.call_count == 0
  179. class TestComputeDisallowedAreasPrimeBlob:
  180. setting_property_dict = {"machine_width": {"value": 50},
  181. "machine_depth": {"value": 100},
  182. "prime_blob_enable": {"value": True},
  183. "extruder_prime_pos_x": {"value": 25},
  184. "extruder_prime_pos_y": {"value": 50},
  185. "machine_center_is_zero": {"value": True},
  186. "material_shrinkage_percentage": {"value": 100.0},
  187. "material_shrinkage_percentage_xy": {"value": 100.0},
  188. "material_shrinkage_percentage_z": {"value": 100.0},
  189. }
  190. def getPropertySideEffect(*args, **kwargs):
  191. properties = TestComputeDisallowedAreasPrimeBlob.setting_property_dict.get(args[1])
  192. if properties:
  193. return properties.get(args[2])
  194. def test_noGlobalContainer(self, build_volume: BuildVolume):
  195. # No global container and no extruders, so we expect no blob areas
  196. assert build_volume._computeDisallowedAreasPrimeBlob(12, []) == {}
  197. def test_noExtruders(self, build_volume: BuildVolume):
  198. mocked_stack = MagicMock()
  199. mocked_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  200. build_volume._global_container_stack = mocked_stack
  201. # No extruders, so still expect that we get no area
  202. assert build_volume._computeDisallowedAreasPrimeBlob(12, []) == {}
  203. def test_singleExtruder(self, build_volume: BuildVolume):
  204. mocked_global_stack = MagicMock(name = "mocked_global_stack")
  205. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  206. mocked_extruder_stack = MagicMock(name = "mocked_extruder_stack")
  207. mocked_extruder_stack.getId = MagicMock(return_value = "0")
  208. mocked_extruder_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  209. build_volume._global_container_stack = mocked_global_stack
  210. # Create a polygon that should be the result
  211. resulting_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  212. # Since we want a blob of size 12;
  213. resulting_polygon = resulting_polygon.getMinkowskiHull(Polygon.approximatedCircle(12))
  214. # In the The translation result is 25, -50 (due to the settings used)
  215. resulting_polygon = resulting_polygon.translate(25, -50)
  216. assert build_volume._computeDisallowedAreasPrimeBlob(12, [mocked_extruder_stack]) == {"0": [resulting_polygon]}
  217. class TestCalculateExtraZClearance:
  218. setting_property_dict = {"retraction_hop": {"value": 12},
  219. "retraction_hop_enabled": {"value": True},
  220. "material_shrinkage_percentage": {"value": 100.0},
  221. "material_shrinkage_percentage_xy": {"value": 100.0},
  222. "material_shrinkage_percentage_z": {"value": 100.0},
  223. }
  224. def getPropertySideEffect(*args, **kwargs):
  225. properties = TestCalculateExtraZClearance.setting_property_dict.get(args[1])
  226. if properties:
  227. return properties.get(args[2])
  228. def test_noContainerStack(self, build_volume: BuildVolume):
  229. assert build_volume._calculateExtraZClearance([]) == 0
  230. def test_withRetractionHop(self, build_volume: BuildVolume):
  231. mocked_global_stack = MagicMock(name="mocked_global_stack")
  232. mocked_extruder = MagicMock()
  233. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  234. build_volume._global_container_stack = mocked_global_stack
  235. # It should be 12 because we have the hop enabled and the hop distance is set to 12
  236. assert build_volume._calculateExtraZClearance([mocked_extruder]) == 12
  237. def test_withoutRetractionHop(self, build_volume: BuildVolume):
  238. mocked_global_stack = MagicMock(name="mocked_global_stack")
  239. mocked_extruder = MagicMock()
  240. mocked_extruder.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  241. build_volume._global_container_stack = mocked_global_stack
  242. patched_dictionary = self.setting_property_dict.copy()
  243. patched_dictionary["retraction_hop_enabled"] = {"value": False}
  244. with patch.dict(self.setting_property_dict, patched_dictionary):
  245. # It should be 12 because we have the hop enabled and the hop distance is set to 12
  246. assert build_volume._calculateExtraZClearance([mocked_extruder]) == 0
  247. class TestRebuild:
  248. setting_property_dict = {
  249. "material_shrinkage_percentage": {"value": 100.0},
  250. "material_shrinkage_percentage_xy": {"value": 100.0},
  251. "material_shrinkage_percentage_z": {"value": 100.0},
  252. }
  253. def getPropertySideEffect(*args, **kwargs):
  254. properties = TestCalculateExtraZClearance.setting_property_dict.get(args[1])
  255. if properties:
  256. return properties.get(args[2])
  257. def test_zeroWidthHeightDepth(self, build_volume: BuildVolume):
  258. build_volume.rebuild()
  259. assert build_volume.getMeshData() is None
  260. def test_engineIsNotRead(self, build_volume: BuildVolume):
  261. build_volume.setWidth(10)
  262. build_volume.setHeight(10)
  263. build_volume.setDepth(10)
  264. build_volume.rebuild()
  265. assert build_volume.getMeshData() is None
  266. def test_noGlobalStack(self, build_volume: BuildVolume):
  267. build_volume.setWidth(10)
  268. build_volume.setHeight(10)
  269. build_volume.setDepth(10)
  270. # Fake the the "engine is created callback"
  271. build_volume._onEngineCreated()
  272. build_volume.rebuild()
  273. assert build_volume.getMeshData() is None
  274. def test_updateBoundingBox(self, build_volume: BuildVolume):
  275. build_volume.setWidth(10)
  276. build_volume.setHeight(10)
  277. build_volume.setDepth(10)
  278. mocked_global_stack = MagicMock()
  279. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  280. build_volume._global_container_stack = mocked_global_stack
  281. build_volume.getEdgeDisallowedSize = MagicMock(return_value = 0)
  282. build_volume.updateNodeBoundaryCheck = MagicMock()
  283. # Fake the the "engine is created callback"
  284. build_volume._onEngineCreated()
  285. build_volume.rebuild()
  286. bounding_box = build_volume.getBoundingBox()
  287. assert bounding_box.minimum == Vector(-5.0, -1.0, -5.0)
  288. assert bounding_box.maximum == Vector(5.0, 10.0, 5.0)
  289. class TestUpdateMachineSizeProperties:
  290. setting_property_dict = {"machine_width": {"value": 50},
  291. "machine_depth": {"value": 100},
  292. "machine_height": {"value": 200},
  293. "machine_shape": {"value": "DERP!"},
  294. "material_shrinkage_percentage": {"value": 100.0},
  295. "material_shrinkage_percentage_xy": {"value": 100.0},
  296. "material_shrinkage_percentage_z": {"value": 100.0},
  297. }
  298. def getPropertySideEffect(*args, **kwargs):
  299. properties = TestUpdateMachineSizeProperties.setting_property_dict.get(args[1])
  300. if properties:
  301. return properties.get(args[2])
  302. def test_noGlobalStack(self, build_volume: BuildVolume):
  303. build_volume._updateMachineSizeProperties()
  304. assert build_volume._width == 0
  305. assert build_volume._height == 0
  306. assert build_volume._depth == 0
  307. assert build_volume._shape == ""
  308. def test_happy(self, build_volume: BuildVolume):
  309. mocked_global_stack = MagicMock(name="mocked_global_stack")
  310. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  311. build_volume._global_container_stack = mocked_global_stack
  312. build_volume._updateMachineSizeProperties()
  313. assert build_volume._width == 50
  314. assert build_volume._height == 200
  315. assert build_volume._depth == 100
  316. assert build_volume._shape == "DERP!"
  317. class TestGetEdgeDisallowedSize:
  318. setting_property_dict = {}
  319. bed_adhesion_size = 1
  320. @pytest.fixture()
  321. def build_volume(self, build_volume):
  322. build_volume._calculateBedAdhesionSize = MagicMock(return_value = 1)
  323. return build_volume
  324. def getPropertySideEffect(*args, **kwargs):
  325. properties = TestGetEdgeDisallowedSize.setting_property_dict.get(args[1])
  326. if properties:
  327. return properties.get(args[2])
  328. def createMockedStack(self):
  329. mocked_global_stack = MagicMock(name="mocked_global_stack")
  330. mocked_global_stack.getProperty = MagicMock(side_effect=self.getPropertySideEffect)
  331. return mocked_global_stack
  332. def test_noGlobalContainer(self, build_volume: BuildVolume):
  333. assert build_volume.getEdgeDisallowedSize() == 0
  334. def test_unknownAdhesion(self, build_volume: BuildVolume):
  335. build_volume._global_container_stack = self.createMockedStack()
  336. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  337. #with pytest.raises(Exception):
  338. # Since we don't have any adhesion set, this should break.
  339. build_volume.getEdgeDisallowedSize()
  340. def test_oneAtATime(self, build_volume: BuildVolume):
  341. build_volume._global_container_stack = self.createMockedStack()
  342. with patch("cura.Settings.ExtruderManager.ExtruderManager.getInstance"):
  343. with patch.dict(self.setting_property_dict, {"print_sequence": {"value": "one_at_a_time"}}):
  344. assert build_volume.getEdgeDisallowedSize() == 0.1