From 9f189b06942d94624cbbc7930d1bdcb058ef3553 Mon Sep 17 00:00:00 2001 From: Roman Dvorak Date: Tue, 21 May 2024 22:29:46 +0200 Subject: [PATCH 1/5] align Z in pack --- src/build123d/pack.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/build123d/pack.py b/src/build123d/pack.py index dc97650e..2d8b074b 100644 --- a/src/build123d/pack.py +++ b/src/build123d/pack.py @@ -14,7 +14,7 @@ from dataclasses import dataclass from typing import Callable, Collection, Optional, cast -from build123d import Location, Shape +from build123d import Location, Shape, Pos def _pack2d( @@ -119,7 +119,7 @@ def grow_down(w, h): return [(t[1], t[2]) for t in sorted(translations, key=lambda t: t[0])] -def pack(objects: Collection[Shape], padding: float) -> Collection[Shape]: +def pack(objects: Collection[Shape], padding: float, align_z: bool = True) -> Collection[Shape]: """Pack objects in a squarish area in Plane.XY.""" bounding_boxes = {o: o.bounding_box().size + (padding, padding) for o in objects} translations = _pack2d( @@ -128,10 +128,16 @@ def pack(objects: Collection[Shape], padding: float) -> Collection[Shape]: length_fn=lambda o: bounding_boxes[cast(Shape, o)].Y, ) translated = [ - Location((t[0] - o.bounding_box().min.X, t[1] - o.bounding_box().min.Y, 0)) * o + Location((t[0] - o.bounding_box().min.X, t[1] - o.bounding_box().min.Y, 0)) * Pos((0, 0, -o.bounding_box().min.Z if align_z else 0)) * o for (o, t) in zip(objects, translations) ] + for o in objects: + print(o.bounding_box() ) + + for o in translated: + print(o.bounding_box() ) + # Assert the packing didn't cause any overlaps. def _overlapping(bb1, bb2): # Boundaries of the intersection of the two bounding boxes. From fe832a80fd1c525603083141119e5e4a3d5cd847 Mon Sep 17 00:00:00 2001 From: Roman Dvorak Date: Tue, 21 May 2024 22:37:55 +0200 Subject: [PATCH 2/5] clean code --- src/build123d/pack.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/build123d/pack.py b/src/build123d/pack.py index 2d8b074b..a18a54bc 100644 --- a/src/build123d/pack.py +++ b/src/build123d/pack.py @@ -132,12 +132,6 @@ def pack(objects: Collection[Shape], padding: float, align_z: bool = True) -> Co for (o, t) in zip(objects, translations) ] - for o in objects: - print(o.bounding_box() ) - - for o in translated: - print(o.bounding_box() ) - # Assert the packing didn't cause any overlaps. def _overlapping(bb1, bb2): # Boundaries of the intersection of the two bounding boxes. From 5c30afbc0f2a1fd37ac19b3d012c6c7d573737ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Dvo=C5=99=C3=A1k?= Date: Sun, 26 May 2024 14:48:33 +0200 Subject: [PATCH 3/5] Update pack.py --- src/build123d/pack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build123d/pack.py b/src/build123d/pack.py index a18a54bc..47893c4a 100644 --- a/src/build123d/pack.py +++ b/src/build123d/pack.py @@ -119,7 +119,7 @@ def grow_down(w, h): return [(t[1], t[2]) for t in sorted(translations, key=lambda t: t[0])] -def pack(objects: Collection[Shape], padding: float, align_z: bool = True) -> Collection[Shape]: +def pack(objects: Collection[Shape], padding: float, align_z: bool = False) -> Collection[Shape]: """Pack objects in a squarish area in Plane.XY.""" bounding_boxes = {o: o.bounding_box().size + (padding, padding) for o in objects} translations = _pack2d( From f3b639388203ec1f17227304b33e2659337be7f3 Mon Sep 17 00:00:00 2001 From: Roman Dvorak Date: Sun, 26 May 2024 15:02:50 +0200 Subject: [PATCH 4/5] update docs --- docs/tips.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/tips.rst b/docs/tips.rst index e3aa84eb..8bd9d7f2 100644 --- a/docs/tips.rst +++ b/docs/tips.rst @@ -172,6 +172,14 @@ padding (right): :align: right +By default, the original Z value of all objects packed using the :meth:`pack.pack` function is preserved. +If you want to align all objects so that they are "placed" on the zero Z coordinate, the :meth:`pack` +function has an `align_z` argument. When set to `True`, this will align all objects. + +This can be useful, for example, when preparing print setups for 3D printing, giving you full control +over this alignment so you don't have to leave it to the slicer. + + .. _are_glob_imports_bad_practice: *********************************************** From 1669db97d1fa461ecce4394418bb86de4a15ad74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Dvo=C5=99=C3=A1k?= Date: Sun, 26 May 2024 20:29:23 +0200 Subject: [PATCH 5/5] Update pack's function docstring --- src/build123d/pack.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/build123d/pack.py b/src/build123d/pack.py index 47893c4a..88ca5804 100644 --- a/src/build123d/pack.py +++ b/src/build123d/pack.py @@ -120,7 +120,17 @@ def grow_down(w, h): def pack(objects: Collection[Shape], padding: float, align_z: bool = False) -> Collection[Shape]: - """Pack objects in a squarish area in Plane.XY.""" + """Pack objects in a squarish area in Plane.XY. + + Args: + objects (Collection[Shape]): objects to arrange + padding (float): space between objects + align_z (bool, optional): align shape bottoms to Plane.XY. Defaults to False. + + Returns: + Collection[Shape]: rearranged objects + """ + bounding_boxes = {o: o.bounding_box().size + (padding, padding) for o in objects} translations = _pack2d( objects,