SliceInfo.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import json
  4. import os
  5. import platform
  6. import time
  7. from PyQt5.QtCore import pyqtSlot, QObject
  8. from UM.Extension import Extension
  9. from UM.Application import Application
  10. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  11. from UM.Message import Message
  12. from UM.i18n import i18nCatalog
  13. from UM.Logger import Logger
  14. from UM.PluginRegistry import PluginRegistry
  15. from UM.Qt.Duration import DurationFormat
  16. from .SliceInfoJob import SliceInfoJob
  17. catalog = i18nCatalog("cura")
  18. ## This Extension runs in the background and sends several bits of information to the Ultimaker servers.
  19. # The data is only sent when the user in question gave permission to do so. All data is anonymous and
  20. # no model files are being sent (Just a SHA256 hash of the model).
  21. class SliceInfo(QObject, Extension):
  22. info_url = "https://stats.ultimaker.com/api/cura"
  23. def __init__(self, parent = None):
  24. QObject.__init__(self, parent)
  25. Extension.__init__(self)
  26. Application.getInstance().getOutputDeviceManager().writeStarted.connect(self._onWriteStarted)
  27. Application.getInstance().getPreferences().addPreference("info/send_slice_info", True)
  28. Application.getInstance().getPreferences().addPreference("info/asked_send_slice_info", False)
  29. self._more_info_dialog = None
  30. self._example_data_content = None
  31. if not Application.getInstance().getPreferences().getValue("info/asked_send_slice_info"):
  32. self.send_slice_info_message = Message(catalog.i18nc("@info", "Cura collects anonymized usage statistics."),
  33. lifetime = 0,
  34. dismissable = False,
  35. title = catalog.i18nc("@info:title", "Collecting Data"))
  36. self.send_slice_info_message.addAction("MoreInfo", name = catalog.i18nc("@action:button", "More info"), icon = None,
  37. description = catalog.i18nc("@action:tooltip", "See more information on what data Cura sends."), button_style = Message.ActionButtonStyle.LINK)
  38. self.send_slice_info_message.addAction("Dismiss", name = catalog.i18nc("@action:button", "Allow"), icon = None,
  39. description = catalog.i18nc("@action:tooltip", "Allow Cura to send anonymized usage statistics to help prioritize future improvements to Cura. Some of your preferences and settings are sent, the Cura version and a hash of the models you're slicing."))
  40. self.send_slice_info_message.actionTriggered.connect(self.messageActionTriggered)
  41. self.send_slice_info_message.show()
  42. Application.getInstance().initializationFinished.connect(self._onAppInitialized)
  43. def _onAppInitialized(self):
  44. if self._more_info_dialog is None:
  45. self._more_info_dialog = self._createDialog("MoreInfoWindow.qml")
  46. ## Perform action based on user input.
  47. # Note that clicking "Disable" won't actually disable the data sending, but rather take the user to preferences where they can disable it.
  48. def messageActionTriggered(self, message_id, action_id):
  49. Application.getInstance().getPreferences().setValue("info/asked_send_slice_info", True)
  50. if action_id == "MoreInfo":
  51. self.showMoreInfoDialog()
  52. self.send_slice_info_message.hide()
  53. def showMoreInfoDialog(self):
  54. if self._more_info_dialog is None:
  55. self._more_info_dialog = self._createDialog("MoreInfoWindow.qml")
  56. self._more_info_dialog.open()
  57. def _createDialog(self, qml_name):
  58. Logger.log("d", "Creating dialog [%s]", qml_name)
  59. file_path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), qml_name)
  60. dialog = Application.getInstance().createQmlComponent(file_path, {"manager": self})
  61. return dialog
  62. @pyqtSlot(result = str)
  63. def getExampleData(self) -> str:
  64. if self._example_data_content is None:
  65. file_path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), "example_data.json")
  66. with open(file_path, "r", encoding = "utf-8") as f:
  67. self._example_data_content = f.read()
  68. return self._example_data_content
  69. @pyqtSlot(bool)
  70. def setSendSliceInfo(self, enabled: bool):
  71. Application.getInstance().getPreferences().setValue("info/send_slice_info", enabled)
  72. def _onWriteStarted(self, output_device):
  73. try:
  74. if not Application.getInstance().getPreferences().getValue("info/send_slice_info"):
  75. Logger.log("d", "'info/send_slice_info' is turned off.")
  76. return # Do nothing, user does not want to send data
  77. application = Application.getInstance()
  78. machine_manager = application.getMachineManager()
  79. print_information = application.getPrintInformation()
  80. global_stack = machine_manager.activeMachine
  81. data = dict() # The data that we're going to submit.
  82. data["time_stamp"] = time.time()
  83. data["schema_version"] = 0
  84. data["cura_version"] = application.getVersion()
  85. active_mode = Application.getInstance().getPreferences().getValue("cura/active_mode")
  86. if active_mode == 0:
  87. data["active_mode"] = "recommended"
  88. else:
  89. data["active_mode"] = "custom"
  90. definition_changes = global_stack.definitionChanges
  91. machine_settings_changed_by_user = False
  92. if definition_changes.getId() != "empty":
  93. # Now a definition_changes container will always be created for a stack,
  94. # so we also need to check if there is any instance in the definition_changes container
  95. if definition_changes.getAllKeys():
  96. machine_settings_changed_by_user = True
  97. data["machine_settings_changed_by_user"] = machine_settings_changed_by_user
  98. data["language"] = Application.getInstance().getPreferences().getValue("general/language")
  99. data["os"] = {"type": platform.system(), "version": platform.version()}
  100. data["active_machine"] = {"definition_id": global_stack.definition.getId(),
  101. "manufacturer": global_stack.definition.getMetaDataEntry("manufacturer", "")}
  102. # add extruder specific data to slice info
  103. data["extruders"] = []
  104. extruders = list(global_stack.extruders.values())
  105. extruders = sorted(extruders, key = lambda extruder: extruder.getMetaDataEntry("position"))
  106. for extruder in extruders:
  107. extruder_dict = dict()
  108. extruder_dict["active"] = machine_manager.activeStack == extruder
  109. extruder_dict["material"] = {"GUID": extruder.material.getMetaData().get("GUID", ""),
  110. "type": extruder.material.getMetaData().get("material", ""),
  111. "brand": extruder.material.getMetaData().get("brand", "")
  112. }
  113. extruder_position = int(extruder.getMetaDataEntry("position", "0"))
  114. if len(print_information.materialLengths) > extruder_position:
  115. extruder_dict["material_used"] = print_information.materialLengths[extruder_position]
  116. extruder_dict["variant"] = extruder.variant.getName()
  117. extruder_dict["nozzle_size"] = extruder.getProperty("machine_nozzle_size", "value")
  118. extruder_settings = dict()
  119. extruder_settings["wall_line_count"] = extruder.getProperty("wall_line_count", "value")
  120. extruder_settings["retraction_enable"] = extruder.getProperty("retraction_enable", "value")
  121. extruder_settings["infill_sparse_density"] = extruder.getProperty("infill_sparse_density", "value")
  122. extruder_settings["infill_pattern"] = extruder.getProperty("infill_pattern", "value")
  123. extruder_settings["gradual_infill_steps"] = extruder.getProperty("gradual_infill_steps", "value")
  124. extruder_settings["default_material_print_temperature"] = extruder.getProperty("default_material_print_temperature", "value")
  125. extruder_settings["material_print_temperature"] = extruder.getProperty("material_print_temperature", "value")
  126. extruder_dict["extruder_settings"] = extruder_settings
  127. data["extruders"].append(extruder_dict)
  128. data["quality_profile"] = global_stack.quality.getMetaData().get("quality_type")
  129. data["models"] = []
  130. # Listing all files placed on the build plate
  131. for node in DepthFirstIterator(application.getController().getScene().getRoot()):
  132. if node.callDecoration("isSliceable"):
  133. model = dict()
  134. model["hash"] = node.getMeshData().getHash()
  135. bounding_box = node.getBoundingBox()
  136. model["bounding_box"] = {"minimum": {"x": bounding_box.minimum.x,
  137. "y": bounding_box.minimum.y,
  138. "z": bounding_box.minimum.z},
  139. "maximum": {"x": bounding_box.maximum.x,
  140. "y": bounding_box.maximum.y,
  141. "z": bounding_box.maximum.z}}
  142. model["transformation"] = {"data": str(node.getWorldTransformation().getData()).replace("\n", "")}
  143. extruder_position = node.callDecoration("getActiveExtruderPosition")
  144. model["extruder"] = 0 if extruder_position is None else int(extruder_position)
  145. model_settings = dict()
  146. model_stack = node.callDecoration("getStack")
  147. if model_stack:
  148. model_settings["support_enabled"] = model_stack.getProperty("support_enable", "value")
  149. model_settings["support_extruder_nr"] = int(model_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  150. # Mesh modifiers;
  151. model_settings["infill_mesh"] = model_stack.getProperty("infill_mesh", "value")
  152. model_settings["cutting_mesh"] = model_stack.getProperty("cutting_mesh", "value")
  153. model_settings["support_mesh"] = model_stack.getProperty("support_mesh", "value")
  154. model_settings["anti_overhang_mesh"] = model_stack.getProperty("anti_overhang_mesh", "value")
  155. model_settings["wall_line_count"] = model_stack.getProperty("wall_line_count", "value")
  156. model_settings["retraction_enable"] = model_stack.getProperty("retraction_enable", "value")
  157. # Infill settings
  158. model_settings["infill_sparse_density"] = model_stack.getProperty("infill_sparse_density", "value")
  159. model_settings["infill_pattern"] = model_stack.getProperty("infill_pattern", "value")
  160. model_settings["gradual_infill_steps"] = model_stack.getProperty("gradual_infill_steps", "value")
  161. model["model_settings"] = model_settings
  162. data["models"].append(model)
  163. print_times = print_information.printTimes()
  164. data["print_times"] = {"travel": int(print_times["travel"].getDisplayString(DurationFormat.Format.Seconds)),
  165. "support": int(print_times["support"].getDisplayString(DurationFormat.Format.Seconds)),
  166. "infill": int(print_times["infill"].getDisplayString(DurationFormat.Format.Seconds)),
  167. "total": int(print_information.currentPrintTime.getDisplayString(DurationFormat.Format.Seconds))}
  168. print_settings = dict()
  169. print_settings["layer_height"] = global_stack.getProperty("layer_height", "value")
  170. # Support settings
  171. print_settings["support_enabled"] = global_stack.getProperty("support_enable", "value")
  172. print_settings["support_extruder_nr"] = int(global_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  173. # Platform adhesion settings
  174. print_settings["adhesion_type"] = global_stack.getProperty("adhesion_type", "value")
  175. # Shell settings
  176. print_settings["wall_line_count"] = global_stack.getProperty("wall_line_count", "value")
  177. print_settings["retraction_enable"] = global_stack.getProperty("retraction_enable", "value")
  178. # Prime tower settings
  179. print_settings["prime_tower_enable"] = global_stack.getProperty("prime_tower_enable", "value")
  180. # Infill settings
  181. print_settings["infill_sparse_density"] = global_stack.getProperty("infill_sparse_density", "value")
  182. print_settings["infill_pattern"] = global_stack.getProperty("infill_pattern", "value")
  183. print_settings["gradual_infill_steps"] = global_stack.getProperty("gradual_infill_steps", "value")
  184. print_settings["print_sequence"] = global_stack.getProperty("print_sequence", "value")
  185. data["print_settings"] = print_settings
  186. # Send the name of the output device type that is used.
  187. data["output_to"] = type(output_device).__name__
  188. # Convert data to bytes
  189. binary_data = json.dumps(data).encode("utf-8")
  190. # Sending slice info non-blocking
  191. reportJob = SliceInfoJob(self.info_url, binary_data)
  192. reportJob.start()
  193. except Exception:
  194. # We really can't afford to have a mistake here, as this would break the sending of g-code to a device
  195. # (Either saving or directly to a printer). The functionality of the slice data is not *that* important.
  196. Logger.logException("e", "Exception raised while sending slice info.") # But we should be notified about these problems of course.