ModelChecker.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal, pyqtProperty
  5. from UM.Application import Application
  6. from UM.Extension import Extension
  7. from UM.Logger import Logger
  8. from UM.Message import Message
  9. from UM.i18n import i18nCatalog
  10. from UM.PluginRegistry import PluginRegistry
  11. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  12. catalog = i18nCatalog("cura")
  13. class ModelChecker(QObject, Extension):
  14. ## Signal that gets emitted when anything changed that we need to check.
  15. onChanged = pyqtSignal()
  16. def __init__(self):
  17. super().__init__()
  18. self._button_view = None
  19. self._caution_message = Message("", #Message text gets set when the message gets shown, to display the models in question.
  20. lifetime = 0,
  21. title = catalog.i18nc("@info:title", "3D Model Assistant"))
  22. Application.getInstance().initializationFinished.connect(self._pluginsInitialized)
  23. Application.getInstance().getController().getScene().sceneChanged.connect(self._onChanged)
  24. Application.getInstance().globalContainerStackChanged.connect(self._onChanged)
  25. ## Pass-through to allow UM.Signal to connect with a pyqtSignal.
  26. def _onChanged(self, *args, **kwargs):
  27. self.onChanged.emit()
  28. ## Called when plug-ins are initialized.
  29. #
  30. # This makes sure that we listen to changes of the material and that the
  31. # button is created that indicates warnings with the current set-up.
  32. def _pluginsInitialized(self):
  33. Application.getInstance().getMachineManager().rootMaterialChanged.connect(self.onChanged)
  34. self._createView()
  35. def checkObjectsForShrinkage(self):
  36. shrinkage_threshold = 0.5 #From what shrinkage percentage a warning will be issued about the model size.
  37. warning_size_xy = 150 #The horizontal size of a model that would be too large when dealing with shrinking materials.
  38. warning_size_z = 100 #The vertical size of a model that would be too large when dealing with shrinking materials.
  39. # This function can be triggered in the middle of a machine change, so do not proceed if the machine change
  40. # has not done yet.
  41. global_container_stack = Application.getInstance().getGlobalContainerStack()
  42. if global_container_stack is None:
  43. return False
  44. material_shrinkage = self._getMaterialShrinkage()
  45. warning_nodes = []
  46. # Check node material shrinkage and bounding box size
  47. for node in self.sliceableNodes():
  48. node_extruder_position = node.callDecoration("getActiveExtruderPosition")
  49. # This function can be triggered in the middle of a machine change, so do not proceed if the machine change
  50. # has not done yet.
  51. if str(node_extruder_position) not in global_container_stack.extruders:
  52. Application.getInstance().callLater(lambda: self.onChanged.emit())
  53. return False
  54. if material_shrinkage[node_extruder_position] > shrinkage_threshold:
  55. bbox = node.getBoundingBox()
  56. if bbox.width >= warning_size_xy or bbox.depth >= warning_size_xy or bbox.height >= warning_size_z:
  57. warning_nodes.append(node)
  58. self._caution_message.setText(catalog.i18nc(
  59. "@info:status",
  60. "<p>One or more 3D models may not print optimally due to the model size and material configuration:</p>\n"
  61. "<p>{model_names}</p>\n"
  62. "<p>Find out how to ensure the best possible print quality and reliability.</p>\n"
  63. "<p><a href=\"https://ultimaker.com/3D-model-assistant\">View print quality guide</a></p>"
  64. ).format(model_names = ", ".join([n.getName() for n in warning_nodes])))
  65. return len(warning_nodes) > 0
  66. def sliceableNodes(self):
  67. # Add all sliceable scene nodes to check
  68. scene = Application.getInstance().getController().getScene()
  69. for node in DepthFirstIterator(scene.getRoot()):
  70. if node.callDecoration("isSliceable"):
  71. yield node
  72. ## Creates the view used by show popup. The view is saved because of the fairly aggressive garbage collection.
  73. def _createView(self):
  74. Logger.log("d", "Creating model checker view.")
  75. # Create the plugin dialog component
  76. path = os.path.join(PluginRegistry.getInstance().getPluginPath("ModelChecker"), "ModelChecker.qml")
  77. self._button_view = Application.getInstance().createQmlComponent(path, {"manager": self})
  78. # The qml is only the button
  79. Application.getInstance().addAdditionalComponent("jobSpecsButton", self._button_view)
  80. Logger.log("d", "Model checker view created.")
  81. @pyqtProperty(bool, notify = onChanged)
  82. def hasWarnings(self):
  83. danger_shrinkage = self.checkObjectsForShrinkage()
  84. return any((danger_shrinkage, )) #If any of the checks fail, show the warning button.
  85. @pyqtSlot()
  86. def showWarnings(self):
  87. self._caution_message.show()
  88. def _getMaterialShrinkage(self):
  89. global_container_stack = Application.getInstance().getGlobalContainerStack()
  90. if global_container_stack is None:
  91. return {}
  92. material_shrinkage = {}
  93. # Get all shrinkage values of materials used
  94. for extruder_position, extruder in global_container_stack.extruders.items():
  95. shrinkage = extruder.material.getProperty("material_shrinkage_percentage", "value")
  96. if shrinkage is None:
  97. shrinkage = 0
  98. material_shrinkage[extruder_position] = shrinkage
  99. return material_shrinkage