SliceInfo.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from cura.CuraApplication import CuraApplication
  4. from cura.Settings.ExtruderManager import ExtruderManager
  5. from UM.Extension import Extension
  6. from UM.Application import Application
  7. from UM.Preferences import Preferences
  8. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  9. from UM.Message import Message
  10. from UM.i18n import i18nCatalog
  11. from UM.Logger import Logger
  12. import time
  13. from UM.Qt.Duration import DurationFormat
  14. from .SliceInfoJob import SliceInfoJob
  15. import platform
  16. import math
  17. import urllib.request
  18. import urllib.parse
  19. import json
  20. catalog = i18nCatalog("cura")
  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. class SliceInfo(Extension):
  25. info_url = "https://stats.ultimaker.com/api/cura"
  26. def __init__(self):
  27. super().__init__()
  28. Application.getInstance().getOutputDeviceManager().writeStarted.connect(self._onWriteStarted)
  29. Preferences.getInstance().addPreference("info/send_slice_info", True)
  30. Preferences.getInstance().addPreference("info/asked_send_slice_info", False)
  31. if not Preferences.getInstance().getValue("info/asked_send_slice_info"):
  32. self.send_slice_info_message = Message(catalog.i18nc("@info", "Cura collects anonymised slicing statistics. You can disable this in the preferences."), lifetime = 0, dismissable = False)
  33. self.send_slice_info_message.addAction("Dismiss", catalog.i18nc("@action:button", "Dismiss"), None, "")
  34. self.send_slice_info_message.actionTriggered.connect(self.messageActionTriggered)
  35. self.send_slice_info_message.show()
  36. def messageActionTriggered(self, message_id, action_id):
  37. self.send_slice_info_message.hide()
  38. Preferences.getInstance().setValue("info/asked_send_slice_info", True)
  39. def _onWriteStarted(self, output_device):
  40. try:
  41. if not Preferences.getInstance().getValue("info/send_slice_info"):
  42. Logger.log("d", "'info/send_slice_info' is turned off.")
  43. return # Do nothing, user does not want to send data
  44. global_container_stack = Application.getInstance().getGlobalContainerStack()
  45. print_information = Application.getInstance().getPrintInformation()
  46. data = dict() # The data that we're going to submit.
  47. data["time_stamp"] = time.time()
  48. data["schema_version"] = 0
  49. data["cura_version"] = Application.getInstance().getVersion()
  50. active_mode = Preferences.getInstance().getValue("cura/active_mode")
  51. if active_mode == 0:
  52. data["active_mode"] = "recommended"
  53. else:
  54. data["active_mode"] = "custom"
  55. definition_changes = global_container_stack.definitionChanges
  56. machine_settings_changed_by_user = False
  57. if definition_changes.getId() != "empty":
  58. # Now a definition_changes container will always be created for a stack,
  59. # so we also need to check if there is any instance in the definition_changes container
  60. if definition_changes.getAllKeys():
  61. machine_settings_changed_by_user = True
  62. data["machine_settings_changed_by_user"] = machine_settings_changed_by_user
  63. data["language"] = Preferences.getInstance().getValue("general/language")
  64. data["os"] = {"type": platform.system(), "version": platform.version()}
  65. data["active_machine"] = {"definition_id": global_container_stack.definition.getId(), "manufacturer": global_container_stack.definition.getMetaData().get("manufacturer","")}
  66. data["extruders"] = []
  67. extruder_count = len(global_container_stack.extruders)
  68. extruders = []
  69. if extruder_count > 1:
  70. extruders = list(ExtruderManager.getInstance().getMachineExtruders(global_container_stack.getId()))
  71. extruders = sorted(extruders, key = lambda extruder: extruder.getMetaDataEntry("position"))
  72. if not extruders:
  73. extruders = [global_container_stack]
  74. for extruder in extruders:
  75. extruder_dict = dict()
  76. extruder_dict["active"] = ExtruderManager.getInstance().getActiveExtruderStack() == extruder
  77. extruder_dict["material"] = {"GUID": extruder.material.getMetaData().get("GUID", ""),
  78. "type": extruder.material.getMetaData().get("material", ""),
  79. "brand": extruder.material.getMetaData().get("brand", "")
  80. }
  81. extruder_dict["material_used"] = print_information.materialLengths[int(extruder.getMetaDataEntry("position", "0"))]
  82. extruder_dict["variant"] = extruder.variant.getName()
  83. extruder_dict["nozzle_size"] = extruder.getProperty("machine_nozzle_size", "value")
  84. extruder_settings = dict()
  85. extruder_settings["wall_line_count"] = extruder.getProperty("wall_line_count", "value")
  86. extruder_settings["retraction_enable"] = extruder.getProperty("retraction_enable", "value")
  87. extruder_settings["infill_sparse_density"] = extruder.getProperty("infill_sparse_density", "value")
  88. extruder_settings["infill_pattern"] = extruder.getProperty("infill_pattern", "value")
  89. extruder_settings["gradual_infill_steps"] = extruder.getProperty("gradual_infill_steps", "value")
  90. extruder_settings["default_material_print_temperature"] = extruder.getProperty("default_material_print_temperature", "value")
  91. extruder_settings["material_print_temperature"] = extruder.getProperty("material_print_temperature", "value")
  92. extruder_dict["extruder_settings"] = extruder_settings
  93. data["extruders"].append(extruder_dict)
  94. data["quality_profile"] = global_container_stack.quality.getMetaData().get("quality_type")
  95. data["models"] = []
  96. # Listing all files placed on the build plate
  97. for node in DepthFirstIterator(CuraApplication.getInstance().getController().getScene().getRoot()):
  98. if node.callDecoration("isSliceable"):
  99. model = dict()
  100. model["hash"] = node.getMeshData().getHash()
  101. bounding_box = node.getBoundingBox()
  102. model["bounding_box"] = {"minimum": {"x": bounding_box.minimum.x,
  103. "y": bounding_box.minimum.y,
  104. "z": bounding_box.minimum.z},
  105. "maximum": {"x": bounding_box.maximum.x,
  106. "y": bounding_box.maximum.y,
  107. "z": bounding_box.maximum.z}}
  108. model["transformation"] = {"data": str(node.getWorldTransformation().getData()).replace("\n", "")}
  109. extruder_position = node.callDecoration("getActiveExtruderPosition")
  110. model["extruder"] = 0 if extruder_position is None else int(extruder_position)
  111. model_settings = dict()
  112. model_stack = node.callDecoration("getStack")
  113. if model_stack:
  114. model_settings["support_enabled"] = model_stack.getProperty("support_enable", "value")
  115. model_settings["support_extruder_nr"] = int(model_stack.getProperty("support_extruder_nr", "value"))
  116. # Mesh modifiers;
  117. model_settings["infill_mesh"] = model_stack.getProperty("infill_mesh", "value")
  118. model_settings["cutting_mesh"] = model_stack.getProperty("cutting_mesh", "value")
  119. model_settings["support_mesh"] = model_stack.getProperty("support_mesh", "value")
  120. model_settings["anti_overhang_mesh"] = model_stack.getProperty("anti_overhang_mesh", "value")
  121. model_settings["wall_line_count"] = model_stack.getProperty("wall_line_count", "value")
  122. model_settings["retraction_enable"] = model_stack.getProperty("retraction_enable", "value")
  123. # Infill settings
  124. model_settings["infill_sparse_density"] = model_stack.getProperty("infill_sparse_density", "value")
  125. model_settings["infill_pattern"] = model_stack.getProperty("infill_pattern", "value")
  126. model_settings["gradual_infill_steps"] = model_stack.getProperty("gradual_infill_steps", "value")
  127. model["model_settings"] = model_settings
  128. data["models"].append(model)
  129. print_times = print_information.printTimesPerFeature
  130. data["print_times"] = {"travel": int(print_times["travel"].getDisplayString(DurationFormat.Format.Seconds)),
  131. "support": int(print_times["support"].getDisplayString(DurationFormat.Format.Seconds)),
  132. "infill": int(print_times["infill"].getDisplayString(DurationFormat.Format.Seconds)),
  133. "total": int(print_information.currentPrintTime.getDisplayString(DurationFormat.Format.Seconds))}
  134. print_settings = dict()
  135. print_settings["layer_height"] = global_container_stack.getProperty("layer_height", "value")
  136. # Support settings
  137. print_settings["support_enabled"] = global_container_stack.getProperty("support_enable", "value")
  138. print_settings["support_extruder_nr"] = int(global_container_stack.getProperty("support_extruder_nr", "value"))
  139. # Platform adhesion settings
  140. print_settings["adhesion_type"] = global_container_stack.getProperty("adhesion_type", "value")
  141. # Shell settings
  142. print_settings["wall_line_count"] = global_container_stack.getProperty("wall_line_count", "value")
  143. print_settings["retraction_enable"] = global_container_stack.getProperty("retraction_enable", "value")
  144. # Prime tower settings
  145. print_settings["prime_tower_enable"] = global_container_stack.getProperty("prime_tower_enable", "value")
  146. # Infill settings
  147. print_settings["infill_sparse_density"] = global_container_stack.getProperty("infill_sparse_density", "value")
  148. print_settings["infill_pattern"] = global_container_stack.getProperty("infill_pattern", "value")
  149. print_settings["gradual_infill_steps"] = global_container_stack.getProperty("gradual_infill_steps", "value")
  150. print_settings["print_sequence"] = global_container_stack.getProperty("print_sequence", "value")
  151. data["print_settings"] = print_settings
  152. # Send the name of the output device type that is used.
  153. data["output_to"] = type(output_device).__name__
  154. # Convert data to bytes
  155. binary_data = json.dumps(data).encode("utf-8")
  156. # Sending slice info non-blocking
  157. reportJob = SliceInfoJob(self.info_url, binary_data)
  158. reportJob.start()
  159. except Exception:
  160. # We really can't afford to have a mistake here, as this would break the sending of g-code to a device
  161. # (Either saving or directly to a printer). The functionality of the slice data is not *that* important.
  162. Logger.logException("e", "Exception raised while sending slice info.") # But we should be notified about these problems of course.