BuildVolume.py 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Scene.Camera import Camera
  4. from cura.Scene.CuraSceneNode import CuraSceneNode
  5. from cura.Settings.ExtruderManager import ExtruderManager
  6. from UM.Application import Application #To modify the maximum zoom level.
  7. from UM.i18n import i18nCatalog
  8. from UM.Scene.Platform import Platform
  9. from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
  10. from UM.Scene.SceneNode import SceneNode
  11. from UM.Resources import Resources
  12. from UM.Mesh.MeshBuilder import MeshBuilder
  13. from UM.Math.Vector import Vector
  14. from UM.Math.Matrix import Matrix
  15. from UM.Math.Color import Color
  16. from UM.Math.AxisAlignedBox import AxisAlignedBox
  17. from UM.Math.Polygon import Polygon
  18. from UM.Message import Message
  19. from UM.Signal import Signal
  20. from PyQt5.QtCore import QTimer
  21. from UM.View.RenderBatch import RenderBatch
  22. from UM.View.GL.OpenGL import OpenGL
  23. catalog = i18nCatalog("cura")
  24. import numpy
  25. import math
  26. import copy
  27. from typing import List, Optional
  28. # Radius of disallowed area in mm around prime. I.e. how much distance to keep from prime position.
  29. PRIME_CLEARANCE = 6.5
  30. ## Build volume is a special kind of node that is responsible for rendering the printable area & disallowed areas.
  31. class BuildVolume(SceneNode):
  32. raftThicknessChanged = Signal()
  33. def __init__(self, application, parent = None):
  34. super().__init__(parent)
  35. self._application = application
  36. self._machine_manager = self._application.getMachineManager()
  37. self._volume_outline_color = None
  38. self._x_axis_color = None
  39. self._y_axis_color = None
  40. self._z_axis_color = None
  41. self._disallowed_area_color = None
  42. self._error_area_color = None
  43. self._width = 0 #type: float
  44. self._height = 0 #type: float
  45. self._depth = 0 #type: float
  46. self._shape = "" #type: str
  47. self._shader = None
  48. self._origin_mesh = None
  49. self._origin_line_length = 20
  50. self._origin_line_width = 0.5
  51. self._grid_mesh = None
  52. self._grid_shader = None
  53. self._disallowed_areas = []
  54. self._disallowed_areas_no_brim = []
  55. self._disallowed_area_mesh = None
  56. self._error_areas = []
  57. self._error_mesh = None
  58. self.setCalculateBoundingBox(False)
  59. self._volume_aabb = None
  60. self._raft_thickness = 0.0
  61. self._extra_z_clearance = 0.0
  62. self._adhesion_type = None
  63. self._platform = Platform(self)
  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."), title = catalog.i18nc("@info:title", "Build Volume"))
  68. self._global_container_stack = None
  69. self._stack_change_timer = QTimer()
  70. self._stack_change_timer.setInterval(100)
  71. self._stack_change_timer.setSingleShot(True)
  72. self._stack_change_timer.timeout.connect(self._onStackChangeTimerFinished)
  73. self._application.globalContainerStackChanged.connect(self._onStackChanged)
  74. self._onStackChanged()
  75. self._engine_ready = False
  76. self._application.engineCreatedSignal.connect(self._onEngineCreated)
  77. self._has_errors = False
  78. self._application.getController().getScene().sceneChanged.connect(self._onSceneChanged)
  79. #Objects loaded at the moment. We are connected to the property changed events of these objects.
  80. self._scene_objects = set()
  81. self._scene_change_timer = QTimer()
  82. self._scene_change_timer.setInterval(100)
  83. self._scene_change_timer.setSingleShot(True)
  84. self._scene_change_timer.timeout.connect(self._onSceneChangeTimerFinished)
  85. self._setting_change_timer = QTimer()
  86. self._setting_change_timer.setInterval(150)
  87. self._setting_change_timer.setSingleShot(True)
  88. self._setting_change_timer.timeout.connect(self._onSettingChangeTimerFinished)
  89. # Must be after setting _build_volume_message, apparently that is used in getMachineManager.
  90. # activeQualityChanged is always emitted after setActiveVariant, setActiveMaterial and setActiveQuality.
  91. # Therefore this works.
  92. self._machine_manager.activeQualityChanged.connect(self._onStackChanged)
  93. # This should also ways work, and it is semantically more correct,
  94. # but it does not update the disallowed areas after material change
  95. self._machine_manager.activeStackChanged.connect(self._onStackChanged)
  96. # Enable and disable extruder
  97. self._machine_manager.extruderChanged.connect(self.updateNodeBoundaryCheck)
  98. # list of settings which were updated
  99. self._changed_settings_since_last_rebuild = []
  100. def _onSceneChanged(self, source):
  101. if self._global_container_stack:
  102. # Ignore anything that is not something we can slice in the first place!
  103. if source.callDecoration("isSliceable"):
  104. self._scene_change_timer.start()
  105. def _onSceneChangeTimerFinished(self):
  106. root = self._application.getController().getScene().getRoot()
  107. new_scene_objects = set(node for node in BreadthFirstIterator(root) if node.callDecoration("isSliceable"))
  108. if new_scene_objects != self._scene_objects:
  109. for node in new_scene_objects - self._scene_objects: #Nodes that were added to the scene.
  110. self._updateNodeListeners(node)
  111. node.decoratorsChanged.connect(self._updateNodeListeners) # Make sure that decoration changes afterwards also receive the same treatment
  112. for node in self._scene_objects - new_scene_objects: #Nodes that were removed from the scene.
  113. per_mesh_stack = node.callDecoration("getStack")
  114. if per_mesh_stack:
  115. per_mesh_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  116. active_extruder_changed = node.callDecoration("getActiveExtruderChangedSignal")
  117. if active_extruder_changed is not None:
  118. node.callDecoration("getActiveExtruderChangedSignal").disconnect(self._updateDisallowedAreasAndRebuild)
  119. node.decoratorsChanged.disconnect(self._updateNodeListeners)
  120. self.rebuild()
  121. self._scene_objects = new_scene_objects
  122. self._onSettingPropertyChanged("print_sequence", "value") # Create fake event, so right settings are triggered.
  123. ## Updates the listeners that listen for changes in per-mesh stacks.
  124. #
  125. # \param node The node for which the decorators changed.
  126. def _updateNodeListeners(self, node: SceneNode):
  127. per_mesh_stack = node.callDecoration("getStack")
  128. if per_mesh_stack:
  129. per_mesh_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  130. active_extruder_changed = node.callDecoration("getActiveExtruderChangedSignal")
  131. if active_extruder_changed is not None:
  132. active_extruder_changed.connect(self._updateDisallowedAreasAndRebuild)
  133. def setWidth(self, width: float) -> None:
  134. if width is not None:
  135. self._width = width
  136. def setHeight(self, height: float) -> None:
  137. if height is not None:
  138. self._height = height
  139. def setDepth(self, depth: float) -> None:
  140. if depth is not None:
  141. self._depth = depth
  142. def setShape(self, shape: str) -> None:
  143. if shape:
  144. self._shape = shape
  145. ## Get the length of the 3D diagonal through the build volume.
  146. #
  147. # This gives a sense of the scale of the build volume in general.
  148. def getDiagonalSize(self) -> float:
  149. return math.sqrt(self._width * self._width + self._height * self._height + self._depth * self._depth)
  150. def getDisallowedAreas(self) -> List[Polygon]:
  151. return self._disallowed_areas
  152. def getDisallowedAreasNoBrim(self) -> List[Polygon]:
  153. return self._disallowed_areas_no_brim
  154. def setDisallowedAreas(self, areas: List[Polygon]):
  155. self._disallowed_areas = areas
  156. def render(self, renderer):
  157. if not self.getMeshData():
  158. return True
  159. if not self._shader:
  160. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "default.shader"))
  161. self._grid_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "grid.shader"))
  162. theme = self._application.getTheme()
  163. self._grid_shader.setUniformValue("u_plateColor", Color(*theme.getColor("buildplate").getRgb()))
  164. self._grid_shader.setUniformValue("u_gridColor0", Color(*theme.getColor("buildplate_grid").getRgb()))
  165. self._grid_shader.setUniformValue("u_gridColor1", Color(*theme.getColor("buildplate_grid_minor").getRgb()))
  166. renderer.queueNode(self, mode = RenderBatch.RenderMode.Lines)
  167. renderer.queueNode(self, mesh = self._origin_mesh, backface_cull = True)
  168. renderer.queueNode(self, mesh = self._grid_mesh, shader = self._grid_shader, backface_cull = True)
  169. if self._disallowed_area_mesh:
  170. renderer.queueNode(self, mesh = self._disallowed_area_mesh, shader = self._shader, transparent = True, backface_cull = True, sort = -9)
  171. if self._error_mesh:
  172. renderer.queueNode(self, mesh=self._error_mesh, shader=self._shader, transparent=True,
  173. backface_cull=True, sort=-8)
  174. return True
  175. ## For every sliceable node, update node._outside_buildarea
  176. #
  177. def updateNodeBoundaryCheck(self):
  178. root = self._application.getController().getScene().getRoot()
  179. nodes = list(BreadthFirstIterator(root))
  180. group_nodes = []
  181. build_volume_bounding_box = self.getBoundingBox()
  182. if build_volume_bounding_box:
  183. # It's over 9000!
  184. build_volume_bounding_box = build_volume_bounding_box.set(bottom=-9001)
  185. else:
  186. # No bounding box. This is triggered when running Cura from command line with a model for the first time
  187. # In that situation there is a model, but no machine (and therefore no build volume.
  188. return
  189. for node in nodes:
  190. # Need to check group nodes later
  191. if node.callDecoration("isGroup"):
  192. group_nodes.append(node) # Keep list of affected group_nodes
  193. if node.callDecoration("isSliceable") or node.callDecoration("isGroup"):
  194. if node.collidesWithBbox(build_volume_bounding_box):
  195. node.setOutsideBuildArea(True)
  196. continue
  197. if node.collidesWithArea(self.getDisallowedAreas()):
  198. node.setOutsideBuildArea(True)
  199. continue
  200. # Mark the node as outside build volume if the set extruder is disabled
  201. extruder_position = node.callDecoration("getActiveExtruderPosition")
  202. if extruder_position not in self._global_container_stack.extruders:
  203. continue
  204. if not self._global_container_stack.extruders[extruder_position].isEnabled:
  205. node.setOutsideBuildArea(True)
  206. continue
  207. node.setOutsideBuildArea(False)
  208. # Group nodes should override the _outside_buildarea property of their children.
  209. for group_node in group_nodes:
  210. children = group_node.getAllChildren()
  211. # Check if one or more children are non-printable and if so, set the parent as non-printable:
  212. for child_node in children:
  213. if child_node.isOutsideBuildArea():
  214. group_node.setOutsideBuildArea(True)
  215. break
  216. # Apply results of the check to all children of the group:
  217. for child_node in children:
  218. child_node.setOutsideBuildArea(group_node.isOutsideBuildArea())
  219. ## Update the outsideBuildArea of a single node, given bounds or current build volume
  220. def checkBoundsAndUpdate(self, node: CuraSceneNode, bounds: Optional[AxisAlignedBox] = None):
  221. if not isinstance(node, CuraSceneNode):
  222. return
  223. if bounds is None:
  224. build_volume_bounding_box = self.getBoundingBox()
  225. if build_volume_bounding_box:
  226. # It's over 9000!
  227. build_volume_bounding_box = build_volume_bounding_box.set(bottom=-9001)
  228. else:
  229. # No bounding box. This is triggered when running Cura from command line with a model for the first time
  230. # In that situation there is a model, but no machine (and therefore no build volume.
  231. return
  232. else:
  233. build_volume_bounding_box = bounds
  234. if node.callDecoration("isSliceable") or node.callDecoration("isGroup"):
  235. if node.collidesWithBbox(build_volume_bounding_box):
  236. node.setOutsideBuildArea(True)
  237. return
  238. if node.collidesWithArea(self.getDisallowedAreas()):
  239. node.setOutsideBuildArea(True)
  240. return
  241. # Mark the node as outside build volume if the set extruder is disabled
  242. extruder_position = node.callDecoration("getActiveExtruderPosition")
  243. if not self._global_container_stack.extruders[extruder_position].isEnabled:
  244. node.setOutsideBuildArea(True)
  245. return
  246. node.setOutsideBuildArea(False)
  247. ## Recalculates the build volume & disallowed areas.
  248. def rebuild(self):
  249. if not self._width or not self._height or not self._depth:
  250. return
  251. if not self._engine_ready:
  252. return
  253. if not self._volume_outline_color:
  254. theme = self._application.getTheme()
  255. self._volume_outline_color = Color(*theme.getColor("volume_outline").getRgb())
  256. self._x_axis_color = Color(*theme.getColor("x_axis").getRgb())
  257. self._y_axis_color = Color(*theme.getColor("y_axis").getRgb())
  258. self._z_axis_color = Color(*theme.getColor("z_axis").getRgb())
  259. self._disallowed_area_color = Color(*theme.getColor("disallowed_area").getRgb())
  260. self._error_area_color = Color(*theme.getColor("error_area").getRgb())
  261. min_w = -self._width / 2
  262. max_w = self._width / 2
  263. min_h = 0.0
  264. max_h = self._height
  265. min_d = -self._depth / 2
  266. max_d = self._depth / 2
  267. z_fight_distance = 0.2 # Distance between buildplate and disallowed area meshes to prevent z-fighting
  268. if self._shape != "elliptic":
  269. # Outline 'cube' of the build volume
  270. mb = MeshBuilder()
  271. mb.addLine(Vector(min_w, min_h, min_d), Vector(max_w, min_h, min_d), color = self._volume_outline_color)
  272. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, max_h, min_d), color = self._volume_outline_color)
  273. mb.addLine(Vector(min_w, max_h, min_d), Vector(max_w, max_h, min_d), color = self._volume_outline_color)
  274. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, max_h, min_d), color = self._volume_outline_color)
  275. mb.addLine(Vector(min_w, min_h, max_d), Vector(max_w, min_h, max_d), color = self._volume_outline_color)
  276. mb.addLine(Vector(min_w, min_h, max_d), Vector(min_w, max_h, max_d), color = self._volume_outline_color)
  277. mb.addLine(Vector(min_w, max_h, max_d), Vector(max_w, max_h, max_d), color = self._volume_outline_color)
  278. mb.addLine(Vector(max_w, min_h, max_d), Vector(max_w, max_h, max_d), color = self._volume_outline_color)
  279. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, min_h, max_d), color = self._volume_outline_color)
  280. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, min_h, max_d), color = self._volume_outline_color)
  281. mb.addLine(Vector(min_w, max_h, min_d), Vector(min_w, max_h, max_d), color = self._volume_outline_color)
  282. mb.addLine(Vector(max_w, max_h, min_d), Vector(max_w, max_h, max_d), color = self._volume_outline_color)
  283. self.setMeshData(mb.build())
  284. # Build plate grid mesh
  285. mb = MeshBuilder()
  286. mb.addQuad(
  287. Vector(min_w, min_h - z_fight_distance, min_d),
  288. Vector(max_w, min_h - z_fight_distance, min_d),
  289. Vector(max_w, min_h - z_fight_distance, max_d),
  290. Vector(min_w, min_h - z_fight_distance, max_d)
  291. )
  292. for n in range(0, 6):
  293. v = mb.getVertex(n)
  294. mb.setVertexUVCoordinates(n, v[0], v[2])
  295. self._grid_mesh = mb.build()
  296. else:
  297. # Bottom and top 'ellipse' of the build volume
  298. aspect = 1.0
  299. scale_matrix = Matrix()
  300. if self._width != 0:
  301. # Scale circular meshes by aspect ratio if width != height
  302. aspect = self._depth / self._width
  303. scale_matrix.compose(scale = Vector(1, 1, aspect))
  304. mb = MeshBuilder()
  305. mb.addArc(max_w, Vector.Unit_Y, center = (0, min_h - z_fight_distance, 0), color = self._volume_outline_color)
  306. mb.addArc(max_w, Vector.Unit_Y, center = (0, max_h, 0), color = self._volume_outline_color)
  307. self.setMeshData(mb.build().getTransformed(scale_matrix))
  308. # Build plate grid mesh
  309. mb = MeshBuilder()
  310. mb.addVertex(0, min_h - z_fight_distance, 0)
  311. mb.addArc(max_w, Vector.Unit_Y, center = Vector(0, min_h - z_fight_distance, 0))
  312. sections = mb.getVertexCount() - 1 # Center point is not an arc section
  313. indices = []
  314. for n in range(0, sections - 1):
  315. indices.append([0, n + 2, n + 1])
  316. mb.addIndices(numpy.asarray(indices, dtype = numpy.int32))
  317. mb.calculateNormals()
  318. for n in range(0, mb.getVertexCount()):
  319. v = mb.getVertex(n)
  320. mb.setVertexUVCoordinates(n, v[0], v[2] * aspect)
  321. self._grid_mesh = mb.build().getTransformed(scale_matrix)
  322. # Indication of the machine origin
  323. if self._global_container_stack.getProperty("machine_center_is_zero", "value"):
  324. origin = (Vector(min_w, min_h, min_d) + Vector(max_w, min_h, max_d)) / 2
  325. else:
  326. origin = Vector(min_w, min_h, max_d)
  327. mb = MeshBuilder()
  328. mb.addCube(
  329. width = self._origin_line_length,
  330. height = self._origin_line_width,
  331. depth = self._origin_line_width,
  332. center = origin + Vector(self._origin_line_length / 2, 0, 0),
  333. color = self._x_axis_color
  334. )
  335. mb.addCube(
  336. width = self._origin_line_width,
  337. height = self._origin_line_length,
  338. depth = self._origin_line_width,
  339. center = origin + Vector(0, self._origin_line_length / 2, 0),
  340. color = self._y_axis_color
  341. )
  342. mb.addCube(
  343. width = self._origin_line_width,
  344. height = self._origin_line_width,
  345. depth = self._origin_line_length,
  346. center = origin - Vector(0, 0, self._origin_line_length / 2),
  347. color = self._z_axis_color
  348. )
  349. self._origin_mesh = mb.build()
  350. disallowed_area_height = 0.1
  351. disallowed_area_size = 0
  352. if self._disallowed_areas:
  353. mb = MeshBuilder()
  354. color = self._disallowed_area_color
  355. for polygon in self._disallowed_areas:
  356. points = polygon.getPoints()
  357. if len(points) == 0:
  358. continue
  359. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  360. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  361. for point in points:
  362. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height, self._clamp(point[1], min_d, max_d))
  363. mb.addFace(first, previous_point, new_point, color = color)
  364. previous_point = new_point
  365. # Find the largest disallowed area to exclude it from the maximum scale bounds.
  366. # This is a very nasty hack. This pretty much only works for UM machines.
  367. # This disallowed area_size needs a -lot- of rework at some point in the future: TODO
  368. 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.
  369. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  370. else:
  371. size = 0
  372. disallowed_area_size = max(size, disallowed_area_size)
  373. self._disallowed_area_mesh = mb.build()
  374. else:
  375. self._disallowed_area_mesh = None
  376. if self._error_areas:
  377. mb = MeshBuilder()
  378. for error_area in self._error_areas:
  379. color = self._error_area_color
  380. points = error_area.getPoints()
  381. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height,
  382. self._clamp(points[0][1], min_d, max_d))
  383. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height,
  384. self._clamp(points[0][1], min_d, max_d))
  385. for point in points:
  386. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height,
  387. self._clamp(point[1], min_d, max_d))
  388. mb.addFace(first, previous_point, new_point, color=color)
  389. previous_point = new_point
  390. self._error_mesh = mb.build()
  391. else:
  392. self._error_mesh = None
  393. self._volume_aabb = AxisAlignedBox(
  394. minimum = Vector(min_w, min_h - 1.0, min_d),
  395. maximum = Vector(max_w, max_h - self._raft_thickness - self._extra_z_clearance, max_d))
  396. bed_adhesion_size = self.getEdgeDisallowedSize()
  397. # As this works better for UM machines, we only add the disallowed_area_size for the z direction.
  398. # This is probably wrong in all other cases. TODO!
  399. # The +1 and -1 is added as there is always a bit of extra room required to work properly.
  400. scale_to_max_bounds = AxisAlignedBox(
  401. minimum = Vector(min_w + bed_adhesion_size + 1, min_h, min_d + disallowed_area_size - bed_adhesion_size + 1),
  402. maximum = Vector(max_w - bed_adhesion_size - 1, max_h - self._raft_thickness - self._extra_z_clearance, max_d - disallowed_area_size + bed_adhesion_size - 1)
  403. )
  404. self._application.getController().getScene()._maximum_bounds = scale_to_max_bounds
  405. self.updateNodeBoundaryCheck()
  406. def getBoundingBox(self) -> AxisAlignedBox:
  407. return self._volume_aabb
  408. def getRaftThickness(self) -> float:
  409. return self._raft_thickness
  410. def _updateRaftThickness(self):
  411. old_raft_thickness = self._raft_thickness
  412. if self._global_container_stack.extruders:
  413. # This might be called before the extruder stacks have initialised, in which case getting the adhesion_type fails
  414. self._adhesion_type = self._global_container_stack.getProperty("adhesion_type", "value")
  415. self._raft_thickness = 0.0
  416. if self._adhesion_type == "raft":
  417. self._raft_thickness = (
  418. self._global_container_stack.getProperty("raft_base_thickness", "value") +
  419. self._global_container_stack.getProperty("raft_interface_thickness", "value") +
  420. self._global_container_stack.getProperty("raft_surface_layers", "value") *
  421. self._global_container_stack.getProperty("raft_surface_thickness", "value") +
  422. self._global_container_stack.getProperty("raft_airgap", "value") -
  423. self._global_container_stack.getProperty("layer_0_z_overlap", "value"))
  424. # Rounding errors do not matter, we check if raft_thickness has changed at all
  425. if old_raft_thickness != self._raft_thickness:
  426. self.setPosition(Vector(0, -self._raft_thickness, 0), SceneNode.TransformSpace.World)
  427. self.raftThicknessChanged.emit()
  428. def _updateExtraZClearance(self) -> None:
  429. extra_z = 0.0
  430. extruders = ExtruderManager.getInstance().getUsedExtruderStacks()
  431. use_extruders = False
  432. for extruder in extruders:
  433. if extruder.getProperty("retraction_hop_enabled", "value"):
  434. retraction_hop = extruder.getProperty("retraction_hop", "value")
  435. if extra_z is None or retraction_hop > extra_z:
  436. extra_z = retraction_hop
  437. use_extruders = True
  438. if not use_extruders:
  439. # If no extruders, take global value.
  440. if self._global_container_stack.getProperty("retraction_hop_enabled", "value"):
  441. extra_z = self._global_container_stack.getProperty("retraction_hop", "value")
  442. if extra_z != self._extra_z_clearance:
  443. self._extra_z_clearance = extra_z
  444. def _onStackChanged(self):
  445. self._stack_change_timer.start()
  446. ## Update the build volume visualization
  447. def _onStackChangeTimerFinished(self):
  448. if self._global_container_stack:
  449. self._global_container_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  450. extruders = ExtruderManager.getInstance().getActiveExtruderStacks()
  451. for extruder in extruders:
  452. extruder.propertyChanged.disconnect(self._onSettingPropertyChanged)
  453. self._global_container_stack = self._application.getGlobalContainerStack()
  454. if self._global_container_stack:
  455. self._global_container_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  456. extruders = ExtruderManager.getInstance().getActiveExtruderStacks()
  457. for extruder in extruders:
  458. extruder.propertyChanged.connect(self._onSettingPropertyChanged)
  459. self._width = self._global_container_stack.getProperty("machine_width", "value")
  460. machine_height = self._global_container_stack.getProperty("machine_height", "value")
  461. if self._global_container_stack.getProperty("print_sequence", "value") == "one_at_a_time" and len(self._scene_objects) > 1:
  462. self._height = min(self._global_container_stack.getProperty("gantry_height", "value"), machine_height)
  463. if self._height < machine_height:
  464. self._build_volume_message.show()
  465. else:
  466. self._build_volume_message.hide()
  467. else:
  468. self._height = self._global_container_stack.getProperty("machine_height", "value")
  469. self._build_volume_message.hide()
  470. self._depth = self._global_container_stack.getProperty("machine_depth", "value")
  471. self._shape = self._global_container_stack.getProperty("machine_shape", "value")
  472. self._updateDisallowedAreas()
  473. self._updateRaftThickness()
  474. self._updateExtraZClearance()
  475. if self._engine_ready:
  476. self.rebuild()
  477. camera = Application.getInstance().getController().getCameraTool()
  478. if camera:
  479. diagonal = self.getDiagonalSize()
  480. if diagonal > 1:
  481. camera.setZoomRange(min = 0.1, max = diagonal * 5) #You can zoom out up to 5 times the diagonal. This gives some space around the volume.
  482. def _onEngineCreated(self):
  483. self._engine_ready = True
  484. self.rebuild()
  485. def _onSettingChangeTimerFinished(self):
  486. rebuild_me = False
  487. update_disallowed_areas = False
  488. update_raft_thickness = False
  489. update_extra_z_clearance = True
  490. for setting_key in self._changed_settings_since_last_rebuild:
  491. if setting_key == "print_sequence":
  492. machine_height = self._global_container_stack.getProperty("machine_height", "value")
  493. if self._application.getGlobalContainerStack().getProperty("print_sequence", "value") == "one_at_a_time" and len(self._scene_objects) > 1:
  494. self._height = min(self._global_container_stack.getProperty("gantry_height", "value"), machine_height)
  495. if self._height < machine_height:
  496. self._build_volume_message.show()
  497. else:
  498. self._build_volume_message.hide()
  499. else:
  500. self._height = self._global_container_stack.getProperty("machine_height", "value")
  501. self._build_volume_message.hide()
  502. update_disallowed_areas = True
  503. rebuild_me = True
  504. # sometimes the machine size or shape settings are adjusted on the active machine, we should reflect this
  505. if setting_key in self._machine_settings:
  506. self._height = self._global_container_stack.getProperty("machine_height", "value")
  507. self._width = self._global_container_stack.getProperty("machine_width", "value")
  508. self._depth = self._global_container_stack.getProperty("machine_depth", "value")
  509. self._shape = self._global_container_stack.getProperty("machine_shape", "value")
  510. update_extra_z_clearance = True
  511. update_disallowed_areas = True
  512. rebuild_me = True
  513. if setting_key in self._skirt_settings + self._prime_settings + self._tower_settings + self._ooze_shield_settings + self._distance_settings + self._extruder_settings:
  514. update_disallowed_areas = True
  515. rebuild_me = True
  516. if setting_key in self._raft_settings:
  517. update_raft_thickness = True
  518. rebuild_me = True
  519. if setting_key in self._extra_z_settings:
  520. update_extra_z_clearance = True
  521. rebuild_me = True
  522. if setting_key in self._limit_to_extruder_settings:
  523. update_disallowed_areas = True
  524. rebuild_me = True
  525. # We only want to update all of them once.
  526. if update_disallowed_areas:
  527. self._updateDisallowedAreas()
  528. if update_raft_thickness:
  529. self._updateRaftThickness()
  530. if update_extra_z_clearance:
  531. self._updateExtraZClearance()
  532. if rebuild_me:
  533. self.rebuild()
  534. # We just did a rebuild, reset the list.
  535. self._changed_settings_since_last_rebuild = []
  536. def _onSettingPropertyChanged(self, setting_key: str, property_name: str):
  537. if property_name != "value":
  538. return
  539. if setting_key not in self._changed_settings_since_last_rebuild:
  540. self._changed_settings_since_last_rebuild.append(setting_key)
  541. self._setting_change_timer.start()
  542. def hasErrors(self) -> bool:
  543. return self._has_errors
  544. ## Calls _updateDisallowedAreas and makes sure the changes appear in the
  545. # scene.
  546. #
  547. # This is required for a signal to trigger the update in one go. The
  548. # ``_updateDisallowedAreas`` method itself shouldn't call ``rebuild``,
  549. # since there may be other changes before it needs to be rebuilt, which
  550. # would hit performance.
  551. def _updateDisallowedAreasAndRebuild(self):
  552. self._updateDisallowedAreas()
  553. self._updateRaftThickness()
  554. self._updateExtraZClearance()
  555. self.rebuild()
  556. def _updateDisallowedAreas(self):
  557. if not self._global_container_stack:
  558. return
  559. self._error_areas = []
  560. extruder_manager = ExtruderManager.getInstance()
  561. used_extruders = extruder_manager.getUsedExtruderStacks()
  562. disallowed_border_size = self.getEdgeDisallowedSize()
  563. if not used_extruders:
  564. # If no extruder is used, assume that the active extruder is used (else nothing is drawn)
  565. if extruder_manager.getActiveExtruderStack():
  566. used_extruders = [extruder_manager.getActiveExtruderStack()]
  567. else:
  568. used_extruders = [self._global_container_stack]
  569. result_areas = self._computeDisallowedAreasStatic(disallowed_border_size, used_extruders) #Normal machine disallowed areas can always be added.
  570. prime_areas = self._computeDisallowedAreasPrimeBlob(disallowed_border_size, used_extruders)
  571. result_areas_no_brim = self._computeDisallowedAreasStatic(0, used_extruders) #Where the priming is not allowed to happen. This is not added to the result, just for collision checking.
  572. prime_disallowed_areas = copy.deepcopy(result_areas_no_brim)
  573. #Check if prime positions intersect with disallowed areas.
  574. for extruder in used_extruders:
  575. extruder_id = extruder.getId()
  576. collision = False
  577. for prime_polygon in prime_areas[extruder_id]:
  578. for disallowed_polygon in prime_disallowed_areas[extruder_id]:
  579. if prime_polygon.intersectsPolygon(disallowed_polygon) is not None:
  580. collision = True
  581. break
  582. if collision:
  583. break
  584. #Also check other prime positions (without additional offset).
  585. for other_extruder_id in prime_areas:
  586. if extruder_id == other_extruder_id: #It is allowed to collide with itself.
  587. continue
  588. for other_prime_polygon in prime_areas[other_extruder_id]:
  589. if prime_polygon.intersectsPolygon(other_prime_polygon):
  590. collision = True
  591. break
  592. if collision:
  593. break
  594. if collision:
  595. break
  596. result_areas[extruder_id].extend(prime_areas[extruder_id])
  597. result_areas_no_brim[extruder_id].extend(prime_areas[extruder_id])
  598. nozzle_disallowed_areas = extruder.getProperty("nozzle_disallowed_areas", "value")
  599. for area in nozzle_disallowed_areas:
  600. polygon = Polygon(numpy.array(area, numpy.float32))
  601. polygon_disallowed_border = polygon.getMinkowskiHull(Polygon.approximatedCircle(disallowed_border_size))
  602. result_areas[extruder_id].append(polygon_disallowed_border) #Don't perform the offset on these.
  603. #polygon_minimal_border = polygon.getMinkowskiHull(5)
  604. result_areas_no_brim[extruder_id].append(polygon) # no brim
  605. # Add prime tower location as disallowed area.
  606. if len(used_extruders) > 1: #No prime tower in single-extrusion.
  607. if len([x for x in used_extruders if x.isEnabled == True]) > 1: #No prime tower if only one extruder is enabled
  608. prime_tower_collision = False
  609. prime_tower_areas = self._computeDisallowedAreasPrinted(used_extruders)
  610. for extruder_id in prime_tower_areas:
  611. for i_area, prime_tower_area in enumerate(prime_tower_areas[extruder_id]):
  612. for area in result_areas[extruder_id]:
  613. if prime_tower_area.intersectsPolygon(area) is not None:
  614. prime_tower_collision = True
  615. break
  616. if prime_tower_collision: #Already found a collision.
  617. break
  618. if (ExtruderManager.getInstance().getResolveOrValue("prime_tower_brim_enable") and
  619. ExtruderManager.getInstance().getResolveOrValue("adhesion_type") != "raft"):
  620. prime_tower_areas[extruder_id][i_area] = prime_tower_area.getMinkowskiHull(
  621. Polygon.approximatedCircle(disallowed_border_size))
  622. if not prime_tower_collision:
  623. result_areas[extruder_id].extend(prime_tower_areas[extruder_id])
  624. result_areas_no_brim[extruder_id].extend(prime_tower_areas[extruder_id])
  625. else:
  626. self._error_areas.extend(prime_tower_areas[extruder_id])
  627. self._has_errors = len(self._error_areas) > 0
  628. self._disallowed_areas = []
  629. for extruder_id in result_areas:
  630. self._disallowed_areas.extend(result_areas[extruder_id])
  631. self._disallowed_areas_no_brim = []
  632. for extruder_id in result_areas_no_brim:
  633. self._disallowed_areas_no_brim.extend(result_areas_no_brim[extruder_id])
  634. ## Computes the disallowed areas for objects that are printed with print
  635. # features.
  636. #
  637. # This means that the brim, travel avoidance and such will be applied to
  638. # these features.
  639. #
  640. # \return A dictionary with for each used extruder ID the disallowed areas
  641. # where that extruder may not print.
  642. def _computeDisallowedAreasPrinted(self, used_extruders):
  643. result = {}
  644. for extruder in used_extruders:
  645. result[extruder.getId()] = []
  646. #Currently, the only normally printed object is the prime tower.
  647. if ExtruderManager.getInstance().getResolveOrValue("prime_tower_enable"):
  648. prime_tower_size = self._global_container_stack.getProperty("prime_tower_size", "value")
  649. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  650. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  651. prime_tower_x = self._global_container_stack.getProperty("prime_tower_position_x", "value")
  652. prime_tower_y = - self._global_container_stack.getProperty("prime_tower_position_y", "value")
  653. if not self._global_container_stack.getProperty("machine_center_is_zero", "value"):
  654. prime_tower_x = prime_tower_x - machine_width / 2 #Offset by half machine_width and _depth to put the origin in the front-left.
  655. prime_tower_y = prime_tower_y + machine_depth / 2
  656. if (ExtruderManager.getInstance().getResolveOrValue("prime_tower_brim_enable") and
  657. ExtruderManager.getInstance().getResolveOrValue("adhesion_type") != "raft"):
  658. brim_size = (
  659. extruder.getProperty("brim_line_count", "value") *
  660. extruder.getProperty("skirt_brim_line_width", "value") / 100.0 *
  661. extruder.getProperty("initial_layer_line_width_factor", "value")
  662. )
  663. prime_tower_x -= brim_size
  664. prime_tower_y += brim_size
  665. if self._global_container_stack.getProperty("prime_tower_circular", "value"):
  666. radius = prime_tower_size / 2
  667. prime_tower_area = Polygon.approximatedCircle(radius)
  668. prime_tower_area = prime_tower_area.translate(prime_tower_x - radius, prime_tower_y - radius)
  669. else:
  670. prime_tower_area = Polygon([
  671. [prime_tower_x - prime_tower_size, prime_tower_y - prime_tower_size],
  672. [prime_tower_x, prime_tower_y - prime_tower_size],
  673. [prime_tower_x, prime_tower_y],
  674. [prime_tower_x - prime_tower_size, prime_tower_y],
  675. ])
  676. prime_tower_area = prime_tower_area.getMinkowskiHull(Polygon.approximatedCircle(0))
  677. for extruder in used_extruders:
  678. result[extruder.getId()].append(prime_tower_area) #The prime tower location is the same for each extruder, regardless of offset.
  679. return result
  680. ## Computes the disallowed areas for the prime blobs.
  681. #
  682. # These are special because they are not subject to things like brim or
  683. # travel avoidance. They do get a dilute with the border size though
  684. # because they may not intersect with brims and such of other objects.
  685. #
  686. # \param border_size The size with which to offset the disallowed areas
  687. # due to skirt, brim, travel avoid distance, etc.
  688. # \param used_extruders The extruder stacks to generate disallowed areas
  689. # for.
  690. # \return A dictionary with for each used extruder ID the prime areas.
  691. def _computeDisallowedAreasPrimeBlob(self, border_size, used_extruders):
  692. result = {}
  693. machine_width = self._global_container_stack.getProperty("machine_width", "value")
  694. machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
  695. for extruder in used_extruders:
  696. prime_blob_enabled = extruder.getProperty("prime_blob_enable", "value")
  697. prime_x = extruder.getProperty("extruder_prime_pos_x", "value")
  698. prime_y = -extruder.getProperty("extruder_prime_pos_y", "value")
  699. #Ignore extruder prime position if it is not set or if blob is disabled
  700. if (prime_x == 0 and prime_y == 0) or not prime_blob_enabled:
  701. result[extruder.getId()] = []
  702. continue
  703. if not self._global_container_stack.getProperty("machine_center_is_zero", "value"):
  704. prime_x = prime_x - machine_width / 2 #Offset by half machine_width and _depth to put the origin in the front-left.
  705. prime_y = prime_y + machine_depth / 2
  706. prime_polygon = Polygon.approximatedCircle(PRIME_CLEARANCE)
  707. prime_polygon = prime_polygon.getMinkowskiHull(Polygon.approximatedCircle(border_size))
  708. prime_polygon = prime_polygon.translate(prime_x, prime_y)
  709. result[extruder.getId()] = [prime_polygon]
  710. return result
  711. ## Computes the disallowed areas that are statically placed in the machine.
  712. #
  713. # It computes different disallowed areas depending on the offset of the
  714. # extruder. The resulting dictionary will therefore have an entry for each
  715. # extruder that is used.
  716. #
  717. # \param border_size The size with which to offset the disallowed areas
  718. # due to skirt, brim, travel avoid distance, etc.
  719. # \param used_extruders The extruder stacks to generate disallowed areas
  720. # for.
  721. # \return A dictionary with for each used extruder ID the disallowed areas
  722. # where that extruder may not print.
  723. def _computeDisallowedAreasStatic(self, border_size, used_extruders):
  724. #Convert disallowed areas to polygons and dilate them.
  725. machine_disallowed_polygons = []
  726. for area in self._global_container_stack.getProperty("machine_disallowed_areas", "value"):
  727. polygon = Polygon(numpy.array(area, numpy.float32))
  728. polygon = polygon.getMinkowskiHull(Polygon.approximatedCircle(border_size))
  729. machine_disallowed_polygons.append(polygon)
  730. # For certain machines we don't need to compute disallowed areas for each nozzle.
  731. # So we check here and only do the nozzle offsetting if needed.
  732. nozzle_offsetting_for_disallowed_areas = self._global_container_stack.getMetaDataEntry(
  733. "nozzle_offsetting_for_disallowed_areas", True)
  734. result = {}
  735. for extruder in used_extruders:
  736. extruder_id = extruder.getId()
  737. offset_x = extruder.getProperty("machine_nozzle_offset_x", "value")
  738. if offset_x is None:
  739. offset_x = 0
  740. offset_y = extruder.getProperty("machine_nozzle_offset_y", "value")
  741. if offset_y is None:
  742. offset_y = 0
  743. offset_y = -offset_y #Y direction of g-code is the inverse of Y direction of Cura's scene space.
  744. result[extruder_id] = []
  745. for polygon in machine_disallowed_polygons:
  746. result[extruder_id].append(polygon.translate(offset_x, offset_y)) #Compensate for the nozzle offset of this extruder.
  747. #Add the border around the edge of the build volume.
  748. left_unreachable_border = 0
  749. right_unreachable_border = 0
  750. top_unreachable_border = 0
  751. bottom_unreachable_border = 0
  752. # Only do nozzle offsetting if needed
  753. if nozzle_offsetting_for_disallowed_areas:
  754. #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.
  755. for other_extruder in ExtruderManager.getInstance().getActiveExtruderStacks():
  756. other_offset_x = other_extruder.getProperty("machine_nozzle_offset_x", "value")
  757. if other_offset_x is None:
  758. other_offset_x = 0
  759. other_offset_y = other_extruder.getProperty("machine_nozzle_offset_y", "value")
  760. if other_offset_y is None:
  761. other_offset_y = 0
  762. other_offset_y = -other_offset_y
  763. left_unreachable_border = min(left_unreachable_border, other_offset_x - offset_x)
  764. right_unreachable_border = max(right_unreachable_border, other_offset_x - offset_x)
  765. top_unreachable_border = min(top_unreachable_border, other_offset_y - offset_y)
  766. bottom_unreachable_border = max(bottom_unreachable_border, other_offset_y - offset_y)
  767. half_machine_width = self._global_container_stack.getProperty("machine_width", "value") / 2
  768. half_machine_depth = self._global_container_stack.getProperty("machine_depth", "value") / 2
  769. if self._shape != "elliptic":
  770. if border_size - left_unreachable_border > 0:
  771. result[extruder_id].append(Polygon(numpy.array([
  772. [-half_machine_width, -half_machine_depth],
  773. [-half_machine_width, half_machine_depth],
  774. [-half_machine_width + border_size - left_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border],
  775. [-half_machine_width + border_size - left_unreachable_border, -half_machine_depth + border_size - top_unreachable_border]
  776. ], numpy.float32)))
  777. if border_size + right_unreachable_border > 0:
  778. result[extruder_id].append(Polygon(numpy.array([
  779. [half_machine_width, half_machine_depth],
  780. [half_machine_width, -half_machine_depth],
  781. [half_machine_width - border_size - right_unreachable_border, -half_machine_depth + border_size - top_unreachable_border],
  782. [half_machine_width - border_size - right_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border]
  783. ], numpy.float32)))
  784. if border_size + bottom_unreachable_border > 0:
  785. result[extruder_id].append(Polygon(numpy.array([
  786. [-half_machine_width, half_machine_depth],
  787. [half_machine_width, half_machine_depth],
  788. [half_machine_width - border_size - right_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border],
  789. [-half_machine_width + border_size - left_unreachable_border, half_machine_depth - border_size - bottom_unreachable_border]
  790. ], numpy.float32)))
  791. if border_size - top_unreachable_border > 0:
  792. result[extruder_id].append(Polygon(numpy.array([
  793. [half_machine_width, -half_machine_depth],
  794. [-half_machine_width, -half_machine_depth],
  795. [-half_machine_width + border_size - left_unreachable_border, -half_machine_depth + border_size - top_unreachable_border],
  796. [half_machine_width - border_size - right_unreachable_border, -half_machine_depth + border_size - top_unreachable_border]
  797. ], numpy.float32)))
  798. else:
  799. sections = 32
  800. arc_vertex = [0, half_machine_depth - border_size]
  801. for i in range(0, sections):
  802. quadrant = math.floor(4 * i / sections)
  803. vertices = []
  804. if quadrant == 0:
  805. vertices.append([-half_machine_width, half_machine_depth])
  806. elif quadrant == 1:
  807. vertices.append([-half_machine_width, -half_machine_depth])
  808. elif quadrant == 2:
  809. vertices.append([half_machine_width, -half_machine_depth])
  810. elif quadrant == 3:
  811. vertices.append([half_machine_width, half_machine_depth])
  812. vertices.append(arc_vertex)
  813. angle = 2 * math.pi * (i + 1) / sections
  814. arc_vertex = [-(half_machine_width - border_size) * math.sin(angle), (half_machine_depth - border_size) * math.cos(angle)]
  815. vertices.append(arc_vertex)
  816. result[extruder_id].append(Polygon(numpy.array(vertices, numpy.float32)))
  817. if border_size > 0:
  818. result[extruder_id].append(Polygon(numpy.array([
  819. [-half_machine_width, -half_machine_depth],
  820. [-half_machine_width, half_machine_depth],
  821. [-half_machine_width + border_size, 0]
  822. ], numpy.float32)))
  823. result[extruder_id].append(Polygon(numpy.array([
  824. [-half_machine_width, half_machine_depth],
  825. [ half_machine_width, half_machine_depth],
  826. [ 0, half_machine_depth - border_size]
  827. ], numpy.float32)))
  828. result[extruder_id].append(Polygon(numpy.array([
  829. [ half_machine_width, half_machine_depth],
  830. [ half_machine_width, -half_machine_depth],
  831. [ half_machine_width - border_size, 0]
  832. ], numpy.float32)))
  833. result[extruder_id].append(Polygon(numpy.array([
  834. [ half_machine_width,-half_machine_depth],
  835. [-half_machine_width,-half_machine_depth],
  836. [ 0, -half_machine_depth + border_size]
  837. ], numpy.float32)))
  838. return result
  839. ## Private convenience function to get a setting from every extruder.
  840. #
  841. # For single extrusion machines, this gets the setting from the global
  842. # stack.
  843. #
  844. # \return A sequence of setting values, one for each extruder.
  845. def _getSettingFromAllExtruders(self, setting_key):
  846. all_values = ExtruderManager.getInstance().getAllExtruderSettings(setting_key, "value")
  847. all_types = ExtruderManager.getInstance().getAllExtruderSettings(setting_key, "type")
  848. for i in range(len(all_values)):
  849. if not all_values[i] and (all_types[i] == "int" or all_types[i] == "float"):
  850. all_values[i] = 0
  851. return all_values
  852. ## Calculate the disallowed radius around the edge.
  853. #
  854. # This disallowed radius is to allow for space around the models that is
  855. # not part of the collision radius, such as bed adhesion (skirt/brim/raft)
  856. # and travel avoid distance.
  857. def getEdgeDisallowedSize(self):
  858. if not self._global_container_stack or not self._global_container_stack.extruders:
  859. return 0
  860. container_stack = self._global_container_stack
  861. used_extruders = ExtruderManager.getInstance().getUsedExtruderStacks()
  862. # If we are printing one at a time, we need to add the bed adhesion size to the disallowed areas of the objects
  863. if container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  864. return 0.1 # Return a very small value, so we do draw disallowed area's near the edges.
  865. adhesion_type = container_stack.getProperty("adhesion_type", "value")
  866. skirt_brim_line_width = self._global_container_stack.getProperty("skirt_brim_line_width", "value")
  867. initial_layer_line_width_factor = self._global_container_stack.getProperty("initial_layer_line_width_factor", "value")
  868. if adhesion_type == "skirt":
  869. skirt_distance = self._global_container_stack.getProperty("skirt_gap", "value")
  870. skirt_line_count = self._global_container_stack.getProperty("skirt_line_count", "value")
  871. bed_adhesion_size = skirt_distance + (skirt_brim_line_width * skirt_line_count) * initial_layer_line_width_factor / 100.0
  872. for extruder_stack in used_extruders:
  873. bed_adhesion_size += extruder_stack.getProperty("skirt_brim_line_width", "value") * extruder_stack.getProperty("initial_layer_line_width_factor", "value") / 100.0
  874. # We don't create an additional line for the extruder we're printing the skirt with.
  875. bed_adhesion_size -= skirt_brim_line_width * initial_layer_line_width_factor / 100.0
  876. elif (adhesion_type == "brim" or
  877. (self._global_container_stack.getProperty("prime_tower_brim_enable", "value") and
  878. self._global_container_stack.getProperty("adhesion_type", "value") != "raft")):
  879. brim_line_count = self._global_container_stack.getProperty("brim_line_count", "value")
  880. bed_adhesion_size = skirt_brim_line_width * brim_line_count * initial_layer_line_width_factor / 100.0
  881. for extruder_stack in used_extruders:
  882. bed_adhesion_size += extruder_stack.getProperty("skirt_brim_line_width", "value") * extruder_stack.getProperty("initial_layer_line_width_factor", "value") / 100.0
  883. # We don't create an additional line for the extruder we're printing the brim with.
  884. bed_adhesion_size -= skirt_brim_line_width * initial_layer_line_width_factor / 100.0
  885. elif adhesion_type == "raft":
  886. bed_adhesion_size = self._global_container_stack.getProperty("raft_margin", "value")
  887. elif adhesion_type == "none":
  888. bed_adhesion_size = 0
  889. else:
  890. raise Exception("Unknown bed adhesion type. Did you forget to update the build volume calculations for your new bed adhesion type?")
  891. max_length_available = 0.5 * min(
  892. self._global_container_stack.getProperty("machine_width", "value"),
  893. self._global_container_stack.getProperty("machine_depth", "value")
  894. )
  895. bed_adhesion_size = min(bed_adhesion_size, max_length_available)
  896. support_expansion = 0
  897. support_enabled = self._global_container_stack.getProperty("support_enable", "value")
  898. support_offset = self._global_container_stack.getProperty("support_offset", "value")
  899. if support_enabled and support_offset:
  900. support_expansion += support_offset
  901. farthest_shield_distance = 0
  902. if container_stack.getProperty("draft_shield_enabled", "value"):
  903. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("draft_shield_dist", "value"))
  904. if container_stack.getProperty("ooze_shield_enabled", "value"):
  905. farthest_shield_distance = max(farthest_shield_distance, container_stack.getProperty("ooze_shield_dist", "value"))
  906. move_from_wall_radius = 0 # Moves that start from outer wall.
  907. move_from_wall_radius = max(move_from_wall_radius, max(self._getSettingFromAllExtruders("infill_wipe_dist")))
  908. avoid_enabled_per_extruder = [stack.getProperty("travel_avoid_other_parts","value") for stack in used_extruders]
  909. travel_avoid_distance_per_extruder = [stack.getProperty("travel_avoid_distance", "value") for stack in used_extruders]
  910. for avoid_other_parts_enabled, avoid_distance in zip(avoid_enabled_per_extruder, travel_avoid_distance_per_extruder): #For each extruder (or just global).
  911. if avoid_other_parts_enabled:
  912. move_from_wall_radius = max(move_from_wall_radius, avoid_distance)
  913. # Now combine our different pieces of data to get the final border size.
  914. # Support expansion is added to the bed adhesion, since the bed adhesion goes around support.
  915. # Support expansion is added to farthest shield distance, since the shields go around support.
  916. border_size = max(move_from_wall_radius, support_expansion + farthest_shield_distance, support_expansion + bed_adhesion_size)
  917. return border_size
  918. def _clamp(self, value, min_value, max_value):
  919. return max(min(value, max_value), min_value)
  920. _machine_settings = ["machine_width", "machine_depth", "machine_height", "machine_shape", "machine_center_is_zero"]
  921. _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", "initial_layer_line_width_factor"]
  922. _raft_settings = ["adhesion_type", "raft_base_thickness", "raft_interface_thickness", "raft_surface_layers", "raft_surface_thickness", "raft_airgap", "layer_0_z_overlap"]
  923. _extra_z_settings = ["retraction_hop_enabled", "retraction_hop"]
  924. _prime_settings = ["extruder_prime_pos_x", "extruder_prime_pos_y", "extruder_prime_pos_z", "prime_blob_enable"]
  925. _tower_settings = ["prime_tower_enable", "prime_tower_circular", "prime_tower_size", "prime_tower_position_x", "prime_tower_position_y", "prime_tower_brim_enable"]
  926. _ooze_shield_settings = ["ooze_shield_enabled", "ooze_shield_dist"]
  927. _distance_settings = ["infill_wipe_dist", "travel_avoid_distance", "support_offset", "support_enable", "travel_avoid_other_parts", "travel_avoid_supports"]
  928. _extruder_settings = ["support_enable", "support_bottom_enable", "support_roof_enable", "support_infill_extruder_nr", "support_extruder_nr_layer_0", "support_bottom_extruder_nr", "support_roof_extruder_nr", "brim_line_count", "adhesion_extruder_nr", "adhesion_type"] #Settings that can affect which extruders are used.
  929. _limit_to_extruder_settings = ["wall_extruder_nr", "wall_0_extruder_nr", "wall_x_extruder_nr", "top_bottom_extruder_nr", "infill_extruder_nr", "support_infill_extruder_nr", "support_extruder_nr_layer_0", "support_bottom_extruder_nr", "support_roof_extruder_nr", "adhesion_extruder_nr"]