Added clock & loft examples, refactored sketch polar

This commit is contained in:
Roger Maitland 2022-07-15 15:05:32 -04:00
parent e891fd2f72
commit 863c00f994
4 changed files with 114 additions and 20 deletions

59
examples/clock.py Normal file
View file

@ -0,0 +1,59 @@
"""
name: clock.py
by: Gumyr
date: July 15th 2022
desc:
This example demonstrates using polar coordinates in a sketch.
license:
Copyright 2022 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 cadquery import Vector
from build123d.build123d_common import *
from build123d.build_line import *
from build123d.build_sketch import *
clock_radius = 10
with BuildSketch() as minute_indicator:
with BuildLine() as outline:
l1 = CenterArc((0, 0), clock_radius * 0.975, 0.75, 4.5)
l2 = CenterArc((0, 0), clock_radius * 0.925, 0.75, 4.5)
Line(l1 @ 0, l2 @ 0)
Line(l1 @ 1, l2 @ 1)
BuildFace()
FilletSketch(*minute_indicator.vertices(), radius=clock_radius * 0.01)
with BuildSketch() as clock_face:
Circle(clock_radius)
PolarArrayToSketch(0, 0, 360, 60)
AddToSketch(minute_indicator.sketch, mode=Mode.SUBTRACT)
PolarArrayToSketch(clock_radius * 0.875, 0, 360, 12)
SlotOverall(clock_radius * 0.05, clock_radius * 0.025, mode=Mode.SUBTRACT)
for hour in range(1, 13):
PolarArrayToSketch(clock_radius * 0.75, -hour * 30 + 90, 360, 1, rotate=False)
Text(
str(hour),
fontsize=clock_radius * 0.175,
font_style=FontStyle.BOLD,
halign=Halign.CENTER,
mode=Mode.SUBTRACT,
)
if "show_object" in locals():
show_object(clock_face.sketch, name="clock_face")

43
examples/loft.py Normal file
View file

@ -0,0 +1,43 @@
"""
name: loft.py
by: Gumyr
date: July 15th 2022
desc:
This example demonstrates lofting a set of sketches, selecting
the top and bottom by type, and shelling.
license:
Copyright 2022 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.build123d_common import *
from build123d.build_sketch import *
from build123d.build_part import *
with BuildPart() as art:
slice_count = 10
for i in range(slice_count + 1):
Workplanes(Plane(origin=(0, 0, i * 3), normal=(0, 0, 1)))
with BuildSketch() as slice:
Circle(10 * sin(i * pi / slice_count) + 5)
Loft()
top_bottom = art.faces().filter_by_type(Type.PLANE)
Shell(*top_bottom, thickness=0.5)
if "show_object" in locals():
show_object(art.part, name="art")

View file

@ -93,7 +93,7 @@ class Transition(Enum):
TRANSFORMED = auto()
class Font_Style(Enum):
class FontStyle(Enum):
"""Text Font Styles"""
REGULAR = auto()

View file

@ -499,25 +499,17 @@ class PolarArrayToSketch:
if count < 1:
raise ValueError(f"At least 1 elements required, requested {count}")
x = radius * sin(radians(start_angle))
y = radius * cos(radians(start_angle))
angle_step = (stop_angle - start_angle) / count
if rotate:
loc = Location(Vector(x, y), Vector(0, 0, 1), -start_angle)
else:
loc = Location(Vector(x, y))
new_locations = [loc]
angle = (stop_angle - start_angle) / (count - 1)
for i in range(1, count):
phi = start_angle + (angle * i)
x = radius * sin(radians(phi))
y = radius * cos(radians(phi))
if rotate:
loc = Location(Vector(x, y), Vector(0, 0, 1), -phi)
else:
loc = Location(Vector(x, y))
new_locations.append(loc)
# Note: rotate==False==0 so the location orientation doesn't change
new_locations = [
Location(
Vector(radius, 0).rotateZ(start_angle + angle_step * i),
Vector(0, 0, 1),
rotate * angle_step * i,
)
for i in range(count)
]
BuildSketch._get_context().locations = new_locations
@ -982,7 +974,7 @@ class Text(Compound):
fontsize: float,
font: str = "Arial",
font_path: str = None,
font_style: Font_Style = Font_Style.REGULAR,
font_style: FontStyle = FontStyle.REGULAR,
halign: Halign = Halign.LEFT,
valign: Valign = Valign.CENTER,
path: Union[Edge, Wire] = None,