ConvexHullNode.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING
  4. from UM.Application import Application
  5. from UM.Math.Polygon import Polygon
  6. from UM.Qt.QtApplication import QtApplication
  7. from UM.Scene.SceneNode import SceneNode
  8. from UM.Resources import Resources
  9. from UM.Math.Color import Color
  10. from UM.Mesh.MeshBuilder import MeshBuilder # To create a mesh to display the convex hull with.
  11. from UM.View.GL.OpenGL import OpenGL
  12. if TYPE_CHECKING:
  13. from UM.Mesh.MeshData import MeshData
  14. class ConvexHullNode(SceneNode):
  15. shader = None # To prevent the shader from being re-built over and over again, only load it once.
  16. ## Convex hull node is a special type of scene node that is used to display an area, to indicate the
  17. # location an object uses on the buildplate. This area (or area's in case of one at a time printing) is
  18. # then displayed as a transparent shadow. If the adhesion type is set to raft, the area is extruded
  19. # to represent the raft as well.
  20. def __init__(self, node: SceneNode, hull: Optional[Polygon], thickness: float, parent: Optional[SceneNode] = None) -> None:
  21. super().__init__(parent)
  22. self.setCalculateBoundingBox(False)
  23. self._original_parent = parent
  24. # Color of the drawn convex hull
  25. if not Application.getInstance().getIsHeadLess():
  26. theme = QtApplication.getInstance().getTheme()
  27. if theme:
  28. self._color = Color(*theme.getColor("convex_hull").getRgb())
  29. else:
  30. self._color = Color(0, 0, 0)
  31. else:
  32. self._color = Color(0, 0, 0)
  33. # The y-coordinate of the convex hull mesh. Must not be 0, to prevent z-fighting.
  34. self._mesh_height = 0.1
  35. self._thickness = thickness
  36. # The node this mesh is "watching"
  37. self._node = node
  38. # Area of the head + fans for display as a shadow on the buildplate
  39. self._convex_hull_head_mesh = None # type: Optional[MeshData]
  40. self._node.decoratorsChanged.connect(self._onNodeDecoratorsChanged)
  41. self._onNodeDecoratorsChanged(self._node)
  42. self._hull = hull
  43. if self._hull:
  44. hull_mesh_builder = MeshBuilder()
  45. if hull_mesh_builder.addConvexPolygonExtrusion(
  46. self._hull.getPoints()[::-1], # bottom layer is reversed
  47. self._mesh_height - thickness, self._mesh_height, color = self._color):
  48. hull_mesh = hull_mesh_builder.build()
  49. self.setMeshData(hull_mesh)
  50. def getHull(self):
  51. return self._hull
  52. def getThickness(self):
  53. return self._thickness
  54. def getWatchedNode(self):
  55. return self._node
  56. def render(self, renderer):
  57. if not ConvexHullNode.shader:
  58. ConvexHullNode.shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  59. ConvexHullNode.shader.setUniformValue("u_diffuseColor", self._color)
  60. ConvexHullNode.shader.setUniformValue("u_opacity", 0.6)
  61. if self.getParent():
  62. if self.getMeshData() and isinstance(self._node, SceneNode) and self._node.callDecoration("getBuildPlateNumber") == Application.getInstance().getMultiBuildPlateModel().activeBuildPlate:
  63. # The object itself (+ adhesion in one-at-a-time mode)
  64. renderer.queueNode(self, transparent = True, shader = ConvexHullNode.shader, backface_cull = True, sort = -8)
  65. if self._convex_hull_head_mesh:
  66. # The full head. Rendered as a hint to the user: If this area overlaps another object A; this object
  67. # cannot be printed after A, because the head would hit A while printing the current object
  68. renderer.queueNode(self, shader = ConvexHullNode.shader, transparent = True, mesh = self._convex_hull_head_mesh, backface_cull = True, sort = -8)
  69. return True
  70. def _onNodeDecoratorsChanged(self, node: SceneNode) -> None:
  71. convex_hull_head = self._node.callDecoration("getConvexHullHeadFull")
  72. if convex_hull_head:
  73. convex_hull_head_builder = MeshBuilder()
  74. convex_hull_head_builder.addConvexPolygon(convex_hull_head.getPoints(), self._mesh_height - self._thickness)
  75. self._convex_hull_head_mesh = convex_hull_head_builder.build()
  76. if not node:
  77. return