forked from applemuncy/boxmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_boxmaker.py
408 lines (319 loc) · 22.2 KB
/
my_boxmaker.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Generates Inkscape SVG file containing box components needed to
laser cut a tabbed construction box taking kerf and clearance into account
Copyright (C) 2016 Apple Muncy [email protected]
Copyright (C) 2011 elliot white [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
__version__ = "0.91" ### please report bugs, suggestions etc to [email protected] ###
from ink_helper import *
_ = gettext.gettext
import inkex
inkex.localize()
class TSlotBoxMaker(inkex.Effect):
"""Top level class to handle setup and parse options.
"""
def __init__(self):
# Call the base class constructor.
inkex.Effect.__init__(self)
panels = ['front_panel','back_panel','left_panel','right_panel','top_panel','bottom_panel',
'divider_panel']
edge = ['bottom_edge','right_edge','top_edge','left_edge']
ns_h = ['nutslot','screw_hole']
# Define options
for a in panels :
for b in edge :
for c in ns_h :
d = a + '_'+ b + '_'+ c
self.OptionParser.add_option(('--' + d) ,action='store', type="inkbool",
dest= d ,default=True,help='Draw nut slots / screw holes')
cutout_options =['center_X','center_Y','dim_X','dim_Y','corner_R']
for a in panels :
for b in cutout_options :
d = a + '_' + b
self.OptionParser.add_option(('--'+d) , action='store', type='float',
dest = d , default = 0.0 , help ='cutout options')
e = a + '_cutout'
self.OptionParser.add_option(('--' + e) ,action='store', type="inkbool",
dest= e ,default=True,help='Draw cutout, True/False')
self.OptionParser.add_option(('--has_divider' ) ,action='store', type="inkbool",
dest= 'has_divider' ,default=True,help='has_divider ? True/False')
self.OptionParser.add_option('--divider_distance_from_top',action='store',type='float',
dest='divider_distance_from_top',default=100,help='Divider distance from top panel')
self.OptionParser.add_option('--unit',action='store',type='string',
dest='unit',default='mm',help='Measure Units')
self.OptionParser.add_option('--inside',action='store',type='int',
dest='inside',default=0,help='Int/Ext Dimension')
self.OptionParser.add_option('--length',action='store',type='float',
dest='length' ,default=100,help='Length of Box')
self.OptionParser.add_option('--width',action='store',type='float',
dest='width',default=100,help='Width of Box')
self.OptionParser.add_option('--depth',action='store',type='float',
dest='depth',default=100,help='Depth of Box')
self.OptionParser.add_option('--length_tab_width',action='store',type='float',
dest='length_tab_width',default=25,help='Lengthwise Nominal Tab Width ')
self.OptionParser.add_option('--width_tab_width',action='store',type='float',
dest='width_tab_width',default=25,help='Widthwize Nominal Tab Width')
self.OptionParser.add_option('--depth_tab_width',action='store',type='float',
dest='depth_tab_width',default=25,help='Depthwize Nominal Tab Width')
self.OptionParser.add_option('--equal',action='store',type='string',
dest='equal',default='Fixed',help='Equal/Prop Tabs')
self.OptionParser.add_option('--thickness',action='store',type='float',
dest='thickness',default=10,help='Thickness of Material')
self.OptionParser.add_option('--kerf',action='store',type='float',
dest='kerf',default=0.23,help='Kerf (width) of cut')
self.OptionParser.add_option('--clearance',action='store',type='float',
dest='clearance',default=0.02,help='Clearance of joints')
self.OptionParser.add_option('--style',action='store',type='int',
dest='style',default=25,help='Layout/Style')
self.OptionParser.add_option('--spacing',action='store',type='float',
dest='spacing',default=25,help='Part Spacing')
self.OptionParser.add_option('--screw_length',action='store',type='float',
dest='screw_length',default=16,help='Screw Length')
self.OptionParser.add_option('--screw_diameter',action='store',type='float',
dest='screw_diameter',default=3,help='Screw Diameter')
self.OptionParser.add_option('--nut_height',action='store',type='float',
dest='nut_height',default=2.26,help='Nut Height')
self.OptionParser.add_option('--nut_diameter',action='store',type='float',
dest='nut_diameter',default=5.44,help='Nut Diameter')
self.OptionParser.add_option('--add_bearings',action='store',type='inkbool',
dest='add_bearings',default='False',help='Add bearing holes and stepper motor holes')
self.OptionParser.add_option('--bearing_diameter',action='store',type='float',
dest='bearing_diameter',default=22,help='Bearing Diameter')
self.OptionParser.add_option('--stepper_motor',action='store',type='string',
dest='stepper_motor',default='nema-17',help='Stepper Motor Type')
self.OptionParser.add_option('--drive_belt' , action='store', type= 'float',
dest='drive_belt', default = 200 , help='Length of the stepper motor drive belt')
self.OptionParser.add_option('--axis_offset' , action='store', type= 'float',
dest='axis_offset', default = 18 , help='Vertical offset of the bearing axis')
self.OptionParser.add_option('--debug',action='store',type='inkbool',
dest='debug',default=False,help='Debug mode On/Off')
# here so we can have tabs - but we do not use it directly - else error
self.OptionParser.add_option("", "--active-tab",
action="store", type="string",
dest="active_tab", default='title', # use a legitmate default
help="Active tab.")
self.OptionParser.add_option("", "--panel-options",
action="store", type="string",
dest="active_tab", default='title', # use a legitmate default
help="Active tab.")
self.OptionParser.add_option("", "--inactive-tab",
action="store", type="string",
dest="active_tab", default='title', # use a legitmate default
help="Active tab.")
def effect(self):
box_dict ={}
'''
stuff all options inside a dictionary to avoid using global variables
'''
box_dict['front_panel_bottom_edge_nutslot'] = self.options.front_panel_bottom_edge_nutslot
box_dict['front_panel_bottom_edge_screw_hole'] = self.options.front_panel_bottom_edge_screw_hole
box_dict['front_panel_right_edge_nutslot'] = self.options.front_panel_right_edge_nutslot
box_dict['front_panel_right_edge_screw_hole'] = self.options.front_panel_right_edge_screw_hole
box_dict['front_panel_top_edge_nutslot'] = self.options.front_panel_top_edge_nutslot
box_dict['front_panel_top_edge_screw_hole'] = self.options.front_panel_top_edge_screw_hole
box_dict['front_panel_left_edge_nutslot'] = self.options.front_panel_left_edge_nutslot
box_dict['front_panel_left_edge_screw_hole'] = self.options.front_panel_left_edge_screw_hole
box_dict['back_panel_bottom_edge_nutslot'] = self.options.back_panel_bottom_edge_nutslot
box_dict['back_panel_bottom_edge_screw_hole'] = self.options.back_panel_bottom_edge_screw_hole
box_dict['back_panel_right_edge_nutslot'] = self.options.back_panel_right_edge_nutslot
box_dict['back_panel_right_edge_screw_hole'] = self.options.back_panel_right_edge_screw_hole
box_dict['back_panel_top_edge_nutslot'] = self.options.back_panel_top_edge_nutslot
box_dict['back_panel_top_edge_screw_hole'] = self.options.back_panel_top_edge_screw_hole
box_dict['back_panel_left_edge_nutslot'] = self.options.back_panel_left_edge_nutslot
box_dict['back_panel_left_edge_screw_hole'] = self.options.back_panel_left_edge_screw_hole
box_dict['left_panel_bottom_edge_nutslot'] = self.options.left_panel_bottom_edge_nutslot
box_dict['left_panel_bottom_edge_screw_hole'] = self.options.left_panel_bottom_edge_screw_hole
box_dict['left_panel_right_edge_nutslot'] = self.options.left_panel_right_edge_nutslot
box_dict['left_panel_right_edge_screw_hole'] = self.options.left_panel_right_edge_screw_hole
box_dict['left_panel_top_edge_nutslot'] = self.options.left_panel_top_edge_nutslot
box_dict['left_panel_top_edge_screw_hole'] = self.options.left_panel_top_edge_screw_hole
box_dict['left_panel_left_edge_nutslot'] = self.options.left_panel_left_edge_nutslot
box_dict['left_panel_left_edge_screw_hole'] = self.options.left_panel_left_edge_screw_hole
box_dict['right_panel_bottom_edge_nutslot'] = self.options.right_panel_bottom_edge_nutslot
box_dict['right_panel_bottom_edge_screw_hole'] = self.options.right_panel_bottom_edge_screw_hole
box_dict['right_panel_right_edge_nutslot'] = self.options.right_panel_right_edge_nutslot
box_dict['right_panel_right_edge_screw_hole'] = self.options.right_panel_right_edge_screw_hole
box_dict['right_panel_top_edge_nutslot'] = self.options.right_panel_top_edge_nutslot
box_dict['right_panel_top_edge_screw_hole'] = self.options.right_panel_top_edge_screw_hole
box_dict['right_panel_left_edge_nutslot'] = self.options.right_panel_left_edge_nutslot
box_dict['right_panel_left_edge_screw_hole'] = self.options.right_panel_left_edge_screw_hole
box_dict['top_panel_bottom_edge_nutslot'] = self.options.top_panel_bottom_edge_nutslot
box_dict['top_panel_bottom_edge_screw_hole'] = self.options.top_panel_bottom_edge_screw_hole
box_dict['top_panel_right_edge_nutslot'] = self.options.top_panel_right_edge_nutslot
box_dict['top_panel_right_edge_screw_hole'] = self.options.top_panel_right_edge_screw_hole
box_dict['top_panel_top_edge_nutslot'] = self.options.top_panel_top_edge_nutslot
box_dict['top_panel_top_edge_screw_hole'] = self.options.top_panel_top_edge_screw_hole
box_dict['top_panel_left_edge_nutslot'] = self.options.top_panel_left_edge_nutslot
box_dict['top_panel_left_edge_screw_hole'] = self.options.top_panel_left_edge_screw_hole
#not a bit of indirection of the screw holes
box_dict['divider_panel_bottom_edge_nutslot'] = self.options.divider_panel_bottom_edge_nutslot
box_dict['front_panel_slot_row_screw_hole'] = self.options.divider_panel_bottom_edge_screw_hole
box_dict['divider_panel_right_edge_nutslot'] = self.options.divider_panel_right_edge_nutslot
box_dict['right_panel_slot_row_screw_hole'] = self.options.divider_panel_right_edge_screw_hole
box_dict['divider_panel_top_edge_nutslot'] = self.options.divider_panel_top_edge_nutslot
box_dict['back_panel_slot_row_screw_hole'] = self.options.divider_panel_top_edge_screw_hole
box_dict['divider_panel_left_edge_nutslot'] = self.options.divider_panel_left_edge_nutslot
box_dict['left_panel_slot_row_screw_hole'] = self.options.divider_panel_left_edge_screw_hole
box_dict['bottom_panel_bottom_edge_nutslot'] = self.options.bottom_panel_bottom_edge_nutslot
box_dict['bottom_panel_bottom_edge_screw_hole'] = self.options.bottom_panel_bottom_edge_screw_hole
box_dict['bottom_panel_right_edge_nutslot'] = self.options.bottom_panel_right_edge_nutslot
box_dict['bottom_panel_right_edge_screw_hole'] = self.options.bottom_panel_right_edge_screw_hole
box_dict['bottom_panel_top_edge_nutslot'] = self.options.bottom_panel_top_edge_nutslot
box_dict['bottom_panel_top_edge_screw_hole'] = self.options.bottom_panel_top_edge_screw_hole
box_dict['bottom_panel_left_edge_nutslot'] = self.options.bottom_panel_left_edge_nutslot
box_dict['bottom_panel_left_edge_screw_hole'] = self.options.bottom_panel_left_edge_screw_hole
box_dict['front_panel_cutout'] = self.options.front_panel_cutout
box_dict['left_panel_cutout'] = self.options.left_panel_cutout
box_dict['right_panel_cutout'] = self.options.right_panel_cutout
box_dict['back_panel_cutout'] = self.options.back_panel_cutout
box_dict['top_panel_cutout'] = self.options.top_panel_cutout
box_dict['has_divider'] = self.options.has_divider
box_dict['divider_panel_cutout'] = self.options.divider_panel_cutout
box_dict['bottom_panel_cutout'] = self.options.bottom_panel_cutout
box_dict['debug'] = self.options.debug
# Get access to main SVG document element and get its dimensions.
svg = self.document.getroot()
# Get the attibutes:
box_dict['widthDoc'] = self.unittouu(svg.get('width'))
box_dict['heightDoc'] = self.unittouu(svg.get('height'))
# Create a new layer.
layer = inkex.etree.SubElement(svg, 'g')
layer.set(inkex.addNS('label', 'inkscape'), 'newlayer')
layer.set(inkex.addNS('groupmode', 'inkscape'), 'layer')
box_dict['parent']=self.current_layer
#parent=self.current_layer
# Get script's option values.
unit=self.options.unit
box_dict['front_panel_center_X'] = self.unittouu( str(self.options.front_panel_center_X) + unit)
box_dict['front_panel_center_Y'] = self.unittouu( str(self.options.front_panel_center_Y) + unit )
box_dict['front_panel_dim_X'] = self.unittouu( str(self.options.front_panel_dim_X) + unit )
box_dict['front_panel_dim_Y'] = self.unittouu( str(self.options.front_panel_dim_Y) + unit )
box_dict['front_panel_corner_R'] = self.unittouu( str(self.options.front_panel_corner_R) + unit )
box_dict['right_panel_center_X'] = self.unittouu( str(self.options.right_panel_center_X) + unit )
box_dict['right_panel_center_Y'] = self.unittouu( str(self.options.right_panel_center_Y) + unit )
box_dict['right_panel_dim_X'] = self.unittouu( str(self.options.right_panel_dim_X) + unit )
box_dict['right_panel_dim_Y'] = self.unittouu( str(self.options.right_panel_dim_Y) + unit )
box_dict['right_panel_corner_R'] = self.unittouu( str(self.options.right_panel_corner_R) + unit )
box_dict['left_panel_center_X'] = self.unittouu( str(self.options.left_panel_center_X) + unit )
box_dict['left_panel_center_Y'] = self.unittouu( str(self.options.left_panel_center_Y) + unit )
box_dict['left_panel_dim_X'] = self.unittouu( str(self.options.left_panel_dim_X) + unit )
box_dict['left_panel_dim_Y'] = self.unittouu( str(self.options.left_panel_dim_Y) + unit )
box_dict['left_panel_corner_R'] = self.unittouu( str(self.options.left_panel_corner_R) + unit )
box_dict['back_panel_center_X'] = self.unittouu( str(self.options.back_panel_center_X) + unit )
box_dict['back_panel_center_Y'] = self.unittouu( str(self.options.back_panel_center_Y) + unit )
box_dict['back_panel_dim_X'] = self.unittouu( str(self.options.back_panel_dim_X) + unit )
box_dict['back_panel_dim_Y'] = self.unittouu( str(self.options.back_panel_dim_Y) + unit )
box_dict['back_panel_corner_R'] = self.unittouu( str(self.options.back_panel_corner_R) + unit )
box_dict['top_panel_center_X'] = self.unittouu( str(self.options.top_panel_center_X) + unit )
box_dict['top_panel_center_Y'] = self.unittouu( str(self.options.top_panel_center_Y) + unit )
box_dict['top_panel_dim_X'] = self.unittouu( str(self.options.top_panel_dim_X) + unit )
box_dict['top_panel_dim_Y'] = self.unittouu( str(self.options.top_panel_dim_Y) + unit )
box_dict['top_panel_corner_R'] = self.unittouu( str(self.options.top_panel_corner_R) + unit )
box_dict['divider_distance_from_top'] = self.unittouu(
str(self.options.divider_distance_from_top) + unit )
box_dict['divider_panel_center_X'] = self.unittouu( str(self.options.divider_panel_center_X) + unit )
box_dict['divider_panel_center_Y'] = self.unittouu( str(self.options.divider_panel_center_Y) + unit )
box_dict['divider_panel_dim_X'] = self.unittouu( str(self.options.divider_panel_dim_X) + unit )
box_dict['divider_panel_dim_Y'] = self.unittouu( str(self.options.divider_panel_dim_Y) + unit )
box_dict['divider_panel_corner_R'] = self.unittouu( str(self.options.divider_panel_corner_R) + unit )
box_dict['bottom_panel_center_X'] = self.unittouu( str(self.options.bottom_panel_center_X) + unit )
box_dict['bottom_panel_center_Y'] = self.unittouu( str(self.options.bottom_panel_center_Y) + unit )
box_dict['bottom_panel_dim_X'] = self.unittouu( str(self.options.bottom_panel_dim_X) + unit )
box_dict['bottom_panel_dim_Y'] = self.unittouu( str(self.options.bottom_panel_dim_Y) + unit )
box_dict['bottom_panel_corner_R'] = self.unittouu( str(self.options.bottom_panel_corner_R) + unit )
thickness = self.unittouu( str(self.options.thickness) + unit )
inside=self.options.inside
X = self.unittouu( str(self.options.length) + unit )
Y = self.unittouu( str(self.options.width) + unit )
Z = self.unittouu( str(self.options.depth) + unit )
box_dict['nom_length_tab_width']=self.unittouu( str(self.options.length_tab_width) + unit )
box_dict['nom_width_tab_width']=self.unittouu( str(self.options.width_tab_width) + unit )
box_dict['nom_depth_tab_width']=self.unittouu( str(self.options.depth_tab_width) + unit )
box_dict['add_bearings'] = self.options.add_bearings
box_dict['bearing_diameter'] = self.unittouu( str(self.options.bearing_diameter ) + unit )
box_dict['stepper_motor'] = self.unittouu( str(self.options.stepper_motor) + unit )
box_dict['drive_belt'] = self.unittouu( str(self.options.drive_belt) + unit )
box_dict['axis_offset'] = self.unittouu( str(self.options.axis_offset) + unit )
box_dict['slot_length'] = self.unittouu( str(2.00) + unit)
box_dict['bearing_inset'] = self.unittouu( str(30) + unit)
box_dict['bearing_drop'] = self.unittouu( str(20) + unit)
box_dict['screw_offset'] = self.unittouu( str(31.00/2.00) + unit)
box_dict['gear_factor'] = self.unittouu( str(36.00) + unit)
box_dict['equalTabs']=self.options.equal
kerf = self.unittouu( str(self.options.kerf) + unit )
clearance = self.unittouu( str(self.options.clearance) + unit )
layout=self.options.style
box_dict['spacing'] = self.unittouu( str(self.options.spacing) + unit )
box_dict['screw_length'] = self.unittouu( str(self.options.screw_length) + unit )
box_dict['screw_diameter'] = self.unittouu( str(self.options.screw_diameter) + unit )
box_dict['nut_height'] = self.unittouu( str(self.options.nut_height) + unit )
box_dict['nut_diameter'] = self.unittouu( str(self.options.nut_diameter) + unit )
if inside: # if inside dimension selected correct values to outside dimension
X+=thickness*2
Y+=thickness*2
Z+=thickness*2
box_dict['length']= X
box_dict['width']=Y
box_dict['depth']=Z
box_dict['thickness']= thickness
box_dict['correction']=kerf-clearance
'''
Be careful to add all dictionary enteries before instanciating the box
'''
# check input values mainly to avoid python errors
# TODO restrict values to *correct* solutions
error=0
if min(X,Y,Z)==0:
inkex.errormsg(_('Error: Dimensions must be non zero'))
error=1
if max(X,Y,Z)>max(box_dict['widthDoc'],box_dict['heightDoc'])*10: # crude test
inkex.errormsg(_('Error: Dimensions Too Large'))
error=1
if X < 3*box_dict['nom_length_tab_width']:
inkex.errormsg(_('Error: Length Tab size too large'))
error=1
if Y < 3*box_dict['nom_width_tab_width']:
inkex.errormsg(_('Error: Width Tab size too large'))
error=1
if Z < 3*box_dict['nom_depth_tab_width']:
inkex.errormsg(_('Error: Depth Tab size too large'))
error=1
if min(box_dict['nom_length_tab_width'],box_dict['nom_width_tab_width'],box_dict['nom_depth_tab_width']) <thickness:
inkex.errormsg(_('Error: Tab size too small'))
error=1
if thickness==0:
inkex.errormsg(_('Error: Thickness is zero'))
error=1
if thickness>min(X,Y,Z)/3: # crude test
inkex.errormsg(_('Error: Material too thick'))
error=1
if box_dict['correction']>min(X,Y,Z)/3: # crude test
inkex.errormsg(_('Error: Kerf/Clearence too large'))
error=1
if box_dict['spacing']>max(X,Y,Z)*10: # crude test
inkex.errormsg(_('Error: Spacing too large'))
error=1
if box_dict['spacing']<kerf:
inkex.errormsg(_('Error: Spacing too small'))
error=1
if error: exit()
if box_dict['debug'] :
inkex.errormsg('length = {0} width = {1} depth = {2} '.format( X,Y,Z))
from my_box import Box
box = Box(box_dict)
# Create effect instance and apply it.
e = TSlotBoxMaker()
e.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99