Updated custom_sketch_objects.py example, cleaned up unused files

This commit is contained in:
Roger Maitland 2023-01-21 14:44:34 -05:00
parent 79c27d7b7d
commit 8fe58fab1a
4 changed files with 62 additions and 68 deletions

View file

@ -1,35 +0,0 @@
import build123d as b
class MyRectangle(b.BaseSketchOperation):
def __init__(
self,
width: float,
height: float,
rotation: float = 0,
centered: tuple[bool, bool] = (True, True),
mode: b.Mode = b.Mode.ADD,
):
face = b.Face.make_rect(height, width)
super().__init__(face, rotation, centered, mode)
class MyHole(b.BaseSketchOperation):
def __init__(
self,
radius: float,
rotation: float = 0,
centered: tuple[bool, bool] = (True, True),
mode: b.Mode = b.Mode.SUBTRACT,
):
face = b.Face.make_from_wires(b.Wire.make_circle(radius))
super().__init__(face, rotation, centered, mode)
with b.BuildSketch() as builder:
MyRectangle(width=20, height=15, centered=(True, True), rotation=45)
MyHole(radius=3)
if "show_object" in locals():
show_object(builder.sketch.wrapped)

View file

@ -0,0 +1,61 @@
"""
name: custom_sketch_objects.py
by: Gumyr
date: Jan 21st 2023
desc:
This example demonstrates user generated custom BuildSketch objects.
license:
Copyright 2023 Gumyr
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from build123d import *
class MyRectangle(BaseSketchObject):
def __init__(
self,
width: float,
height: float,
rotation: float = 0,
centered: tuple[bool, bool] = (True, True),
mode: Mode = Mode.ADD,
):
face = Face.make_rect(height, width)
super().__init__(face, rotation, centered, mode)
class MyHole(BaseSketchObject):
def __init__(
self,
radius: float,
rotation: float = 0,
centered: tuple[bool, bool] = (True, True),
mode: Mode = Mode.SUBTRACT,
):
face = Face.make_from_wires(Wire.make_circle(radius))
super().__init__(face, rotation, centered, mode)
with BuildSketch() as builder:
MyRectangle(width=20, height=15, centered=(True, True), rotation=45)
MyHole(radius=3)
if "show_object" in locals():
show_object(builder.sketch.wrapped)

View file

@ -83,6 +83,7 @@ __all__ = [
"MakeFace",
"MakeHull",
"Offset",
"BaseSketchObject",
"Circle",
"Ellipse",
"Polygon",

View file

@ -1,33 +0,0 @@
from build123d import Face, Mode, BuildSketch, Location, LocationList, Compound, Vector
class BaseSketchOperation(Compound):
_applies_to = [BuildSketch._tag()]
def __init__(
self,
face: Face,
rotation: float = 0,
centered: tuple[bool, bool] = (True, True),
mode: Mode = Mode.ADD,
):
context: BuildSketch = BuildSketch._get_context(self)
bounding_box = face.bounding_box()
center_offset = Vector(
0 if centered[0] else bounding_box.xlen / 2,
0 if centered[1] else bounding_box.ylen / 2,
)
face = face.locate(
Location((0, 0, 0), (0, 0, 1), rotation) * Location(center_offset)
)
new_faces = [
face.moved(location)
for location in LocationList._get_context().local_locations
]
for face in new_faces:
context._add_to_context(face, mode=mode)
Compound.__init__(self, Compound.make_compound(new_faces).wrapped)