-
Notifications
You must be signed in to change notification settings - Fork 1
/
MortiseAndTenonBoolean.py
208 lines (158 loc) · 6.52 KB
/
MortiseAndTenonBoolean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import rhinoscriptsyntax as rs
import math
def curveErrorCheck(curve):
if rs.IsCurveClosed(curve):
error = False
else:
print "Failed...Curve must be closed"
error = True
if rs.IsCurvePlanar(curve):
error = error
else:
print "Failed...Curve must be planar"
error = True
return error
def lineErrorCheck(curves):
count = 0
error = False
for curve in curves:
count = count + 1
if rs.IsLine(curve):
None
else:
error = True
if error == True:
print "Failed...Curve must be rectangular"
if count != 4:
print "Failed....To many line segments, redraw rectangle"
error = True
return error
def main():
rectangle = rs.GetObject("Select rectangle to create mortise and tenon from", rs.filter.curve, True, True)
errorCheck = curveErrorCheck(rectangle)
if errorCheck == True:
return
lines = rs.ExplodeCurves(rectangle)
errorCheck = lineErrorCheck(lines)
if errorCheck == True:
return
face = rs.GetObject("Select tenon surface", rs.filter.surface, False, True, None, True)
length = rs.GetReal("Enter tenon length", number=None)
if length and length != 0:
x = 0
else:
print "Failed....No length was entered"
return
depth = rs.GetReal("Enter mortise depth", number=length+0.05)
if depth and depth != 0:
x = 0
else:
print "Failed....No depth was entered"
return
fit = rs.GetReal("Enter mortise fit", number=0.01)
line1 = rs.AddLine(rs.CurveStartPoint(lines[0]),rs.CurveEndPoint(lines[0]))
line2 = rs.AddLine(rs.CurveStartPoint(lines[1]),rs.CurveEndPoint(lines[1]))
line3 = rs.AddLine(rs.CurveStartPoint(lines[2]),rs.CurveEndPoint(lines[2]))
line4 = rs.AddLine(rs.CurveStartPoint(lines[3]),rs.CurveEndPoint(lines[3]))
rs.DeleteObjects(lines)
lines = line1, line2, line3, line4
if rs.CurveLength(lines[0]) > rs.CurveLength(lines[1]):
smallside = rs.CurveLength(lines[1])
longside1 = lines[0]
longside2 = lines[2]
else:
smallside = rs.CurveLength(lines[0])
longside1 = lines[1]
longside2 = lines[3]
filletRadius = smallside/2
fillet1 = rs.CurveFilletPoints (lines[0], lines[1])
fillet2 = rs.CurveFilletPoints (lines[1], lines[2])
fillet3 = rs.CurveFilletPoints (lines[2], lines[3])
fillet4 = rs.CurveFilletPoints (lines[3], lines[0])
arc1 = rs.AddFilletCurve(lines[0],lines[1], radius = filletRadius)
arc2 = rs.AddFilletCurve(lines[1],lines[2], radius = filletRadius)
arc3 = rs.AddFilletCurve(lines[2],lines[3], radius = filletRadius)
arc4 = rs.AddFilletCurve(lines[3],lines[0], radius = filletRadius)
arcs = arc1, arc2, arc3, arc4
arcs = rs.JoinCurves(arcs)
arcEnd1 = rs.CurveEndPoint(arcs[0])
arcStart1 = rs.CurveStartPoint(arcs[0])
arcEnd2 = rs.CurveEndPoint(arcs[1])
arcStart2 = rs.CurveStartPoint(arcs[1])
if rs.Distance(arcEnd1, arcEnd2) > rs.Distance(arcEnd1,arcStart2):
temp = arcEnd2
arcEnd2 = arcStart2
arcStart2 = temp
line1 = rs.AddLine(arcEnd1, arcEnd2)
line2 = rs.AddLine(arcStart1, arcStart2)
curves = line1, arcs[0], arcs[1], line2
tenonOut = rs.JoinCurves(curves)
tenonSurf = rs.AddPlanarSrf(tenonOut)
point = rs.SurfacePoints(face)
param = rs.SurfaceClosestPoint(face, point[0])
normal = rs.SurfaceNormal(face, param)
normal = normal * length
vect = rs.AddLine( arcEnd1, arcEnd1 + normal )
tenon = rs.ExtrudeSurface(tenonSurf, vect, cap=True)
rs.DeleteObjects(curves)
arcs = arc1, arc2, arc3, arc4
rs.DeleteObjects(arcs)
rs.DeleteObject(rectangle)
rs.ExtendCurveLength(longside1, 0, 0, fit)
rs.ExtendCurveLength(longside1, 0, 1, fit)
rs.ExtendCurveLength(longside2, 0, 0, fit)
rs.ExtendCurveLength(longside2, 0, 1, fit)
if rs.Distance(rs.CurveEndPoint(longside1), rs.CurveEndPoint(longside2)) < rs.Distance(rs.CurveStartPoint(longside1), rs.CurveEndPoint(longside2)):
line1Start = rs.CurveEndPoint(longside1)
line1End = rs.CurveEndPoint(longside2)
line2Start = rs.CurveStartPoint(longside1)
line2End = rs.CurveStartPoint(longside2)
else:
line1Start = rs.CurveStartPoint(longside1)
line1End = rs.CurveEndPoint(longside2)
line2Start = rs.CurveEndPoint(longside1)
line2End = rs.CurveStartPoint(longside2)
shortside1 = rs.AddLine(line1Start, line1End)
shortside2 = rs.AddLine(line2Start, line2End)
arc1 = rs.AddFilletCurve(longside1, shortside1, radius = filletRadius)
arc2 = rs.AddFilletCurve(shortside1, longside2, radius = filletRadius)
arc3 = rs.AddFilletCurve(longside2, shortside2, radius = filletRadius)
arc4 = rs.AddFilletCurve(shortside2, longside1, radius = filletRadius)
arcs = arc1, arc2, arc3, arc4
arcs = rs.JoinCurves(arcs)
arcEnd1 = rs.CurveEndPoint(arcs[0])
arcStart1 = rs.CurveStartPoint(arcs[0])
arcEnd2 = rs.CurveEndPoint(arcs[1])
arcStart2 = rs.CurveStartPoint(arcs[1])
if rs.Distance(arcEnd1, arcEnd2) > rs.Distance(arcEnd1,arcStart2):
temp = arcEnd2
arcEnd2 = arcStart2
arcStart2 = temp
line1 = rs.AddLine(arcEnd1, arcEnd2)
line2 = rs.AddLine(arcStart1, arcStart2)
curves = line1, arcs[0], arcs[1], line2
mortiseOut = rs.JoinCurves(curves)
mortiseSurf = rs.AddPlanarSrf(mortiseOut)
param = rs.SurfaceClosestPoint(face, point[0])
normal = rs.SurfaceNormal(face, param)
normal = normal * depth
vect = rs.AddLine( arcEnd1, arcEnd1 + normal )
mortise = rs.ExtrudeSurface(mortiseSurf, vect, cap=True)
rs.DeleteObject(shortside1)
rs.DeleteObject(shortside2)
rs.DeleteObject(mortiseOut)
rs.DeleteObject(mortiseSurf)
rs.DeleteObjects(curves)
rs.DeleteObjects(lines)
arcs = arc1, arc2, arc3, arc4
rs.DeleteObjects(arcs)
rs.DeleteObject(rectangle)
rs.DeleteObject(tenonOut)
rs.DeleteObject(tenonSurf)
mortiseSide = rs.GetObject("Select part to mortise", rs.filter.polysurface, False, False)
tenonSide = rs.GetObject("Select part to tenon", rs.filter.polysurface, False, False)
tenonUnion = tenonSide, tenon
rs.BooleanDifference(mortiseSide, mortise, delete_input = True)
rs.BooleanUnion(tenonUnion, delete_input = True)
return
main()