gumyr / build123d

A python CAD programming library
Apache License 2.0
382 stars 72 forks source link

Add `align_z` argument to pack() Method. #632

Closed roman-dvorak closed 1 month ago

roman-dvorak commented 1 month ago

Hi,

I've added a new argument align_z to the pack() method. When you enable this, it aligns all objects to the same Z axis.

Motivation and Context

I made this change to help with preparing print plates for 3D printing (set of multiple 3D bodies). The current pack() method only arranges objects in the X and Y coordinates. With the new align_z option, it becomes much easier to prepare objects for printing without needing to adjust them by a slicer.

How Has This Been Tested?

I did some manual testing to make sure the objects align correctly in the Z axis. See following images:

Without z_align (default)
parts = [box, lid]
p = pack(parts, padding=5, align_z=False)
show(p, reset_camera=Camera.KEEP)

obrazek obrazek

With z_align
parts = [box, lid]
p = pack(parts, padding=5, align_z=True)
show(p, reset_camera=Camera.KEEP)

obrazek

Thank you!

gumyr commented 1 month ago

The original implementation didn't have the complete docstring - would you add the following (or with any improvements you can think of) to the pack function?

    """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
    """

Thanks for the submission!

roman-dvorak commented 1 month ago

Thanks for the comment. I added it as you wrote it, I have no problem with it and it is correct. Thanks for the help.

I would also like to expand the documentation because the mention of pack function is quite hidden in between 'tips'. I would probably look for such information in the 'assemblies' section. So I would do that in a separate page and additional PR. Do you have any suggestions on where to place it in the documentation?

gumyr commented 1 month ago

Knowing where folks look for stuff is on of the difficult parts of writing good documentation so if you think you'd look in Assemblies I'm okay with that. Maybe a reference in the 3D exporters section would be helpful as well if the next step is to 3D print their objects?

I've merged the PR - thanks again!