build123d/docs/objects_2d.py
2023-07-30 11:25:09 -04:00

255 lines
8.1 KiB
Python

# [Setup]
from build123d import *
dot = Circle(0.05)
# [Setup]
svg_opts1 = {"pixel_scale": 100, "show_axes": False, "show_hidden": False}
svg_opts2 = {"pixel_scale": 300, "show_axes": True, "show_hidden": False}
svg_opts3 = {"pixel_scale": 2, "show_axes": False, "show_hidden": False}
svg_opts4 = {"pixel_scale": 5, "show_axes": False, "show_hidden": False}
# [Ex. 1]
with BuildSketch() as example_1:
Circle(1)
# [Ex. 1]
s = 100 / max(*example_1.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_1.sketch)
svg.write("assets/circle_example.svg")
# [Ex. 2]
with BuildSketch() as example_2:
Ellipse(1.5, 1)
# [Ex. 2]
s = 100 / max(*example_2.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_2.sketch)
svg.write("assets/ellipse_example.svg")
# [Ex. 3]
with BuildSketch() as example_3:
inner = PolarLocations(0.5, 5, 0).local_locations
outer = PolarLocations(1.5, 5, 36).local_locations
points = [p.position for pair in zip(inner, outer) for p in pair]
Polygon(*points)
# [Ex. 3]
s = 100 / max(*example_3.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_3.sketch)
svg.write("assets/polygon_example.svg")
# [Ex. 4]
with BuildSketch() as example_4:
Rectangle(2, 1)
# [Ex. 4]
s = 100 / max(*example_4.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_4.sketch)
svg.write("assets/rectangle_example.svg")
# [Ex. 5]
with BuildSketch() as example_5:
RectangleRounded(2, 1, 0.25)
# [Ex. 5]
s = 100 / max(*example_5.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_5.sketch)
svg.write("assets/rectangle_rounded_example.svg")
# [Ex. 6]
with BuildSketch() as example_6:
RegularPolygon(1, 6)
# [Ex. 6]
s = 100 / max(*example_6.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_6.sketch)
svg.write("assets/regular_polygon_example.svg")
# [Ex. 7]
with BuildSketch() as example_7:
arc = Edge.make_circle(1, start_angle=0, end_angle=45)
SlotArc(arc, 0.25)
# [Ex. 7]
s = 100 / max(*example_7.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_layer("dashed", line_type=LineType.DASHED)
svg.add_shape(example_7.sketch)
svg.add_shape(arc, "dashed")
svg.write("assets/slot_arc_example.svg")
# [Ex. 8]
with BuildSketch() as example_8:
c = (0, 0)
p = (0, 1)
SlotCenterPoint(c, p, 0.25)
# [Ex. 8]
s = 100 / max(*example_8.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_layer("dashed", line_type=LineType.DASHED)
svg.add_shape(example_8.sketch)
svg.add_shape(dot.moved(Location(c)), "dashed")
svg.add_shape(dot.moved(Location(p)), "dashed")
svg.write("assets/slot_center_point_example.svg")
# [Ex. 9]
with BuildSketch() as example_9:
SlotCenterToCenter(1, 0.25, rotation=90)
# [Ex. 9]
s = 100 / max(*example_9.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_9.sketch)
svg.write("assets/slot_center_to_center_example.svg")
# [Ex. 10]
with BuildSketch() as example_10:
SlotOverall(1, 0.25)
# [Ex. 10]
s = 100 / max(*example_10.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_10.sketch)
svg.write("assets/slot_overall_example.svg")
# [Ex. 11]
with BuildSketch() as example_11:
Text("text", 1)
# [Ex. 11]
s = 100 / max(*example_11.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(example_11.sketch)
svg.write("assets/text_example.svg")
# [Ex. 12]
with BuildSketch() as example_12:
t = Trapezoid(2, 1, 80)
with Locations((-0.6, -0.3)):
Text("80°", 0.3, mode=Mode.SUBTRACT)
# [Ex. 12]
s = 100 / max(*example_12.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_layer("dashed", line_type=LineType.DASHED)
svg.add_shape(
Edge.make_circle(
0.75,
Plane(t.vertices().group_by(Axis.Y)[0].sort_by(Axis.X)[0].to_tuple()),
start_angle=0,
end_angle=80,
),
"dashed",
)
svg.add_shape(example_12.sketch)
svg.write("assets/trapezoid_example.svg")
# [Ex. 13]
length, radius = 40.0, 60.0
with BuildSketch() as circle_with_hole:
Circle(radius=radius)
Rectangle(width=length, height=length, mode=Mode.SUBTRACT)
# [Ex. 13]
s = 100 / max(*circle_with_hole.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(circle_with_hole.sketch)
svg.write("assets/circle_with_hole.svg")
# [Ex. 14]
with BuildPart() as controller:
# Create the side view of the controller
with BuildSketch(Plane.YZ) as profile:
with BuildLine():
Polyline((0, 0), (0, 40), (20, 80), (40, 80), (40, 0), (0, 0))
# Create a filled face from the perimeter drawing
make_face()
# Extrude to create the basis controller shape
extrude(amount=30, both=True)
# Round off all the edges
fillet(controller.edges(), radius=3)
# Hollow out the controller
offset(amount=-1, mode=Mode.SUBTRACT)
# Extract the face that will house the display
display_face = (
controller.faces()
.filter_by(GeomType.PLANE)
.filter_by_position(Axis.Z, 50, 70)[0]
)
# Create a workplane from the face
display_workplane = Plane(
origin=display_face.center(), x_dir=(1, 0, 0), z_dir=display_face.normal_at()
)
# Place the sketch directly on the controller
with BuildSketch(display_workplane) as display:
RectangleRounded(40, 30, 2)
with GridLocations(45, 35, 2, 2):
Circle(1)
# Cut the display sketch through the controller
extrude(amount=-1, mode=Mode.SUBTRACT)
# [Ex. 14]
visible, hidden = controller.part.project_to_viewport((70, -50, 120))
max_dimension = max(*Compound(children=visible + hidden).bounding_box().size)
exporter = ExportSVG(scale=100 / max_dimension)
exporter.add_layer("Visible")
exporter.add_layer("Hidden", line_color=(99, 99, 99), line_type=LineType.ISO_DOT)
exporter.add_shape(visible, layer="Visible")
exporter.add_shape(hidden, layer="Hidden")
exporter.write(f"assets/controller.svg")
# [Align]
with BuildSketch() as align:
with GridLocations(1, 1, 2, 2):
Circle(0.5)
Circle(0.49, mode=Mode.SUBTRACT)
with GridLocations(1, 1, 1, 2):
Circle(0.5)
Circle(0.49, mode=Mode.SUBTRACT)
with GridLocations(1, 1, 2, 1):
Circle(0.5)
Circle(0.49, mode=Mode.SUBTRACT)
with Locations((0, 0)):
Circle(0.5)
Circle(0.49, mode=Mode.SUBTRACT)
# Top Right: (MIN, MIN)
with Locations((0.75, 0.75)):
Text("MIN\nMIN", font="FreeSerif", font_size=0.07)
# Top Center: (CENTER, MIN)
with Locations((0.0, 0.75 + 0.07 / 2)):
Text("CENTER", font="FreeSerif", font_size=0.07)
with Locations((0.0, 0.75 - 0.07 / 2)):
Text("MIN", font="FreeSerif", font_size=0.07)
# Top Left: (MAX, MIN)
with Locations((-0.75, 0.75 + 0.07 / 2)):
Text("MAX", font="FreeSerif", font_size=0.07)
with Locations((-0.75, 0.75 - 0.07 / 2)):
Text("MIN", font="FreeSerif", font_size=0.07)
# Center Right: (MIN, CENTER)
with Locations((0.75, 0.07 / 2)):
Text("MIN", font="FreeSerif", font_size=0.07)
with Locations((0.75, -0.07 / 2)):
Text("CENTER", font="FreeSerif", font_size=0.07)
# Center: (CENTER, CENTER)
with Locations((0, 0)):
Text("CENTER\nCENTER", font="FreeSerif", font_size=0.07)
# Center Left: (MAX, CENTER)
with Locations((-0.75, 0.07 / 2)):
Text("MAX", font="FreeSerif", font_size=0.07)
with Locations((-0.75, -0.07 / 2)):
Text("CENTER", font="FreeSerif", font_size=0.07)
# Bottom Right: (MIN, MAX)
with Locations((0.75, -0.75 + 0.07 / 2)):
Text("MIN", font="FreeSerif", font_size=0.07)
with Locations((0.75, -0.75 - 0.07 / 2)):
Text("MAX", font="FreeSerif", font_size=0.07)
# Bottom Center: (CENTER, MAX)
with Locations((0.0, -0.75 + 0.07 / 2)):
Text("MAX", font="FreeSerif", font_size=0.07)
with Locations((0.0, -0.75 - 0.07 / 2)):
Text("CENTER", font="FreeSerif", font_size=0.07)
# Bottom Left: (MAx, MAX)
with Locations((-0.75, -0.75)):
Text("MAX\nMAX", font="FreeSerif", font_size=0.07)
s = 100 / max(*align.sketch.bounding_box().size)
svg = ExportSVG(scale=s)
svg.add_shape(align.sketch)
svg.write("assets/align.svg")