ArrangeObjectsJob.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Application import Application
  4. from UM.Job import Job
  5. from UM.Scene.SceneNode import SceneNode
  6. from UM.Math.Vector import Vector
  7. from UM.Operations.TranslateOperation import TranslateOperation
  8. from UM.Operations.GroupedOperation import GroupedOperation
  9. from UM.Logger import Logger
  10. from UM.Message import Message
  11. from UM.i18n import i18nCatalog
  12. i18n_catalog = i18nCatalog("cura")
  13. from cura.Scene.ZOffsetDecorator import ZOffsetDecorator
  14. from cura.Arranging.Arrange import Arrange
  15. from cura.Arranging.ShapeArray import ShapeArray
  16. from typing import List
  17. class ArrangeObjectsJob(Job):
  18. def __init__(self, nodes: List[SceneNode], fixed_nodes: List[SceneNode], min_offset = 8):
  19. super().__init__()
  20. self._nodes = nodes
  21. self._fixed_nodes = fixed_nodes
  22. self._min_offset = min_offset
  23. def run(self):
  24. status_message = Message(i18n_catalog.i18nc("@info:status", "Finding new location for objects"),
  25. lifetime = 0,
  26. dismissable=False,
  27. progress = 0,
  28. title = i18n_catalog.i18nc("@info:title", "Finding Location"))
  29. status_message.show()
  30. global_container_stack = Application.getInstance().getGlobalContainerStack()
  31. machine_width = global_container_stack.getProperty("machine_width", "value")
  32. machine_depth = global_container_stack.getProperty("machine_depth", "value")
  33. arranger = Arrange.create(x = machine_width, y = machine_depth, fixed_nodes = self._fixed_nodes)
  34. # Collect nodes to be placed
  35. nodes_arr = [] # fill with (size, node, offset_shape_arr, hull_shape_arr)
  36. for node in self._nodes:
  37. offset_shape_arr, hull_shape_arr = ShapeArray.fromNode(node, min_offset = self._min_offset)
  38. nodes_arr.append((offset_shape_arr.arr.shape[0] * offset_shape_arr.arr.shape[1], node, offset_shape_arr, hull_shape_arr))
  39. # Sort the nodes with the biggest area first.
  40. nodes_arr.sort(key=lambda item: item[0])
  41. nodes_arr.reverse()
  42. # Place nodes one at a time
  43. start_priority = 0
  44. last_priority = start_priority
  45. last_size = None
  46. grouped_operation = GroupedOperation()
  47. found_solution_for_all = True
  48. not_fit_count = 0
  49. for idx, (size, node, offset_shape_arr, hull_shape_arr) in enumerate(nodes_arr):
  50. # For performance reasons, we assume that when a location does not fit,
  51. # it will also not fit for the next object (while what can be untrue).
  52. if last_size == size: # This optimization works if many of the objects have the same size
  53. start_priority = last_priority
  54. else:
  55. start_priority = 0
  56. best_spot = arranger.bestSpot(offset_shape_arr, start_prio=start_priority)
  57. x, y = best_spot.x, best_spot.y
  58. node.removeDecorator(ZOffsetDecorator)
  59. if node.getBoundingBox():
  60. center_y = node.getWorldPosition().y - node.getBoundingBox().bottom
  61. else:
  62. center_y = 0
  63. if x is not None: # We could find a place
  64. last_size = size
  65. last_priority = best_spot.priority
  66. arranger.place(x, y, hull_shape_arr) # take place before the next one
  67. grouped_operation.addOperation(TranslateOperation(node, Vector(x, center_y, y), set_position = True))
  68. else:
  69. Logger.log("d", "Arrange all: could not find spot!")
  70. found_solution_for_all = False
  71. grouped_operation.addOperation(TranslateOperation(node, Vector(200, center_y, -not_fit_count * 20), set_position = True))
  72. not_fit_count += 1
  73. status_message.setProgress((idx + 1) / len(nodes_arr) * 100)
  74. Job.yieldThread()
  75. grouped_operation.push()
  76. status_message.hide()
  77. if not found_solution_for_all:
  78. no_full_solution_message = Message(i18n_catalog.i18nc("@info:status", "Unable to find a location within the build volume for all objects"),
  79. title = i18n_catalog.i18nc("@info:title", "Can't Find Location"))
  80. no_full_solution_message.show()
  81. self.finished.emit(self)