Skip to content

Commit b4bbaea

Browse files
committed
-- Added arc_inverted() and separated it from arc().
-- unit testing for same
1 parent ee2ea8a commit b4bbaea

2 files changed

Lines changed: 68 additions & 61 deletions

File tree

‎solid/test/test_utils.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
( right, [2], '\n\ntranslate(v = [2, 0, 0]);'),
1717
( forward, [2], '\n\ntranslate(v = [0, 2, 0]);'),
1818
( back, [2], '\n\ntranslate(v = [0, -2, 0]);'),
19-
( arc, [10, 0, 90, 24], '\n\ndifference() {\n\tcircle(r = 10, $fn = 24);\n\trotate(a = 0) {\n\t\ttranslate(v = [-90, -100]) {\n\t\t\tsquare(center = false, size = [100, 100]);\n\t\t}\n\t}\n\trotate(a = -90) {\n\t\ttranslate(v = [-90, -100]) {\n\t\t\tsquare(center = false, size = [100, 100]);\n\t\t}\n\t}\n}'),
20-
( 'arc_inverted', arc, [10, 0, 90, 24, True], '\n\ndifference() {\n\tintersection() {\n\t\trotate(a = 0) {\n\t\t\ttranslate(v = [-990, 0]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t\trotate(a = 90) {\n\t\t\ttranslate(v = [-990, -1000]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t}\n\tcircle(r = 10, $fn = 24);\n}'),
19+
( arc, [10, 0, 90, 24], '\n\ndifference() {\n\tcircle(r = 10, $fn = 24);\n\trotate(a = 0) {\n\t\ttranslate(v = [0, -10, 0]) {\n\t\t\tsquare(center = true, size = [30, 20]);\n\t\t}\n\t}\n\trotate(a = -90) {\n\t\ttranslate(v = [0, -10, 0]) {\n\t\t\tsquare(center = true, size = [30, 20]);\n\t\t}\n\t}\n}'),
20+
( arc_inverted, [10, 0, 90, 24], '\n\ndifference() {\n\tintersection() {\n\t\trotate(a = 0) {\n\t\t\ttranslate(v = [-990, 0]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t\trotate(a = 90) {\n\t\t\ttranslate(v = [-990, -1000]) {\n\t\t\t\tsquare(center = false, size = [1000, 1000]);\n\t\t\t}\n\t\t}\n\t}\n\tcircle(r = 10, $fn = 24);\n}'),
2121
( 'transform_to_point_scad', transform_to_point, [cube(2), [2,2,2], [3,3,1]], '\n\nmultmatrix(m = [[0.7071067812, -0.1622214211, -0.6882472016, 2], [-0.7071067812, -0.1622214211, -0.6882472016, 2], [0.0000000000, 0.9733285268, -0.2294157339, 2], [0, 0, 0, 1.0000000000]]) {\n\tcube(size = 2);\n}'),
2222
( 'offset_polygon_inside', offset_polygon, [tri, 2, True], '\n\npolygon(paths = [[0, 1, 2]], points = [[2.0000000000, 2.0000000000, 0.0000000000], [5.1715728753, 2.0000000000, 0.0000000000], [2.0000000000, 5.1715728753, 0.0000000000]]);'),
2323
( 'offset_polygon_outside', offset_polygon, [tri, 2, False], '\n\npolygon(paths = [[0, 1, 2]], points = [[-2.0000000000, -2.0000000000, 0.0000000000], [14.8284271247, -2.0000000000, 0.0000000000], [-2.0000000000, 14.8284271247, 0.0000000000]]);'),

‎solid/utils.py‎

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -202,78 +202,85 @@ def rot_x_to_neg_y( obj):
202202
# =======
203203
# = Arc =
204204
# =======
205-
def arc( rad, start_degrees, end_degrees, segments=None, invert=False):
205+
def arc( rad, start_degrees, end_degrees, segments=None):
206206
# Note: the circle that this arc is drawn from gets segments,
207207
# not the arc itself. That means a quarter-circle arc will
208208
# have segments/4 segments.
209209

210-
# invert=True will leave the portion of a circumscribed square of sides
211-
# 2*rad that is NOT in the arc behind. This is most useful for 90-degree
212-
# segments, since it's what you'll add to create fillets and take away
213-
# to create rounds.
214-
# Also: an inverted arc (bounded by an arc and the tangent lines at
215-
# both endpoints) is only valid for end_degrees - start_degrees <= 180.
216-
# This method raises ValueError for inappropriate inverted arcs.
217210
bottom_half_square = back( rad)(square( [3*rad, 2*rad], center=True))
218211
top_half_square = forward( rad)( square( [3*rad, 2*rad], center=True))
219212

220213
start_shape = circle( rad, segments=segments)
221-
222214

