mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-09 23:05:01 -06:00
review comments fixed
Co-authored-by: Casper Lamboo <c.lamboo@ultimaker.com> CURA-7951
This commit is contained in:
parent
4096fc864b
commit
118f49a052
7 changed files with 76 additions and 90 deletions
|
@ -16,7 +16,7 @@ i18n_catalog = i18nCatalog("cura")
|
|||
|
||||
class ArrangeObjectsJob(Job):
|
||||
def __init__(self, nodes: List[SceneNode], fixed_nodes: List[SceneNode], min_offset = 8,
|
||||
grid_arrange: bool = False) -> None:
|
||||
*, grid_arrange: bool = False) -> None:
|
||||
super().__init__()
|
||||
self._nodes = nodes
|
||||
self._fixed_nodes = fixed_nodes
|
||||
|
@ -33,13 +33,7 @@ class ArrangeObjectsJob(Job):
|
|||
status_message.show()
|
||||
|
||||
try:
|
||||
|
||||
if self._grid_arrange:
|
||||
grid_arrange = GridArrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes)
|
||||
found_solution_for_all = grid_arrange.arrange()
|
||||
|
||||
else:
|
||||
found_solution_for_all = arrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes)
|
||||
found_solution_for_all = arrange(self._nodes, Application.getInstance().getBuildVolume(), self._fixed_nodes, grid_arrange= self._grid_arrange)
|
||||
|
||||
except: # If the thread crashes, the message should still close
|
||||
Logger.logException("e", "Unable to arrange the objects on the buildplate. The arrange algorithm has crashed.")
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
import math
|
||||
from typing import List, TYPE_CHECKING, Optional, Tuple, Set
|
||||
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from UM.Scene.SceneNode import SceneNode
|
||||
|
||||
from UM.Application import Application
|
||||
from UM.Math import AxisAlignedBox
|
||||
from UM.Math.Vector import Vector
|
||||
|
@ -10,23 +15,16 @@ from UM.Operations.TranslateOperation import TranslateOperation
|
|||
|
||||
|
||||
class GridArrange:
|
||||
offset_x: float = 10
|
||||
offset_y: float = 10
|
||||
|
||||
_grid_width: float
|
||||
_grid_height: float
|
||||
|
||||
_nodes_to_arrange: List["SceneNode"]
|
||||
_fixed_nodes: List["SceneNode"]
|
||||
_build_volume: "BuildVolume"
|
||||
_build_volume_bounding_box: AxisAlignedBox
|
||||
|
||||
def __init__(self, nodes_to_arrange: List["SceneNode"], build_volume: "BuildVolume", fixed_nodes: List["SceneNode"] = []):
|
||||
def __init__(self, nodes_to_arrange: List["SceneNode"], build_volume: "BuildVolume", fixed_nodes: List["SceneNode"] = None):
|
||||
if fixed_nodes is None:
|
||||
fixed_nodes = []
|
||||
self._nodes_to_arrange = nodes_to_arrange
|
||||
self._build_volume = build_volume
|
||||
self._build_volume_bounding_box = build_volume.getBoundingBox()
|
||||
self._fixed_nodes = fixed_nodes
|
||||
|
||||
self._offset_x: float = 10
|
||||
self._offset_y: float = 10
|
||||
self._grid_width = 0
|
||||
self._grid_height = 0
|
||||
for node in self._nodes_to_arrange:
|
||||
|
@ -40,11 +38,6 @@ class GridArrange:
|
|||
self._initial_leftover_grid_x = math.floor(self._initial_leftover_grid_x)
|
||||
self._initial_leftover_grid_y = math.floor(self._initial_leftover_grid_y)
|
||||
|
||||
def arrange(self)-> bool:
|
||||
grouped_operation, not_fit_count = self.createGroupOperationForArrange()
|
||||
grouped_operation.push()
|
||||
return not_fit_count == 0
|
||||
|
||||
def createGroupOperationForArrange(self) -> Tuple[GroupedOperation, int]:
|
||||
# Find grid indexes that intersect with fixed objects
|
||||
fixed_nodes_grid_ids = set()
|
||||
|
@ -96,8 +89,8 @@ class GridArrange:
|
|||
|
||||
def moveNodeOnGrid(self, node: "SceneNode", grid_x: int, grid_y: int) -> "Operation.Operation":
|
||||
coord_grid_x, coord_grid_y = self.gridSpaceToCoordSpace(grid_x, grid_y)
|
||||
center_grid_x = coord_grid_x + (0.5 * (self._grid_width + self.offset_x))
|
||||
center_grid_y = coord_grid_y + (0.5 * (self._grid_height + self.offset_y))
|
||||
center_grid_x = coord_grid_x + (0.5 * (self._grid_width + self._offset_x))
|
||||
center_grid_y = coord_grid_y + (0.5 * (self._grid_height + self._offset_y))
|
||||
|
||||
bounding_box = node.getBoundingBox()
|
||||
center_node_x = (bounding_box.left + bounding_box.right) * 0.5
|
||||
|
@ -134,13 +127,13 @@ class GridArrange:
|
|||
return grid_idx
|
||||
|
||||
def gridSpaceToCoordSpace(self, x: float, y: float) -> Tuple[float, float]:
|
||||
grid_x = x * (self._grid_width + self.offset_x) + self._build_volume_bounding_box.left
|
||||
grid_y = y * (self._grid_height + self.offset_y) + self._build_volume_bounding_box.back
|
||||
grid_x = x * (self._grid_width + self._offset_x) + self._build_volume_bounding_box.left
|
||||
grid_y = y * (self._grid_height + self._offset_y) + self._build_volume_bounding_box.back
|
||||
return grid_x, grid_y
|
||||
|
||||
def coordSpaceToGridSpace(self, grid_x: float, grid_y: float) -> Tuple[float, float]:
|
||||
coord_x = (grid_x - self._build_volume_bounding_box.left) / (self._grid_width + self.offset_x)
|
||||
coord_y = (grid_y - self._build_volume_bounding_box.back) / (self._grid_height + self.offset_y)
|
||||
coord_x = (grid_x - self._build_volume_bounding_box.left) / (self._grid_width + self._offset_x)
|
||||
coord_y = (grid_y - self._build_volume_bounding_box.back) / (self._grid_height + self._offset_y)
|
||||
return coord_x, coord_y
|
||||
|
||||
def checkGridUnderDiscSpace(self, grid_x: int, grid_y: int) -> bool:
|
||||
|
|
|
@ -15,7 +15,7 @@ from UM.Operations.AddSceneNodeOperation import AddSceneNodeOperation
|
|||
from UM.Operations.GroupedOperation import GroupedOperation
|
||||
from UM.Operations.RotateOperation import RotateOperation
|
||||
from UM.Operations.TranslateOperation import TranslateOperation
|
||||
|
||||
from cura.Arranging.GridArrange import GridArrange
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from UM.Scene.SceneNode import SceneNode
|
||||
|
@ -27,6 +27,7 @@ def findNodePlacement(
|
|||
build_volume: "BuildVolume",
|
||||
fixed_nodes: Optional[List["SceneNode"]] = None,
|
||||
factor: int = 10000,
|
||||
*,
|
||||
lock_rotation: bool = False
|
||||
) -> Tuple[bool, List[Item]]:
|
||||
"""
|
||||
|
@ -124,30 +125,36 @@ def createGroupOperationForArrange(nodes_to_arrange: List["SceneNode"],
|
|||
build_volume: "BuildVolume",
|
||||
fixed_nodes: Optional[List["SceneNode"]] = None,
|
||||
factor: int = 10000,
|
||||
*,
|
||||
add_new_nodes_in_scene: bool = False,
|
||||
lock_rotation: bool = False) -> Tuple[GroupedOperation, int]:
|
||||
scene_root = Application.getInstance().getController().getScene().getRoot()
|
||||
found_solution_for_all, node_items = findNodePlacement(nodes_to_arrange, build_volume, fixed_nodes, factor,
|
||||
lock_rotation)
|
||||
lock_rotation: bool = False,
|
||||
grid_arrange: bool = False) -> Tuple[GroupedOperation, int]:
|
||||
if grid_arrange:
|
||||
grid = GridArrange(nodes_to_arrange, build_volume, fixed_nodes)
|
||||
return grid.createGroupOperationForArrange()
|
||||
else:
|
||||
scene_root = Application.getInstance().getController().getScene().getRoot()
|
||||
found_solution_for_all, node_items = findNodePlacement(nodes_to_arrange, build_volume, fixed_nodes, factor,
|
||||
lock_rotation = lock_rotation)
|
||||
|
||||
not_fit_count = 0
|
||||
grouped_operation = GroupedOperation()
|
||||
for node, node_item in zip(nodes_to_arrange, node_items):
|
||||
if add_new_nodes_in_scene:
|
||||
grouped_operation.addOperation(AddSceneNodeOperation(node, scene_root))
|
||||
not_fit_count = 0
|
||||
grouped_operation = GroupedOperation()
|
||||
for node, node_item in zip(nodes_to_arrange, node_items):
|
||||
if add_new_nodes_in_scene:
|
||||
grouped_operation.addOperation(AddSceneNodeOperation(node, scene_root))
|
||||
|
||||
if node_item.binId() == 0:
|
||||
# We found a spot for it
|
||||
rotation_matrix = Matrix()
|
||||
rotation_matrix.setByRotationAxis(node_item.rotation(), Vector(0, -1, 0))
|
||||
grouped_operation.addOperation(RotateOperation(node, Quaternion.fromMatrix(rotation_matrix)))
|
||||
grouped_operation.addOperation(TranslateOperation(node, Vector(node_item.translation().x() / factor, 0,
|
||||
node_item.translation().y() / factor)))
|
||||
else:
|
||||
# We didn't find a spot
|
||||
grouped_operation.addOperation(
|
||||
TranslateOperation(node, Vector(200, node.getWorldPosition().y, -not_fit_count * 20), set_position = True))
|
||||
not_fit_count += 1
|
||||
if node_item.binId() == 0:
|
||||
# We found a spot for it
|
||||
rotation_matrix = Matrix()
|
||||
rotation_matrix.setByRotationAxis(node_item.rotation(), Vector(0, -1, 0))
|
||||
grouped_operation.addOperation(RotateOperation(node, Quaternion.fromMatrix(rotation_matrix)))
|
||||
grouped_operation.addOperation(TranslateOperation(node, Vector(node_item.translation().x() / factor, 0,
|
||||
node_item.translation().y() / factor)))
|
||||
else:
|
||||
# We didn't find a spot
|
||||
grouped_operation.addOperation(
|
||||
TranslateOperation(node, Vector(200, node.getWorldPosition().y, -not_fit_count * 20), set_position = True))
|
||||
not_fit_count += 1
|
||||
|
||||
return grouped_operation, not_fit_count
|
||||
|
||||
|
@ -158,7 +165,8 @@ def arrange(
|
|||
fixed_nodes: Optional[List["SceneNode"]] = None,
|
||||
factor=10000,
|
||||
add_new_nodes_in_scene: bool = False,
|
||||
lock_rotation: bool = False
|
||||
lock_rotation: bool = False,
|
||||
grid_arrange: bool = False
|
||||
) -> bool:
|
||||
"""
|
||||
Find placement for a set of scene nodes, and move them by using a single grouped operation.
|
||||
|
@ -174,6 +182,6 @@ def arrange(
|
|||
"""
|
||||
|
||||
grouped_operation, not_fit_count = createGroupOperationForArrange(nodes_to_arrange, build_volume, fixed_nodes,
|
||||
factor, add_new_nodes_in_scene, lock_rotation)
|
||||
factor, add_new_nodes_in_scene = add_new_nodes_in_scene, lock_rotation = lock_rotation, grid_arrange = grid_arrange)
|
||||
grouped_operation.push()
|
||||
return not_fit_count == 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue