BuildVolume.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from UM.View.Renderer import Renderer
  4. from UM.Scene.SceneNode import SceneNode
  5. from UM.Application import Application
  6. from UM.Resources import Resources
  7. from UM.Mesh.MeshData import MeshData
  8. from UM.Mesh.MeshBuilder import MeshBuilder
  9. from UM.Math.Vector import Vector
  10. from UM.Math.Color import Color
  11. from UM.Math.AxisAlignedBox import AxisAlignedBox
  12. from UM.Math.Polygon import Polygon
  13. import numpy
  14. class BuildVolume(SceneNode):
  15. VolumeOutlineColor = Color(12, 169, 227, 255)
  16. def __init__(self, parent = None):
  17. super().__init__(parent)
  18. self._width = 0
  19. self._height = 0
  20. self._depth = 0
  21. self._material = None
  22. self._grid_mesh = None
  23. self._grid_material = None
  24. self._disallowed_areas = []
  25. self._disallowed_area_mesh = None
  26. self.setCalculateBoundingBox(False)
  27. self._active_profile = None
  28. self._active_instance = None
  29. Application.getInstance().getMachineManager().activeMachineInstanceChanged.connect(self._onActiveInstanceChanged)
  30. self._onActiveInstanceChanged()
  31. Application.getInstance().getMachineManager().activeProfileChanged.connect(self._onActiveProfileChanged)
  32. self._onActiveProfileChanged()
  33. def setWidth(self, width):
  34. if width: self._width = width
  35. def setHeight(self, height):
  36. if height: self._height = height
  37. def setDepth(self, depth):
  38. if depth: self._depth = depth
  39. def getDisallowedAreas(self):
  40. return self._disallowed_areas
  41. def setDisallowedAreas(self, areas):
  42. self._disallowed_areas = areas
  43. def render(self, renderer):
  44. if not self.getMeshData():
  45. return True
  46. if not self._material:
  47. self._material = renderer.createMaterial(
  48. Resources.getPath(Resources.Shaders, "basic.vert"),
  49. Resources.getPath(Resources.Shaders, "vertexcolor.frag")
  50. )
  51. self._grid_material = renderer.createMaterial(
  52. Resources.getPath(Resources.Shaders, "basic.vert"),
  53. Resources.getPath(Resources.Shaders, "grid.frag")
  54. )
  55. self._grid_material.setUniformValue("u_gridColor0", Color(245, 245, 245, 255))
  56. self._grid_material.setUniformValue("u_gridColor1", Color(205, 202, 201, 255))
  57. renderer.queueNode(self, material = self._material, mode = Renderer.RenderLines)
  58. renderer.queueNode(self, mesh = self._grid_mesh, material = self._grid_material, force_single_sided = True)
  59. if self._disallowed_area_mesh:
  60. renderer.queueNode(self, mesh = self._disallowed_area_mesh, material = self._material, transparent = True)
  61. return True
  62. def rebuild(self):
  63. if self._width == 0 or self._height == 0 or self._depth == 0:
  64. return
  65. min_w = -self._width / 2
  66. max_w = self._width / 2
  67. min_h = 0.0
  68. max_h = self._height
  69. min_d = -self._depth / 2
  70. max_d = self._depth / 2
  71. mb = MeshBuilder()
  72. mb.addLine(Vector(min_w, min_h, min_d), Vector(max_w, min_h, min_d), color = self.VolumeOutlineColor)
  73. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, max_h, min_d), color = self.VolumeOutlineColor)
  74. mb.addLine(Vector(min_w, max_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  75. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, max_h, min_d), color = self.VolumeOutlineColor)
  76. mb.addLine(Vector(min_w, min_h, max_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  77. mb.addLine(Vector(min_w, min_h, max_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  78. mb.addLine(Vector(min_w, max_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  79. mb.addLine(Vector(max_w, min_h, max_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  80. mb.addLine(Vector(min_w, min_h, min_d), Vector(min_w, min_h, max_d), color = self.VolumeOutlineColor)
  81. mb.addLine(Vector(max_w, min_h, min_d), Vector(max_w, min_h, max_d), color = self.VolumeOutlineColor)
  82. mb.addLine(Vector(min_w, max_h, min_d), Vector(min_w, max_h, max_d), color = self.VolumeOutlineColor)
  83. mb.addLine(Vector(max_w, max_h, min_d), Vector(max_w, max_h, max_d), color = self.VolumeOutlineColor)
  84. self.setMeshData(mb.getData())
  85. mb = MeshBuilder()
  86. mb.addQuad(
  87. Vector(min_w, min_h, min_d),
  88. Vector(max_w, min_h, min_d),
  89. Vector(max_w, min_h, max_d),
  90. Vector(min_w, min_h, max_d)
  91. )
  92. self._grid_mesh = mb.getData()
  93. for n in range(0, 6):
  94. v = self._grid_mesh.getVertex(n)
  95. self._grid_mesh.setVertexUVCoordinates(n, v[0], v[2])
  96. disallowed_area_height = 0.2
  97. disallowed_area_size = 0
  98. if self._disallowed_areas:
  99. mb = MeshBuilder()
  100. color = Color(0.0, 0.0, 0.0, 0.15)
  101. for polygon in self._disallowed_areas:
  102. points = polygon.getPoints()
  103. first = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  104. previous_point = Vector(self._clamp(points[0][0], min_w, max_w), disallowed_area_height, self._clamp(points[0][1], min_d, max_d))
  105. for point in points:
  106. new_point = Vector(self._clamp(point[0], min_w, max_w), disallowed_area_height, self._clamp(point[1], min_d, max_d))
  107. mb.addFace(first, previous_point, new_point, color = color)
  108. previous_point = new_point
  109. # Find the largest disallowed area to exclude it from the maximum scale bounds
  110. size = abs(numpy.max(points[:, 1]) - numpy.min(points[:, 1]))
  111. disallowed_area_size = max(size, disallowed_area_size)
  112. self._disallowed_area_mesh = mb.getData()
  113. else:
  114. self._disallowed_area_mesh = None
  115. self._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. profile = Application.getInstance().getMachineManager().getActiveProfile()
  118. if profile:
  119. skirt_size = self._getSkirtSize(profile)
  120. scale_to_max_bounds = AxisAlignedBox(
  121. minimum = Vector(min_w + skirt_size, min_h, min_d + skirt_size + disallowed_area_size),
  122. maximum = Vector(max_w - skirt_size, max_h, max_d - skirt_size - disallowed_area_size)
  123. )
  124. Application.getInstance().getController().getScene()._maximum_bounds = scale_to_max_bounds
  125. def _onActiveInstanceChanged(self):
  126. self._active_instance = Application.getInstance().getMachineManager().getActiveMachineInstance()
  127. if self._active_instance:
  128. self._width = self._active_instance.getMachineSettingValue("machine_width")
  129. self._height = self._active_instance.getMachineSettingValue("machine_height")
  130. self._depth = self._active_instance.getMachineSettingValue("machine_depth")
  131. self._updateDisallowedAreas()
  132. self.rebuild()
  133. def _onActiveProfileChanged(self):
  134. if self._active_profile:
  135. self._active_profile.settingValueChanged.disconnect(self._onSettingValueChanged)
  136. self._active_profile = Application.getInstance().getMachineManager().getActiveProfile()
  137. if self._active_profile:
  138. self._active_profile.settingValueChanged.connect(self._onSettingValueChanged)
  139. self._updateDisallowedAreas()
  140. self.rebuild()
  141. def _onSettingValueChanged(self, setting):
  142. if setting in self._skirt_settings:
  143. self._updateDisallowedAreas()
  144. self.rebuild()
  145. def _updateDisallowedAreas(self):
  146. if not self._active_instance or not self._active_profile:
  147. return
  148. disallowed_areas = self._active_instance.getMachineSettingValue("machine_disallowed_areas")
  149. areas = []
  150. skirt_size = 0.0
  151. if self._active_profile:
  152. skirt_size = self._getSkirtSize(self._active_profile)
  153. if disallowed_areas:
  154. for area in disallowed_areas:
  155. poly = Polygon(numpy.array(area, numpy.float32))
  156. poly = poly.getMinkowskiHull(Polygon(numpy.array([
  157. [-skirt_size, 0],
  158. [-skirt_size * 0.707, skirt_size * 0.707],
  159. [0, skirt_size],
  160. [skirt_size * 0.707, skirt_size * 0.707],
  161. [skirt_size, 0],
  162. [skirt_size * 0.707, -skirt_size * 0.707],
  163. [0, -skirt_size],
  164. [-skirt_size * 0.707, -skirt_size * 0.707]
  165. ], numpy.float32)))
  166. areas.append(poly)
  167. if skirt_size > 0:
  168. half_machine_width = self._active_instance.getMachineSettingValue("machine_width") / 2
  169. half_machine_depth = self._active_instance.getMachineSettingValue("machine_depth") / 2
  170. areas.append(Polygon(numpy.array([
  171. [-half_machine_width, -half_machine_depth],
  172. [-half_machine_width, half_machine_depth],
  173. [-half_machine_width + skirt_size, half_machine_depth - skirt_size],
  174. [-half_machine_width + skirt_size, -half_machine_depth + skirt_size]
  175. ], numpy.float32)))
  176. areas.append(Polygon(numpy.array([
  177. [half_machine_width, half_machine_depth],
  178. [half_machine_width, -half_machine_depth],
  179. [half_machine_width - skirt_size, -half_machine_depth + skirt_size],
  180. [half_machine_width - skirt_size, half_machine_depth - skirt_size]
  181. ], numpy.float32)))
  182. areas.append(Polygon(numpy.array([
  183. [-half_machine_width, half_machine_depth],
  184. [half_machine_width, half_machine_depth],
  185. [half_machine_width - skirt_size, half_machine_depth - skirt_size],
  186. [-half_machine_width + skirt_size, half_machine_depth - skirt_size]
  187. ], numpy.float32)))
  188. areas.append(Polygon(numpy.array([
  189. [half_machine_width, -half_machine_depth],
  190. [-half_machine_width, -half_machine_depth],
  191. [-half_machine_width + skirt_size, -half_machine_depth + skirt_size],
  192. [half_machine_width - skirt_size, -half_machine_depth + skirt_size]
  193. ], numpy.float32)))
  194. self._disallowed_areas = areas
  195. def _getSkirtSize(self, profile):
  196. skirt_size = 0.0
  197. adhesion_type = profile.getSettingValue("adhesion_type")
  198. if adhesion_type == "skirt":
  199. skirt_distance = profile.getSettingValue("skirt_gap")
  200. skirt_line_count = profile.getSettingValue("skirt_line_count")
  201. skirt_size = skirt_distance + (skirt_line_count * profile.getSettingValue("skirt_line_width"))
  202. elif adhesion_type == "brim":
  203. brim_line_count = profile.getSettingValue("brim_line_count")
  204. skirt_size = brim_line_count * profile.getSettingValue("skirt_line_width")
  205. elif adhesion_type == "raft":
  206. skirt_size = profile.getSettingValue("raft_margin")
  207. if profile.getSettingValue("draft_shield_enabled"):
  208. skirt_size += profile.getSettingValue("draft_shield_dist")
  209. skirt_size += profile.getSettingValue("xy_offset")
  210. return skirt_size
  211. def _clamp(self, value, min_value, max_value):
  212. return max(min(value, max_value), min_value)
  213. _skirt_settings = ["adhesion_type", "skirt_gap", "skirt_line_count", "skirt_line_width", "brim_line_count", "raft_margin", "draft_shield_enabled", "draft_shield_dist", "xy_offset"]