BuildVolume.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.i18n import i18nCatalog
  4. from UM.Scene.SceneNode import SceneNode
  5. from UM.Application import Application
  6. from UM.Resources import Resources
  7. from UM.Mesh.MeshBuilder import MeshBuilder
  8. from UM.Math.Vector import Vector
  9. from UM.Math.Color import Color
  10. from UM.Math.AxisAlignedBox import AxisAlignedBox
  11. from UM.Math.Polygon import Polygon
  12. from UM.Message import Message
  13. from UM.View.RenderBatch import RenderBatch
  14. from UM.View.GL.OpenGL import OpenGL
  15. catalog = i18nCatalog("cura")
  16. import numpy
  17. ## Build volume is a special kind of node that is responsible for rendering the printable area & disallowed areas.
  18. class BuildVolume(SceneNode):
  19. VolumeOutlineColor = Color(12, 169, 227, 255)
  20. def __init__(self, parent = None):
  21. super().__init__(parent)
  22. self._width = 0
  23. self._height = 0
  24. self._depth = 0
  25. self._shader = None
  26. self._grid_mesh = None
  27. self._grid_shader = None
  28. self._disallowed_areas = []
  29. self._disallowed_area_mesh = None
  30. self.setCalculateBoundingBox(False)
  31. self._volume_aabb = None
  32. self._active_container_stack = None
  33. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerStackChanged)
  34. self._onGlobalContainerStackChanged()
  35. def setWidth(self, width):
  36. if width: self._width = width
  37. def setHeight(self, height):
  38. if height: self._height = height
  39. def setDepth(self, depth):
  40. if depth: self._depth = depth
  41. def getDisallowedAreas(self):
  42. return self._disallowed_areas
  43. def setDisallowedAreas(self, areas):
  44. self._disallowed_areas = areas
  45. def render(self, renderer):
  46. if not self.getMeshData():
  47. return True
  48. if not self._shader:
  49. self._shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "default.shader"))
  50. self._grid_shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "grid.shader"))
  51. renderer.queueNode(self, mode = RenderBatch.RenderMode.Lines)
  52. renderer.queueNode(self, mesh = self._grid_mesh, shader = self._grid_shader, backface_cull = True)
  53. if self._disallowed_area_mesh:
  54. renderer.queueNode(self, mesh = self._disallowed_area_mesh, shader = self._shader, transparent = True, backface_cull = True, sort = -9)
  55. return True
  56. ## Recalculates the build volume & disallowed areas.
  57. def rebuild(self):
  58. if not self._width or not self._height or not self._depth:
  59. return
  60. min_w = -self._width / 2
  61. max_w = self._width / 2
  62. min_h = 0.0
  63. max_h = self._height
  64. min_d = -self._depth / 2
  65. max_d = self._depth / 2
  66. mb = MeshBuilder()
  67. mb.addLine(Vector(min_w, min_h, min_d), Vector(max_w, min_h, min_d), color = self.VolumeOutlineColor)
  68. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, max_h, min_d), color = self.VolumeOutlineColor)
  69. mb.addLine(Vector(min_w, max_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  70. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  71. mb.addLine(Vector(min_w, min_h, max_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  72. mb.addLine(Vector(min_w, min_h, max_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  73. mb.addLine(Vector(min_w, max_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  74. mb.addLine(Vector(max_w, min_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  75. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, min_h, max_d), color = self.VolumeOutlineColor)
  76. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  77. mb.addLine(Vector(min_w, max_h, min_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  78. mb.addLine(Vector(max_w, max_h, min_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  79. self.setMeshData(mb.build())
  80. mb = MeshBuilder()
  81. mb.addQuad(
  82. Vector(min_w, min_h - 0.2, min_d),
  83. Vector(max_w, min_h - 0.2, min_d),
  84. Vector(max_w, min_h - 0.2, max_d),
  85. Vector(min_w, min_h - 0.2, max_d)
  86. )
  87. for n in range(0, 6):
  88. v = mb.getVertex(n)
  89. mb.setVertexUVCoordinates(n, v[0], v[2])
  90. self._grid_mesh = mb.build()
  91. disallowed_area_height = 0.1
  92. disallowed_area_size = 0
  93. if self._disallowed_areas:
  94. mb = MeshBuilder()
  95. color = Color(0.0, 0.0, 0.0, 0.15)
  96. for polygon in self._disallowed_areas:
  97. points = polygon.getPoints()
  98. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  99. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  100. for point in points:
  101. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height, self._clamp(point[1], min_d, max_d))
  102. mb.addFace(first, previous_point, new_point, color = color)
  103. previous_point = new_point
  104. # Find the largest disallowed area to exclude it from the maximum scale bounds.
  105. # This is a very nasty hack. This pretty much only works for UM machines.
  106. # This disallowed area_size needs a -lot- of rework at some point in the future: TODO
  107. 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.
  108. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  109. else:
  110. size = 0
  111. disallowed_area_size = max(size, disallowed_area_size)
  112. self._disallowed_area_mesh = mb.build()
  113. else:
  114. self._disallowed_area_mesh = None
  115. self._volume_aabb = AxisAlignedBox(minimum = Vector(min_w, min_h - 1.0, min_d), maximum = Vector(max_w, max_h, max_d))
  116. skirt_size = 0.0
  117. container_stack = Application.getInstance().getGlobalContainerStack()
  118. if container_stack:
  119. skirt_size = self._getSkirtSize(container_stack)
  120. # As this works better for UM machines, we only add the disallowed_area_size for the z direction.
  121. # This is probably wrong in all other cases. TODO!
  122. # The +1 and -1 is added as there is always a bit of extra room required to work properly.
  123. scale_to_max_bounds = AxisAlignedBox(
  124. minimum = Vector(min_w + skirt_size + 1, min_h, min_d + disallowed_area_size - skirt_size + 1),
  125. maximum = Vector(max_w - skirt_size - 1, max_h, max_d - disallowed_area_size + skirt_size - 1)
  126. )
  127. Application.getInstance().getController().getScene()._maximum_bounds = scale_to_max_bounds
  128. def getBoundingBox(self):
  129. return self._volume_aabb
  130. def _buildVolumeMessage(self):
  131. Message(catalog.i18nc(
  132. "@info:status",
  133. "The build volume height has been reduced due to the value of the"
  134. " \"Print Sequence\" setting to prevent the gantry from colliding"
  135. " with printed objects."), lifetime=10).show()
  136. def _onGlobalContainerStackChanged(self):
  137. if self._active_container_stack:
  138. self._active_container_stack.propertyChanged.disconnect(self._onSettingPropertyChanged)
  139. self._active_container_stack = Application.getInstance().getGlobalContainerStack()
  140. if self._active_container_stack:
  141. self._active_container_stack.propertyChanged.connect(self._onSettingPropertyChanged)
  142. self._width = self._active_container_stack.getProperty("machine_width", "value")
  143. if self._active_container_stack.getProperty("print_sequence", "value") == "one_at_a_time":
  144. self._height = self._active_container_stack.getProperty("gantry_height", "value")
  145. self._buildVolumeMessage()
  146. else:
  147. self._height = self._active_container_stack.getProperty("machine_height", "value")
  148. self._depth = self._active_container_stack.getProperty("machine_depth", "value")
  149. self._updateDisallowedAreas()
  150. self.rebuild()
  151. def _onSettingPropertyChanged(self, setting_key, property_name):
  152. if property_name != "value":
  153. return
  154. if setting_key == "print_sequence":
  155. if Application.getInstance().getGlobalContainerStack().getProperty("print_sequence", "value") == "one_at_a_time":
  156. self._height = self._active_container_stack.getProperty("gantry_height", "value")
  157. self._buildVolumeMessage()
  158. else:
  159. self._height = self._active_container_stack.getProperty("machine_height", "value")
  160. self.rebuild()
  161. if setting_key in self._skirt_settings:
  162. self._updateDisallowedAreas()
  163. self.rebuild()
  164. def _updateDisallowedAreas(self):
  165. if not self._active_container_stack:
  166. return
  167. disallowed_areas = self._active_container_stack.getProperty("machine_disallowed_areas", "value")
  168. areas = []
  169. skirt_size = self._getSkirtSize(self._active_container_stack)
  170. if disallowed_areas:
  171. # Extend every area already in the disallowed_areas with the skirt size.
  172. for area in disallowed_areas:
  173. poly = Polygon(numpy.array(area, numpy.float32))
  174. poly = poly.getMinkowskiHull(Polygon(numpy.array([
  175. [-skirt_size, 0],
  176. [-skirt_size * 0.707, skirt_size * 0.707],
  177. [0, skirt_size],
  178. [skirt_size * 0.707, skirt_size * 0.707],
  179. [skirt_size, 0],
  180. [skirt_size * 0.707, -skirt_size * 0.707],
  181. [0, -skirt_size],
  182. [-skirt_size * 0.707, -skirt_size * 0.707]
  183. ], numpy.float32)))
  184. areas.append(poly)
  185. # Add the skirt areas around the borders of the build plate.
  186. if skirt_size > 0:
  187. half_machine_width = self._active_container_stack.getProperty("machine_width", "value") / 2
  188. half_machine_depth = self._active_container_stack.getProperty("machine_depth", "value") / 2
  189. areas.append(Polygon(numpy.array([
  190. [-half_machine_width, -half_machine_depth],
  191. [-half_machine_width, half_machine_depth],
  192. [-half_machine_width + skirt_size, half_machine_depth - skirt_size],
  193. [-half_machine_width + skirt_size, -half_machine_depth + skirt_size]
  194. ], numpy.float32)))
  195. areas.append(Polygon(numpy.array([
  196. [half_machine_width, half_machine_depth],
  197. [half_machine_width, -half_machine_depth],
  198. [half_machine_width - skirt_size, -half_machine_depth + skirt_size],
  199. [half_machine_width - skirt_size, half_machine_depth - skirt_size]
  200. ], numpy.float32)))
  201. areas.append(Polygon(numpy.array([
  202. [-half_machine_width, half_machine_depth],
  203. [half_machine_width, half_machine_depth],
  204. [half_machine_width - skirt_size, half_machine_depth - skirt_size],
  205. [-half_machine_width + skirt_size, half_machine_depth - skirt_size]
  206. ], numpy.float32)))
  207. areas.append(Polygon(numpy.array([
  208. [half_machine_width, -half_machine_depth],
  209. [-half_machine_width, -half_machine_depth],
  210. [-half_machine_width + skirt_size, -half_machine_depth + skirt_size],
  211. [half_machine_width - skirt_size, -half_machine_depth + skirt_size]
  212. ], numpy.float32)))
  213. self._disallowed_areas = areas
  214. ## Convenience function to calculate the size of the bed adhesion.
  215. def _getSkirtSize(self, container_stack):
  216. skirt_size = 0.0
  217. adhesion_type = container_stack.getProperty("adhesion_type", "value")
  218. if adhesion_type == "skirt":
  219. skirt_distance = container_stack.getProperty("skirt_gap", "value")
  220. skirt_line_count = container_stack.getProperty("skirt_line_count", "value")
  221. skirt_size = skirt_distance + (skirt_line_count * container_stack.getProperty("skirt_line_width", "value"))
  222. elif adhesion_type == "brim":
  223. skirt_size = container_stack.getProperty("brim_line_count", "value") * container_stack.getProperty("skirt_line_width", "value")
  224. elif adhesion_type == "raft":
  225. skirt_size = container_stack.getProperty("raft_margin", "value")
  226. if container_stack.getProperty("draft_shield_enabled", "value"):
  227. skirt_size += container_stack.getProperty("draft_shield_dist", "value")
  228. if container_stack.getProperty("xy_offset", "value"):
  229. skirt_size += container_stack.getProperty("xy_offset", "value")
  230. return skirt_size
  231. def _clamp(self, value, min_value, max_value):
  232. return max(min(value, max_value), min_value)
  233. _skirt_settings = ["adhesion_type", "skirt_gap", "skirt_line_count", "skirt_line_width", "brim_width", "brim_line_count", "raft_margin", "draft_shield_enabled", "draft_shield_dist", "xy_offset"]