PrintInformation.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, pyqtSignal, pyqtProperty, pyqtSlot
  4. from UM.Application import Application
  5. from UM.Qt.Duration import Duration
  6. from UM.Preferences import Preferences
  7. import cura.Settings.ExtruderManager
  8. import math
  9. import os.path
  10. import unicodedata
  11. ## A class for processing and calculating minimum, current and maximum print time as well as managing the job name
  12. #
  13. # This class contains all the logic relating to calculation and slicing for the
  14. # time/quality slider concept. It is a rather tricky combination of event handling
  15. # and state management. The logic behind this is as follows:
  16. #
  17. # - A scene change or setting change event happens.
  18. # We track what the source was of the change, either a scene change, a setting change, an active machine change or something else.
  19. # - This triggers a new slice with the current settings - this is the "current settings pass".
  20. # - When the slice is done, we update the current print time and material amount.
  21. # - If the source of the slice was not a Setting change, we start the second slice pass, the "low quality settings pass". Otherwise we stop here.
  22. # - When that is done, we update the minimum print time and start the final slice pass, the "high quality settings pass".
  23. # - When the high quality pass is done, we update the maximum print time.
  24. #
  25. # This class also mangles the current machine name and the filename of the first loaded mesh into a job name.
  26. # This job name is requested by the JobSpecs qml file.
  27. class PrintInformation(QObject):
  28. class SlicePass:
  29. CurrentSettings = 1
  30. LowQualitySettings = 2
  31. HighQualitySettings = 3
  32. class SliceReason:
  33. SceneChanged = 1
  34. SettingChanged = 2
  35. ActiveMachineChanged = 3
  36. Other = 4
  37. def __init__(self, parent = None):
  38. super().__init__(parent)
  39. self._current_print_time = Duration(None, self)
  40. self._material_lengths = []
  41. self._material_weights = []
  42. self._backend = Application.getInstance().getBackend()
  43. if self._backend:
  44. self._backend.printDurationMessage.connect(self._onPrintDurationMessage)
  45. self._job_name = ""
  46. self._abbr_machine = ""
  47. Application.getInstance().globalContainerStackChanged.connect(self._setAbbreviatedMachineName)
  48. Application.getInstance().fileLoaded.connect(self.setJobName)
  49. currentPrintTimeChanged = pyqtSignal()
  50. @pyqtProperty(Duration, notify = currentPrintTimeChanged)
  51. def currentPrintTime(self):
  52. return self._current_print_time
  53. materialLengthsChanged = pyqtSignal()
  54. @pyqtProperty("QVariantList", notify = materialLengthsChanged)
  55. def materialLengths(self):
  56. return self._material_lengths
  57. materialWeightsChanged = pyqtSignal()
  58. @pyqtProperty("QVariantList", notify = materialWeightsChanged)
  59. def materialWeights(self):
  60. return self._material_weights
  61. def _onPrintDurationMessage(self, total_time, material_amounts):
  62. self._current_print_time.setDuration(total_time)
  63. self.currentPrintTimeChanged.emit()
  64. # Material amount is sent as an amount of mm^3, so calculate length from that
  65. r = Application.getInstance().getGlobalContainerStack().getProperty("material_diameter", "value") / 2
  66. self._material_lengths = []
  67. self._material_weights = []
  68. extruder_stacks = list(cura.Settings.ExtruderManager.getInstance().getMachineExtruders(Application.getInstance().getGlobalContainerStack().getId()))
  69. for index, amount in enumerate(material_amounts):
  70. ## Find the right extruder stack. As the list isn't sorted because it's a annoying generator, we do some
  71. # list comprehension filtering to solve this for us.
  72. if extruder_stacks: # Multi extrusion machine
  73. extruder_stack = [extruder for extruder in extruder_stacks if extruder.getMetaDataEntry("position") == str(index)][0]
  74. density = extruder_stack.getMetaDataEntry("properties", {}).get("density", 0)
  75. else: # Machine with no extruder stacks
  76. density = Application.getInstance().getGlobalContainerStack().getMetaDataEntry("properties", {}).get("density", 0)
  77. self._material_weights.append(float(amount) * float(density) / 1000)
  78. self._material_lengths.append(round((amount / (math.pi * r ** 2)) / 1000, 2))
  79. self.materialLengthsChanged.emit()
  80. self.materialWeightsChanged.emit()
  81. @pyqtSlot(str)
  82. def setJobName(self, name):
  83. # Ensure that we don't use entire path but only filename
  84. name = os.path.basename(name)
  85. # when a file is opened using the terminal; the filename comes from _onFileLoaded and still contains its
  86. # extension. This cuts the extension off if necessary.
  87. name = os.path.splitext(name)[0]
  88. if self._job_name != name:
  89. self._job_name = name
  90. self.jobNameChanged.emit()
  91. jobNameChanged = pyqtSignal()
  92. @pyqtProperty(str, notify = jobNameChanged)
  93. def jobName(self):
  94. return self._job_name
  95. @pyqtSlot(str, result = str)
  96. def createJobName(self, base_name):
  97. base_name = self._stripAccents(base_name)
  98. self._setAbbreviatedMachineName()
  99. if Preferences.getInstance().getValue("cura/jobname_prefix"):
  100. return self._abbr_machine + "_" + base_name
  101. else:
  102. return base_name
  103. ## Created an acronymn-like abbreviated machine name from the currently active machine name
  104. # Called each time the global stack is switched
  105. def _setAbbreviatedMachineName(self):
  106. global_container_stack = Application.getInstance().getGlobalContainerStack()
  107. if not global_container_stack:
  108. self._abbr_machine = ""
  109. return
  110. global_stack_name = global_container_stack.getName()
  111. split_name = global_stack_name.split(" ")
  112. abbr_machine = ""
  113. for word in split_name:
  114. if word.lower() == "ultimaker":
  115. abbr_machine += "UM"
  116. elif word.isdigit():
  117. abbr_machine += word
  118. else:
  119. abbr_machine += self._stripAccents(word.strip("()[]{}#").upper())[0]
  120. self._abbr_machine = abbr_machine
  121. ## Utility method that strips accents from characters (eg: â -> a)
  122. def _stripAccents(self, str):
  123. return ''.join(char for char in unicodedata.normalize('NFD', str) if unicodedata.category(char) != 'Mn')