BuildVolume.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from cura.Settings.ExtruderManager import ExtruderManager
  4. from UM.i18n import i18nCatalog
  5. from UM.Scene.Platform import Platform
  6. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  7. from UM.Scene.SceneNode import SceneNode
  8. from UM.Application import Application
  9. from UM.Resources import Resources
  10. from UM.Mesh.MeshBuilder import MeshBuilder
  11. from UM.Math.Vector import Vector
  12. from UM.Math.Color import Color
  13. from UM.Math.AxisAlignedBox import AxisAlignedBox
  14. from UM.Math.Polygon import Polygon
  15. from UM.Message import Message
  16. from UM.Signal import Signal
  17. from PyQt5.QtCore import QTimer
  18. from UM.View.RenderBatch import RenderBatch
  19. from UM.View.GL.OpenGL import OpenGL
  20. catalog = i18nCatalog("cura")
  21. import numpy
  22. import copy
  23. import UM.Settings.ContainerRegistry
  24. # Setting for clearance around the prime
  25. PRIME_CLEARANCE = 1.5
  26. ## Build volume is a special kind of node that is responsible for rendering the printable area & disallowed areas.
  27. class BuildVolume(SceneNode):
  28. VolumeOutlineColor = Color(12, 169, 227, 255)
  29. XAxisColor = Color(255, 0, 0, 255)
  30. YAxisColor = Color(0, 0, 255, 255)
  31. ZAxisColor = Color(0, 255, 0, 255)
  32. raftThicknessChanged = Signal()
  33. def __init__(self, parent = None):
  34. super().__init__(parent)
  35. self._width = 0
  36. self._height = 0
  37. self._depth = 0
  38. self._shader = None
  39. self._origin_mesh = None
  40. self._origin_line_length = 20
  41. self._origin_line_width = 0.5
  42. self._grid_mesh = None
  43. self._grid_shader = None
  44. self._disallowed_areas = []
  45. self._disallowed_area_mesh = None
  46. self._error_areas = []
  47. self._error_mesh = None
  48. self.setCalculateBoundingBox(False)
  49. self._volume_aabb = None
  50. self._raft_thickness = 0.0
  51. self._adhesion_type = None
  52. self._platform = Platform(self)
  53. self._global_container_stack = None
  54. Application.getInstance().globalContainerStackChanged.connect(self._onStackChanged)
  55. self._onStackChanged()
  56. self._has_errors = False
  57. Application.getInstance().getController().getScene().sceneChanged.connect(self._onSceneChanged)
  58. #Objects loaded at the moment. We are connected to the property changed events of these objects.
  59. self._scene_objects = set()
  60. self._change_timer = QTimer()
  61. self._change_timer.setInterval(100)
  62. self._change_timer.setSingleShot(True)
  63. self._change_timer.timeout.connect(self._onChangeTimerFinished)
  64. self._build_volume_message = Message(catalog.i18nc("@info:status",
  65. "The build volume height has been reduced due to the value of the"
  66. " \"Print Sequence\" setting to prevent the gantry from colliding"
  67. " with printed models."))
  68. # Must be after setting _build_volume_message, apparently that is used in getMachineManager.
  69. # activeQualityChanged is always emitted after setActiveVariant, setActiveMaterial and setActiveQuality.
  70. # Therefore this works.
  71. Application.getInstance().getMachineManager().activeQualityChanged.connect(self._onStackChanged)
  72. # This should also ways work, and it is semantically more correct,
  73. # but it does not update the disallowed areas after material change
  74. Application.getInstance().getMachineManager().activeStackChanged.connect(self._onStackChanged)
  75. def _onSceneChanged(self, source):
  76. if self._global_container_stack:
  77. self._change_timer.start()
  78. def _onChangeTimerFinished(self):
  79. root = Application.getInstance().getController().getScene().getRoot()
  80. new_scene_objects = set(node for node in BreadthFirstIterator(root) if node.getMeshData() and type(node) is SceneNode)
  81. if new_scene_objects != self._scene_objects:
  82. for node in new_scene_objects - self._scene_objects: #Nodes that were added to the scene.
  83. node.decoratorsChanged.connect(self._onNodeDecoratorChanged)
  84. for node in self._scene_objects - new_scene_objects: #Nodes that were removed from the scene.
  85. per_mesh_stack = node.callDecoration("getStack")
  86. if per_mesh_stack:
  87. per_mesh_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  88. active_extruder_changed = node.callDecoration("getActiveExtruderChangedSignal")
  89. if active_extruder_changed is not None:
  90. node.callDecoration("getActiveExtruderChangedSignal").disconnect(self._updateDisallowedAreas)
  91. node.decoratorsChanged.disconnect(self._onNodeDecoratorChanged)
  92. self._scene_objects = new_scene_objects
  93. self._onSettingPropertyChanged("print_sequence", "value") # Create fake event, so right settings are triggered.
  94. ## Updates the listeners that listen for changes in per-mesh stacks.
  95. #
  96. # \param node The node for which the decorators changed.
  97. def _onNodeDecoratorChanged(self, node):
  98. per_mesh_stack = node.callDecoration("getStack")
  99. if per_mesh_stack:
  100. per_mesh_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  101. active_extruder_changed = node.callDecoration("getActiveExtruderChangedSignal")
  102. if active_extruder_changed is not None:
  103. active_extruder_changed.connect(self._updateDisallowedAreas)
  104. self._updateDisallowedAreas()
  105. def setWidth(self, width):
  106. if width: self._width = width
  107. def setHeight(self, height):
  108. if height: self._height = height
  109. def setDepth(self, depth):
  110. if depth: self._depth = depth
  111. def getDisallowedAreas(self):
  112. return self._disallowed_areas
  113. def setDisallowedAreas(self, areas):
  114. self._disallowed_areas = areas
  115. def render(self, renderer):
  116. if not self.getMeshData():
  117. return True
  118. if not self._shader:
  119. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "default.shader"))
  120. self._grid_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "grid.shader"))
  121. renderer.queueNode(self, mode = RenderBatch.RenderMode.Lines)
  122. renderer.queueNode(self, mesh = self._origin_mesh)
  123. renderer.queueNode(self, mesh = self._grid_mesh, shader = self._grid_shader, backface_cull = True)
  124. if self._disallowed_area_mesh:
  125. renderer.queueNode(self, mesh = self._disallowed_area_mesh, shader = self._shader, transparent = True, backface_cull = True, sort = -9)
  126. if self._error_mesh:
  127. renderer.queueNode(self, mesh=self._error_mesh, shader=self._shader, transparent=True,
  128. backface_cull=True, sort=-8)
  129. return True
  130. ## Recalculates the build volume & disallowed areas.
  131. def rebuild(self):
  132. if not self._width or not self._height or not self._depth:
  133. return
  134. min_w = -self._width / 2
  135. max_w = self._width / 2
  136. min_h = 0.0
  137. max_h = self._height
  138. min_d = -self._depth / 2
  139. max_d = self._depth / 2
  140. mb = MeshBuilder()
  141. # Outline 'cube' of the build volume
  142. mb.addLine(Vector(min_w, min_h, min_d), Vector(max_w, min_h, min_d), color = self.VolumeOutlineColor)
  143. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, max_h, min_d), color = self.VolumeOutlineColor)
  144. mb.addLine(Vector(min_w, max_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  145. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  146. mb.addLine(Vector(min_w, min_h, max_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  147. mb.addLine(Vector(min_w, min_h, max_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  148. mb.addLine(Vector(min_w, max_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  149. mb.addLine(Vector(max_w, min_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  150. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, min_h, max_d), color = self.VolumeOutlineColor)
  151. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  152. mb.addLine(Vector(min_w, max_h, min_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  153. mb.addLine(Vector(max_w, max_h, min_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  154. self.setMeshData(mb.build())
  155. mb = MeshBuilder()
  156. # Indication of the machine origin
  157. if self._global_container_stack.getProperty("machine_center_is_zero", "value"):
  158. origin = (Vector(min_w, min_h, min_d) + Vector(max_w, min_h, max_d)) / 2
  159. else:
  160. origin = Vector(min_w, min_h, max_d)
  161. mb.addCube(
  162. width = self._origin_line_length,
  163. height = self._origin_line_width,
  164. depth = self._origin_line_width,
  165. center = origin + Vector(self._origin_line_length / 2, 0, 0),
  166. color = self.XAxisColor
  167. )
  168. mb.addCube(
  169. width = self._origin_line_width,
  170. height = self._origin_line_length,
  171. depth = self._origin_line_width,
  172. center = origin + Vector(0, self._origin_line_length / 2, 0),
  173. color = self.YAxisColor
  174. )
  175. mb.addCube(
  176. width = self._origin_line_width,
  177. height = self._origin_line_width,
  178. depth = self._origin_line_length,
  179. center = origin - Vector(0, 0, self._origin_line_length / 2),
  180. color = self.ZAxisColor
  181. )
  182. self._origin_mesh = mb.build()
  183. mb = MeshBuilder()
  184. mb.addQuad(
  185. Vector(min_w, min_h - 0.2, min_d),
  186. Vector(max_w, min_h - 0.2, min_d),
  187. Vector(max_w, min_h - 0.2, max_d),
  188. Vector(min_w, min_h - 0.2, max_d)
  189. )
  190. for n in range(0, 6):
  191. v = mb.getVertex(n)
  192. mb.setVertexUVCoordinates(n, v[0], v[2])
  193. self._grid_mesh = mb.build()
  194. disallowed_area_height = 0.1
  195. disallowed_area_size = 0
  196. if self._disallowed_areas:
  197. mb = MeshBuilder()
  198. color = Color(0.0, 0.0, 0.0, 0.15)
  199. for polygon in self._disallowed_areas:
  200. points = polygon.getPoints()
  201. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  202. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  203. for point in points:
  204. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height, self._clamp(point[1], min_d, max_d))
  205. mb.addFace(first, previous_point, new_point, color = color)
  206. previous_point = new_point
  207. # Find the largest disallowed area to exclude it from the maximum scale bounds.
  208. # This is a very nasty hack. This pretty much only works for UM machines.
  209. # This disallowed area_size needs a -lot- of rework at some point in the future: TODO
  210. if numpy.min(points[:, 1]) >= 0: # This filters out all areas that have points to the left of the centre. This is done to filter the skirt area.
  211. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  212. else:
  213. size = 0
  214. disallowed_area_size = max(size, disallowed_area_size)
  215. self._disallowed_area_mesh = mb.build()
  216. else:
  217. self._disallowed_area_mesh = None
  218. if self._error_areas:
  219. mb = MeshBuilder()
  220. for error_area in self._error_areas:
  221. color = Color(1.0, 0.0, 0.0, 0.5)
  222. points = error_area.getPoints()
  223. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height,
  224. self._clamp(points[0][1], min_d, max_d))
  225. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height,
  226. self._clamp(points[0][1], min_d, max_d))
  227. for point in points:
  228. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height,
  229. self._clamp(point[1], min_d, max_d))
  230. mb.addFace(first, previous_point, new_point, color=color)
  231. previous_point = new_point
  232. self._error_mesh = mb.build()
  233. else:
  234. self._error_mesh = None
  235. self._volume_aabb = AxisAlignedBox(
  236. minimum = Vector(min_w, min_h - 1.0, min_d),
  237. maximum = Vector(max_w, max_h - self._raft_thickness, max_d))
  238. bed_adhesion_size = self._getEdgeDisallowedSize()
  239. # As this works better for UM machines, we only add the disallowed_area_size for the z direction.
  240. # This is probably wrong in all other cases. TODO!
  241. # The +1 and -1 is added as there is always a bit of extra room required to work properly.
  242. scale_to_max_bounds = AxisAlignedBox(
  243. minimum = Vector(min_w + bed_adhesion_size + 1, min_h, min_d + disallowed_area_size - bed_adhesion_size + 1),
  244. maximum = Vector(max_w - bed_adhesion_size - 1, max_h - self._raft_thickness, max_d - disallowed_area_size + bed_adhesion_size - 1)
  245. )
  246. Application.getInstance().getController().getScene()._maximum_bounds = scale_to_max_bounds
  247. def getBoundingBox(self):
  248. return self._volume_aabb
  249. def getRaftThickness(self):
  250. return self._raft_thickness
  251. def _updateRaftThickness(self):
  252. old_raft_thickness = self._raft_thickness
  253. self._adhesion_type = self._global_container_stack.getProperty("adhesion_type", "value")
  254. self._raft_thickness = 0.0
  255. if self._adhesion_type == "raft":
  256. self._raft_thickness = (
  257. self._global_container_stack.getProperty("raft_base_thickness", "value") +
  258. self._global_container_stack.getProperty("raft_interface_thickness", "value") +
  259. self._global_container_stack.getProperty("raft_surface_layers", "value") *
  260. self._global_container_stack.getProperty("raft_surface_thickness", "value") +
  261. self._global_container_stack.getProperty("raft_airgap", "value"))
  262. # Rounding errors do not matter, we check if raft_thickness has changed at all
  263. if old_raft_thickness != self._raft_thickness:
  264. self.setPosition(Vector(0, -self._raft_thickness, 0), SceneNode.TransformSpace.World)
  265. self.raftThicknessChanged.emit()
  266. ## Update the build volume visualization
  267. def _onStackChanged(self):
  268. if self._global_container_stack:
  269. self._global_container_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  270. extruders = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  271. for extruder in extruders:
  272. extruder.propertyChanged.disconnect(self._onSettingPropertyChanged)
  273. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  274. if self._global_container_stack:
  275. self._global_container_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  276. extruders = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  277. for extruder in extruders:
  278. extruder.propertyChanged.connect(self._onSettingPropertyChanged)
  279. self._width = self._global_container_stack.getProperty("machine_width", "value")
  280. machine_height = self._global_container_stack.getProperty("machine_height", "value")
  281. if self._global_container_stack.getProperty("print_sequence", "value") == "one_at_a_time" and len(self._scene_objects) > 1:
  282. self._height = min(self._global_container_stack.getProperty("gantry_height", "value"), machine_height)
  283. if self._height < machine_height:
  284. self._build_volume_message.show()
  285. else:
  286. self._build_volume_message.hide()
  287. else:
  288. self._height = self._global_container_stack.getProperty("machine_height", "value")
  289. self._build_volume_message.hide()
  290. self._depth = self._global_container_stack.getProperty("machine_depth", "value")
  291. self._updateDisallowedAreas()
  292. self._updateRaftThickness()
  293. self.rebuild()
  294. def _onSettingPropertyChanged(self, setting_key, property_name):
  295. if property_name != "value":
  296. return
  297. rebuild_me = False
  298. if setting_key == "print_sequence":
  299. machine_height = self._global_container_stack.getProperty("machine_height", "value")
  300. if Application.getInstance().getGlobalContainerStack().getProperty("print_sequence", "value") == "one_at_a_time" and len(self._scene_objects) > 1:
  301. self._height = min(self._global_container_stack.getProperty("gantry_height", "value"), machine_height)
  302. if self._height < machine_height:
  303. self._build_volume_message.show()
  304. else:
  305. self._build_volume_message.hide()
  306. else:
  307. self._height = self._global_container_stack.getProperty("machine_height", "value")
  308. self._build_volume_message.hide()
  309. rebuild_me = True
  310. if setting_key in self._skirt_settings or setting_key in self._prime_settings or setting_key in self._tower_settings or setting_key == "print_sequence" or setting_key in self._ooze_shield_settings or setting_key in self._distance_settings or setting_key in self._extruder_settings:
  311. self._updateDisallowedAreas()
  312. rebuild_me = True
  313. if setting_key in self._raft_settings:
  314. self._updateRaftThickness()
  315. rebuild_me = True
  316. if rebuild_me:
  317. self.rebuild()
  318. def hasErrors(self):
  319. return self._has_errors
  320. def _updateDisallowedAreas(self):
  321. if not self._global_container_stack:
  322. return
  323. self._error_areas = []
  324. extruder_manager = ExtruderManager.getInstance()
  325. used_extruders = extruder_manager.getUsedExtruderStacks()
  326. disallowed_border_size = self._getEdgeDisallowedSize()
  327. result_areas = self._computeDisallowedAreasStatic(disallowed_border_size, used_extruders) #Normal machine disallowed areas can always be added.
  328. prime_areas = self._computeDisallowedAreasPrime(disallowed_border_size, used_extruders)
  329. prime_disallowed_areas = self._computeDisallowedAreasStatic(0, used_extruders) #Where the priming is not allowed to happen. This is not added to the result, just for collision checking.
  330. #Check if prime positions intersect with disallowed areas.
  331. for extruder in used_extruders:
  332. extruder_id = extruder.getId()
  333. collision = False
  334. for prime_polygon in prime_areas[extruder_id]:
  335. for disallowed_polygon in prime_disallowed_areas[extruder_id]:
  336. if prime_polygon.intersectsPolygon(disallowed_polygon) is not None:
  337. collision = True
  338. break
  339. if collision:
  340. break
  341. #Also check other prime positions (without additional offset).
  342. for other_extruder_id in prime_areas:
  343. if extruder_id == other_extruder_id: #It is allowed to collide with itself.
  344. continue
  345. for other_prime_polygon in prime_areas[other_extruder_id]:
  346. if prime_polygon.intersectsPolygon(other_prime_polygon):
  347. collision = True
  348. break
  349. if collision:
  350. break
  351. if collision:
  352. break
  353. if not collision:
  354. #Prime areas are valid. Add as normal.
  355. result_areas[extruder_id].extend(prime_areas[extruder_id])
  356. nozzle_disallowed_areas = extruder.getProperty("nozzle_disallowed_areas", "value")
  357. for area in nozzle_disallowed_areas:
  358. polygon = Polygon(numpy.array(area, numpy.float32))
  359. polygon = polygon.getMinkowskiHull(Polygon.approximatedCircle(disallowed_border_size))
  360. result_areas[extruder_id].append(polygon) #Don't perform the offset on these.
  361. # Add prime tower location as disallowed area.
  362. prime_tower_collision = False
  363. prime_tower_areas = self._computeDisallowedAreasPrinted(used_extruders)
  364. for extruder_id in prime_tower_areas:
  365. for prime_tower_area in prime_tower_areas[extruder_id]:
  366. for area in result_areas[extruder_id]:
  367. if prime_tower_area.intersectsPolygon(area) is not None:
  368. prime_tower_collision = True
  369. break
  370. if prime_tower_collision: #Already found a collision.
  371. break
  372. if not prime_tower_collision:
  373. result_areas[extruder_id].extend(prime_tower_areas[extruder_id])
  374. else:
  375. self._error_areas.extend(prime_tower_areas[extruder_id])
  376. self._has_errors = len(self._error_areas) > 0
  377. self._disallowed_areas = []
  378. for extruder_id in result_areas:
  379. self._disallowed_areas.extend(result_areas[extruder_id])
  380. ## Computes the disallowed areas for objects that are printed with print
  381. # features.
  382. #
  383. # This means that the brim, travel avoidance and such will be applied to
  384. # these features.
  385. #
  386. # \return A dictionary with for each used extruder ID the disallowed areas
  387. # where that extruder may not print.
  388. def _computeDisallowedAreasPrinted(self, used_extruders):
  389. result = {}
  390. for extruder in used_extruders:
  391. result[extruder.getId()] = []
  392. #Currently, the only normally printed object is the prime tower.
  393. if ExtruderManager.getInstance().getResolveOrValue("prime_tower_enable") == True:
  394. prime_tower_size = self._global_container_stack.getProperty("prime_tower_size", "value")
  395. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  396. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  397. prime_tower_x = self._global_container_stack.getProperty("prime_tower_position_x", "value") - machine_width / 2 #Offset by half machine_width and _depth to put the origin in the front-left.
  398. prime_tower_y = - self._global_container_stack.getProperty("prime_tower_position_y", "value") + machine_depth / 2
  399. prime_tower_area = Polygon([
  400. [prime_tower_x - prime_tower_size, prime_tower_y - prime_tower_size],
  401. [prime_tower_x, prime_tower_y - prime_tower_size],
  402. [prime_tower_x, prime_tower_y],
  403. [prime_tower_x - prime_tower_size, prime_tower_y],
  404. ])
  405. prime_tower_area = prime_tower_area.getMinkowskiHull(Polygon.approximatedCircle(0))
  406. for extruder in used_extruders:
  407. result[extruder.getId()].append(prime_tower_area) #The prime tower location is the same for each extruder, regardless of offset.
  408. return result
  409. ## Computes the disallowed areas for the prime locations.
  410. #
  411. # These are special because they are not subject to things like brim or
  412. # travel avoidance. They do get a dilute with the border size though
  413. # because they may not intersect with brims and such of other objects.
  414. #
  415. # \param border_size The size with which to offset the disallowed areas
  416. # due to skirt, brim, travel avoid distance, etc.
  417. # \param used_extruders The extruder stacks to generate disallowed areas
  418. # for.
  419. # \return A dictionary with for each used extruder ID the prime areas.
  420. def _computeDisallowedAreasPrime(self, border_size, used_extruders):
  421. result = {}
  422. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  423. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  424. for extruder in used_extruders:
  425. prime_x = extruder.getProperty("extruder_prime_pos_x", "value") - machine_width / 2 #Offset by half machine_width and _depth to put the origin in the front-left.
  426. prime_y = machine_depth / 2 - extruder.getProperty("extruder_prime_pos_y", "value")
  427. prime_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  428. prime_polygon = prime_polygon.translate(prime_x, prime_y)
  429. prime_polygon = prime_polygon.getMinkowskiHull(Polygon.approximatedCircle(border_size))
  430. result[extruder.getId()] = [prime_polygon]
  431. return result
  432. ## Computes the disallowed areas that are statically placed in the machine.
  433. #
  434. # It computes different disallowed areas depending on the offset of the
  435. # extruder. The resulting dictionary will therefore have an entry for each
  436. # extruder that is used.
  437. #
  438. # \param border_size The size with which to offset the disallowed areas
  439. # due to skirt, brim, travel avoid distance, etc.
  440. # \param used_extruders The extruder stacks to generate disallowed areas
  441. # for.
  442. # \return A dictionary with for each used extruder ID the disallowed areas
  443. # where that extruder may not print.
  444. def _computeDisallowedAreasStatic(self, border_size, used_extruders):
  445. #Convert disallowed areas to polygons and dilate them.
  446. machine_disallowed_polygons = []
  447. for area in self._global_container_stack.getProperty("machine_disallowed_areas", "value"):
  448. polygon = Polygon(numpy.array(area, numpy.float32))
  449. polygon = polygon.getMinkowskiHull(Polygon.approximatedCircle(border_size))
  450. machine_disallowed_polygons.append(polygon)
  451. result = {}
  452. for extruder in used_extruders:
  453. extruder_id = extruder.getId()
  454. offset_x = extruder.getProperty("machine_nozzle_offset_x", "value")
  455. if not offset_x:
  456. offset_x = 0
  457. offset_y = extruder.getProperty("machine_nozzle_offset_y", "value")
  458. if not offset_y:
  459. offset_y = 0
  460. result[extruder_id] = []
  461. for polygon in machine_disallowed_polygons:
  462. result[extruder_id].append(polygon.translate(offset_x, offset_y)) #Compensate for the nozzle offset of this extruder.
  463. #Add the border around the edge of the build volume.
  464. left_unreachable_border = 0
  465. right_unreachable_border = 0
  466. top_unreachable_border = 0
  467. bottom_unreachable_border = 0
  468. #The build volume is defined as the union of the area that all extruders can reach, so we need to know the relative offset to all extruders.
  469. for other_extruder in ExtruderManager.getInstance().getActiveExtruderStacks():
  470. other_offset_x = other_extruder.getProperty("machine_nozzle_offset_x", "value")
  471. other_offset_y = other_extruder.getProperty("machine_nozzle_offset_y", "value")
  472. left_unreachable_border = min(left_unreachable_border, other_offset_x - offset_x)
  473. right_unreachable_border = max(right_unreachable_border, other_offset_x - offset_x)
  474. top_unreachable_border = min(top_unreachable_border, other_offset_y - offset_y)
  475. bottom_unreachable_border = max(bottom_unreachable_border, other_offset_y - offset_y)
  476. half_machine_width = self._global_container_stack.getProperty("machine_width", "value") / 2
  477. half_machine_depth = self._global_container_stack.getProperty("machine_depth", "value") / 2
  478. if border_size - left_unreachable_border > 0:
  479. result[extruder_id].append(Polygon(numpy.array([
  480. [-half_machine_width, -half_machine_depth],
  481. [-half_machine_width, half_machine_depth],
  482. [-half_machine_width + border_size - left_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border],
  483. [-half_machine_width + border_size - left_unreachable_border, -half_machine_depth + border_size - top_unreachable_border]
  484. ], numpy.float32)))
  485. if border_size + right_unreachable_border > 0:
  486. result[extruder_id].append(Polygon(numpy.array([
  487. [half_machine_width, half_machine_depth],
  488. [half_machine_width, -half_machine_depth],
  489. [half_machine_width - border_size - right_unreachable_border, -half_machine_depth + border_size - top_unreachable_border],
  490. [half_machine_width - border_size - right_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border]
  491. ], numpy.float32)))
  492. if border_size + bottom_unreachable_border > 0:
  493. result[extruder_id].append(Polygon(numpy.array([
  494. [-half_machine_width, half_machine_depth],
  495. [half_machine_width, half_machine_depth],
  496. [half_machine_width - border_size - right_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border],
  497. [-half_machine_width + border_size - left_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border]
  498. ], numpy.float32)))
  499. if border_size - top_unreachable_border > 0:
  500. result[extruder_id].append(Polygon(numpy.array([
  501. [half_machine_width, -half_machine_depth],
  502. [-half_machine_width, -half_machine_depth],
  503. [-half_machine_width + border_size - left_unreachable_border, -half_machine_depth + border_size - top_unreachable_border],
  504. [half_machine_width - border_size - right_unreachable_border, -half_machine_depth + border_size - top_unreachable_border]
  505. ], numpy.float32)))
  506. return result
  507. ## Private convenience function to get a setting from the adhesion
  508. # extruder.
  509. #
  510. # \param setting_key The key of the setting to get.
  511. # \param property The property to get from the setting.
  512. # \return The property of the specified setting in the adhesion extruder.
  513. def _getSettingFromAdhesionExtruder(self, setting_key, property = "value"):
  514. return self._getSettingFromExtruder(setting_key, "adhesion_extruder_nr", property)
  515. ## Private convenience function to get a setting from every extruder.
  516. #
  517. # For single extrusion machines, this gets the setting from the global
  518. # stack.
  519. #
  520. # \return A sequence of setting values, one for each extruder.
  521. def _getSettingFromAllExtruders(self, setting_key, property = "value"):
  522. return ExtruderManager.getInstance().getAllExtruderSettings(setting_key, property)
  523. ## Private convenience function to get a setting from the support infill
  524. # extruder.
  525. #
  526. # \param setting_key The key of the setting to get.
  527. # \param property The property to get from the setting.
  528. # \return The property of the specified setting in the support infill
  529. # extruder.
  530. def _getSettingFromSupportInfillExtruder(self, setting_key, property = "value"):
  531. return self._getSettingFromExtruder(setting_key, "support_infill_extruder_nr", property)
  532. ## Helper function to get a setting from an extruder specified in another
  533. # setting.
  534. #
  535. # \param setting_key The key of the setting to get.
  536. # \param extruder_setting_key The key of the setting that specifies from
  537. # which extruder to get the setting, if there are multiple extruders.
  538. # \param property The property to get from the setting.
  539. # \return The property of the specified setting in the specified extruder.
  540. def _getSettingFromExtruder(self, setting_key, extruder_setting_key, property = "value"):
  541. multi_extrusion = self._global_container_stack.getProperty("machine_extruder_count", "value") > 1
  542. if not multi_extrusion:
  543. return self._global_container_stack.getProperty(setting_key, property)
  544. extruder_index = self._global_container_stack.getProperty(extruder_setting_key, "value")
  545. if extruder_index == "-1": # If extruder index is -1 use global instead
  546. return self._global_container_stack.getProperty(setting_key, property)
  547. extruder_stack_id = ExtruderManager.getInstance().extruderIds[str(extruder_index)]
  548. stack = UM.Settings.ContainerRegistry.getInstance().findContainerStacks(id = extruder_stack_id)[0]
  549. return stack.getProperty(setting_key, property)
  550. ## Convenience function to calculate the disallowed radius around the edge.
  551. #
  552. # This disallowed radius is to allow for space around the models that is
  553. # not part of the collision radius, such as bed adhesion (skirt/brim/raft)
  554. # and travel avoid distance.
  555. def _getEdgeDisallowedSize(self):
  556. if not self._global_container_stack:
  557. return 0
  558. container_stack = self._global_container_stack
  559. # If we are printing one at a time, we need to add the bed adhesion size to the disallowed areas of the objects
  560. if container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  561. return 0.1 # Return a very small value, so we do draw disallowed area's near the edges.
  562. adhesion_type = container_stack.getProperty("adhesion_type", "value")
  563. if adhesion_type == "skirt":
  564. skirt_distance = self._getSettingFromAdhesionExtruder("skirt_gap")
  565. skirt_line_count = self._getSettingFromAdhesionExtruder("skirt_line_count")
  566. bed_adhesion_size = skirt_distance + (skirt_line_count * self._getSettingFromAdhesionExtruder("skirt_brim_line_width"))
  567. if self._global_container_stack.getProperty("machine_extruder_count", "value") > 1:
  568. adhesion_extruder_nr = int(self._global_container_stack.getProperty("adhesion_extruder_nr", "value"))
  569. extruder_values = ExtruderManager.getInstance().getAllExtruderValues("skirt_brim_line_width")
  570. del extruder_values[adhesion_extruder_nr] # Remove the value of the adhesion extruder nr.
  571. for value in extruder_values:
  572. bed_adhesion_size += value
  573. elif adhesion_type == "brim":
  574. bed_adhesion_size = self._getSettingFromAdhesionExtruder("brim_line_count") * self._getSettingFromAdhesionExtruder("skirt_brim_line_width")
  575. if self._global_container_stack.getProperty("machine_extruder_count", "value") > 1:
  576. adhesion_extruder_nr = int(self._global_container_stack.getProperty("adhesion_extruder_nr", "value"))
  577. extruder_values = ExtruderManager.getInstance().getAllExtruderValues("skirt_brim_line_width")
  578. del extruder_values[adhesion_extruder_nr] # Remove the value of the adhesion extruder nr.
  579. for value in extruder_values:
  580. bed_adhesion_size += value
  581. elif adhesion_type == "raft":
  582. bed_adhesion_size = self._getSettingFromAdhesionExtruder("raft_margin")
  583. else:
  584. raise Exception("Unknown bed adhesion type. Did you forget to update the build volume calculations for your new bed adhesion type?")
  585. support_expansion = 0
  586. if self._getSettingFromSupportInfillExtruder("support_offset") and self._global_container_stack.getProperty("support_enable", "value"):
  587. support_expansion += self._getSettingFromSupportInfillExtruder("support_offset")
  588. farthest_shield_distance = 0
  589. if container_stack.getProperty("draft_shield_enabled", "value"):
  590. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("draft_shield_dist", "value"))
  591. if container_stack.getProperty("ooze_shield_enabled", "value"):
  592. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("ooze_shield_dist", "value"))
  593. move_from_wall_radius = 0 # Moves that start from outer wall.
  594. move_from_wall_radius = max(move_from_wall_radius, max(self._getSettingFromAllExtruders("infill_wipe_dist")))
  595. avoid_enabled_per_extruder = self._getSettingFromAllExtruders(("travel_avoid_other_parts"))
  596. avoid_distance_per_extruder = self._getSettingFromAllExtruders("travel_avoid_distance")
  597. for index, avoid_other_parts_enabled in enumerate(avoid_enabled_per_extruder): #For each extruder (or just global).
  598. if avoid_other_parts_enabled:
  599. move_from_wall_radius = max(move_from_wall_radius, avoid_distance_per_extruder[index]) #Index of the same extruder.
  600. #Now combine our different pieces of data to get the final border size.
  601. #Support expansion is added to the bed adhesion, since the bed adhesion goes around support.
  602. #Support expansion is added to farthest shield distance, since the shields go around support.
  603. border_size = max(move_from_wall_radius, support_expansion + farthest_shield_distance, support_expansion + bed_adhesion_size)
  604. return border_size
  605. def _clamp(self, value, min_value, max_value):
  606. return max(min(value, max_value), min_value)
  607. _skirt_settings = ["adhesion_type", "skirt_gap", "skirt_line_count", "skirt_brim_line_width", "brim_width", "brim_line_count", "raft_margin", "draft_shield_enabled", "draft_shield_dist"]
  608. _raft_settings = ["adhesion_type", "raft_base_thickness", "raft_interface_thickness", "raft_surface_layers", "raft_surface_thickness", "raft_airgap"]
  609. _prime_settings = ["extruder_prime_pos_x", "extruder_prime_pos_y", "extruder_prime_pos_z"]
  610. _tower_settings = ["prime_tower_enable", "prime_tower_size", "prime_tower_position_x", "prime_tower_position_y"]
  611. _ooze_shield_settings = ["ooze_shield_enabled", "ooze_shield_dist"]
  612. _distance_settings = ["infill_wipe_dist", "travel_avoid_distance", "support_offset", "support_enable", "travel_avoid_other_parts"]
  613. _extruder_settings = ["support_enable", "support_interface_enable", "support_infill_extruder_nr", "support_extruder_nr_layer_0", "support_interface_extruder_nr", "brim_line_count", "adhesion_extruder_nr", "adhesion_type"] #Settings that can affect which extruders are used.