ArrangeObjectsJob.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import List
  4. from UM.Application import Application
  5. from UM.Job import Job
  6. from UM.Logger import Logger
  7. from UM.Message import Message
  8. from UM.Scene.SceneNode import SceneNode
  9. from UM.i18n import i18nCatalog
  10. from cura.Arranging.GridArrange import GridArrange
  11. from cura.Arranging.Nest2DArrange import Nest2DArrange
  12. i18n_catalog = i18nCatalog("cura")
  13. class ArrangeObjectsJob(Job):
  14. def __init__(self, nodes: List[SceneNode], fixed_nodes: List[SceneNode], min_offset = 8,
  15. *, grid_arrange: bool = False) -> None:
  16. super().__init__()
  17. self._nodes = nodes
  18. self._fixed_nodes = fixed_nodes
  19. self._min_offset = min_offset
  20. self._grid_arrange = grid_arrange
  21. def run(self):
  22. found_solution_for_all = False
  23. status_message = Message(i18n_catalog.i18nc("@info:status", "Finding new location for objects"),
  24. lifetime = 0,
  25. dismissable = False,
  26. progress = 0,
  27. title = i18n_catalog.i18nc("@info:title", "Finding Location"))
  28. status_message.show()
  29. try:
  30. if self._grid_arrange:
  31. arranger = GridArrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes)
  32. else:
  33. arranger = Nest2DArrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes,
  34. factor=1000)
  35. found_solution_for_all = arranger.arrange()
  36. except: # If the thread crashes, the message should still close
  37. Logger.logException("e", "Unable to arrange the objects on the buildplate. The arrange algorithm has crashed.")
  38. status_message.hide()
  39. if not found_solution_for_all:
  40. no_full_solution_message = Message(
  41. i18n_catalog.i18nc("@info:status",
  42. "Unable to find a location within the build volume for all objects"),
  43. title = i18n_catalog.i18nc("@info:title", "Can't Find Location"),
  44. message_type = Message.MessageType.ERROR)
  45. no_full_solution_message.show()
  46. self.finished.emit(self)