BuildVolume.py 19 KB

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