-
Notifications
You must be signed in to change notification settings - Fork 1
/
jscad-utils-parts.jscad
239 lines (211 loc) · 8 KB
/
jscad-utils-parts.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
/**
* A collection of parts for use in jscad. Requires jscad-utils.
* ![parts example](jsdoc2md/hexagon.png)
* @example
*include('jscad-utils-color.jscad');
*
*function mainx(params) {
* util.init(CSG);
*
* // draws a blue hexagon
* return Parts.Hexagon(10, 5).color('blue');
*}
* @type {Object}
* @module jscad-utils-parts
* @exports Parts
*/
Parts = {
BBox: function (object) {
return CSG.cube({
center: object.centroid(),
radius: object.size().dividedBy(2)
});
},
Cube: function (width) {
var r = util.divA(util.array.fromxyz(width), 2);
return CSG.cube({
center: r,
radius: r
});
},
RoundedCube: function (...args) {
if (args[0].getBounds) {
var size = util.size(args[0].getBounds());
var r = [size.x / 2, size.y / 2];
var thickness = size.z;
var corner_radius = args[1];
} else {
var r = [args[0] / 2, args[1] / 2]; // eslint-disable-line no-redeclare
var thickness = args[2]; // eslint-disable-line no-redeclare
var corner_radius = args[3]; // eslint-disable-line no-redeclare
}
// console.log('RoundedCube.args', size, r, thickness, corner_radius);
var roundedcube = CAG.roundedRectangle({
center: [r[0], r[1], 0],
radius: r,
roundradius: corner_radius
}).extrude({
offset: [0, 0, thickness || 1.62]
});
return roundedcube;
},
/**
* [Cylinder description]
* @param {Number} diameter Diameter of the cylinder
* @param {Number} height Height of the cylinder
* @param {Number} options Options passed to the `CSG.cylinder` function.
* @return {CSG} A CSG Cylinder
*/
Cylinder: function (diameter, height, options) {
options = util.defaults(options, {
start: [0, 0, 0],
end: [0, 0, height],
radius: diameter / 2
});
return CSG.cylinder(options);
},
Cone: function (diameter1, diameter2, height) {
return CSG.cylinder({
start: [0, 0, 0],
end: [0, 0, height],
radiusStart: diameter1 / 2,
radiusEnd: diameter2 / 2
});
},
/**
* Crate a hexagon.
* @param {number} diameter Outside diameter of the hexagon
* @param {number} height height of the hexagon
*/
Hexagon: function (diameter, height) {
var radius = diameter / 2;
var sqrt3 = Math.sqrt(3) / 2;
var hex = CAG.fromPoints([
[radius, 0],
[radius / 2, radius * sqrt3],
[-radius / 2, radius * sqrt3],
[-radius, 0],
[-radius / 2, -radius * sqrt3],
[radius / 2, -radius * sqrt3]
]);
return hex.extrude({
offset: [0, 0, height]
});
},
Triangle: function (base, height) {
var radius = base / 2;
var tri = CAG.fromPoints([
[-radius, 0],
[radius, 0],
[0, Math.sin(30) * radius]
]);
return tri.extrude({
offset: [0, 0, height]
});
},
/**
* Create a tube
* @param {Number} outsideDiameter Outside diameter of the tube
* @param {Number} insideDiameter Inside diameter of the tube
* @param {Number} height Height of the tube
* @param {Object} outsideOptions Options passed to the outside cylinder
* @param {Object} insideOptions Options passed to the inside cylinder
* @returns {CSG} A CSG Tube
*/
Tube: function Tube(outsideDiameter, insideDiameter, height, outsideOptions, insideOptions) {
return Parts.Cylinder(outsideDiameter, height, outsideOptions).subtract(Parts.Cylinder(insideDiameter, height, insideOptions || outsideOptions));
},
Board: function (width, height, corner_radius, thickness) {
var r = util.divA([width, height], 2);
var board = CAG.roundedRectangle({
center: [r[0], r[1], 0],
radius: r,
roundradius: corner_radius
}).extrude({
offset: [0, 0, thickness || 1.62]
});
return board;
},
Hardware: {
Orientation: {
up: {
head: 'outside-',
clear: 'inside+'
},
down: {
head: 'outside+',
clear: 'inside-'
}
},
Screw: function (head, thread, headClearSpace, options) {
options = util.defaults(options, {
orientation: 'up',
clearance: [0, 0, 0]
});
var orientation = Parts.Hardware.Orientation[options.orientation];
var group = util.group('head,thread', {
head: head.color('gray'),
thread: thread.snap(head, 'z', orientation.head).color('silver'),
});
if (headClearSpace) {
group.add(headClearSpace
.enlarge(options.clearance)
.snap(head, 'z', orientation.clear)
.color('red'), 'headClearSpace', true);
}
return group;
},
/**
* Creates a `Group` object with a Pan Head Screw.
* @param {number} headDiameter Diameter of the head of the screw
* @param {number} headLength Length of the head
* @param {number} diameter Diameter of the threaded shaft
* @param {number} length Length of the threaded shaft
* @param {number} clearLength Length of the clearance section of the head.
* @param {object} options Screw options include orientation and clerance scale.
*/
PanHeadScrew: function (headDiameter, headLength, diameter, length, clearLength, options) {
var head = Parts.Cylinder(headDiameter, headLength);
var thread = Parts.Cylinder(diameter, length);
if (clearLength) {
var headClearSpace = Parts.Cylinder(headDiameter, clearLength);
}
return Parts.Hardware.Screw(head, thread, headClearSpace, options);
},
/**
* Creates a `Group` object with a Hex Head Screw.
* @param {number} headDiameter Diameter of the head of the screw
* @param {number} headLength Length of the head
* @param {number} diameter Diameter of the threaded shaft
* @param {number} length Length of the threaded shaft
* @param {number} clearLength Length of the clearance section of the head.
* @param {object} options Screw options include orientation and clerance scale.
*/
HexHeadScrew: function (headDiameter, headLength, diameter, length, clearLength, options) {
var head = Parts.Hexagon(headDiameter, headLength);
var thread = Parts.Cylinder(diameter, length);
if (clearLength) {
var headClearSpace = Parts.Hexagon(headDiameter, clearLength);
}
return Parts.Hardware.Screw(head, thread, headClearSpace, options);
},
/**
* Create a Flat Head Screw
* @param {number} headDiameter head diameter
* @param {number} headLength head length
* @param {number} diameter thread diameter
* @param {number} length thread length
* @param {number} clearLength clearance length
* @param {object} options options
*/
FlatHeadScrew: function (headDiameter, headLength, diameter, length, clearLength, options) {
var head = Parts.Cone(headDiameter, diameter, headLength);
// var head = Parts.Cylinder(headDiameter, headLength);
var thread = Parts.Cylinder(diameter, length);
if (clearLength) {
var headClearSpace = Parts.Cylinder(headDiameter, clearLength);
}
return Parts.Hardware.Screw(head, thread, headClearSpace, options);
}
}
};