SliceInfo.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # Copyright (c) 2023 UltiMaker
  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 typing import Optional, Set, TYPE_CHECKING
  8. from PyQt6.QtCore import pyqtSlot, QObject
  9. from PyQt6.QtNetwork import QNetworkRequest
  10. from UM.Extension import Extension
  11. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  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 cura import ApplicationMetadata
  17. if TYPE_CHECKING:
  18. from PyQt6.QtNetwork import QNetworkReply
  19. catalog = i18nCatalog("cura")
  20. class SliceInfo(QObject, Extension):
  21. """This Extension runs in the background and sends several bits of information to the UltiMaker servers.
  22. The data is only sent when the user in question gave permission to do so. All data is anonymous and
  23. no model files are being sent (Just a SHA256 hash of the model).
  24. """
  25. info_url = "https://stats.ultimaker.com/api/cura"
  26. def __init__(self, parent = None):
  27. QObject.__init__(self, parent)
  28. Extension.__init__(self)
  29. from cura.CuraApplication import CuraApplication
  30. self._application = CuraApplication.getInstance()
  31. self._application.getOutputDeviceManager().writeStarted.connect(self._onWriteStarted)
  32. self._application.getPreferences().addPreference("info/send_slice_info", True)
  33. self._application.getPreferences().addPreference("info/asked_send_slice_info", False)
  34. self._more_info_dialog = None
  35. self._example_data_content = None
  36. self._application.initializationFinished.connect(self._onAppInitialized)
  37. def _onAppInitialized(self):
  38. # DO NOT read any preferences values in the constructor because at the time plugins are created, no version
  39. # upgrade has been performed yet because version upgrades are plugins too!
  40. if self._more_info_dialog is None:
  41. self._more_info_dialog = self._createDialog("MoreInfoWindow.qml")
  42. def messageActionTriggered(self, message_id, action_id):
  43. """Perform action based on user input.
  44. Note that clicking "Disable" won't actually disable the data sending, but rather take the user to preferences where they can disable it.
  45. """
  46. self._application.getPreferences().setValue("info/asked_send_slice_info", True)
  47. if action_id == "MoreInfo":
  48. self.showMoreInfoDialog()
  49. self.send_slice_info_message.hide()
  50. def showMoreInfoDialog(self):
  51. if self._more_info_dialog is None:
  52. self._more_info_dialog = self._createDialog("MoreInfoWindow.qml")
  53. self._more_info_dialog.show()
  54. def _createDialog(self, qml_name):
  55. Logger.log("d", "Creating dialog [%s]", qml_name)
  56. file_path = os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), qml_name)
  57. dialog = self._application.createQmlComponent(file_path, {"manager": self})
  58. return dialog
  59. @pyqtSlot(result = str)
  60. def getExampleData(self) -> Optional[str]:
  61. if self._example_data_content is None:
  62. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  63. if not plugin_path:
  64. Logger.log("e", "Could not get plugin path!", self.getPluginId())
  65. return None
  66. file_path = os.path.join(plugin_path, "example_data.html")
  67. if file_path:
  68. try:
  69. with open(file_path, "r", encoding = "utf-8") as f:
  70. self._example_data_content = f.read()
  71. except EnvironmentError as e:
  72. Logger.error(f"Unable to read example slice info data to show to the user: {e}")
  73. self._example_data_content = "<i>" + catalog.i18nc("@text", "Unable to read example data file.") + "</i>"
  74. return self._example_data_content
  75. @pyqtSlot(bool)
  76. def setSendSliceInfo(self, enabled: bool):
  77. self._application.getPreferences().setValue("info/send_slice_info", enabled)
  78. def _getUserModifiedSettingKeys(self) -> list:
  79. machine_manager = self._application.getMachineManager()
  80. global_stack = machine_manager.activeMachine
  81. user_modified_setting_keys = set() # type: Set[str]
  82. for stack in [global_stack] + global_stack.extruderList:
  83. # Get all settings in user_changes and quality_changes
  84. all_keys = stack.userChanges.getAllKeys() | stack.qualityChanges.getAllKeys()
  85. user_modified_setting_keys |= all_keys
  86. return list(sorted(user_modified_setting_keys))
  87. def _onWriteStarted(self, output_device):
  88. try:
  89. if not self._application.getPreferences().getValue("info/send_slice_info"):
  90. Logger.log("d", "'info/send_slice_info' is turned off.")
  91. return # Do nothing, user does not want to send data
  92. machine_manager = self._application.getMachineManager()
  93. print_information = self._application.getPrintInformation()
  94. user_profile = self._application.getCuraAPI().account.userProfile
  95. global_stack = machine_manager.activeMachine
  96. data = dict() # The data that we're going to submit.
  97. data["time_stamp"] = time.time()
  98. data["schema_version"] = 0
  99. data["cura_version"] = self._application.getVersion()
  100. data["cura_build_type"] = ApplicationMetadata.CuraBuildType
  101. org_id = user_profile.get("organization_id", None) if user_profile else None
  102. data["is_logged_in"] = self._application.getCuraAPI().account.isLoggedIn
  103. data["organization_id"] = org_id if org_id else None
  104. data["subscriptions"] = user_profile.get("subscriptions", []) if user_profile else []
  105. data["slice_uuid"] = print_information.slice_uuid
  106. active_mode = self._application.getPreferences().getValue("cura/active_mode")
  107. if active_mode == 0:
  108. data["active_mode"] = "recommended"
  109. else:
  110. data["active_mode"] = "custom"
  111. data["camera_view"] = self._application.getPreferences().getValue("general/camera_perspective_mode")
  112. if data["camera_view"] == "orthographic":
  113. data["camera_view"] = "orthogonal" #The database still only recognises the old name "orthogonal".
  114. definition_changes = global_stack.definitionChanges
  115. machine_settings_changed_by_user = False
  116. if definition_changes.getId() != "empty":
  117. # Now a definition_changes container will always be created for a stack,
  118. # so we also need to check if there is any instance in the definition_changes container
  119. if definition_changes.getAllKeys():
  120. machine_settings_changed_by_user = True
  121. data["machine_settings_changed_by_user"] = machine_settings_changed_by_user
  122. data["language"] = self._application.getPreferences().getValue("general/language")
  123. data["os"] = {"type": platform.system(), "version": platform.version()}
  124. data["active_machine"] = {"definition_id": global_stack.definition.getId(),
  125. "manufacturer": global_stack.definition.getMetaDataEntry("manufacturer", "")}
  126. # add extruder specific data to slice info
  127. data["extruders"] = []
  128. extruders = global_stack.extruderList
  129. extruders = sorted(extruders, key = lambda extruder: extruder.getMetaDataEntry("position"))
  130. for extruder in extruders:
  131. extruder_dict = dict()
  132. extruder_dict["active"] = machine_manager.activeStack == extruder
  133. extruder_dict["material"] = {"GUID": extruder.material.getMetaData().get("GUID", ""),
  134. "type": extruder.material.getMetaData().get("material", ""),
  135. "brand": extruder.material.getMetaData().get("brand", "")
  136. }
  137. extruder_position = int(extruder.getMetaDataEntry("position", "0"))
  138. if len(print_information.materialLengths) > extruder_position:
  139. extruder_dict["material_used"] = print_information.materialLengths[extruder_position]
  140. extruder_dict["variant"] = extruder.variant.getName()
  141. extruder_dict["nozzle_size"] = extruder.getProperty("machine_nozzle_size", "value")
  142. extruder_settings = dict()
  143. extruder_settings["wall_line_count"] = extruder.getProperty("wall_line_count", "value")
  144. extruder_settings["retraction_enable"] = extruder.getProperty("retraction_enable", "value")
  145. extruder_settings["infill_sparse_density"] = extruder.getProperty("infill_sparse_density", "value")
  146. extruder_settings["infill_pattern"] = extruder.getProperty("infill_pattern", "value")
  147. extruder_settings["gradual_infill_steps"] = extruder.getProperty("gradual_infill_steps", "value")
  148. extruder_settings["default_material_print_temperature"] = extruder.getProperty("default_material_print_temperature", "value")
  149. extruder_settings["material_print_temperature"] = extruder.getProperty("material_print_temperature", "value")
  150. extruder_dict["extruder_settings"] = extruder_settings
  151. data["extruders"].append(extruder_dict)
  152. data["intent_category"] = global_stack.getIntentCategory()
  153. data["quality_profile"] = global_stack.quality.getMetaData().get("quality_type")
  154. data["user_modified_setting_keys"] = self._getUserModifiedSettingKeys()
  155. data["models"] = []
  156. # Listing all files placed on the build plate
  157. for node in DepthFirstIterator(self._application.getController().getScene().getRoot()):
  158. if node.callDecoration("isSliceable"):
  159. model = dict()
  160. model["hash"] = node.getMeshData().getHash()
  161. bounding_box = node.getBoundingBox()
  162. if not bounding_box:
  163. continue
  164. model["bounding_box"] = {"minimum": {"x": bounding_box.minimum.x,
  165. "y": bounding_box.minimum.y,
  166. "z": bounding_box.minimum.z},
  167. "maximum": {"x": bounding_box.maximum.x,
  168. "y": bounding_box.maximum.y,
  169. "z": bounding_box.maximum.z}}
  170. model["transformation"] = {"data": str(node.getWorldTransformation(copy = False).getData()).replace("\n", "")}
  171. extruder_position = node.callDecoration("getActiveExtruderPosition")
  172. model["extruder"] = 0 if extruder_position is None else int(extruder_position)
  173. model_settings = dict()
  174. model_stack = node.callDecoration("getStack")
  175. if model_stack:
  176. model_settings["support_enabled"] = model_stack.getProperty("support_enable", "value")
  177. model_settings["support_extruder_nr"] = int(model_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  178. # Mesh modifiers;
  179. model_settings["infill_mesh"] = model_stack.getProperty("infill_mesh", "value")
  180. model_settings["cutting_mesh"] = model_stack.getProperty("cutting_mesh", "value")
  181. model_settings["support_mesh"] = model_stack.getProperty("support_mesh", "value")
  182. model_settings["anti_overhang_mesh"] = model_stack.getProperty("anti_overhang_mesh", "value")
  183. model_settings["wall_line_count"] = model_stack.getProperty("wall_line_count", "value")
  184. model_settings["retraction_enable"] = model_stack.getProperty("retraction_enable", "value")
  185. # Infill settings
  186. model_settings["infill_sparse_density"] = model_stack.getProperty("infill_sparse_density", "value")
  187. model_settings["infill_pattern"] = model_stack.getProperty("infill_pattern", "value")
  188. model_settings["gradual_infill_steps"] = model_stack.getProperty("gradual_infill_steps", "value")
  189. model["model_settings"] = model_settings
  190. if node.source_mime_type is None:
  191. model["mime_type"] = ""
  192. else:
  193. model["mime_type"] = node.source_mime_type.name
  194. data["models"].append(model)
  195. print_times = print_information.printTimes()
  196. data["print_times"] = {"travel": int(print_times["travel"].getDisplayString(DurationFormat.Format.Seconds)),
  197. "support": int(print_times["support"].getDisplayString(DurationFormat.Format.Seconds)),
  198. "infill": int(print_times["infill"].getDisplayString(DurationFormat.Format.Seconds)),
  199. "total": int(print_information.currentPrintTime.getDisplayString(DurationFormat.Format.Seconds))}
  200. print_settings = dict()
  201. print_settings["layer_height"] = global_stack.getProperty("layer_height", "value")
  202. # Support settings
  203. print_settings["support_enabled"] = global_stack.getProperty("support_enable", "value")
  204. print_settings["support_extruder_nr"] = int(global_stack.getExtruderPositionValueWithDefault("support_extruder_nr"))
  205. # Platform adhesion settings
  206. print_settings["adhesion_type"] = global_stack.getProperty("adhesion_type", "value")
  207. # Shell settings
  208. print_settings["wall_line_count"] = global_stack.getProperty("wall_line_count", "value")
  209. print_settings["retraction_enable"] = global_stack.getProperty("retraction_enable", "value")
  210. # Prime tower settings
  211. print_settings["prime_tower_mode"] = global_stack.getProperty("prime_tower_mode", "value")
  212. # Infill settings
  213. print_settings["infill_sparse_density"] = global_stack.getProperty("infill_sparse_density", "value")
  214. print_settings["infill_pattern"] = global_stack.getProperty("infill_pattern", "value")
  215. print_settings["gradual_infill_steps"] = global_stack.getProperty("gradual_infill_steps", "value")
  216. print_settings["print_sequence"] = global_stack.getProperty("print_sequence", "value")
  217. data["print_settings"] = print_settings
  218. # Send the name of the output device type that is used.
  219. data["output_to"] = type(output_device).__name__
  220. # Engine Statistics (Slicing Time, ...)
  221. # Call it backend-time, sice we might want to get the actual slice time from the engine itself,
  222. # to also identify problems in between the users pressing the button and the engine actually starting
  223. # (and the other way around with data that arrives back from the engine).
  224. time_setup = 0.0
  225. time_backend = 0.0
  226. if not print_information.preSliced:
  227. backend_info = self._application.getBackend().resetAndReturnLastSliceTimeStats()
  228. time_start_process = backend_info["time_start_process"]
  229. time_send_message = backend_info["time_send_message"]
  230. time_end_slice = backend_info["time_end_slice"]
  231. if time_start_process and time_send_message and time_end_slice:
  232. time_setup = time_send_message - time_start_process
  233. time_backend = time_end_slice - time_send_message
  234. data["engine_stats"] = {
  235. "is_presliced": int(print_information.preSliced),
  236. "time_setup": int(round(time_setup)),
  237. "time_backend": int(round(time_backend)),
  238. }
  239. # Convert data to bytes
  240. binary_data = json.dumps(data).encode("utf-8")
  241. # Send slice info non-blocking
  242. network_manager = self._application.getHttpRequestManager()
  243. network_manager.post(self.info_url, data = binary_data,
  244. callback = self._onRequestFinished, error_callback = self._onRequestError)
  245. except Exception:
  246. # We really can't afford to have a mistake here, as this would break the sending of g-code to a device
  247. # (Either saving or directly to a printer). The functionality of the slice data is not *that* important.
  248. Logger.logException("e", "Exception raised while sending slice info.") # But we should be notified about these problems of course.
  249. def _onRequestFinished(self, reply: "QNetworkReply") -> None:
  250. status_code = reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute)
  251. if status_code == 200:
  252. Logger.log("i", "SliceInfo sent successfully")
  253. return
  254. data = reply.readAll().data().decode("utf-8")
  255. Logger.log("e", "SliceInfo request failed, status code %s, data: %s", status_code, data)
  256. def _onRequestError(self, reply: "QNetworkReply", error: "QNetworkReply.NetworkError") -> None:
  257. Logger.log("e", "Got error for SliceInfo request: %s", reply.errorString())