PerObjectSettingsTool.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Uranium is released under the terms of the LGPLv3 or higher.
  3. from UM.Tool import Tool
  4. from UM.Scene.Selection import Selection
  5. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  6. from UM.Application import Application
  7. from UM.Preferences import Preferences
  8. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  9. from cura.Settings.ExtruderManager import ExtruderManager
  10. from UM.Settings.SettingInstance import SettingInstance
  11. from UM.Event import Event
  12. ## This tool allows the user to add & change settings per node in the scene.
  13. # The settings per object are kept in a ContainerStack, which is linked to a node by decorator.
  14. class PerObjectSettingsTool(Tool):
  15. def __init__(self):
  16. super().__init__()
  17. self._model = None
  18. self.setExposedProperties("SelectedObjectId", "ContainerID", "SelectedActiveExtruder", "MeshType")
  19. self._advanced_mode = False
  20. self._multi_extrusion = False
  21. self._single_model_selected = False
  22. Selection.selectionChanged.connect(self.propertyChanged)
  23. Preferences.getInstance().preferenceChanged.connect(self._onPreferenceChanged)
  24. self._onPreferenceChanged("cura/active_mode")
  25. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalContainerChanged)
  26. self._onGlobalContainerChanged()
  27. Selection.selectionChanged.connect(self._updateEnabled)
  28. def event(self, event):
  29. super().event(event)
  30. if event.type == Event.MousePressEvent and self._controller.getToolsEnabled():
  31. self.operationStopped.emit(self)
  32. return False
  33. def getSelectedObjectId(self):
  34. selected_object = Selection.getSelectedObject(0)
  35. selected_object_id = id(selected_object)
  36. return selected_object_id
  37. def getContainerID(self):
  38. selected_object = Selection.getSelectedObject(0)
  39. try:
  40. return selected_object.callDecoration("getStack").getId()
  41. except AttributeError:
  42. return ""
  43. ## Gets the active extruder of the currently selected object.
  44. #
  45. # \return The active extruder of the currently selected object.
  46. def getSelectedActiveExtruder(self):
  47. selected_object = Selection.getSelectedObject(0)
  48. return selected_object.callDecoration("getActiveExtruder")
  49. ## Changes the active extruder of the currently selected object.
  50. #
  51. # \param extruder_stack_id The ID of the extruder to print the currently
  52. # selected object with.
  53. def setSelectedActiveExtruder(self, extruder_stack_id):
  54. selected_object = Selection.getSelectedObject(0)
  55. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  56. if not stack:
  57. selected_object.addDecorator(SettingOverrideDecorator())
  58. selected_object.callDecoration("setActiveExtruder", extruder_stack_id)
  59. def setMeshType(self, mesh_type):
  60. selected_object = Selection.getSelectedObject(0)
  61. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  62. if not stack:
  63. selected_object.addDecorator(SettingOverrideDecorator())
  64. stack = selected_object.callDecoration("getStack")
  65. settings = stack.getTop()
  66. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  67. if property_key != mesh_type:
  68. if settings.getInstance(property_key):
  69. settings.removeInstance(property_key)
  70. else:
  71. if not (settings.getInstance(property_key) and settings.getProperty(property_key, "value")):
  72. definition = stack.getSettingDefinition(property_key)
  73. new_instance = SettingInstance(definition, settings)
  74. new_instance.setProperty("value", True)
  75. new_instance.resetState() # Ensure that the state is not seen as a user state.
  76. settings.addInstance(new_instance)
  77. def getMeshType(self):
  78. selected_object = Selection.getSelectedObject(0)
  79. stack = selected_object.callDecoration("getStack") #Don't try to get the active extruder since it may be None anyway.
  80. if not stack:
  81. return ""
  82. settings = stack.getTop()
  83. for property_key in ["infill_mesh", "cutting_mesh", "support_mesh", "anti_overhang_mesh"]:
  84. if settings.getInstance(property_key) and settings.getProperty(property_key, "value"):
  85. return property_key
  86. return ""
  87. def _onPreferenceChanged(self, preference):
  88. if preference == "cura/active_mode":
  89. self._advanced_mode = Preferences.getInstance().getValue(preference) == 1
  90. self._updateEnabled()
  91. def _onGlobalContainerChanged(self):
  92. global_container_stack = Application.getInstance().getGlobalContainerStack()
  93. if global_container_stack:
  94. # used for enabling or disabling per extruder settings per object
  95. self._multi_extrusion = global_container_stack.getProperty("machine_extruder_count", "value") > 1
  96. extruder_stack = ExtruderManager.getInstance().getExtruderStack(0)
  97. if extruder_stack:
  98. root_node = Application.getInstance().getController().getScene().getRoot()
  99. for node in DepthFirstIterator(root_node):
  100. new_stack_id = extruder_stack.getId()
  101. # Get position of old extruder stack for this node
  102. old_extruder_pos = node.callDecoration("getActiveExtruderPosition")
  103. if old_extruder_pos is not None:
  104. # Fetch current (new) extruder stack at position
  105. new_stack = ExtruderManager.getInstance().getExtruderStack(old_extruder_pos)
  106. if new_stack:
  107. new_stack_id = new_stack.getId()
  108. node.callDecoration("setActiveExtruder", new_stack_id)
  109. self._updateEnabled()
  110. def _updateEnabled(self):
  111. selected_objects = Selection.getAllSelectedObjects()
  112. if len(selected_objects)> 1:
  113. self._single_model_selected = False
  114. elif len(selected_objects) == 1 and selected_objects[0].callDecoration("isGroup"):
  115. self._single_model_selected = False # Group is selected, so tool needs to be disabled
  116. else:
  117. self._single_model_selected = True
  118. Application.getInstance().getController().toolEnabledChanged.emit(self._plugin_id, self._advanced_mode and self._single_model_selected)