SliceInfo.py 19 KB

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