ArrangeObjectsJob.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. if self._grid_arrange:
  30. arranger = GridArrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes)
  31. else:
  32. arranger = Nest2DArrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes,
  33. factor=1000)
  34. found_solution_for_all = False
  35. try:
  36. found_solution_for_all = arranger.arrange()
  37. except: # If the thread crashes, the message should still close
  38. Logger.logException("e",
  39. "Unable to arrange the objects on the buildplate. The arrange algorithm has crashed.")
  40. status_message.hide()
  41. if not found_solution_for_all:
  42. no_full_solution_message = Message(
  43. i18n_catalog.i18nc("@info:status",
  44. "Unable to find a location within the build volume for all objects"),
  45. title = i18n_catalog.i18nc("@info:title", "Can't Find Location"),
  46. message_type = Message.MessageType.ERROR)
  47. no_full_solution_message.show()
  48. self.finished.emit(self)