BuildVolume.py 55 KB

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