ModelChecker.py 6.0 KB

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