CuraSceneNode.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from copy import deepcopy
  4. from typing import List
  5. from UM.Application import Application
  6. from UM.Math.AxisAlignedBox import AxisAlignedBox
  7. from UM.Scene.SceneNode import SceneNode
  8. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  9. ## Scene nodes that are models are only seen when selecting the corresponding build plate
  10. # Note that many other nodes can just be UM SceneNode objects.
  11. class CuraSceneNode(SceneNode):
  12. def __init__(self, *args, **kwargs):
  13. super().__init__(*args, **kwargs)
  14. if "no_setting_override" not in kwargs:
  15. self.addDecorator(SettingOverrideDecorator()) # now we always have a getActiveExtruderPosition, unless explicitly disabled
  16. self._outside_buildarea = False
  17. def setOutsideBuildArea(self, new_value):
  18. self._outside_buildarea = new_value
  19. def isOutsideBuildArea(self):
  20. return self._outside_buildarea or self.callDecoration("getBuildPlateNumber") < 0
  21. def isVisible(self):
  22. return super().isVisible() and self.callDecoration("getBuildPlateNumber") == Application.getInstance().getMultiBuildPlateModel().activeBuildPlate
  23. def isSelectable(self) -> bool:
  24. return super().isSelectable() and self.callDecoration("getBuildPlateNumber") == Application.getInstance().getMultiBuildPlateModel().activeBuildPlate
  25. ## Get the extruder used to print this node. If there is no active node, then the extruder in position zero is returned
  26. # TODO The best way to do it is by adding the setActiveExtruder decorator to every node when is loaded
  27. def getPrintingExtruder(self):
  28. global_container_stack = Application.getInstance().getGlobalContainerStack()
  29. per_mesh_stack = self.callDecoration("getStack")
  30. extruders = list(global_container_stack.extruders.values())
  31. # Use the support extruder instead of the active extruder if this is a support_mesh
  32. if per_mesh_stack:
  33. if per_mesh_stack.getProperty("support_mesh", "value"):
  34. return extruders[int(global_container_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))]
  35. # It's only set if you explicitly choose an extruder
  36. extruder_id = self.callDecoration("getActiveExtruder")
  37. for extruder in extruders:
  38. # Find out the extruder if we know the id.
  39. if extruder_id is not None:
  40. if extruder_id == extruder.getId():
  41. return extruder
  42. else: # If the id is unknown, then return the extruder in the position 0
  43. try:
  44. if extruder.getMetaDataEntry("position", default = "0") == "0": # Check if the position is zero
  45. return extruder
  46. except ValueError:
  47. continue
  48. # This point should never be reached
  49. return None
  50. ## Return the color of the material used to print this model
  51. def getDiffuseColor(self) -> List[float]:
  52. printing_extruder = self.getPrintingExtruder()
  53. material_color = "#808080" # Fallback color
  54. if printing_extruder is not None and printing_extruder.material:
  55. material_color = printing_extruder.material.getMetaDataEntry("color_code", default = material_color)
  56. # Colors are passed as rgb hex strings (eg "#ffffff"), and the shader needs
  57. # an rgba list of floats (eg [1.0, 1.0, 1.0, 1.0])
  58. return [
  59. int(material_color[1:3], 16) / 255,
  60. int(material_color[3:5], 16) / 255,
  61. int(material_color[5:7], 16) / 255,
  62. 1.0
  63. ]
  64. ## Return if the provided bbox collides with the bbox of this scene node
  65. def collidesWithBbox(self, check_bbox):
  66. bbox = self.getBoundingBox()
  67. # Mark the node as outside the build volume if the bounding box test fails.
  68. if check_bbox.intersectsBox(bbox) != AxisAlignedBox.IntersectionResult.FullIntersection:
  69. return True
  70. return False
  71. ## Return if any area collides with the convex hull of this scene node
  72. def collidesWithArea(self, areas):
  73. convex_hull = self.callDecoration("getConvexHull")
  74. if convex_hull:
  75. if not convex_hull.isValid():
  76. return False
  77. # Check for collisions between disallowed areas and the object
  78. for area in areas:
  79. overlap = convex_hull.intersectsPolygon(area)
  80. if overlap is None:
  81. continue
  82. return True
  83. return False
  84. ## Override of SceneNode._calculateAABB to exclude non-printing-meshes from bounding box
  85. def _calculateAABB(self):
  86. aabb = None
  87. if self._mesh_data:
  88. aabb = self._mesh_data.getExtents(self.getWorldTransformation())
  89. else: # If there is no mesh_data, use a boundingbox that encompasses the local (0,0,0)
  90. position = self.getWorldPosition()
  91. aabb = AxisAlignedBox(minimum = position, maximum = position)
  92. for child in self._children:
  93. if child.callDecoration("isNonPrintingMesh"):
  94. # Non-printing-meshes inside a group should not affect push apart or drop to build plate
  95. continue
  96. if aabb is None:
  97. aabb = child.getBoundingBox()
  98. else:
  99. aabb = aabb + child.getBoundingBox()
  100. self._aabb = aabb
  101. ## Taken from SceneNode, but replaced SceneNode with CuraSceneNode
  102. def __deepcopy__(self, memo):
  103. copy = CuraSceneNode(no_setting_override = True) # Setting override will be added later
  104. copy.setTransformation(self.getLocalTransformation())
  105. copy.setMeshData(self._mesh_data)
  106. copy.setVisible(deepcopy(self._visible, memo))
  107. copy._selectable = deepcopy(self._selectable, memo)
  108. copy._name = deepcopy(self._name, memo)
  109. for decorator in self._decorators:
  110. copy.addDecorator(deepcopy(decorator, memo))
  111. for child in self._children:
  112. copy.addChild(deepcopy(child, memo))
  113. self.calculateBoundingBoxMesh()
  114. return copy
  115. def transformChanged(self) -> None:
  116. self._transformChanged()