BuildVolume.py 56 KB

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