BuildVolume.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. extruder_manager = ExtruderManager.getInstance()
  308. used_extruders = extruder_manager.getUsedExtruderStacks()
  309. disallowed_border_size = self._getEdgeDisallowedSize()
  310. result_areas = self._computeDisallowedAreasStatic(disallowed_border_size, used_extruders) #Normal machine disallowed areas can always be added.
  311. prime_areas = self._computeDisallowedAreasPrime(disallowed_border_size, used_extruders)
  312. 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.
  313. #Check if prime positions intersect with disallowed areas.
  314. for extruder in used_extruders:
  315. extruder_id = extruder.getId()
  316. collision = False
  317. for prime_polygon in prime_areas[extruder_id]:
  318. for disallowed_polygon in prime_disallowed_areas[extruder_id]:
  319. if prime_polygon.intersectsPolygon(disallowed_polygon) is not None:
  320. collision = True
  321. break
  322. if collision:
  323. break
  324. #Also check other prime positions (without additional offset).
  325. for other_extruder_id in prime_areas:
  326. if extruder_id == other_extruder_id: #It is allowed to collide with itself.
  327. continue
  328. for other_prime_polygon in prime_areas[other_extruder_id]:
  329. if prime_polygon.intersectsPolygon(other_prime_polygon):
  330. collision = True
  331. break
  332. if collision:
  333. break
  334. if collision:
  335. break
  336. if not collision:
  337. #Prime areas are valid. Add as normal.
  338. result_areas[extruder_id].extend(prime_areas[extruder_id])
  339. nozzle_disallowed_areas = extruder.getProperty("nozzle_disallowed_areas", "value")
  340. for area in nozzle_disallowed_areas:
  341. polygon = Polygon(numpy.array(area, numpy.float32))
  342. polygon = polygon.getMinkowskiHull(Polygon.approximatedCircle(disallowed_border_size))
  343. result_areas[extruder_id].append(polygon) #Don't perform the offset on these.
  344. # Add prime tower location as disallowed area.
  345. prime_tower_collision = False
  346. prime_tower_areas = self._computeDisallowedAreasPrinted(used_extruders)
  347. for extruder_id in prime_tower_areas:
  348. for prime_tower_area in prime_tower_areas[extruder_id]:
  349. for area in result_areas[extruder_id]:
  350. if prime_tower_area.intersectsPolygon(area) is not None:
  351. prime_tower_collision = True
  352. break
  353. if prime_tower_collision: #Already found a collision.
  354. break
  355. if not prime_tower_collision:
  356. result_areas[extruder_id].extend(prime_tower_areas[extruder_id])
  357. else:
  358. self._error_areas.extend(prime_tower_areas[extruder_id])
  359. self._has_errors = len(self._error_areas) > 0
  360. self._disallowed_areas = []
  361. for extruder_id in result_areas:
  362. self._disallowed_areas.extend(result_areas[extruder_id])
  363. ## Computes the disallowed areas for objects that are printed with print
  364. # features.
  365. #
  366. # This means that the brim, travel avoidance and such will be applied to
  367. # these features.
  368. #
  369. # \return A dictionary with for each used extruder ID the disallowed areas
  370. # where that extruder may not print.
  371. def _computeDisallowedAreasPrinted(self, used_extruders):
  372. result = {}
  373. for extruder in used_extruders:
  374. result[extruder.getId()] = []
  375. #Currently, the only normally printed object is the prime tower.
  376. if ExtruderManager.getInstance().getResolveOrValue("prime_tower_enable") == True:
  377. prime_tower_size = self._global_container_stack.getProperty("prime_tower_size", "value")
  378. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  379. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  380. 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.
  381. prime_tower_y = - self._global_container_stack.getProperty("prime_tower_position_y", "value") + machine_depth / 2
  382. prime_tower_area = Polygon([
  383. [prime_tower_x - prime_tower_size, prime_tower_y - prime_tower_size],
  384. [prime_tower_x, prime_tower_y - prime_tower_size],
  385. [prime_tower_x, prime_tower_y],
  386. [prime_tower_x - prime_tower_size, prime_tower_y],
  387. ])
  388. prime_tower_area = prime_tower_area.getMinkowskiHull(Polygon.approximatedCircle(0))
  389. for extruder in used_extruders:
  390. result[extruder.getId()].append(prime_tower_area) #The prime tower location is the same for each extruder, regardless of offset.
  391. return result
  392. ## Computes the disallowed areas for the prime locations.
  393. #
  394. # These are special because they are not subject to things like brim or
  395. # travel avoidance. They do get a dilute with the border size though
  396. # because they may not intersect with brims and such of other objects.
  397. #
  398. # \param border_size The size with which to offset the disallowed areas
  399. # due to skirt, brim, travel avoid distance, etc.
  400. # \param used_extruders The extruder stacks to generate disallowed areas
  401. # for.
  402. # \return A dictionary with for each used extruder ID the prime areas.
  403. def _computeDisallowedAreasPrime(self, border_size, used_extruders):
  404. result = {}
  405. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  406. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  407. for extruder in used_extruders:
  408. 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.
  409. prime_y = machine_depth / 2 - extruder.getProperty("extruder_prime_pos_y", "value")
  410. prime_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  411. prime_polygon = prime_polygon.translate(prime_x, prime_y)
  412. prime_polygon = prime_polygon.getMinkowskiHull(Polygon.approximatedCircle(border_size))
  413. result[extruder.getId()] = [prime_polygon]
  414. return result
  415. ## Computes the disallowed areas that are statically placed in the machine.
  416. #
  417. # It computes different disallowed areas depending on the offset of the
  418. # extruder. The resulting dictionary will therefore have an entry for each
  419. # extruder that is used.
  420. #
  421. # \param border_size The size with which to offset the disallowed areas
  422. # due to skirt, brim, travel avoid distance, etc.
  423. # \param used_extruders The extruder stacks to generate disallowed areas
  424. # for.
  425. # \return A dictionary with for each used extruder ID the disallowed areas
  426. # where that extruder may not print.
  427. def _computeDisallowedAreasStatic(self, border_size, used_extruders):
  428. #Convert disallowed areas to polygons and dilate them.
  429. machine_disallowed_polygons = []
  430. for area in self._global_container_stack.getProperty("machine_disallowed_areas", "value"):
  431. polygon = Polygon(numpy.array(area, numpy.float32))
  432. polygon = polygon.getMinkowskiHull(Polygon.approximatedCircle(border_size))
  433. machine_disallowed_polygons.append(polygon)
  434. result = {}
  435. for extruder in used_extruders:
  436. extruder_id = extruder.getId()
  437. offset_x = extruder.getProperty("machine_nozzle_offset_x", "value")
  438. if not offset_x:
  439. offset_x = 0
  440. offset_y = extruder.getProperty("machine_nozzle_offset_y", "value")
  441. if not offset_y:
  442. offset_y = 0
  443. result[extruder_id] = []
  444. for polygon in machine_disallowed_polygons:
  445. result[extruder_id].append(polygon.translate(offset_x, offset_y)) #Compensate for the nozzle offset of this extruder.
  446. #Add the border around the edge of the build volume.
  447. half_machine_width = self._global_container_stack.getProperty("machine_width", "value") / 2
  448. half_machine_depth = self._global_container_stack.getProperty("machine_depth", "value") / 2
  449. if border_size + offset_x > 0:
  450. result[extruder_id].append(Polygon(numpy.array([
  451. [-half_machine_width, -half_machine_depth],
  452. [-half_machine_width, half_machine_depth],
  453. [-half_machine_width + border_size + offset_x, half_machine_depth - border_size + offset_y],
  454. [-half_machine_width + border_size + offset_x, -half_machine_depth + border_size + offset_y]
  455. ], numpy.float32)))
  456. if border_size - offset_x > 0:
  457. result[extruder_id].append(Polygon(numpy.array([
  458. [half_machine_width, half_machine_depth],
  459. [half_machine_width, -half_machine_depth],
  460. [half_machine_width - border_size + offset_x, -half_machine_depth + border_size + offset_y],
  461. [half_machine_width - border_size + offset_x, half_machine_depth - border_size + offset_y]
  462. ], numpy.float32)))
  463. if border_size - offset_y > 0:
  464. result[extruder_id].append(Polygon(numpy.array([
  465. [-half_machine_width, half_machine_depth],
  466. [half_machine_width, half_machine_depth],
  467. [half_machine_width - border_size + offset_x, half_machine_depth - border_size + offset_y],
  468. [-half_machine_width + border_size + offset_x, half_machine_depth - border_size + offset_y]
  469. ], numpy.float32)))
  470. if border_size + offset_y > 0:
  471. result[extruder_id].append(Polygon(numpy.array([
  472. [half_machine_width, -half_machine_depth],
  473. [-half_machine_width, -half_machine_depth],
  474. [-half_machine_width + border_size + offset_x, -half_machine_depth + border_size + offset_y],
  475. [half_machine_width - border_size + offset_x, -half_machine_depth + border_size + offset_y]
  476. ], numpy.float32)))
  477. return result
  478. ## Private convenience function to get a setting from the adhesion
  479. # extruder.
  480. #
  481. # \param setting_key The key of the setting to get.
  482. # \param property The property to get from the setting.
  483. # \return The property of the specified setting in the adhesion extruder.
  484. def _getSettingFromAdhesionExtruder(self, setting_key, property = "value"):
  485. return self._getSettingFromExtruder(setting_key, "adhesion_extruder_nr", property)
  486. ## Private convenience function to get a setting from every extruder.
  487. #
  488. # For single extrusion machines, this gets the setting from the global
  489. # stack.
  490. #
  491. # \return A sequence of setting values, one for each extruder.
  492. def _getSettingFromAllExtruders(self, setting_key, property = "value"):
  493. return ExtruderManager.getInstance().getAllExtruderSettings(setting_key, property)
  494. ## Private convenience function to get a setting from the support infill
  495. # extruder.
  496. #
  497. # \param setting_key The key of the setting to get.
  498. # \param property The property to get from the setting.
  499. # \return The property of the specified setting in the support infill
  500. # extruder.
  501. def _getSettingFromSupportInfillExtruder(self, setting_key, property = "value"):
  502. return self._getSettingFromExtruder(setting_key, "support_infill_extruder_nr", property)
  503. ## Helper function to get a setting from an extruder specified in another
  504. # setting.
  505. #
  506. # \param setting_key The key of the setting to get.
  507. # \param extruder_setting_key The key of the setting that specifies from
  508. # which extruder to get the setting, if there are multiple extruders.
  509. # \param property The property to get from the setting.
  510. # \return The property of the specified setting in the specified extruder.
  511. def _getSettingFromExtruder(self, setting_key, extruder_setting_key, property = "value"):
  512. multi_extrusion = self._global_container_stack.getProperty("machine_extruder_count", "value") > 1
  513. if not multi_extrusion:
  514. return self._global_container_stack.getProperty(setting_key, property)
  515. extruder_index = self._global_container_stack.getProperty(extruder_setting_key, "value")
  516. if extruder_index == "-1": # If extruder index is -1 use global instead
  517. return self._global_container_stack.getProperty(setting_key, property)
  518. extruder_stack_id = ExtruderManager.getInstance().extruderIds[str(extruder_index)]
  519. stack = UM.Settings.ContainerRegistry.getInstance().findContainerStacks(id = extruder_stack_id)[0]
  520. return stack.getProperty(setting_key, property)
  521. ## Convenience function to calculate the disallowed radius around the edge.
  522. #
  523. # This disallowed radius is to allow for space around the models that is
  524. # not part of the collision radius, such as bed adhesion (skirt/brim/raft)
  525. # and travel avoid distance.
  526. def _getEdgeDisallowedSize(self):
  527. if not self._global_container_stack:
  528. return 0
  529. container_stack = self._global_container_stack
  530. # If we are printing one at a time, we need to add the bed adhesion size to the disallowed areas of the objects
  531. if container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  532. return 0.1 # Return a very small value, so we do draw disallowed area's near the edges.
  533. adhesion_type = container_stack.getProperty("adhesion_type", "value")
  534. if adhesion_type == "skirt":
  535. skirt_distance = self._getSettingFromAdhesionExtruder("skirt_gap")
  536. skirt_line_count = self._getSettingFromAdhesionExtruder("skirt_line_count")
  537. bed_adhesion_size = skirt_distance + (skirt_line_count * self._getSettingFromAdhesionExtruder("skirt_brim_line_width"))
  538. if self._global_container_stack.getProperty("machine_extruder_count", "value") > 1:
  539. adhesion_extruder_nr = int(self._global_container_stack.getProperty("adhesion_extruder_nr", "value"))
  540. extruder_values = ExtruderManager.getInstance().getAllExtruderValues("skirt_brim_line_width")
  541. del extruder_values[adhesion_extruder_nr] # Remove the value of the adhesion extruder nr.
  542. for value in extruder_values:
  543. bed_adhesion_size += value
  544. elif adhesion_type == "brim":
  545. bed_adhesion_size = self._getSettingFromAdhesionExtruder("brim_line_count") * self._getSettingFromAdhesionExtruder("skirt_brim_line_width")
  546. if self._global_container_stack.getProperty("machine_extruder_count", "value") > 1:
  547. adhesion_extruder_nr = int(self._global_container_stack.getProperty("adhesion_extruder_nr", "value"))
  548. extruder_values = ExtruderManager.getInstance().getAllExtruderValues("skirt_brim_line_width")
  549. del extruder_values[adhesion_extruder_nr] # Remove the value of the adhesion extruder nr.
  550. for value in extruder_values:
  551. bed_adhesion_size += value
  552. elif adhesion_type == "raft":
  553. bed_adhesion_size = self._getSettingFromAdhesionExtruder("raft_margin")
  554. else:
  555. raise Exception("Unknown bed adhesion type. Did you forget to update the build volume calculations for your new bed adhesion type?")
  556. support_expansion = 0
  557. if self._getSettingFromSupportInfillExtruder("support_offset") and self._global_container_stack.getProperty("support_enable", "value"):
  558. support_expansion += self._getSettingFromSupportInfillExtruder("support_offset")
  559. farthest_shield_distance = 0
  560. if container_stack.getProperty("draft_shield_enabled", "value"):
  561. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("draft_shield_dist", "value"))
  562. if container_stack.getProperty("ooze_shield_enabled", "value"):
  563. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("ooze_shield_dist", "value"))
  564. move_from_wall_radius = 0 # Moves that start from outer wall.
  565. move_from_wall_radius = max(move_from_wall_radius, max(self._getSettingFromAllExtruders("infill_wipe_dist")))
  566. avoid_enabled_per_extruder = self._getSettingFromAllExtruders(("travel_avoid_other_parts"))
  567. avoid_distance_per_extruder = self._getSettingFromAllExtruders("travel_avoid_distance")
  568. for index, avoid_other_parts_enabled in enumerate(avoid_enabled_per_extruder): #For each extruder (or just global).
  569. if avoid_other_parts_enabled:
  570. move_from_wall_radius = max(move_from_wall_radius, avoid_distance_per_extruder[index]) #Index of the same extruder.
  571. #Now combine our different pieces of data to get the final border size.
  572. #Support expansion is added to the bed adhesion, since the bed adhesion goes around support.
  573. #Support expansion is added to farthest shield distance, since the shields go around support.
  574. border_size = max(move_from_wall_radius, support_expansion + farthest_shield_distance, support_expansion + bed_adhesion_size)
  575. return border_size
  576. def _clamp(self, value, min_value, max_value):
  577. return max(min(value, max_value), min_value)
  578. _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"]
  579. _raft_settings = ["adhesion_type", "raft_base_thickness", "raft_interface_thickness", "raft_surface_layers", "raft_surface_thickness", "raft_airgap"]
  580. _prime_settings = ["extruder_prime_pos_x", "extruder_prime_pos_y", "extruder_prime_pos_z"]
  581. _tower_settings = ["prime_tower_enable", "prime_tower_size", "prime_tower_position_x", "prime_tower_position_y"]
  582. _ooze_shield_settings = ["ooze_shield_enabled", "ooze_shield_dist"]
  583. _distance_settings = ["infill_wipe_dist", "travel_avoid_distance", "support_offset", "support_enable", "travel_avoid_other_parts"]