BuildVolume.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. # Number of objects loaded at the moment.
  59. self._number_of_objects = 0
  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_number_of_objects = len([node for node in BreadthFirstIterator(root) if node.getMeshData() and type(node) is SceneNode])
  81. if new_number_of_objects != self._number_of_objects:
  82. recalculate = False
  83. if self._global_container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  84. recalculate = (new_number_of_objects < 2 and self._number_of_objects > 1) or (new_number_of_objects > 1 and self._number_of_objects < 2)
  85. self._number_of_objects = new_number_of_objects
  86. if recalculate:
  87. self._onSettingPropertyChanged("print_sequence", "value") # Create fake event, so right settings are triggered.
  88. def setWidth(self, width):
  89. if width: self._width = width
  90. def setHeight(self, height):
  91. if height: self._height = height
  92. def setDepth(self, depth):
  93. if depth: self._depth = depth
  94. def getDisallowedAreas(self):
  95. return self._disallowed_areas
  96. def setDisallowedAreas(self, areas):
  97. self._disallowed_areas = areas
  98. def render(self, renderer):
  99. if not self.getMeshData():
  100. return True
  101. if not self._shader:
  102. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "default.shader"))
  103. self._grid_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "grid.shader"))
  104. renderer.queueNode(self, mode = RenderBatch.RenderMode.Lines)
  105. renderer.queueNode(self, mesh = self._origin_mesh)
  106. renderer.queueNode(self, mesh = self._grid_mesh, shader = self._grid_shader, backface_cull = True)
  107. if self._disallowed_area_mesh:
  108. renderer.queueNode(self, mesh = self._disallowed_area_mesh, shader = self._shader, transparent = True, backface_cull = True, sort = -9)
  109. if self._error_mesh:
  110. renderer.queueNode(self, mesh=self._error_mesh, shader=self._shader, transparent=True,
  111. backface_cull=True, sort=-8)
  112. return True
  113. ## Recalculates the build volume & disallowed areas.
  114. def rebuild(self):
  115. if not self._width or not self._height or not self._depth:
  116. return
  117. min_w = -self._width / 2
  118. max_w = self._width / 2
  119. min_h = 0.0
  120. max_h = self._height
  121. min_d = -self._depth / 2
  122. max_d = self._depth / 2
  123. mb = MeshBuilder()
  124. # Outline 'cube' of the build volume
  125. mb.addLine(Vector(min_w, min_h, min_d), Vector(max_w, min_h, min_d), color = self.VolumeOutlineColor)
  126. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, max_h, min_d), color = self.VolumeOutlineColor)
  127. mb.addLine(Vector(min_w, max_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  128. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  129. mb.addLine(Vector(min_w, min_h, max_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  130. mb.addLine(Vector(min_w, min_h, max_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  131. mb.addLine(Vector(min_w, max_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  132. mb.addLine(Vector(max_w, min_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  133. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, min_h, max_d), color = self.VolumeOutlineColor)
  134. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  135. mb.addLine(Vector(min_w, max_h, min_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  136. mb.addLine(Vector(max_w, max_h, min_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  137. self.setMeshData(mb.build())
  138. mb = MeshBuilder()
  139. # Indication of the machine origin
  140. if self._global_container_stack.getProperty("machine_center_is_zero", "value"):
  141. origin = (Vector(min_w, min_h, min_d) + Vector(max_w, min_h, max_d)) / 2
  142. else:
  143. origin = Vector(min_w, min_h, max_d)
  144. mb.addCube(
  145. width = self._origin_line_length,
  146. height = self._origin_line_width,
  147. depth = self._origin_line_width,
  148. center = origin + Vector(self._origin_line_length / 2, 0, 0),
  149. color = self.XAxisColor
  150. )
  151. mb.addCube(
  152. width = self._origin_line_width,
  153. height = self._origin_line_length,
  154. depth = self._origin_line_width,
  155. center = origin + Vector(0, self._origin_line_length / 2, 0),
  156. color = self.YAxisColor
  157. )
  158. mb.addCube(
  159. width = self._origin_line_width,
  160. height = self._origin_line_width,
  161. depth = self._origin_line_length,
  162. center = origin - Vector(0, 0, self._origin_line_length / 2),
  163. color = self.ZAxisColor
  164. )
  165. self._origin_mesh = mb.build()
  166. mb = MeshBuilder()
  167. mb.addQuad(
  168. Vector(min_w, min_h - 0.2, min_d),
  169. Vector(max_w, min_h - 0.2, min_d),
  170. Vector(max_w, min_h - 0.2, max_d),
  171. Vector(min_w, min_h - 0.2, max_d)
  172. )
  173. for n in range(0, 6):
  174. v = mb.getVertex(n)
  175. mb.setVertexUVCoordinates(n, v[0], v[2])
  176. self._grid_mesh = mb.build()
  177. disallowed_area_height = 0.1
  178. disallowed_area_size = 0
  179. if self._disallowed_areas:
  180. mb = MeshBuilder()
  181. color = Color(0.0, 0.0, 0.0, 0.15)
  182. for polygon in self._disallowed_areas:
  183. points = polygon.getPoints()
  184. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  185. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  186. for point in points:
  187. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height, self._clamp(point[1], min_d, max_d))
  188. mb.addFace(first, previous_point, new_point, color = color)
  189. previous_point = new_point
  190. # Find the largest disallowed area to exclude it from the maximum scale bounds.
  191. # This is a very nasty hack. This pretty much only works for UM machines.
  192. # This disallowed area_size needs a -lot- of rework at some point in the future: TODO
  193. 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.
  194. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  195. else:
  196. size = 0
  197. disallowed_area_size = max(size, disallowed_area_size)
  198. self._disallowed_area_mesh = mb.build()
  199. else:
  200. self._disallowed_area_mesh = None
  201. if self._error_areas:
  202. mb = MeshBuilder()
  203. for error_area in self._error_areas:
  204. color = Color(1.0, 0.0, 0.0, 0.5)
  205. points = error_area.getPoints()
  206. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height,
  207. self._clamp(points[0][1], min_d, max_d))
  208. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height,
  209. self._clamp(points[0][1], min_d, max_d))
  210. for point in points:
  211. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height,
  212. self._clamp(point[1], min_d, max_d))
  213. mb.addFace(first, previous_point, new_point, color=color)
  214. previous_point = new_point
  215. self._error_mesh = mb.build()
  216. else:
  217. self._error_mesh = None
  218. self._volume_aabb = AxisAlignedBox(
  219. minimum = Vector(min_w, min_h - 1.0, min_d),
  220. maximum = Vector(max_w, max_h - self._raft_thickness, max_d))
  221. bed_adhesion_size = self._getEdgeDisallowedSize()
  222. # As this works better for UM machines, we only add the disallowed_area_size for the z direction.
  223. # This is probably wrong in all other cases. TODO!
  224. # The +1 and -1 is added as there is always a bit of extra room required to work properly.
  225. scale_to_max_bounds = AxisAlignedBox(
  226. minimum = Vector(min_w + bed_adhesion_size + 1, min_h, min_d + disallowed_area_size - bed_adhesion_size + 1),
  227. maximum = Vector(max_w - bed_adhesion_size - 1, max_h - self._raft_thickness, max_d - disallowed_area_size + bed_adhesion_size - 1)
  228. )
  229. Application.getInstance().getController().getScene()._maximum_bounds = scale_to_max_bounds
  230. def getBoundingBox(self):
  231. return self._volume_aabb
  232. def getRaftThickness(self):
  233. return self._raft_thickness
  234. def _updateRaftThickness(self):
  235. old_raft_thickness = self._raft_thickness
  236. self._adhesion_type = self._global_container_stack.getProperty("adhesion_type", "value")
  237. self._raft_thickness = 0.0
  238. if self._adhesion_type == "raft":
  239. self._raft_thickness = (
  240. self._global_container_stack.getProperty("raft_base_thickness", "value") +
  241. self._global_container_stack.getProperty("raft_interface_thickness", "value") +
  242. self._global_container_stack.getProperty("raft_surface_layers", "value") *
  243. self._global_container_stack.getProperty("raft_surface_thickness", "value") +
  244. self._global_container_stack.getProperty("raft_airgap", "value"))
  245. # Rounding errors do not matter, we check if raft_thickness has changed at all
  246. if old_raft_thickness != self._raft_thickness:
  247. self.setPosition(Vector(0, -self._raft_thickness, 0), SceneNode.TransformSpace.World)
  248. self.raftThicknessChanged.emit()
  249. ## Update the build volume visualization
  250. def _onStackChanged(self):
  251. if self._global_container_stack:
  252. self._global_container_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  253. extruders = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  254. for extruder in extruders:
  255. extruder.propertyChanged.disconnect(self._onSettingPropertyChanged)
  256. self._global_container_stack = Application.getInstance().getGlobalContainerStack()
  257. if self._global_container_stack:
  258. self._global_container_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  259. extruders = ExtruderManager.getInstance().getMachineExtruders(self._global_container_stack.getId())
  260. for extruder in extruders:
  261. extruder.propertyChanged.connect(self._onSettingPropertyChanged)
  262. self._width = self._global_container_stack.getProperty("machine_width", "value")
  263. machine_height = self._global_container_stack.getProperty("machine_height", "value")
  264. if self._global_container_stack.getProperty("print_sequence", "value") == "one_at_a_time" and self._number_of_objects > 1:
  265. self._height = min(self._global_container_stack.getProperty("gantry_height", "value"), machine_height)
  266. if self._height < machine_height:
  267. self._build_volume_message.show()
  268. else:
  269. self._build_volume_message.hide()
  270. else:
  271. self._height = self._global_container_stack.getProperty("machine_height", "value")
  272. self._build_volume_message.hide()
  273. self._depth = self._global_container_stack.getProperty("machine_depth", "value")
  274. self._updateDisallowedAreas()
  275. self._updateRaftThickness()
  276. self.rebuild()
  277. def _onSettingPropertyChanged(self, setting_key, property_name):
  278. if property_name != "value":
  279. return
  280. rebuild_me = False
  281. if setting_key == "print_sequence":
  282. machine_height = self._global_container_stack.getProperty("machine_height", "value")
  283. if Application.getInstance().getGlobalContainerStack().getProperty("print_sequence", "value") == "one_at_a_time" and self._number_of_objects > 1:
  284. self._height = min(self._global_container_stack.getProperty("gantry_height", "value"), machine_height)
  285. if self._height < machine_height:
  286. self._build_volume_message.show()
  287. else:
  288. self._build_volume_message.hide()
  289. else:
  290. self._height = self._global_container_stack.getProperty("machine_height", "value")
  291. self._build_volume_message.hide()
  292. rebuild_me = True
  293. 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:
  294. self._updateDisallowedAreas()
  295. rebuild_me = True
  296. if setting_key in self._raft_settings:
  297. self._updateRaftThickness()
  298. rebuild_me = True
  299. if rebuild_me:
  300. self.rebuild()
  301. def hasErrors(self):
  302. return self._has_errors
  303. def _updateDisallowedAreas(self):
  304. if not self._global_container_stack:
  305. return
  306. self._error_areas = []
  307. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  308. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  309. disallowed_polygons = []
  310. # Check if prime positions intersect with disallowed areas
  311. for area in self._global_container_stack.getProperty("machine_disallowed_areas", "value"):
  312. poly = Polygon(numpy.array(area, numpy.float32))
  313. # Minkowski with zero, to ensure that the polygon is correct & watertight.
  314. poly = poly.getMinkowskiHull(Polygon.approximatedCircle(0))
  315. disallowed_polygons.append(poly)
  316. if disallowed_polygons:
  317. extruder_manager = ExtruderManager.getInstance()
  318. extruders = extruder_manager.getMachineExtruders(self._global_container_stack.getId())
  319. prime_polygons = []
  320. # Each extruder has it's own prime location
  321. for extruder in extruders:
  322. prime_x = extruder.getProperty("extruder_prime_pos_x", "value") - machine_width / 2
  323. prime_y = machine_depth / 2 - extruder.getProperty("extruder_prime_pos_y", "value")
  324. prime_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  325. prime_polygon = prime_polygon.translate(prime_x, prime_y)
  326. collision = False
  327. # Check if prime polygon is intersecting with any of the other disallowed areas.
  328. # Note that we check the prime area without bed adhesion.
  329. for poly in disallowed_polygons:
  330. if prime_polygon.intersectsPolygon(poly) is not None:
  331. collision = True
  332. break
  333. # Also collide with other prime positions
  334. for poly in prime_polygons:
  335. if prime_polygon.intersectsPolygon(poly) is not None:
  336. collision = True
  337. break
  338. if not collision:
  339. # Prime area is valid. Add as normal.
  340. # Once it's added like this, it will recieve a bed adhesion offset, just like the others.
  341. prime_polygons.append(prime_polygon)
  342. else:
  343. self._error_areas.append(prime_polygon)
  344. disallowed_polygons.extend(prime_polygons)
  345. # Extend every area already in the disallowed_areas with the skirt size.
  346. result_areas = self._computeDisallowedAreasStatic()
  347. # Add prime tower location as disallowed area.
  348. prime_tower_collision = False
  349. prime_tower_areas = self._computeDisallowedAreasPrinted()
  350. for prime_tower_area in prime_tower_areas:
  351. for area in result_areas:
  352. if prime_tower_area.intersectsPolygon(area) is not None:
  353. prime_tower_collision = True
  354. break
  355. if prime_tower_collision: #Already found a collision.
  356. break
  357. if not prime_tower_collision:
  358. result_areas.extend(prime_tower_areas)
  359. else:
  360. self._error_areas.extend(prime_tower_areas)
  361. self._has_errors = len(self._error_areas) > 0
  362. self._disallowed_areas = result_areas
  363. ## Computes the disallowed areas for objects that are printed.
  364. #
  365. # These disallowed areas are not offset with the negative of the nozzle
  366. # offset, since the engine already performs the offset for us to make sure
  367. # they are printed in head-coordinates instead of nozzle-coordinates.
  368. #
  369. # \return A list of polygons that represent the disallowed areas.
  370. def _computeDisallowedAreasPrinted(self):
  371. result = []
  372. #Currently, the only normally printed object is the prime tower.
  373. if ExtruderManager.getInstance().getResolveOrValue("prime_tower_enable") == True:
  374. prime_tower_size = self._global_container_stack.getProperty("prime_tower_size", "value")
  375. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  376. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  377. prime_tower_x = self._global_container_stack.getProperty("prime_tower_position_x", "value") - machine_width / 2
  378. prime_tower_y = - self._global_container_stack.getProperty("prime_tower_position_y", "value") + machine_depth / 2
  379. prime_tower_area = Polygon([
  380. [prime_tower_x - prime_tower_size, prime_tower_y - prime_tower_size],
  381. [prime_tower_x, prime_tower_y - prime_tower_size],
  382. [prime_tower_x, prime_tower_y],
  383. [prime_tower_x - prime_tower_size, prime_tower_y],
  384. ])
  385. result.append(prime_tower_area)
  386. return result
  387. ## Computes the disallowed areas that are statically placed in the machine.
  388. #
  389. # These disallowed areas need to be offset with the negative of the nozzle
  390. # offset to check if the disallowed areas are intersected.
  391. #
  392. # \return A list of polygons that represent the disallowed areas. These
  393. # areas are not offset with any nozzle offset yet.
  394. def _computeDisallowedAreasStatic(self):
  395. result = []
  396. if not self._global_container_stack:
  397. return result
  398. disallowed_border_size = self._getEdgeDisallowedSize()
  399. machine_disallowed_areas = copy.deepcopy(self._global_container_stack.getProperty("machine_disallowed_areas", "value"))
  400. if machine_disallowed_areas:
  401. for area in machine_disallowed_areas:
  402. polygon = Polygon(numpy.array(area, numpy.float32))
  403. polygon = polygon.getMinkowskiHull(Polygon.approximatedCircle(disallowed_border_size))
  404. result.append(polygon)
  405. #Add the border around the edge of the build volume.
  406. if disallowed_border_size == 0:
  407. return result #No need to add this border.
  408. half_machine_width = self._global_container_stack.getProperty("machine_width", "value") / 2
  409. half_machine_depth = self._global_container_stack.getProperty("machine_depth", "value") / 2
  410. result.append(Polygon(numpy.array([
  411. [-half_machine_width, -half_machine_depth],
  412. [-half_machine_width, half_machine_depth],
  413. [-half_machine_width + disallowed_border_size, half_machine_depth - disallowed_border_size],
  414. [-half_machine_width + disallowed_border_size, -half_machine_depth + disallowed_border_size]
  415. ], numpy.float32)))
  416. result.append(Polygon(numpy.array([
  417. [half_machine_width, half_machine_depth],
  418. [half_machine_width, -half_machine_depth],
  419. [half_machine_width - disallowed_border_size, -half_machine_depth + disallowed_border_size],
  420. [half_machine_width - disallowed_border_size, half_machine_depth - disallowed_border_size]
  421. ], numpy.float32)))
  422. result.append(Polygon(numpy.array([
  423. [-half_machine_width, half_machine_depth],
  424. [half_machine_width, half_machine_depth],
  425. [half_machine_width - disallowed_border_size, half_machine_depth - disallowed_border_size],
  426. [-half_machine_width + disallowed_border_size, half_machine_depth - disallowed_border_size]
  427. ], numpy.float32)))
  428. result.append(Polygon(numpy.array([
  429. [half_machine_width, -half_machine_depth],
  430. [-half_machine_width, -half_machine_depth],
  431. [-half_machine_width + disallowed_border_size, -half_machine_depth + disallowed_border_size],
  432. [half_machine_width - disallowed_border_size, -half_machine_depth + disallowed_border_size]
  433. ], numpy.float32)))
  434. return result
  435. ## Private convenience function to get a setting from the adhesion
  436. # extruder.
  437. #
  438. # \param setting_key The key of the setting to get.
  439. # \param property The property to get from the setting.
  440. # \return The property of the specified setting in the adhesion extruder.
  441. def _getSettingFromAdhesionExtruder(self, setting_key, property = "value"):
  442. return self._getSettingFromExtruder(setting_key, "adhesion_extruder_nr", property)
  443. ## Private convenience function to get a setting from every extruder.
  444. #
  445. # For single extrusion machines, this gets the setting from the global
  446. # stack.
  447. #
  448. # \return A sequence of setting values, one for each extruder.
  449. def _getSettingFromAllExtruders(self, setting_key, property = "value"):
  450. return ExtruderManager.getInstance().getAllExtruderSettings(setting_key, property)
  451. ## Private convenience function to get a setting from the support infill
  452. # extruder.
  453. #
  454. # \param setting_key The key of the setting to get.
  455. # \param property The property to get from the setting.
  456. # \return The property of the specified setting in the support infill
  457. # extruder.
  458. def _getSettingFromSupportInfillExtruder(self, setting_key, property = "value"):
  459. return self._getSettingFromExtruder(setting_key, "support_infill_extruder_nr", property)
  460. ## Helper function to get a setting from an extruder specified in another
  461. # setting.
  462. #
  463. # \param setting_key The key of the setting to get.
  464. # \param extruder_setting_key The key of the setting that specifies from
  465. # which extruder to get the setting, if there are multiple extruders.
  466. # \param property The property to get from the setting.
  467. # \return The property of the specified setting in the specified extruder.
  468. def _getSettingFromExtruder(self, setting_key, extruder_setting_key, property = "value"):
  469. multi_extrusion = self._global_container_stack.getProperty("machine_extruder_count", "value") > 1
  470. if not multi_extrusion:
  471. return self._global_container_stack.getProperty(setting_key, property)
  472. extruder_index = self._global_container_stack.getProperty(extruder_setting_key, "value")
  473. if extruder_index == "-1": # If extruder index is -1 use global instead
  474. return self._global_container_stack.getProperty(setting_key, property)
  475. extruder_stack_id = ExtruderManager.getInstance().extruderIds[str(extruder_index)]
  476. stack = UM.Settings.ContainerRegistry.getInstance().findContainerStacks(id = extruder_stack_id)[0]
  477. return stack.getProperty(setting_key, property)
  478. ## Convenience function to calculate the disallowed radius around the edge.
  479. #
  480. # This disallowed radius is to allow for space around the models that is
  481. # not part of the collision radius, such as bed adhesion (skirt/brim/raft)
  482. # and travel avoid distance.
  483. def _getEdgeDisallowedSize(self):
  484. if not self._global_container_stack:
  485. return 0
  486. container_stack = self._global_container_stack
  487. # If we are printing one at a time, we need to add the bed adhesion size to the disallowed areas of the objects
  488. if container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  489. return 0.1 # Return a very small value, so we do draw disallowed area's near the edges.
  490. adhesion_type = container_stack.getProperty("adhesion_type", "value")
  491. if adhesion_type == "skirt":
  492. skirt_distance = self._getSettingFromAdhesionExtruder("skirt_gap")
  493. skirt_line_count = self._getSettingFromAdhesionExtruder("skirt_line_count")
  494. bed_adhesion_size = skirt_distance + (skirt_line_count * self._getSettingFromAdhesionExtruder("skirt_brim_line_width"))
  495. if self._global_container_stack.getProperty("machine_extruder_count", "value") > 1:
  496. adhesion_extruder_nr = int(self._global_container_stack.getProperty("adhesion_extruder_nr", "value"))
  497. extruder_values = ExtruderManager.getInstance().getAllExtruderValues("skirt_brim_line_width")
  498. del extruder_values[adhesion_extruder_nr] # Remove the value of the adhesion extruder nr.
  499. for value in extruder_values:
  500. bed_adhesion_size += value
  501. elif adhesion_type == "brim":
  502. bed_adhesion_size = self._getSettingFromAdhesionExtruder("brim_line_count") * self._getSettingFromAdhesionExtruder("skirt_brim_line_width")
  503. if self._global_container_stack.getProperty("machine_extruder_count", "value") > 1:
  504. adhesion_extruder_nr = int(self._global_container_stack.getProperty("adhesion_extruder_nr", "value"))
  505. extruder_values = ExtruderManager.getInstance().getAllExtruderValues("skirt_brim_line_width")
  506. del extruder_values[adhesion_extruder_nr] # Remove the value of the adhesion extruder nr.
  507. for value in extruder_values:
  508. bed_adhesion_size += value
  509. elif adhesion_type == "raft":
  510. bed_adhesion_size = self._getSettingFromAdhesionExtruder("raft_margin")
  511. else:
  512. raise Exception("Unknown bed adhesion type. Did you forget to update the build volume calculations for your new bed adhesion type?")
  513. support_expansion = 0
  514. if self._getSettingFromSupportInfillExtruder("support_offset") and self._global_container_stack.getProperty("support_enable", "value"):
  515. support_expansion += self._getSettingFromSupportInfillExtruder("support_offset")
  516. farthest_shield_distance = 0
  517. if container_stack.getProperty("draft_shield_enabled", "value"):
  518. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("draft_shield_dist", "value"))
  519. if container_stack.getProperty("ooze_shield_enabled", "value"):
  520. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("ooze_shield_dist", "value"))
  521. move_from_wall_radius = 0 # Moves that start from outer wall.
  522. move_from_wall_radius = max(move_from_wall_radius, max(self._getSettingFromAllExtruders("infill_wipe_dist")))
  523. avoid_enabled_per_extruder = self._getSettingFromAllExtruders(("travel_avoid_other_parts"))
  524. avoid_distance_per_extruder = self._getSettingFromAllExtruders("travel_avoid_distance")
  525. for index, avoid_other_parts_enabled in enumerate(avoid_enabled_per_extruder): #For each extruder (or just global).
  526. if avoid_other_parts_enabled:
  527. move_from_wall_radius = max(move_from_wall_radius, avoid_distance_per_extruder[index]) #Index of the same extruder.
  528. #Now combine our different pieces of data to get the final border size.
  529. #Support expansion is added to the bed adhesion, since the bed adhesion goes around support.
  530. #Support expansion is added to farthest shield distance, since the shields go around support.
  531. border_size = max(move_from_wall_radius, support_expansion + farthest_shield_distance, support_expansion + bed_adhesion_size)
  532. return border_size
  533. def _clamp(self, value, min_value, max_value):
  534. return max(min(value, max_value), min_value)
  535. _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"]
  536. _raft_settings = ["adhesion_type", "raft_base_thickness", "raft_interface_thickness", "raft_surface_layers", "raft_surface_thickness", "raft_airgap"]
  537. _prime_settings = ["extruder_prime_pos_x", "extruder_prime_pos_y", "extruder_prime_pos_z"]
  538. _tower_settings = ["prime_tower_enable", "prime_tower_size", "prime_tower_position_x", "prime_tower_position_y"]
  539. _ooze_shield_settings = ["ooze_shield_enabled", "ooze_shield_dist"]
  540. _distance_settings = ["infill_wipe_dist", "travel_avoid_distance", "support_offset", "support_enable", "travel_avoid_other_parts"]