forked from timmaxw/os2cx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
openscad2calculix.scad
449 lines (410 loc) · 13.9 KB
/
openscad2calculix.scad
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* When the openscad2calculix executable is running OpenSCAD, it will redefine
this to a different value. "preview" is only used in the interactive OpenSCAD
editor. */
__openscad2calculix_mode = ["preview"];
function __os2cx_is_vector_3(arg) =
is_list(arg)
&& len(arg) == 3
&& is_num(arg[0])
&& is_num(arg[1])
&& is_num(arg[2]);
function __os2cx_is_num_with_unit(arg) =
is_list(arg)
&& len(arg) == 2
&& is_num(arg[0])
&& is_string(arg[1]);
function __os2cx_is_vector_3_with_unit(arg) =
is_list(arg)
&& len(arg) == 2
&& __os2cx_is_vector_3(arg[0])
&& is_string(arg[1]);
function __os2cx_is_list_of(arg, pred) =
is_list(arg)
&& [] == [for (a = arg) if (!pred(a)) a];
module __os2cx_beacon() {
origin_coords = [0, 0, 0 ];
concave_coords = [0.1, 0.2, 0.3];
corner_x_coords = [1, 0, 0 ];
corner_y_coords = [0, 1, 0 ];
corner_z_coords = [0, 0, 1 ];
origin_id = 0;
concave_id = 1;
corner_x_id = 2;
corner_y_id = 3;
corner_z_id = 4;
polyhedron(
points = [
origin_coords,
concave_coords,
corner_x_coords,
corner_y_coords,
corner_z_coords,
],
faces = [
[origin_id, corner_x_id, corner_y_id],
[origin_id, corner_y_id, corner_z_id],
[origin_id, corner_z_id, corner_x_id],
[concave_id, corner_y_id, corner_x_id],
[concave_id, corner_z_id, corner_y_id],
[concave_id, corner_x_id, corner_z_id],
],
convexity = 2
);
}
module os2cx_analysis_custom(lines, unit_system=undef) {
// Disable this assert since it uses function literals that were added in OpenSCAD 2021.01 and Ubuntu 22.04,
// and not available in OpenSCAD 2019.05 and Ubuntu 20.04.
// Cannot use if() with version_num() since the compiler parser fails, so must be commented out.
// https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Function_literals
/*
assert(
__os2cx_is_list_of(lines, function(l) (
is_string(l)
|| __os2cx_is_list_of(l, function (l2) (
is_string(l2)
|| is_list(l2)
))
)),
"malformed input lines");
}
*/
assert(
is_list(unit_system)
&& len(unit_system) == 3
&& is_string(unit_system[0])
&& is_string(unit_system[1])
&& is_string(unit_system[2]),
"unit_system must be a list of three strings");
assert($children == 0);
if (__openscad2calculix_mode == ["preview"]) {
echo(str("NOTE: To run the CalculiX simulation, open this .scad file ",
"using the OpenSCAD2CalculiX application."));
} else if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "analysis_directive", lines, unit_system);
}
}
/* os2cx_analysis_static_simple() simulates a static load applied to a structure
attached to a fixed base. For example, the weight of a person standing on a
footbridge. */
module os2cx_analysis_static_simple(
fixed=undef,
load=undef,
length_unit=undef
) {
assert(is_string(fixed));
assert(is_string(load));
assert(is_string(length_unit));
assert($children == 0);
os2cx_analysis_custom([
"*INCLUDE, INPUT=objects.inp",
"*STEP",
"*STATIC",
"*BOUNDARY",
[["nset", fixed], ",1,3"],
"*CLOAD",
["*INCLUDE, INPUT=", ["cload_file", load]],
"*NODE FILE",
"U",
"*EL FILE",
"S",
"*END STEP"
], unit_system=[length_unit, "kg", "s"]);
}
/* os2cx_analysis_steady_state_dynamics() simulates oscillating loads applied to
a structure attached to a fixed base. The load is simulated at a range of
frequencies. */
module os2cx_analysis_steady_state_dynamics(
fixed=undef,
load=undef,
length_unit=undef,
num_eigenfrequencies=10,
min_frequency=undef,
max_frequency=undef,
damping_ratio=undef
) {
assert(is_string(fixed));
assert(is_string(load));
assert(is_string(length_unit));
assert(is_num(num_eigenfrequencies));
assert(__os2cx_is_num_with_unit(min_frequency) && min_frequency[1] == "Hz",
"min_frequency must be [a number, \"Hz\"]");
assert(__os2cx_is_num_with_unit(max_frequency) && max_frequency[1] == "Hz",
"max_frequency must be [a number, \"Hz\"]");
assert(min_frequency[0] < max_frequency[0]);
assert(is_num(damping_ratio));
assert($children == 0);
os2cx_analysis_custom([
"*INCLUDE, INPUT=objects.inp",
"*BOUNDARY",
[["nset", fixed], ",1,3"],
"*STEP",
"*FREQUENCY,STORAGE=yes",
str(num_eigenfrequencies),
"*NODE FILE",
"U",
"*END STEP",
"*STEP",
"*MODAL DAMPING",
str("1", ",", num_eigenfrequencies, ",", damping_ratio),
"*STEADY STATE DYNAMICS",
str(min_frequency[0], ",", max_frequency[0], ",", 10),
"*CLOAD",
["*INCLUDE, INPUT=", ["cload_file", load]],
"*NODE FILE",
"PU,U",
"*EL FILE",
"S",
"*END STEP"
], unit_system=[length_unit, "kg", "s"]);
}
/* os2cx_analysis_steady_state_dynamics_osc_boundary() simulates a structure
attached to an oscillating base. For example, a tower during an earthquake. */
module os2cx_analysis_steady_state_dynamics_osc_boundary(
boundary=undef,
oscillation=undef,
length_unit=undef,
num_eigenfrequencies=10,
min_frequency=undef,
max_frequency=undef,
rayleigh_damping_alpha=undef,
rayleigh_damping_beta=undef,
) {
assert(is_string(boundary));
assert(__os2cx_is_vector_3_with_unit(oscillation));
assert(is_string(length_unit));
assert(oscillation[1] == length_unit,
"oscillation units must be same as length_unit");
assert(is_num(num_eigenfrequencies));
assert(__os2cx_is_num_with_unit(min_frequency) && min_frequency[1] == "Hz",
"min_frequency must be [a number, \"Hz\"]");
assert(__os2cx_is_num_with_unit(max_frequency) && max_frequency[1] == "Hz",
"max_frequency must be [a number, \"Hz\"]");
assert(is_num(rayleigh_damping_alpha));
assert(is_num(rayleigh_damping_beta));
assert($children == 0);
os2cx_analysis_custom([
"*INCLUDE, INPUT=objects.inp",
"*BOUNDARY",
[["nset", boundary], ",1,3"],
"*STEP",
"*FREQUENCY,STORAGE=yes",
str(num_eigenfrequencies),
"*NODE FILE",
"U",
"*END STEP",
"*STEP",
"*MODAL DAMPING,RAYLEIGH",
str(",,", rayleigh_damping_alpha, ",", rayleigh_damping_beta),
"*STEADY STATE DYNAMICS",
str(min_frequency[0], ",", max_frequency[0], ",", 10),
"*BOUNDARY",
[["nset", boundary], ",1,1,", str(oscillation[0][0])],
[["nset", boundary], ",2,2,", str(oscillation[0][1])],
[["nset", boundary], ",3,3,", str(oscillation[0][2])],
"*NODE FILE",
"PU,U",
"*EL FILE",
"S",
"*END STEP"
], unit_system=[length_unit, "kg", "s"]);
}
module os2cx_mesh(
name,
mesher="tetgen",
max_element_size=undef,
material=undef,
element_type=undef,
) {
assert(is_string(name));
assert(is_string(mesher));
assert(is_undef(max_element_size) ||
(is_num(max_element_size) && max_element_size > 0));
assert(is_string(material));
assert(is_undef(element_type) || is_string(element_type));
assert($children > 0);
if (__openscad2calculix_mode == ["preview"]) {
children();
} else if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "mesh_directive",
name, mesher, max_element_size, material, element_type);
} else if (__openscad2calculix_mode == ["mesh", name]) {
children();
}
}
module os2cx_slice(name, direction_vector, direction_angle_tolerance) {
assert(is_string(name));
assert(__os2cx_is_vector_3(direction_vector));
assert(is_num(direction_angle_tolerance));
assert($children > 0);
if (__openscad2calculix_mode == ["preview"] && $preview) {
# children();
} else if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "slice_directive",
name,
direction_vector,
direction_angle_tolerance);
} else if (__openscad2calculix_mode == ["slice", name]) {
children();
}
}
module os2cx_select_volume(name) {
assert(is_string(name));
assert($children > 0);
if (__openscad2calculix_mode == ["preview"] && $preview) {
# children();
} else if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "select_volume_directive", name);
} else if (__openscad2calculix_mode == ["select_volume", name]) {
children();
}
}
module os2cx_select_surface(name, direction_vector, direction_angle_tolerance) {
assert(is_string(name));
assert(__os2cx_is_vector_3(direction_vector));
assert(is_num(direction_angle_tolerance));
assert($children > 0);
if (__openscad2calculix_mode == ["preview"] && $preview) {
if (!is_string(name)) {
echo("ERROR: os2cx_select_surface() parameter must be a string");
}
if ($preview) {
# children();
}
} else if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "select_surface_directive",
name,
"external",
direction_vector,
direction_angle_tolerance);
} else if (__openscad2calculix_mode == ["select_surface", name]) {
children();
}
}
module os2cx_select_surface_internal(
name, direction_vector, direction_angle_tolerance
) {
assert(is_string(name));
assert(__os2cx_is_vector_3(direction_vector));
assert(is_num(direction_angle_tolerance));
assert(direction_angle_tolerance < 90);
assert($children > 0);
if (__openscad2calculix_mode == ["preview"] && $preview) {
# children();
} else if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "select_surface_directive",
name,
"internal",
direction_vector,
direction_angle_tolerance);
} else if (__openscad2calculix_mode == ["select_surface", name]) {
children();
}
}
module os2cx_select_node(name, point) {
assert(is_string(name));
assert(__os2cx_is_vector_3(point));
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "select_node_directive",
name, point);
}
}
module os2cx_create_node(name, point) {
assert(is_string(name));
assert(__os2cx_is_vector_3(point));
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "create_node_directive",
name, point);
}
}
module os2cx_load_volume(
name, volume, force_total=undef, force_per_volume=undef
) {
assert(is_string(name));
assert(is_string(volume));
assert(!is_undef(force_total) || !is_undef(force_per_volume),
"must specify either force_total or force_per_volume");
assert(is_undef(force_total) || is_undef(force_per_volume),
"can't specify both force_total and force_per_volume");
if (force_total != undef) {
assert(__os2cx_is_vector_3_with_unit(force_total));
}
if (force_per_volume != undef) {
assert(__os2cx_is_vector_3_with_unit(force_per_volume));
}
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "load_volume_directive",
name, volume, force_total, force_per_volume);
}
}
module os2cx_load_surface(
name, surface, force_total=undef, force_per_area=undef
) {
assert(is_string(name));
assert(is_string(surface));
assert(!is_undef(force_total) || !is_undef(force_per_area),
"must specify either force_total or force_per_area");
assert(is_undef(force_total) || is_undef(force_per_area),
"can't specify both force_total and force_per_area");
if (force_total != undef) {
assert(__os2cx_is_vector_3_with_unit(force_total));
}
if (force_per_area != undef) {
assert(__os2cx_is_vector_3_with_unit(force_per_area));
}
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "load_surface_directive",
name, surface, force_total, force_per_area);
}
}
module os2cx_material_elastic_simple(
name, youngs_modulus=undef, poissons_ratio=undef, density=undef
) {
assert(is_string(name));
assert(__os2cx_is_num_with_unit(youngs_modulus));
assert(is_num(poissons_ratio));
assert(__os2cx_is_num_with_unit(density));
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "material_elastic_simple_directive",
name, youngs_modulus, poissons_ratio, density);
}
}
module os2cx_override_max_element_size(
volume, max_element_size
) {
assert(is_string(volume));
assert(is_num(max_element_size));
assert(max_element_size > 0);
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "override_max_element_size_directive",
volume, max_element_size);
}
}
module os2cx_override_material(
volume, material
) {
assert(is_string(volume));
assert(is_string(material));
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "override_material_directive",
volume, material);
}
}
module os2cx_measure(
name, volume, variable
) {
assert(is_string(name));
assert(is_string(volume));
assert(is_string(variable));
assert($children == 0);
if (__openscad2calculix_mode == ["inventory"]) {
echo("__openscad2calculix", "measure_directive",
name, volume, variable);
}
}