-
Notifications
You must be signed in to change notification settings - Fork 12
/
quickjoint.py
284 lines (225 loc) · 11.6 KB
/
quickjoint.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
'''
Copyright (C) 2017 Jarrett Rainier jrainier@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import inkex, cmath
from inkex.paths import Path, ZoneClose, Move, Line, line, Curve
from lxml import etree
debugEn = False
def debugMsg(input):
if debugEn:
inkex.utils.debug(input)
def linesNumber(path):
retval = -1
for elem in path:
debugMsg('linesNumber')
debugMsg(elem)
retval = retval + 1
debugMsg('Number of lines : ' + str(retval))
return retval
class QuickJointPath (Path):
def Move(self, point):
'''Append an absolute move instruction to the path, to the specified complex point'''
debugMsg("- move: " + str(point))
self.append(Move(point.real, point.imag))
def Line(self, point):
'''Add an absolute line instruction to the path, to the specified complex point'''
debugMsg("- line: " + str(point))
self.append(Line(point.real, point.imag))
def close(self):
'''Add a Close Path instriction to the path'''
self.append(ZoneClose())
def line(self, vector):
'''Append a relative line command to the path, using the specified vector'''
self.append(line(vector.real, vector.imag))
def get_line(self, n):
'''Return the end points of the nth line in the path as complex numbers, as well as whether that line closes the path.'''
if isinstance(self[n], (Move, Line, ZoneClose)):
start = complex(self[n].x, self[n].y)
elif isinstance(self[n], Curve):
start = complex(self[n].x4, self[n].y4)
# If the next point in the path closes the path, go back to the start.
end = None
closePath = False
if isinstance(self[n+1], ZoneClose):
end = complex(self[0].x, self[0].y)
closePath = True
else:
if isinstance(self[n+1], (Move, Line, ZoneClose)):
end = complex(self[n+1].x, self[n+1].y)
elif isinstance(self[n+1], Curve):
end = complex(self[n+1].x4, self[n+1].y4)
return (start, end, closePath)
class QuickJoint(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument('-s', '--side', type=int, default=0, help='Object face to tabify')
pars.add_argument('-n', '--numtabs', type=int, default=1, help='Number of tabs to add')
pars.add_argument('-l', '--numslots', type=int, default=1, help='Number of slots to add')
pars.add_argument('-t', '--thickness', type=float, default=3.0, help='Material thickness')
pars.add_argument('-k', '--kerf', type=float, default=0.14, help='Measured kerf of cutter')
pars.add_argument('-u', '--units', default='mm', help='Measurement units')
pars.add_argument('-f', '--flipside', type=inkex.Boolean, default=False, help='Flip side of lines that tabs are drawn onto')
pars.add_argument('-a', '--activetab', default='', help='Tab or slot menus')
pars.add_argument('-S', '--featureStart', type=inkex.Boolean, default=False, help='Tab/slot instead of space on the start edge')
pars.add_argument('-E', '--featureEnd', type=inkex.Boolean, default=False, help='Tab/slot instead of space on the end edge')
def draw_parallel(self, start, guideLine, stepDistance):
polR, polPhi = cmath.polar(guideLine)
polR = stepDistance
return (cmath.rect(polR, polPhi) + start)
def draw_perpendicular(self, start, guideLine, stepDistance, invert = False):
polR, polPhi = cmath.polar(guideLine)
polR = stepDistance
debugMsg(polPhi)
if invert:
polPhi += (cmath.pi / 2)
else:
polPhi -= (cmath.pi / 2)
debugMsg(polPhi)
debugMsg(cmath.rect(polR, polPhi))
return (cmath.rect(polR, polPhi) + start)
def draw_box(self, start, lengthVector, height, kerf):
# Kerf is a provided as a positive kerf width. Although tabs
# need to be made larger by the width of the kerf, slots need
# to be made narrower instead, since the cut widens them.
# Calculate kerfed height and length vectors
heightEdge = self.draw_perpendicular(0, lengthVector, height - kerf, self.flipside)
lengthEdge = self.draw_parallel(lengthVector, lengthVector, -kerf)
debugMsg("draw_box; lengthEdge: " + str(lengthEdge) + ", heightEdge: " + str(heightEdge))
cursor = self.draw_parallel(start, lengthEdge, kerf/2)
cursor = self.draw_parallel(cursor, heightEdge, kerf/2)
path = QuickJointPath()
path.Move(cursor)
cursor += lengthEdge
path.Line(cursor)
cursor += heightEdge
path.Line(cursor)
cursor -= lengthEdge
path.Line(cursor)
cursor -= heightEdge
path.Line(cursor)
path.close()
return path
def draw_tabs(self, path, line):
cursor, segCount, segment, closePath = self.get_segments(path, line, self.numtabs)
# Calculate kerf-compensated vectors for the parallel portion of tab and space
tabLine = self.draw_parallel(segment, segment, self.kerf)
spaceLine = self.draw_parallel(segment, segment, -self.kerf)
endspaceLine = segment
# Calculate vectors for tabOut and tabIn: perpendicular away and towards baseline
tabOut = self.draw_perpendicular(0, segment, self.thickness, not self.flipside)
tabIn = self.draw_perpendicular(0, segment, self.thickness, self.flipside)
debugMsg("draw_tabs; tabLine=" + str(tabLine) + " spaceLine=" + str(spaceLine) + " segment=" + str(segment))
drawTab = self.featureStart
newLines = QuickJointPath()
# First line is a move or line to our start point
if isinstance(path[line], Move):
newLines.Move(cursor)
else:
newLines.Line(cursor)
for i in range(segCount):
debugMsg("i = " + str(i))
if drawTab == True:
debugMsg("- tab")
newLines.line(tabOut)
newLines.line(tabLine)
newLines.line(tabIn)
else:
if i == 0 or i == segCount - 1:
debugMsg("- endspace")
newLines.line(endspaceLine)
else:
debugMsg("- space")
newLines.line(spaceLine)
drawTab = not drawTab
if closePath:
newLines.close
return newLines
def add_new_path_from_lines(self, lines, line_style):
slot_id = self.svg.get_unique_id('slot')
g = etree.SubElement(self.svg.get_current_layer(), 'g', {'id':slot_id})
line_atts = { 'style':line_style, 'id':slot_id+'-inner-close-tab', 'd':str(Path(lines)) }
etree.SubElement(g, inkex.addNS('path','svg'), line_atts )
def get_segments(self, path, line, num):
# Calculate number of segments, including all features and spaces
segCount = num * 2 - 1
if not self.featureStart: segCount = segCount + 1
if not self.featureEnd: segCount = segCount + 1
start, end, closePath = QuickJointPath(path).get_line(line)
# Calculate the length of each feature prior to kerf compensation.
# Here we divide the specified edge into equal portions, one for each feature or space.
# Because the specified edge has no kerf compensation, the
# actual length we end up with will be smaller by a kerf. We
# need to use that distance to calculate our segment vector.
edge = end - start
edge = self.draw_parallel(edge, edge, -self.kerf)
segVector = edge / segCount
debugMsg("get_segments; start=" + str(start) + " end=" + str(end) + " edge=" + str(edge) + " segCount=" + str(segCount) + " segVector=" + str(segVector))
return (start, segCount, segVector, closePath)
def draw_slots(self, path):
# Female slot creation
cursor, segCount, segVector, closePath = self.get_segments(path, 0, self.numslots)
# I'm having a really hard time wording why this is necessary, but it is.
# get_segments returns a vector based on a narrower edge; adjust that edge to fit within the edge we were given.
cursor = self.draw_parallel(cursor, segVector, self.kerf/2)
newLines = []
line_style = str(inkex.Style({ 'stroke': '#000000', 'fill': 'none', 'stroke-width': str(self.svg.unittouu('0.1mm')) }))
drawSlot = self.featureStart
for i in range(segCount):
if drawSlot:
self.add_new_path_from_lines(self.draw_box(cursor, segVector, self.thickness, self.kerf), line_style)
cursor = cursor + segVector
drawSlot = not drawSlot
debugMsg("i: " + str(i) + ", cursor: " + str(cursor))
# (We don't modify the path so we don't need to close it)
def effect(self):
self.side = self.options.side
self.numtabs = self.options.numtabs
self.numslots = self.options.numslots
self.thickness = self.svg.unittouu(str(self.options.thickness) + self.options.units)
self.kerf = self.svg.unittouu(str(self.options.kerf) + self.options.units)
self.units = self.options.units
self.featureStart = self.options.featureStart
self.featureEnd = self.options.featureEnd
self.flipside = self.options.flipside
self.activetab = self.options.activetab
for id, node in self.svg.selected.items():
debugMsg(node)
debugMsg('1')
if node.tag == inkex.addNS('path','svg'):
p = list(node.path.to_superpath().to_segments())
debugMsg('2')
debugMsg(p)
lines = linesNumber(p)
lineNum = self.side % lines
debugMsg(lineNum)
newPath = []
if self.activetab == 'tabpage':
newPath = self.draw_tabs(p, lineNum)
debugMsg('2')
debugMsg(p[:lineNum])
debugMsg('3')
debugMsg(newPath)
debugMsg('4')
debugMsg( p[lineNum + 1:])
finalPath = p[:lineNum + 1] + newPath + p[lineNum + 2:]
debugMsg(finalPath)
node.set('d',str(Path(finalPath)))
elif self.activetab == 'slotpage':
newPath = self.draw_slots(p)
if __name__ == '__main__':
QuickJoint().run()