-
Notifications
You must be signed in to change notification settings - Fork 1
/
lens-gear.jscad
372 lines (360 loc) · 11.8 KB
/
lens-gear.jscad
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
//
// Lens adapter
//
//include ('jscad-utils.jscad')
//include ('jscad-utils-color.jscad')
function getParameterDefinitions() {
return [{
name: 'resolution',
type: 'choice',
values: [0, 1, 2, 3, 4, 5],
captions: ['very low (6,16)', 'low (8,24)', 'normal (12,32)', 'high (24,64)', 'very high (48,128)',
'ultra high (96,256)'],
initial: 4,
caption: 'Resolution:'
}, {
name: 'length',
type: 'float',
initial: 45.0,
caption: 'Total adapter Length'
}, {
name: 'teeth',
type: 'float',
initial: 86,
caption: 'Number of teeth'
}, {
name: 'teeth_high',
type: 'float',
initial: 5.84,
caption: 'height of teeth edge relative to inner wide radius'
}, {
name: 'teeth_height',
type: 'float',
initial: 1.73,
caption: 'height of teeth'
}, {
name: 'teeth_low_w',
type: 'float',
initial: 1,
caption: 'width of space between teeths'
}, {
name: 'teeth_high_w',
type: 'float',
initial: 1,
caption: 'width of top of teeths'
}, {
name: 'gear_length',
type: 'float',
initial: 4.0,
caption: 'length of gear section'
}, {
name: 'inner_w_r',
type: 'float',
initial: 76.12/2,
caption: 'Inner wide radius, covering lens switches'
}, {
name: 'inner_length',
type: 'float',
initial: 10,
caption: 'length of section with inner wide radius'
}, {
name: 'cone_slope',
type: 'float',
initial: 1,
caption: 'slope of inner cone from inner wide radius to inner narrow radius'
}, {
name: 'inner_n_r',
type: 'float',
initial: 70/2,
caption: 'Inner narrow radius, covering the lens parrel itself'
}, {
name: 'outer_r',
type: 'float',
initial: 81.1/2,
caption: 'Outer radius'
}, {
name: 'slot_width',
type: 'float',
initial: 1,
caption: 'Width of opening in the ring'
}, {
name: 'handle',
type: 'checkbox',
checked: false,
caption: 'Add handles to ease opening/closing the slot'
}, {
name: 'handle_distance',
type: 'float',
initial: 2,
caption: 'distance of handle from outer ring'
}, {
name: 'handle_radius',
type: 'float',
initial: 3,
caption: 'thickness of handle (outer radius)'
}, {
name: 'handle1_start',
type: 'float',
initial: 6,
caption: 'start position of handle1'
}, {
name: 'handle1_end',
type: 'float',
initial: 14,
caption: 'end position of handle1'
}, {
name: 'handle2_start',
type: 'float',
initial: 15,
caption: 'start position of handle2'
}, {
name: 'handle2_end',
type: 'float',
initial: 23,
caption: 'end position of handle2'
}, {
name: 'handle_width_factor',
type: 'float',
initial: 1.5,
caption: 'Handle width factor relative to slot width'
}, {
name: 'invert',
type: 'checkbox',
checked: false,
caption: 'draw part upside-down (for STL import)'
}, {
name: 'base',
type: 'checkbox',
checked: true,
caption: 'draw flat base'
}, {
name: 'base_thickness',
type: 'float',
initial: 0.2,
caption: 'Thickness of base (if enabled)'
}, {
name: 'part',
type: 'choice',
values: ['piece', 'piece_with_slot'],
captions: ['piece', 'piece with middle slot'],
initial: 'piece',
caption: 'Part:'
}];
}
function main(params) {
var resolutions = [
[6, 16],
[8, 24],
[12, 32],
[24, 64],
[48, 128],
[96, 256]
];
CSG.defaultResolution3D = resolutions[params.resolution][0];
CSG.defaultResolution2D = resolutions[params.resolution][1];
//util.init(CSG);
//
// Input parameters
//
var pi = 3.14159;
// The narrow radius , sa as the lens radius
var inner_n_r = params.inner_n_r;
// A bit wider, giving space to the lens buttons
var inner_w_r = params.inner_w_r;
// Outer diameter of the adapter
var outer_r = params.outer_r;
// Adapter length
var length = params.length;
// Length of the inner space for buttons
var inner_length = params.inner_length;
// Length of the inner cone where we change from inner wide to inner narrow width
var cone_slope = params.cone_slope;
// Length of the gear surrounding the adapter
var gear_length = params.gear_length;
// number of teeth
var teeth = params.teeth;
// distance from inner_w_r to top of nearest teeth
var teeth_high = params.teeth_high;
// distance from inner_w_r to the valley between teeths
var teeth_height = params.teeth_height;
// width of valley between teeths
var teeth_low_w = params.teeth_low_w;
// width of teeth peak
var teeth_high_w = params.teeth_high_w;
// Width of cut slot
var slot_width = params.slot_width;
// Distance of handle from gear ring
var handle_distance = params.handle_distance;
var handle_radius = params.handle_radius;
var handle1_start = params.handle1_start;
var handle1_end = params.handle1_end;
var handle2_start = params.handle2_start;
var handle2_end = params.handle2_end;
var handle_width_factor = params.handle_width_factor;
//
// Calculated:
//
// factor needed to enlarge radius so that the perimeter is enlarged by the slot width
var slot_factor = 2*pi*inner_w_r/(2*pi*inner_w_r+slot_width);
// radius of cylinder on which the teeth starts (a bit larger than outer_r)
var gear_r = inner_w_r + (teeth_high - teeth_height);
// width of teeth base
var teeth_base_d = (2*pi*gear_r)/teeth;
var teeth_base_w = (2*pi*gear_r)/teeth-teeth_low_w;
// Height of teeth triangle (will be trimmed later)
var teeth_triangle_height = teeth_base_w/(teeth_base_w-teeth_high_w)*teeth_height;
// radius of teeth peak
var gear_bounding_r = inner_w_r + teeth_high;
//var teeth_angle = 2*pi*(2*pi*gear_r-slot_width)/(2*pi*gear_r)/teeth;
var teeth_angle = 2*pi/teeth*teeth_base_w/teeth_base_d*slot_factor;
// Main cylinder
var ext_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, length],
radius: outer_r/slot_factor
});
// the small cylinder on which we put the teeth
var gear_cone_length = gear_length+cone_slope*(gear_bounding_r-outer_r)/slot_factor;
var gear_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, gear_cone_length],
radius: gear_r/slot_factor
});
// This cylinder+cone clips the gear teeths
var gear_bounding_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, gear_length],
radius: gear_bounding_r/slot_factor
}).union(CSG.cylinder({
start: [0, 0, gear_length],
end: [0, 0, gear_cone_length],
radiusStart: gear_bounding_r/slot_factor,
radiusEnd: outer_r/slot_factor
}));
//
var in1_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, length],
radius: inner_n_r/slot_factor
});
var in2_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, inner_length],
radius: inner_w_r/slot_factor
}).union(CSG.cylinder({
start: [0, 0, inner_length],
end: [0, 0, inner_length+cone_slope*(inner_w_r-inner_n_r)/slot_factor],
radiusStart: inner_w_r/slot_factor,
radiusEnd: inner_n_r/slot_factor
}));
// Create a single gear teeth
var points = [new CSG.Vector2D.fromAngle(teeth_angle/2).times(gear_r/slot_factor)];
points.push(new CSG.Vector2D.fromAngle(-teeth_angle/2).times(gear_r/slot_factor));
points.push(new CSG.Vector2D((gear_r+teeth_triangle_height)/slot_factor, 0));
var tooth3d = new CSG.Polygon2D(points).extrude({offset: [0, 0, gear_cone_length]});
var teeth3d;
// Now clone it around the adapter
for (i = 0; i < teeth; i++) {
var t = tooth3d.rotateZ((i+0.5)/teeth*360*slot_factor);
teeth3d = (i == 0) ? t : teeth3d.union(t);
}
// Clip it by the bounding cylinder
teeth3d = teeth3d.intersect(gear_bounding_cyl);
var base = ext_cyl.union(gear_bounding_cyl.intersect(union(gear_cyl, teeth3d))).subtract(union(in1_cyl, in2_cyl)).union(teeth3d);
var slot3d;
// Create a slot
if (slot_width > 0) {
var slot_radius = gear_bounding_r/slot_factor;
var cyl1 = CSG.cylinder({
start: [0,0,0],
end: [0, 0, length],
radius: slot_radius
}).intersect(CSG.cube({
corner1: [-slot_radius, 0 , 0],
corner2: [ slot_radius, -slot_radius, length]
}));
var scale_factor1 = (outer_r+handle_radius)/gear_bounding_r;
var scale_factor2 = (outer_r+handle_distance)/gear_bounding_r;
var slot_angular_width = 360*slot_width/(2*pi*slot_radius);
slot3d = cyl1.intersect(cyl1.rotateZ(180-slot_angular_width));
base = base.subtract(slot3d);
if (params.handle) {
var handle1 = cyl1.scale([scale_factor1, scale_factor1, 1])
.subtract(cyl1.scale([scale_factor2, scale_factor2, 1]));
handle1 = handle1.rotateZ(slot_angular_width*handle_width_factor)
.intersect(handle1.rotateZ(180-slot_angular_width*(1+handle_width_factor)));
var handle2_inner_scale = inner_w_r/gear_bounding_r;
var handle2 = cyl1.scale([scale_factor1, scale_factor1, 1])
.subtract(cyl1.scale([handle2_inner_scale, handle2_inner_scale, 1]));
handle2 = handle2.rotateZ(slot_angular_width*handle_width_factor)
.intersect(handle2.rotateZ(180));
var handle3 = handle2.rotateZ(-(1+handle_width_factor)*slot_angular_width);
// Trim height
handle2 = handle2.translate([0, 0, handle1_start]).intersect(handle2.translate([0, 0, handle1_end-length]));
handle3 = handle3.translate([0, 0, handle2_start]).intersect(handle3.translate([0, 0, handle2_end-length]));
var handle4 = handle1.translate([0, 0, handle1_start]).intersect(handle1.translate([0, 0, handle1_end-length]));
var handle5 = handle1.translate([0, 0, handle2_start]).intersect(handle1.translate([0, 0, handle2_end-length]));
// This cylinder+cone clips the handles
var handle24_bounding_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, handle1_end-cone_slope*handle_radius],
radius: (outer_r+handle_radius)/slot_factor
}).union(CSG.cylinder({
start: [0, 0, handle1_end-cone_slope*handle_radius ],
end: [0, 0, handle1_end],
radiusStart: (outer_r+handle_radius)/slot_factor,
radiusEnd: outer_r/slot_factor
}));
var handle35_bounding_cyl = CSG.cylinder({
start: [0,0,0],
end: [0, 0, handle2_end-cone_slope*handle_radius],
radius: (outer_r+handle_radius)/slot_factor
}).union(CSG.cylinder({
start: [0, 0, handle2_end-cone_slope*handle_radius ],
end: [0, 0, handle2_end],
radiusStart: (outer_r+handle_radius)/slot_factor,
radiusEnd: outer_r/slot_factor
}));
if (!params.invert){
handle24_bounding_cyl = handle24_bounding_cyl
.translate([0, 0, -(handle1_start+handle1_end)/2])
.rotateX(180)
.translate([0, 0, (handle1_start+handle1_end)/2]);
handle35_bounding_cyl = handle35_bounding_cyl
.translate([0, 0, -(handle2_start+handle2_end)/2])
.rotateX(180)
.translate([0, 0, (handle2_start+handle2_end)/2]);
}
base = base.union(union(handle2, handle4).intersect(handle24_bounding_cyl))
.union(union(handle3, handle5).intersect(handle35_bounding_cyl));
}
}
if (params.invert) {
base = base.rotateX(180).translate([0, 0, length]);
}
if (params.base) {
base = base.union(CSG.cylinder({
start: [0,0,0],
end: [0, 0, params.base_thickness],
radius: outer_r/slot_factor
}));
if (slot_width > 0) {
base = base.subtract(slot3d);
}
}
/*
var connect = CSG.cube({
center: [small_cyl_offset/2,0,height/2],
radius: [small_cyl_offset/2,width/2,height/2]
});
*/
//
// Render
//
switch (params.part) {
case 'piece':
return base;
case 'piece_with_slot':
return handle1;
}
}