TestBuildVolume.py 21 KB

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