ConvexHullNode.py 3.9 KB

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