ConvexHullNode.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. self._convex_hull_head_mesh = None # type: Optional[MeshData]
  39. self._node.decoratorsChanged.connect(self._onNodeDecoratorsChanged)
  40. self._onNodeDecoratorsChanged(self._node)
  41. self._hull = hull
  42. if self._hull:
  43. hull_mesh_builder = MeshBuilder()
  44. if hull_mesh_builder.addConvexPolygonExtrusion(
  45. self._hull.getPoints()[::-1], # bottom layer is reversed
  46. self._mesh_height - thickness, self._mesh_height, color = self._color):
  47. hull_mesh = hull_mesh_builder.build()
  48. self.setMeshData(hull_mesh)
  49. def getHull(self):
  50. return self._hull
  51. def getThickness(self):
  52. return self._thickness
  53. def getWatchedNode(self):
  54. return self._node
  55. def render(self, renderer):
  56. if not ConvexHullNode.shader:
  57. ConvexHullNode.shader = OpenGL.getInstance().createShaderProgram(Resources.getPath(Resources.Shaders, "transparent_object.shader"))
  58. ConvexHullNode.shader.setUniformValue("u_diffuseColor", self._color)
  59. ConvexHullNode.shader.setUniformValue("u_opacity", 0.6)
  60. if self.getParent():
  61. if self.getMeshData() and isinstance(self._node, SceneNode) and self._node.callDecoration("getBuildPlateNumber") == Application.getInstance().getMultiBuildPlateModel().activeBuildPlate:
  62. renderer.queueNode(self, transparent = True, shader = ConvexHullNode.shader, backface_cull = True, sort = -8)
  63. if self._convex_hull_head_mesh:
  64. renderer.queueNode(self, shader = ConvexHullNode.shader, transparent = True, mesh = self._convex_hull_head_mesh, backface_cull = True, sort = -8)
  65. return True
  66. def _onNodeDecoratorsChanged(self, node: SceneNode) -> None:
  67. convex_hull_head = self._node.callDecoration("getConvexHullHead")
  68. if convex_hull_head:
  69. convex_hull_head_builder = MeshBuilder()
  70. convex_hull_head_builder.addConvexPolygon(convex_hull_head.getPoints(), self._mesh_height - self._thickness)
  71. self._convex_hull_head_mesh = convex_hull_head_builder.build()
  72. if not node:
  73. return