From bd47dfef754c6d979d563c0817de3e70ec767a4f Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 13 Aug 2018 11:23:10 +0200 Subject: [PATCH 1/8] Revert "Revert "Merge pull request #4203 from Ultimaker/CURA-5538-fix-one-at-a-time-order-2"" This reverts commit 89ed2bcff80a7a414f77c455baa0023c631eae7c. --- cura/OneAtATimeIterator.py | 184 +++++++++++++++--------------- cura/Scene/ConvexHullDecorator.py | 2 +- cura/Settings/GlobalStack.py | 3 + 3 files changed, 94 insertions(+), 95 deletions(-) diff --git a/cura/OneAtATimeIterator.py b/cura/OneAtATimeIterator.py index 84d65bae8e..990ed37ab7 100644 --- a/cura/OneAtATimeIterator.py +++ b/cura/OneAtATimeIterator.py @@ -1,112 +1,108 @@ -# Copyright (c) 2015 Ultimaker B.V. +# Copyright (c) 2018 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. +import sys + +from shapely import affinity +from shapely.geometry import Polygon + from UM.Scene.Iterator import Iterator from UM.Scene.SceneNode import SceneNode -from functools import cmp_to_key -from UM.Application import Application -## Iterator that returns a list of nodes in the order that they need to be printed -# If there is no solution an empty list is returned. -# Take note that the list of nodes can have children (that may or may not contain mesh data) + +# Iterator that determines the object print order when one-at a time mode is enabled. +# +# In one-at-a-time mode, only one extruder can be enabled to print. In order to maximize the number of objects we can +# print, we need to print from the corner that's closest to the extruder that's being used. Here is an illustration: +# +# +--------------------------------+ +# | | +# | | +# | | - Rectangle represents the complete print head including fans, etc. +# | X X | y - X's are the nozzles +# | (1) (2) | | +# | | | +# +--------------------------------+ +--> x +# +# In this case, the nozzles are symmetric, nozzle (1) is closer to the bottom left corner while (2) is closer to the +# bottom right. If we use nozzle (1) to print, then we better off printing from the bottom left corner so the print +# head will not collide into an object on its top-right side, which is a very large unused area. Following the same +# logic, if we are printing with nozzle (2), then it's better to print from the bottom-right side. +# +# This iterator determines the print order following the rules above. +# class OneAtATimeIterator(Iterator.Iterator): + def __init__(self, scene_node): - super().__init__(scene_node) # Call super to make multiple inheritence work. - self._hit_map = [[]] + from cura.CuraApplication import CuraApplication + self._global_stack = CuraApplication.getInstance().getGlobalContainerStack() self._original_node_list = [] - + super().__init__(scene_node) # Call super to make multiple inheritance work. + + def getMachineNearestCornerToExtruder(self, global_stack): + head_and_fans_coordinates = global_stack.getHeadAndFansCoordinates() + + used_extruder = None + for extruder in global_stack.extruders.values(): + if extruder.isEnabled: + used_extruder = extruder + break + + extruder_offsets = [used_extruder.getProperty("machine_nozzle_offset_x", "value"), + used_extruder.getProperty("machine_nozzle_offset_y", "value")] + + # find the corner that's closest to the origin + min_distance2 = sys.maxsize + min_coord = None + for coord in head_and_fans_coordinates: + x = coord[0] - extruder_offsets[0] + y = coord[1] - extruder_offsets[1] + + distance2 = x**2 + y**2 + if distance2 <= min_distance2: + min_distance2 = distance2 + min_coord = coord + + return min_coord + def _fillStack(self): + min_coord = self.getMachineNearestCornerToExtruder(self._global_stack) + transform_x = -int(round(min_coord[0] / abs(min_coord[0]))) + transform_y = -int(round(min_coord[1] / abs(min_coord[1]))) + + machine_size = [self._global_stack.getProperty("machine_width", "value"), + self._global_stack.getProperty("machine_depth", "value")] + + def flip_x(polygon): + tm2 = [-1, 0, 0, 1, 0, 0] + return affinity.affine_transform(affinity.translate(polygon, xoff = -machine_size[0]), tm2) + + def flip_y(polygon): + tm2 = [1, 0, 0, -1, 0, 0] + return affinity.affine_transform(affinity.translate(polygon, yoff = -machine_size[1]), tm2) + node_list = [] for node in self._scene_node.getChildren(): if not issubclass(type(node), SceneNode): continue - if node.callDecoration("getConvexHull"): - node_list.append(node) + convex_hull = node.callDecoration("getConvexHull") + if convex_hull: + xmin = min(x for x, _ in convex_hull._points) + xmax = max(x for x, _ in convex_hull._points) + ymin = min(y for _, y in convex_hull._points) + ymax = max(y for _, y in convex_hull._points) + convex_hull_polygon = Polygon.from_bounds(xmin, ymin, xmax, ymax) + if transform_x < 0: + convex_hull_polygon = flip_x(convex_hull_polygon) + if transform_y < 0: + convex_hull_polygon = flip_y(convex_hull_polygon) - if len(node_list) < 2: - self._node_stack = node_list[:] - return + node_list.append({"node": node, + "min_coord": [convex_hull_polygon.bounds[0], convex_hull_polygon.bounds[1]], + }) - # Copy the list - self._original_node_list = node_list[:] - - ## Initialise the hit map (pre-compute all hits between all objects) - self._hit_map = [[self._checkHit(i,j) for i in node_list] for j in node_list] - - # Check if we have to files that block eachother. If this is the case, there is no solution! - for a in range(0,len(node_list)): - for b in range(0,len(node_list)): - if a != b and self._hit_map[a][b] and self._hit_map[b][a]: - return - - # Sort the original list so that items that block the most other objects are at the beginning. - # This does not decrease the worst case running time, but should improve it in most cases. - sorted(node_list, key = cmp_to_key(self._calculateScore)) - - todo_node_list = [_ObjectOrder([], node_list)] - while len(todo_node_list) > 0: - current = todo_node_list.pop() - for node in current.todo: - # Check if the object can be placed with what we have and still allows for a solution in the future - if not self._checkHitMultiple(node, current.order) and not self._checkBlockMultiple(node, current.todo): - # We found a possible result. Create new todo & order list. - new_todo_list = current.todo[:] - new_todo_list.remove(node) - new_order = current.order[:] + [node] - if len(new_todo_list) == 0: - # We have no more nodes to check, so quit looking. - todo_node_list = None - self._node_stack = new_order - - return - todo_node_list.append(_ObjectOrder(new_order, new_todo_list)) - self._node_stack = [] #No result found! - - - # Check if first object can be printed before the provided list (using the hit map) - def _checkHitMultiple(self, node, other_nodes): - node_index = self._original_node_list.index(node) - for other_node in other_nodes: - other_node_index = self._original_node_list.index(other_node) - if self._hit_map[node_index][other_node_index]: - return True - return False - - def _checkBlockMultiple(self, node, other_nodes): - node_index = self._original_node_list.index(node) - for other_node in other_nodes: - other_node_index = self._original_node_list.index(other_node) - if self._hit_map[other_node_index][node_index] and node_index != other_node_index: - return True - return False - - ## Calculate score simply sums the number of other objects it 'blocks' - def _calculateScore(self, a, b): - score_a = sum(self._hit_map[self._original_node_list.index(a)]) - score_b = sum(self._hit_map[self._original_node_list.index(b)]) - return score_a - score_b - - # Checks if A can be printed before B - def _checkHit(self, a, b): - if a == b: - return False - - overlap = a.callDecoration("getConvexHullBoundary").intersectsPolygon(b.callDecoration("getConvexHullHeadFull")) - if overlap: - return True - else: - return False - - -## Internal object used to keep track of a possible order in which to print objects. -class _ObjectOrder(): - def __init__(self, order, todo): - """ - :param order: List of indexes in which to print objects, ordered by printing order. - :param todo: List of indexes which are not yet inserted into the order list. - """ - self.order = order - self.todo = todo + node_list = sorted(node_list, key = lambda d: d["min_coord"]) + self._node_stack = [d["node"] for d in node_list] diff --git a/cura/Scene/ConvexHullDecorator.py b/cura/Scene/ConvexHullDecorator.py index 66bc8a7fc3..367144abfc 100644 --- a/cura/Scene/ConvexHullDecorator.py +++ b/cura/Scene/ConvexHullDecorator.py @@ -229,7 +229,7 @@ class ConvexHullDecorator(SceneNodeDecorator): return offset_hull def _getHeadAndFans(self): - return Polygon(numpy.array(self._global_stack.getProperty("machine_head_with_fans_polygon", "value"), numpy.float32)) + return Polygon(numpy.array(self._global_stack.getHeadAndFansCoordinates(), numpy.float32)) def _compute2DConvexHeadFull(self): return self._compute2DConvexHull().getMinkowskiHull(self._getHeadAndFans()) diff --git a/cura/Settings/GlobalStack.py b/cura/Settings/GlobalStack.py index 66f3290b85..ea955d24b0 100755 --- a/cura/Settings/GlobalStack.py +++ b/cura/Settings/GlobalStack.py @@ -172,6 +172,9 @@ class GlobalStack(CuraContainerStack): return False return True + def getHeadAndFansCoordinates(self): + return self.getProperty("machine_head_with_fans_polygon", "value") + ## private: global_stack_mime = MimeType( From d45d6add4026f898e4c47fc3ecfbe069215d5b5f Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 13 Aug 2018 12:56:41 +0200 Subject: [PATCH 2/8] Add collision detection in on-at-a-time iterator --- cura/OneAtATimeIterator.py | 45 +++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/cura/OneAtATimeIterator.py b/cura/OneAtATimeIterator.py index 990ed37ab7..e7ad833e1c 100644 --- a/cura/OneAtATimeIterator.py +++ b/cura/OneAtATimeIterator.py @@ -6,7 +6,7 @@ import sys from shapely import affinity from shapely.geometry import Polygon -from UM.Scene.Iterator import Iterator +from UM.Scene.Iterator.Iterator import Iterator from UM.Scene.SceneNode import SceneNode @@ -20,7 +20,7 @@ from UM.Scene.SceneNode import SceneNode # | | # | | - Rectangle represents the complete print head including fans, etc. # | X X | y - X's are the nozzles -# | (1) (2) | | +# | (1) (2) | ^ # | | | # +--------------------------------+ +--> x # @@ -31,12 +31,13 @@ from UM.Scene.SceneNode import SceneNode # # This iterator determines the print order following the rules above. # -class OneAtATimeIterator(Iterator.Iterator): +class OneAtATimeIterator(Iterator): def __init__(self, scene_node): from cura.CuraApplication import CuraApplication self._global_stack = CuraApplication.getInstance().getGlobalContainerStack() self._original_node_list = [] + super().__init__(scene_node) # Call super to make multiple inheritance work. def getMachineNearestCornerToExtruder(self, global_stack): @@ -65,6 +66,40 @@ class OneAtATimeIterator(Iterator.Iterator): return min_coord + def _checkForCollisions(self) -> bool: + all_nodes = [] + for node in self._scene_node.getChildren(): + if not issubclass(type(node), SceneNode): + continue + convex_hull = node.callDecoration("getConvexHullHead") + if not convex_hull: + continue + + bounding_box = node.getBoundingBox() + from UM.Math.Polygon import Polygon + bounding_box_polygon = Polygon([[bounding_box.left, bounding_box.front], + [bounding_box.left, bounding_box.back], + [bounding_box.right, bounding_box.back], + [bounding_box.right, bounding_box.front]]) + + all_nodes.append({"node": node, + "bounding_box": bounding_box_polygon, + "convex_hull": convex_hull}) + + has_collisions = False + for i, node_dict in enumerate(all_nodes): + for j, other_node_dict in enumerate(all_nodes): + if i == j: + continue + if node_dict["bounding_box"].intersectsPolygon(other_node_dict["convex_hull"]): + has_collisions = True + break + + if has_collisions: + break + + return has_collisions + def _fillStack(self): min_coord = self.getMachineNearestCornerToExtruder(self._global_stack) transform_x = -int(round(min_coord[0] / abs(min_coord[0]))) @@ -81,6 +116,10 @@ class OneAtATimeIterator(Iterator.Iterator): tm2 = [1, 0, 0, -1, 0, 0] return affinity.affine_transform(affinity.translate(polygon, yoff = -machine_size[1]), tm2) + if self._checkForCollisions(): + self._node_stack = [] + return + node_list = [] for node in self._scene_node.getChildren(): if not issubclass(type(node), SceneNode): From 42b509cd4a5eb51d66ee6474817b6a78ed97ce2b Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 13 Aug 2018 13:44:07 +0200 Subject: [PATCH 3/8] Fix a wrong type hinting --- cura/Settings/MachineManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index 7bf4b1394c..1941324eed 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -49,7 +49,7 @@ class MachineManager(QObject): def __init__(self, parent: QObject = None) -> None: super().__init__(parent) - self._active_container_stack = None # type: Optional[ExtruderManager] + self._active_container_stack = None # type: Optional[ExtruderStack] self._global_container_stack = None # type: Optional[GlobalStack] self._current_root_material_id = {} # type: Dict[str, str] From 25e6fd8becebbb2ddf180daa8a7eb6561bcb4dc0 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 13 Aug 2018 13:47:26 +0200 Subject: [PATCH 4/8] Fix incorrect type hinting --- cura/Settings/MachineManager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index 1941324eed..bdb96b373e 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -1183,7 +1183,7 @@ class MachineManager(QObject): if not self._global_container_stack.variant: self._global_container_stack.variant = self._application.empty_variant_container - def _setMaterial(self, position: str, container_node: ContainerNode = None) -> None: + def _setMaterial(self, position: str, container_node: Optional["ContainerNode"] = None) -> None: if self._global_container_stack is None: return if container_node and container_node.getContainer(): From 8e891b69b006d079ed98b3bdcf5434ef4b507cb5 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 13 Aug 2018 13:48:00 +0200 Subject: [PATCH 5/8] Move type hinting imports into TYPE_CHECKING block --- cura/Settings/MachineManager.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index bdb96b373e..d65bbfddd9 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -21,9 +21,6 @@ from UM.Settings.SettingFunction import SettingFunction from UM.Signal import postponeSignals, CompressTechnique import cura.CuraApplication -from cura.Machines.ContainerNode import ContainerNode #For typing. -from cura.Machines.QualityChangesGroup import QualityChangesGroup #For typing. -from cura.Machines.QualityGroup import QualityGroup #For typing. from cura.Machines.QualityManager import getMachineDefinitionIDForQualitySearch from cura.PrinterOutputDevice import PrinterOutputDevice from cura.PrinterOutput.ConfigurationModel import ConfigurationModel @@ -44,6 +41,10 @@ if TYPE_CHECKING: from cura.Machines.MaterialManager import MaterialManager from cura.Machines.QualityManager import QualityManager from cura.Machines.VariantManager import VariantManager + from cura.Machines.ContainerNode import ContainerNode + from cura.Machines.QualityChangesGroup import QualityChangesGroup + from cura.Machines.QualityGroup import QualityGroup + class MachineManager(QObject): def __init__(self, parent: QObject = None) -> None: @@ -1087,7 +1088,7 @@ class MachineManager(QObject): self.activeQualityGroupChanged.emit() self.activeQualityChangesGroupChanged.emit() - def _setQualityGroup(self, quality_group: Optional[QualityGroup], empty_quality_changes: bool = True) -> None: + def _setQualityGroup(self, quality_group: Optional["QualityGroup"], empty_quality_changes: bool = True) -> None: if self._global_container_stack is None: return if quality_group is None: @@ -1118,7 +1119,7 @@ class MachineManager(QObject): self.activeQualityGroupChanged.emit() self.activeQualityChangesGroupChanged.emit() - def _fixQualityChangesGroupToNotSupported(self, quality_changes_group: QualityChangesGroup) -> None: + def _fixQualityChangesGroupToNotSupported(self, quality_changes_group: "QualityChangesGroup") -> None: nodes = [quality_changes_group.node_for_global] + list(quality_changes_group.nodes_for_extruders.values()) containers = [n.getContainer() for n in nodes if n is not None] for container in containers: @@ -1126,7 +1127,7 @@ class MachineManager(QObject): container.setMetaDataEntry("quality_type", "not_supported") quality_changes_group.quality_type = "not_supported" - def _setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup) -> None: + def _setQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup") -> None: if self._global_container_stack is None: return #Can't change that. quality_type = quality_changes_group.quality_type @@ -1170,13 +1171,13 @@ class MachineManager(QObject): self.activeQualityGroupChanged.emit() self.activeQualityChangesGroupChanged.emit() - def _setVariantNode(self, position: str, container_node: ContainerNode) -> None: + def _setVariantNode(self, position: str, container_node: "ContainerNode") -> None: if container_node.getContainer() is None or self._global_container_stack is None: return self._global_container_stack.extruders[position].variant = container_node.getContainer() self.activeVariantChanged.emit() - def _setGlobalVariant(self, container_node: ContainerNode) -> None: + def _setGlobalVariant(self, container_node: "ContainerNode") -> None: if self._global_container_stack is None: return self._global_container_stack.variant = container_node.getContainer() @@ -1388,7 +1389,7 @@ class MachineManager(QObject): return bool(containers) @pyqtSlot("QVariant") - def setGlobalVariant(self, container_node: ContainerNode) -> None: + def setGlobalVariant(self, container_node: "ContainerNode") -> None: self.blurSettings.emit() with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue): self._setGlobalVariant(container_node) @@ -1438,7 +1439,7 @@ class MachineManager(QObject): self.setVariant(position, variant_node) @pyqtSlot(str, "QVariant") - def setVariant(self, position: str, container_node: ContainerNode) -> None: + def setVariant(self, position: str, container_node: "ContainerNode") -> None: position = str(position) self.blurSettings.emit() with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue): @@ -1462,7 +1463,7 @@ class MachineManager(QObject): ## Optionally provide global_stack if you want to use your own # The active global_stack is treated differently. @pyqtSlot(QObject) - def setQualityGroup(self, quality_group: QualityGroup, no_dialog: bool = False, global_stack: Optional["GlobalStack"] = None) -> None: + def setQualityGroup(self, quality_group: "QualityGroup", no_dialog: bool = False, global_stack: Optional["GlobalStack"] = None) -> None: if global_stack is not None and global_stack != self._global_container_stack: if quality_group is None: Logger.log("e", "Could not set quality group because quality group is None") @@ -1489,11 +1490,11 @@ class MachineManager(QObject): self._application.discardOrKeepProfileChanges() @pyqtProperty(QObject, fset = setQualityGroup, notify = activeQualityGroupChanged) - def activeQualityGroup(self) -> Optional[QualityGroup]: + def activeQualityGroup(self) -> Optional["QualityGroup"]: return self._current_quality_group @pyqtSlot(QObject) - def setQualityChangesGroup(self, quality_changes_group: QualityChangesGroup, no_dialog: bool = False) -> None: + def setQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup", no_dialog: bool = False) -> None: self.blurSettings.emit() with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue): self._setQualityChangesGroup(quality_changes_group) @@ -1512,7 +1513,7 @@ class MachineManager(QObject): stack.userChanges.clear() @pyqtProperty(QObject, fset = setQualityChangesGroup, notify = activeQualityChangesGroupChanged) - def activeQualityChangesGroup(self) -> Optional[QualityChangesGroup]: + def activeQualityChangesGroup(self) -> Optional["QualityChangesGroup"]: return self._current_quality_changes_group @pyqtProperty(str, notify = activeQualityGroupChanged) From e0ad7a9fb4bce64d3c5503877cc1b37086f8c2ab Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Mon, 13 Aug 2018 14:24:25 +0200 Subject: [PATCH 6/8] Add answer commments to some puzzling code --- cura/Settings/GlobalStack.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cura/Settings/GlobalStack.py b/cura/Settings/GlobalStack.py index 49c7b4f04a..dda21f3719 100755 --- a/cura/Settings/GlobalStack.py +++ b/cura/Settings/GlobalStack.py @@ -102,6 +102,9 @@ class GlobalStack(CuraContainerStack): # Handle the "resolve" property. #TODO: Why the hell does this involve threading? + # Answer: Because if multiple threads start resolving properties that have the same underlying properties that's + # related, without taking a note of which thread a resolve paths belongs to, they can bump into each other and + # generate unexpected behaviours. if self._shouldResolve(key, property_name, context): current_thread = threading.current_thread() self._resolving_settings[current_thread.name].add(key) From 6ffe0a9083029d6f78003bd81b2a5b78d5de33ca Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Mon, 13 Aug 2018 14:47:25 +0200 Subject: [PATCH 7/8] Fix indent and typo --- plugins/UM3NetworkPrinting/PrinterVideoStream.qml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/UM3NetworkPrinting/PrinterVideoStream.qml b/plugins/UM3NetworkPrinting/PrinterVideoStream.qml index e34924ba88..68758e095e 100644 --- a/plugins/UM3NetworkPrinting/PrinterVideoStream.qml +++ b/plugins/UM3NetworkPrinting/PrinterVideoStream.qml @@ -16,9 +16,9 @@ Item MouseArea { - anchors.fill: parent - onClicked: OutputDevice.setActivePrinter(null) - z: 0 + anchors.fill: parent + onClicked: OutputDevice.setActivePrinter(null) + z: 0 } Button @@ -28,7 +28,7 @@ Item anchors.bottomMargin: UM.Theme.getSize("default_margin").width anchors.right: cameraImage.right - // TODO: Harcoded sizes + // TODO: Hardcoded sizes width: 20 * screenScaleFactor height: 20 * screenScaleFactor From f91254c7c05faa7451a8dcd217b81eb0ccf1c028 Mon Sep 17 00:00:00 2001 From: Ian Paschal Date: Mon, 13 Aug 2018 15:27:01 +0200 Subject: [PATCH 8/8] Fix typo in comment --- cura/CuraApplication.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index b1a3dadc8e..78986d82ee 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -631,7 +631,7 @@ class CuraApplication(QtApplication): # Cura has multiple locations where instance containers need to be saved, so we need to handle this differently. def saveSettings(self): if not self.started or not self._save_data_enabled: - # Do not do saving during application start or when data should not be safed on quit. + # Do not do saving during application start or when data should not be saved on quit. return ContainerRegistry.getInstance().saveDirtyContainers() self.savePreferences()