BuildVolume.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from cura.Settings.ExtruderManager import ExtruderManager
  4. from UM.i18n import i18nCatalog
  5. from UM.Scene.Platform import Platform
  6. from UM.Scene.SceneNode import SceneNode
  7. from UM.Application import Application
  8. from UM.Resources import Resources
  9. from UM.Mesh.MeshBuilder import MeshBuilder
  10. from UM.Math.Vector import Vector
  11. from UM.Math.Color import Color
  12. from UM.Math.AxisAlignedBox import AxisAlignedBox
  13. from UM.Math.Polygon import Polygon
  14. from UM.Message import Message
  15. from UM.Signal import Signal
  16. from UM.View.RenderBatch import RenderBatch
  17. from UM.View.GL.OpenGL import OpenGL
  18. catalog = i18nCatalog("cura")
  19. import numpy
  20. import copy
  21. # Setting for clearance around the prime
  22. PRIME_CLEARANCE = 10
  23. def approximatedCircleVertices(r):
  24. """
  25. Return vertices from an approximated circle.
  26. :param r: radius
  27. :return: numpy 2-array with the vertices
  28. """
  29. return numpy.array([
  30. [-r, 0],
  31. [-r * 0.707, r * 0.707],
  32. [0, r],
  33. [r * 0.707, r * 0.707],
  34. [r, 0],
  35. [r * 0.707, -r * 0.707],
  36. [0, -r],
  37. [-r * 0.707, -r * 0.707]
  38. ], numpy.float32)
  39. ## Build volume is a special kind of node that is responsible for rendering the printable area & disallowed areas.
  40. class BuildVolume(SceneNode):
  41. VolumeOutlineColor = Color(12, 169, 227, 255)
  42. raftThicknessChanged = Signal()
  43. def __init__(self, parent = None):
  44. super().__init__(parent)
  45. self._width = 0
  46. self._height = 0
  47. self._depth = 0
  48. self._shader = None
  49. self._grid_mesh = None
  50. self._grid_shader = None
  51. self._disallowed_areas = []
  52. self._disallowed_area_mesh = None
  53. self.setCalculateBoundingBox(False)
  54. self._volume_aabb = None
  55. self._raft_thickness = 0.0
  56. self._adhesion_type = None
  57. self._platform = Platform(self)
  58. self._active_container_stack = None
  59. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerStackChanged)
  60. self._onGlobalContainerStackChanged()
  61. def setWidth(self, width):
  62. if width: self._width = width
  63. def setHeight(self, height):
  64. if height: self._height = height
  65. def setDepth(self, depth):
  66. if depth: self._depth = depth
  67. def getDisallowedAreas(self):
  68. return self._disallowed_areas
  69. def setDisallowedAreas(self, areas):
  70. self._disallowed_areas = areas
  71. def render(self, renderer):
  72. if not self.getMeshData():
  73. return True
  74. if not self._shader:
  75. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "default.shader"))
  76. self._grid_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "grid.shader"))
  77. renderer.queueNode(self, mode = RenderBatch.RenderMode.Lines)
  78. renderer.queueNode(self, mesh = self._grid_mesh, shader = self._grid_shader, backface_cull = True)
  79. if self._disallowed_area_mesh:
  80. renderer.queueNode(self, mesh = self._disallowed_area_mesh, shader = self._shader, transparent = True, backface_cull = True, sort = -9)
  81. return True
  82. ## Recalculates the build volume & disallowed areas.
  83. def rebuild(self):
  84. if not self._width or not self._height or not self._depth:
  85. return
  86. min_w = -self._width / 2
  87. max_w = self._width / 2
  88. min_h = 0.0
  89. max_h = self._height
  90. min_d = -self._depth / 2
  91. max_d = self._depth / 2
  92. mb = MeshBuilder()
  93. # Outline 'cube' of the build volume
  94. mb.addLine(Vector(min_w, min_h, min_d), Vector(max_w, min_h, min_d), color = self.VolumeOutlineColor)
  95. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, max_h, min_d), color = self.VolumeOutlineColor)
  96. mb.addLine(Vector(min_w, max_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  97. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  98. mb.addLine(Vector(min_w, min_h, max_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  99. mb.addLine(Vector(min_w, min_h, max_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  100. mb.addLine(Vector(min_w, max_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  101. mb.addLine(Vector(max_w, min_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  102. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, min_h, max_d), color = self.VolumeOutlineColor)
  103. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  104. mb.addLine(Vector(min_w, max_h, min_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  105. mb.addLine(Vector(max_w, max_h, min_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  106. self.setMeshData(mb.build())
  107. mb = MeshBuilder()
  108. mb.addQuad(
  109. Vector(min_w, min_h - 0.2, min_d),
  110. Vector(max_w, min_h - 0.2, min_d),
  111. Vector(max_w, min_h - 0.2, max_d),
  112. Vector(min_w, min_h - 0.2, max_d)
  113. )
  114. for n in range(0, 6):
  115. v = mb.getVertex(n)
  116. mb.setVertexUVCoordinates(n, v[0], v[2])
  117. self._grid_mesh = mb.build()
  118. disallowed_area_height = 0.1
  119. disallowed_area_size = 0
  120. if self._disallowed_areas:
  121. mb = MeshBuilder()
  122. color = Color(0.0, 0.0, 0.0, 0.15)
  123. for polygon in self._disallowed_areas:
  124. points = polygon.getPoints()
  125. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  126. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  127. for point in points:
  128. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height, self._clamp(point[1], min_d, max_d))
  129. mb.addFace(first, previous_point, new_point, color = color)
  130. previous_point = new_point
  131. # Find the largest disallowed area to exclude it from the maximum scale bounds.
  132. # This is a very nasty hack. This pretty much only works for UM machines.
  133. # This disallowed area_size needs a -lot- of rework at some point in the future: TODO
  134. 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.
  135. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  136. else:
  137. size = 0
  138. disallowed_area_size = max(size, disallowed_area_size)
  139. self._disallowed_area_mesh = mb.build()
  140. else:
  141. self._disallowed_area_mesh = None
  142. self._volume_aabb = AxisAlignedBox(
  143. minimum = Vector(min_w, min_h - 1.0, min_d),
  144. maximum = Vector(max_w, max_h - self._raft_thickness, max_d))
  145. bed_adhesion_size = 0.0
  146. container_stack = Application.getInstance().getGlobalContainerStack()
  147. if container_stack:
  148. bed_adhesion_size = self._getBedAdhesionSize(container_stack)
  149. # As this works better for UM machines, we only add the disallowed_area_size for the z direction.
  150. # This is probably wrong in all other cases. TODO!
  151. # The +1 and -1 is added as there is always a bit of extra room required to work properly.
  152. scale_to_max_bounds = AxisAlignedBox(
  153. minimum = Vector(min_w + bed_adhesion_size + 1, min_h, min_d + disallowed_area_size - bed_adhesion_size + 1),
  154. maximum = Vector(max_w - bed_adhesion_size - 1, max_h - self._raft_thickness, max_d - disallowed_area_size + bed_adhesion_size - 1)
  155. )
  156. Application.getInstance().getController().getScene()._maximum_bounds = scale_to_max_bounds
  157. def getBoundingBox(self):
  158. return self._volume_aabb
  159. def _buildVolumeMessage(self):
  160. Message(catalog.i18nc(
  161. "@info:status",
  162. "The build volume height has been reduced due to the value of the"
  163. " \"Print Sequence\" setting to prevent the gantry from colliding"
  164. " with printed models."), lifetime=10).show()
  165. def getRaftThickness(self):
  166. return self._raft_thickness
  167. def _updateRaftThickness(self):
  168. old_raft_thickness = self._raft_thickness
  169. self._adhesion_type = self._active_container_stack.getProperty("adhesion_type", "value")
  170. self._raft_thickness = 0.0
  171. if self._adhesion_type == "raft":
  172. self._raft_thickness = (
  173. self._active_container_stack.getProperty("raft_base_thickness", "value") +
  174. self._active_container_stack.getProperty("raft_interface_thickness", "value") +
  175. self._active_container_stack.getProperty("raft_surface_layers", "value") *
  176. self._active_container_stack.getProperty("raft_surface_thickness", "value") +
  177. self._active_container_stack.getProperty("raft_airgap", "value"))
  178. # Rounding errors do not matter, we check if raft_thickness has changed at all
  179. if old_raft_thickness != self._raft_thickness:
  180. self.setPosition(Vector(0, -self._raft_thickness, 0), SceneNode.TransformSpace.World)
  181. self.raftThicknessChanged.emit()
  182. def _onGlobalContainerStackChanged(self):
  183. if self._active_container_stack:
  184. self._active_container_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  185. self._active_container_stack = Application.getInstance().getGlobalContainerStack()
  186. if self._active_container_stack:
  187. self._active_container_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  188. self._width = self._active_container_stack.getProperty("machine_width", "value")
  189. machine_height = self._active_container_stack.getProperty("machine_height", "value")
  190. if self._active_container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  191. self._height = min(self._active_container_stack.getProperty("gantry_height", "value"), machine_height)
  192. if self._height < machine_height:
  193. self._buildVolumeMessage()
  194. else:
  195. self._height = self._active_container_stack.getProperty("machine_height", "value")
  196. self._depth = self._active_container_stack.getProperty("machine_depth", "value")
  197. self._updateDisallowedAreas()
  198. self._updateRaftThickness()
  199. self.rebuild()
  200. def _onSettingPropertyChanged(self, setting_key, property_name):
  201. if property_name != "value":
  202. return
  203. rebuild_me = False
  204. if setting_key == "print_sequence":
  205. machine_height = self._active_container_stack.getProperty("machine_height", "value")
  206. if Application.getInstance().getGlobalContainerStack().getProperty("print_sequence", "value") == "one_at_a_time":
  207. self._height = min(self._active_container_stack.getProperty("gantry_height", "value"), machine_height)
  208. if self._height < machine_height:
  209. self._buildVolumeMessage()
  210. else:
  211. self._height = self._active_container_stack.getProperty("machine_height", "value")
  212. rebuild_me = True
  213. if setting_key in self._skirt_settings or setting_key in self._prime_settings or setting_key in self._tower_settings:
  214. self._updateDisallowedAreas()
  215. rebuild_me = True
  216. if setting_key in self._raft_settings:
  217. self._updateRaftThickness()
  218. rebuild_me = True
  219. if rebuild_me:
  220. self.rebuild()
  221. def _updateDisallowedAreas(self):
  222. if not self._active_container_stack:
  223. return
  224. disallowed_areas = copy.deepcopy(
  225. self._active_container_stack.getProperty("machine_disallowed_areas", "value"))
  226. areas = []
  227. machine_width = self._active_container_stack.getProperty("machine_width", "value")
  228. machine_depth = self._active_container_stack.getProperty("machine_depth", "value")
  229. # Add prima tower location as disallowed area.
  230. if self._active_container_stack.getProperty("prime_tower_enable", "value"):
  231. half_prime_tower_size = self._active_container_stack.getProperty("prime_tower_size", "value") / 2
  232. prime_tower_x = self._active_container_stack.getProperty("prime_tower_position_x", "value") - machine_width / 2
  233. prime_tower_y = - self._active_container_stack.getProperty("prime_tower_position_y", "value") + machine_depth / 2
  234. disallowed_areas.append([
  235. [prime_tower_x - half_prime_tower_size, prime_tower_y - half_prime_tower_size],
  236. [prime_tower_x + half_prime_tower_size, prime_tower_y - half_prime_tower_size],
  237. [prime_tower_x + half_prime_tower_size, prime_tower_y + half_prime_tower_size],
  238. [prime_tower_x - half_prime_tower_size, prime_tower_y + half_prime_tower_size],
  239. ])
  240. # Add extruder prime locations as disallowed areas.
  241. # Probably needs some rework after coordinate system change.
  242. extruder_manager = ExtruderManager.getInstance()
  243. extruders = extruder_manager.getMachineExtruders(self._active_container_stack.getId())
  244. for single_extruder in extruders:
  245. extruder_prime_pos_x = single_extruder.getProperty("extruder_prime_pos_x", "value")
  246. extruder_prime_pos_y = single_extruder.getProperty("extruder_prime_pos_y", "value")
  247. # TODO: calculate everything in CuraEngine/Firmware/lower left as origin coordinates.
  248. # Here we transform the extruder prime pos (lower left as origin) to Cura coordinates
  249. # (center as origin, y from back to front)
  250. prime_x = extruder_prime_pos_x - machine_width / 2
  251. prime_y = machine_depth / 2 - extruder_prime_pos_y
  252. disallowed_areas.append([
  253. [prime_x - PRIME_CLEARANCE, prime_y - PRIME_CLEARANCE],
  254. [prime_x + PRIME_CLEARANCE, prime_y - PRIME_CLEARANCE],
  255. [prime_x + PRIME_CLEARANCE, prime_y + PRIME_CLEARANCE],
  256. [prime_x - PRIME_CLEARANCE, prime_y + PRIME_CLEARANCE],
  257. ])
  258. bed_adhesion_size = self._getBedAdhesionSize(self._active_container_stack)
  259. if disallowed_areas:
  260. # Extend every area already in the disallowed_areas with the skirt size.
  261. for area in disallowed_areas:
  262. poly = Polygon(numpy.array(area, numpy.float32))
  263. poly = poly.getMinkowskiHull(Polygon(approximatedCircleVertices(bed_adhesion_size)))
  264. areas.append(poly)
  265. # Add the skirt areas around the borders of the build plate.
  266. if bed_adhesion_size > 0:
  267. half_machine_width = self._active_container_stack.getProperty("machine_width", "value") / 2
  268. half_machine_depth = self._active_container_stack.getProperty("machine_depth", "value") / 2
  269. areas.append(Polygon(numpy.array([
  270. [-half_machine_width, -half_machine_depth],
  271. [-half_machine_width, half_machine_depth],
  272. [-half_machine_width + bed_adhesion_size, half_machine_depth - bed_adhesion_size],
  273. [-half_machine_width + bed_adhesion_size, -half_machine_depth + bed_adhesion_size]
  274. ], numpy.float32)))
  275. areas.append(Polygon(numpy.array([
  276. [half_machine_width, half_machine_depth],
  277. [half_machine_width, -half_machine_depth],
  278. [half_machine_width - bed_adhesion_size, -half_machine_depth + bed_adhesion_size],
  279. [half_machine_width - bed_adhesion_size, half_machine_depth - bed_adhesion_size]
  280. ], numpy.float32)))
  281. areas.append(Polygon(numpy.array([
  282. [-half_machine_width, half_machine_depth],
  283. [half_machine_width, half_machine_depth],
  284. [half_machine_width - bed_adhesion_size, half_machine_depth - bed_adhesion_size],
  285. [-half_machine_width + bed_adhesion_size, half_machine_depth - bed_adhesion_size]
  286. ], numpy.float32)))
  287. areas.append(Polygon(numpy.array([
  288. [half_machine_width, -half_machine_depth],
  289. [-half_machine_width, -half_machine_depth],
  290. [-half_machine_width + bed_adhesion_size, -half_machine_depth + bed_adhesion_size],
  291. [half_machine_width - bed_adhesion_size, -half_machine_depth + bed_adhesion_size]
  292. ], numpy.float32)))
  293. self._disallowed_areas = areas
  294. ## Convenience function to calculate the size of the bed adhesion in directions x, y.
  295. def _getBedAdhesionSize(self, container_stack):
  296. skirt_size = 0.0
  297. adhesion_type = container_stack.getProperty("adhesion_type", "value")
  298. if adhesion_type == "skirt":
  299. skirt_distance = container_stack.getProperty("skirt_gap", "value")
  300. skirt_line_count = container_stack.getProperty("skirt_line_count", "value")
  301. skirt_size = skirt_distance + (skirt_line_count * container_stack.getProperty("skirt_brim_line_width", "value"))
  302. elif adhesion_type == "brim":
  303. skirt_size = container_stack.getProperty("brim_line_count", "value") * container_stack.getProperty("skirt_brim_line_width", "value")
  304. elif adhesion_type == "raft":
  305. skirt_size = container_stack.getProperty("raft_margin", "value")
  306. if container_stack.getProperty("draft_shield_enabled", "value"):
  307. skirt_size += container_stack.getProperty("draft_shield_dist", "value")
  308. if container_stack.getProperty("xy_offset", "value"):
  309. skirt_size += container_stack.getProperty("xy_offset", "value")
  310. return skirt_size
  311. def _clamp(self, value, min_value, max_value):
  312. return max(min(value, max_value), min_value)
  313. _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", "xy_offset"]
  314. _raft_settings = ["adhesion_type", "raft_base_thickness", "raft_interface_thickness", "raft_surface_layers", "raft_surface_thickness", "raft_airgap"]
  315. _prime_settings = ["extruder_prime_pos_x", "extruder_prime_pos_y", "extruder_prime_pos_z"]
  316. _tower_settings = ["prime_tower_enable", "prime_tower_size", "prime_tower_position_x", "prime_tower_position_y"]