BuildVolume.py 56 KB

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