MultiplyObjectsJob.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import copy
  4. from typing import List
  5. from UM.Application import Application
  6. from UM.Job import Job
  7. from UM.Math.Vector import Vector
  8. from UM.Message import Message
  9. from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
  10. from UM.Operations.GroupedOperation import GroupedOperation
  11. from UM.Operations.TranslateOperation import TranslateOperation
  12. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  13. from UM.Scene.SceneNode import SceneNode
  14. from UM.i18n import i18nCatalog
  15. from cura.Arranging.Nest2DArrange import arrange, createGroupOperationForArrange
  16. i18n_catalog = i18nCatalog("cura")
  17. class MultiplyObjectsJob(Job):
  18. def __init__(self, objects, count, min_offset = 8):
  19. super().__init__()
  20. self._objects = objects
  21. self._count = count
  22. self._min_offset = min_offset
  23. def run(self) -> None:
  24. status_message = Message(i18n_catalog.i18nc("@info:status", "Multiplying and placing objects"), lifetime = 0,
  25. dismissable = False, progress = 0,
  26. title = i18n_catalog.i18nc("@info:title", "Placing Objects"))
  27. status_message.show()
  28. scene = Application.getInstance().getController().getScene()
  29. global_container_stack = Application.getInstance().getGlobalContainerStack()
  30. if global_container_stack is None:
  31. return # We can't do anything in this case.
  32. root = scene.getRoot()
  33. processed_nodes = [] # type: List[SceneNode]
  34. nodes = []
  35. fixed_nodes = []
  36. for node_ in DepthFirstIterator(root):
  37. # Only count sliceable objects
  38. if node_.callDecoration("isSliceable"):
  39. fixed_nodes.append(node_)
  40. nodes_to_add_without_arrange = []
  41. for node in self._objects:
  42. # If object is part of a group, multiply group
  43. current_node = node
  44. while current_node.getParent() and current_node.getParent().callDecoration("isGroup"):
  45. current_node = current_node.getParent()
  46. if current_node in processed_nodes:
  47. continue
  48. processed_nodes.append(current_node)
  49. for _ in range(self._count):
  50. new_node = copy.deepcopy(node)
  51. # Same build plate
  52. build_plate_number = current_node.callDecoration("getBuildPlateNumber")
  53. new_node.callDecoration("setBuildPlateNumber", build_plate_number)
  54. for child in new_node.getChildren():
  55. child.callDecoration("setBuildPlateNumber", build_plate_number)
  56. if not current_node.getParent().callDecoration("isSliceable"):
  57. nodes.append(new_node)
  58. else:
  59. # The node we're trying to place has another node that is sliceable as a parent.
  60. # As such, we shouldn't arrange it (but it should be added to the scene!)
  61. nodes_to_add_without_arrange.append(new_node)
  62. new_node.setParent(current_node.getParent())
  63. found_solution_for_all = True
  64. group_operation = GroupedOperation()
  65. if nodes:
  66. group_operation, not_fit_count = createGroupOperationForArrange(nodes,
  67. Application.getInstance().getBuildVolume(),
  68. fixed_nodes,
  69. factor = 10000,
  70. add_new_nodes_in_scene = True)
  71. found_solution_for_all = not_fit_count == 0
  72. if nodes_to_add_without_arrange:
  73. for nested_node in nodes_to_add_without_arrange:
  74. group_operation.addOperation(AddSceneNodeOperation(nested_node, nested_node.getParent()))
  75. # Move the node a tiny bit so it doesn't overlap with the existing one.
  76. # This doesn't fix it if someone creates more than one duplicate, but it at least shows that something
  77. # happened (and after moving it, it's clear that there are more underneath)
  78. group_operation.addOperation(TranslateOperation(nested_node, Vector(2.5, 2.5, 2.5)))
  79. group_operation.push()
  80. status_message.hide()
  81. if not found_solution_for_all:
  82. no_full_solution_message = Message(
  83. i18n_catalog.i18nc("@info:status", "Unable to find a location within the build volume for all objects"),
  84. title = i18n_catalog.i18nc("@info:title", "Placing Object"),
  85. message_type = Message.MessageType.WARNING)
  86. no_full_solution_message.show()