223-
wide = 100
224-
high = 100
225-
226-
top_half_square = translate( [-(wide-rad), 0])( square([wide, high], center=False))
227-
bottom_half_square = translate( [-(wide-rad), -high])( square([wide, high], center=False))
228-
229-
if invert:
230-
# We want the area bounded by:
231-
# -- the circle from start_degrees to end_degrees
232-
# -- line tangent to the circle at start_degrees
233-
# -- line tangent to the circle at end_degrees
234-
# Note that this shape is only valid if end_degrees - start_degrees < 180,
235-
# since if the two angles differ by more than 180 degrees,
236-
# the tangent lines don't converge
237-
if end_degrees < start_degrees:
238-
end_degrees += 360
239-
if end_degrees - start_degrees >= 180:
240-
raise ValueError( "Unable to draw inverted arc over 180 or more "
241-
"degrees. start_degrees: %s end_degrees: %s"
242-
%(start_degrees, end_degrees))
243-
244-
wide = 1000
245-
high = 1000
246-
247-
top_half_square = translate( [-(wide-rad), 0])( square([wide, high], center=False))
248-
bottom_half_square = translate( [-(wide-rad), -high])( square([wide, high], center=False))
249-
250-
a = rotate( start_degrees)( top_half_square)
251-
b = rotate( end_degrees)( bottom_half_square)
252-
if False:
253-
a.set_modifier( '#')
254-
b.set_modifier( '#')
255-
ret = (a*b) - start_shape
215+
if abs( (end_degrees - start_degrees)%360) <= 180:
216+
end_angle = end_degrees - 180
217+
ret = difference()(
218+
start_shape,
219+
rotate( a=start_degrees)( bottom_half_square.copy()),
220+
rotate( a= end_angle)( bottom_half_square.copy())
221+
)
256222
else:
257-
if abs( (end_degrees - start_degrees)%360) <= 180:
258-
end_angle = end_degrees - 180
259-
260-
ret = difference()(
261-
start_shape,
262-
rotate( a=start_degrees)( bottom_half_square),
263-
rotate( a= end_angle)( bottom_half_square)
264-
)
265-
else:
266-
ret = intersection( )(
267-
start_shape,
268-
union()(
269-
rotate( a=start_degrees)( top_half_square),
270-
rotate( a=end_degrees)( bottom_half_square)
271-
)
223+
ret = intersection( )(
224+
start_shape,
225+
union()(
226+
rotate( a=start_degrees)( top_half_square.copy()),
227+
rotate( a=end_degrees)( bottom_half_square.copy())
272228
)
229+
)
230+
273231
return ret
232+
233+
def arc_inverted( rad, start_degrees, end_degrees, segments=None):
234+
# Return the segment of an arc *outside* the circle of radius rad,
235+
# bounded by two tangents to the circle. This is the shape
236+
# needed for fillets.
237+
238+
# Note: the circle that this arc is drawn from gets segments,
239+
# not the arc itself. That means a quarter-circle arc will
240+
# have segments/4 segments.
241+
242+
# Leave the portion of a circumscribed square of sides
243+
# 2*rad that is NOT in the arc behind. This is most useful for 90-degree
244+
# segments, since it's what you'll add to create fillets and take away
245+
# to create rounds.
246+
247+
# NOTE: an inverted arc is only valid for end_degrees-start_degrees <= 180.
248+
# If this isn't true, end_degrees and start_degrees will be swapped so
249+
# that an acute angle can be found. end_degrees-start_degrees == 180
250+
# will yield a long rectangle of width 2*radius, since the tangent lines
251+
# will be parallel and never meet.
252+
253+
# Fix start/end degrees as needed; find a way to make an acute angle
254+
if end_degrees < start_degrees:
255+
end_degrees += 360
256+
257+
if end_degrees - start_degrees >= 180:
258+
start_degrees, end_degrees = end_degrees, start_degrees
259+
260+
# We want the area bounded by:
261+
# -- the circle from start_degrees to end_degrees
262+
# -- line tangent to the circle at start_degrees
263+
# -- line tangent to the circle at end_degrees
264+
# Note that this shape is only valid if end_degrees - start_degrees < 180,
265+
# since if the two angles differ by more than 180 degrees,
266+
# the tangent lines don't converge
267+
if end_degrees - start_degrees == 180:
268+
raise ValueError( "Unable to draw inverted arc over 180 or more "
269+
"degrees. start_degrees: %s end_degrees: %s"
270+
%(start_degrees, end_degrees))
271+
272+
wide = 1000
273+
high = 1000
274+
275+
top_half_square = translate( [-(wide-rad), 0])( square([wide, high], center=False))
276+
bottom_half_square = translate( [-(wide-rad), -high])( square([wide, high], center=False))
277+
278+
a = rotate( start_degrees)( top_half_square)
279+
b = rotate( end_degrees)( bottom_half_square)
280+
281+
ret = (a*b) - circle( rad, segments=segments)
274282

275-
276-
283+
return ret
277284

278285
def fillet_2d( a, b, c, rad):
279286
# a, b, and c are three points that form a corner at b.

0 commit comments

Comments
 (0)