gumyr / build123d

A python CAD programming library
Apache License 2.0
402 stars 75 forks source link

Nesting Locations is broken (related to Issue #29) #94

Closed gumyr closed 1 year ago

gumyr commented 1 year ago

All of these should produce the same results:

from build123d import *

shaft_size, mount_spacing, mount_hole = 8, 31, 3
with BuildSketch() as sk1:
    RegularPolygon(100, 7)
    with PolarLocations(55, 7):
        Circle(shaft_size / 2, mode=Mode.SUBTRACT)  # Shaft cutout for NEMA motor
        with GridLocations(mount_spacing, mount_spacing, 2, 2):
            Circle(mount_hole / 2, mode=Mode.SUBTRACT)  # Mounting holes for NEMA motors

with BuildSketch() as sk2:
    RegularPolygon(100, 7)
    motor_locations = PolarLocations(55, 7)
    print(motor_locations.locations)
    with Workplanes(*PolarLocations(55, 7).locations):
        wp = Workplanes._get_context().workplanes
        print(wp)
        Circle(shaft_size / 2, mode=Mode.SUBTRACT)  # Shaft cutout for NEMA motor
        with GridLocations(mount_spacing, mount_spacing, 2, 2):
            Circle(mount_hole / 2, mode=Mode.SUBTRACT)  # Mounting holes for NEMA motors

with BuildSketch() as sk3:
    RegularPolygon(100, 7)
    with BuildSketch(*PolarLocations(55, 7).locations, mode=Mode.SUBTRACT) as sk3b:
        Circle(shaft_size / 2)  # Shaft cutout for NEMA motor
        with GridLocations(mount_spacing, mount_spacing, 2, 2):
            Circle(mount_hole / 2)  # Mounting holes for NEMA motors

with BuildSketch() as motor_mount:
    Circle(shaft_size / 2)  # Shaft cutout for NEMA motor
    with GridLocations(mount_spacing, mount_spacing, 2, 2):
        Circle(mount_hole / 2)  # Mounting holes for NEMA motors
with BuildSketch() as sk4:
    RegularPolygon(100, 7, rotation=-360 / 28)
    with PolarLocations(55, 7):
        Add(motor_mount.sketch, mode=Mode.SUBTRACT)
gumyr commented 1 year ago

Unlimited levels of nesting is now supported. Grids within Polar will be appropriately rotated.

from build123d import *

with BuildSketch() as nested_grid:
    with Locations((-2, -2), (2, 2)):
        with GridLocations(1, 1, 2, 2) as grid:
            Circle(0.1)

with BuildSketch() as nested_locations:
    with Locations((-2, 2), (2, -2)):
        with Locations((-0.5, -0.5), (0.5, 0.5)):
            Circle(0.1)

with BuildSketch() as nested_hex:
    with Locations((-1, 0), (1, 0)):
        with HexLocations(0.4, 3, 3):
            Circle(0.1)

with BuildSketch() as nested_single_polar:
    with PolarLocations(6, 3) as p:
        with GridLocations(1, 1, 2, 2) as grid:
            Circle(0.1)

with BuildSketch() as nested_double_polar:
    with PolarLocations(10, 12):
        with GridLocations(2, 2, 2, 2):
            with HexLocations(0.4, 3, 3):
                Circle(0.1)

if "show_object" in locals():
    show_object(nested_grid.sketch, name="nested grid")
    show_object(nested_locations.sketch, name="nested locations")
    show_object(nested_hex.sketch, name="nested hex")
    show_object(nested_single_polar.sketch, name="nested_single_polar")
    show_object(nested_double_polar.sketch, name="nested_double_polar")

image