ExtruderConfigurationModel.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from PyQt6.QtCore import pyqtProperty, QObject, pyqtSignal
  5. from .MaterialOutputModel import MaterialOutputModel
  6. class ExtruderConfigurationModel(QObject):
  7. extruderConfigurationChanged = pyqtSignal()
  8. def __init__(self, position: int = -1) -> None:
  9. super().__init__()
  10. self._position: int = position
  11. self._material: Optional[MaterialOutputModel] = None
  12. self._hotend_id: Optional[str] = None
  13. def setPosition(self, position: int) -> None:
  14. self._position = position
  15. @pyqtProperty(int, fset = setPosition, notify = extruderConfigurationChanged)
  16. def position(self) -> int:
  17. return self._position
  18. def setMaterial(self, material: Optional[MaterialOutputModel]) -> None:
  19. if material is None or self._material == material:
  20. return
  21. self._material = material
  22. self.extruderConfigurationChanged.emit()
  23. @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
  24. def activeMaterial(self) -> Optional[MaterialOutputModel]:
  25. return self._material
  26. @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
  27. def material(self) -> Optional[MaterialOutputModel]:
  28. return self._material
  29. def setHotendID(self, hotend_id: Optional[str]) -> None:
  30. if self._hotend_id != hotend_id:
  31. self._hotend_id = ExtruderConfigurationModel.applyNameMappingHotend(hotend_id)
  32. self.extruderConfigurationChanged.emit()
  33. @staticmethod
  34. def applyNameMappingHotend(hotendId) -> str:
  35. _EXTRUDER_NAME_MAP = {
  36. "mk14_hot":"1XA",
  37. "mk14_hot_s":"2XA",
  38. "mk14_c":"1C",
  39. "mk14":"1A",
  40. "mk14_s":"2A"
  41. }
  42. if hotendId in _EXTRUDER_NAME_MAP:
  43. return _EXTRUDER_NAME_MAP[hotendId]
  44. return hotendId
  45. @pyqtProperty(str, fset = setHotendID, notify = extruderConfigurationChanged)
  46. def hotendID(self) -> Optional[str]:
  47. return self._hotend_id
  48. def isValid(self) -> bool:
  49. """This method is intended to indicate whether the configuration is valid or not.
  50. The method checks if the mandatory fields are or not set
  51. At this moment is always valid since we allow to have empty material and variants.
  52. """
  53. return True
  54. def __str__(self) -> str:
  55. message_chunks = []
  56. message_chunks.append("Position: " + str(self._position))
  57. message_chunks.append("-")
  58. message_chunks.append("Material: " + self.activeMaterial.type if self.activeMaterial else "empty")
  59. message_chunks.append("-")
  60. message_chunks.append("HotendID: " + self.hotendID if self.hotendID else "empty")
  61. return " ".join(message_chunks)
  62. def __eq__(self, other) -> bool:
  63. if not isinstance(other, ExtruderConfigurationModel):
  64. return False
  65. if self._position != other.position:
  66. return False
  67. # Empty materials should be ignored for comparison
  68. if self.activeMaterial is not None and other.activeMaterial is not None:
  69. if self.activeMaterial.guid != other.activeMaterial.guid:
  70. if self.activeMaterial.guid == "" and other.activeMaterial.guid == "":
  71. # At this point there is no material, so it doesn't matter what the hotend is.
  72. return True
  73. else:
  74. return False
  75. if self.hotendID != other.hotendID:
  76. return False
  77. return True
  78. # Calculating a hash function using the position of the extruder, the material GUID and the hotend id to check if is
  79. # unique within a set
  80. def __hash__(self):
  81. return hash(self._position) ^ (hash(self._material.guid) if self._material is not None else hash(0)) ^ hash(self._hotend_id)