TestBuildVolume.py 21 KB

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