omdl  v1.0
OpenSCAD Mechanical Design Library
select_common_3d.scad
Go to the documentation of this file.
1 //! A standard selection and configuration scheme for common 3D shape construction.
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 2026
6 
7  \copyright
8 
9  This file is part of [omdl] (https://github.com/royasutton/omdl),
10  an OpenSCAD mechanical design library.
11 
12  The \em omdl is free software; you can redistribute it and/or modify
13  it under the terms of the [GNU Lesser General Public License]
14  (http://www.gnu.org/licenses/lgpl.html) as published by the Free
15  Software Foundation; either version 2.1 of the License, or (at
16  your option) any later version.
17 
18  The \em omdl is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with the \em omdl; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26  02110-1301, USA; or see <http://www.gnu.org/licenses/>.
27 
28  \details
29 
30  \amu_define group_name (Select Common 3d)
31  \amu_define group_brief (Selection and configuration for common 3D shapes.)
32 
33  \amu_include (include/amu/doxyg_init_pd_gds_ipg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group and macros.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/doxyg_define_in_parent_open.amu)
42  \amu_include (include/amu/includes_required.amu)
43 *******************************************************************************/
44 
45 //----------------------------------------------------------------------------//
46 
47 //! Select configure and construct one of the available 3d shapes.
48 /***************************************************************************//**
49  \param type <integer> Shape type index.
50 
51  \param argv <decimal-list | decimal> Shape dependent argument vector.
52 
53  \param center <boolean> Center about origin.
54 
55  \param verb <integer> Output verbosity (0-2).
56 
57  \details
58 
59  This module provides a standard scheme for selecting and
60  configuring the arguments of most 3D shapes available in the
61  library, offering a consistent and flexible way to include,
62  configure, and modify 3D shapes.
63 
64  ### argv
65 
66  Each shape is configured using the parameter \p argv. The four
67  elements of this parameter are as follows:
68 
69  e | data type | default value | parameter description
70  ---:|:-----------------:|:-----------------:|:------------------------------------
71  0 | decimal-list-n \| decimal | 1 | \p size : shape size
72  1 | decimal-list-n \| decimal | undef | \p vr (\p sr) : shape rounding
73  2 | integer-list-n \| integer | 0 | \p vrm : shape rounding mode
74  3 | integer | 5 | \p fn : shape rounding facets
75  4 | integer | 0 | shape modifier
76 
77  #### argv[4]: shape modifier
78 
79  v | modifier description
80  :----:|:------------------------------------
81  0 | none
82 
83  ### type
84 
85  Dimensioning is shape dependent. The required dimensioning
86  arguments for each supported shape are shown in the following
87  table. All supported shapes and their associated arguments are
88  summarized in the following table:
89 
90  #### standard shape types
91 
92  t | shapes | parameters | shape reference
93  :----:|:---------------------:|:---------------:|:----------------------------
94  1 | cylinder | h, r1, r2 | [cylinder()]
95  2 | cone | size | cone()
96  3 | cuboid | size | cuboid()
97  4 | ellipsoid | size | ellipsoid()
98  5 | ellipsoid section | size, a1, a2 | ellipsoid_s()
99  6 | quadratic pyramid | size | pyramid_t()
100  7 | triangular pyramid | size | pyramid_q()
101  8 | star 3d | size, n, half | star3d()
102  100+ | Polyhedrons database | size | (see below)
103 
104  \amu_define scope_id (example)
105  \amu_define title (Selection example)
106  \amu_define image_views (top front diag)
107  \amu_define image_size (sxga)
108 
109  \amu_include (include/amu/scope_diagrams_3d.amu)
110 
111  #### custom shape types
112 
113  t | shapes | parameters | function invocation name
114  :----:|:---------------------:|:---------------:|:----------------------------
115  50 | custom shape 1 | size | \p select_common_3d_shape_50()
116  51 | custom shape 2 | size | \p select_common_3d_shape_51()
117  52 | custom shape 3 | size | \p select_common_3d_shape_52()
118  53 | custom shape 4 | size | \p select_common_3d_shape_53()
119  54 | custom shape 5 | size | \p select_common_3d_shape_54()
120 
121  User-defined custom shape functions can be selected by defining a
122  shape function using one of the reserved custom shape function
123  names, and then referencing the corresponding shape type listed in
124  the table above. The custom function prototype and an example is
125  provided below.
126 
127  <b>Prototype:</b>
128 
129  \code{.C}
130  // custom shape module, type 50
131 
132  module select_common_3d_shape_50(size, vr, vrm, fn, center)
133  {
134 
135  };
136  \endcode
137 
138  \amu_define scope_id (example_custom)
139  \amu_define title (Custom shape example)
140  \amu_define image_views (top front diag)
141  \amu_define image_size (sxga)
142 
143  \amu_include (include/amu/scope_diagrams_3d.amu)
144 
145  ## Polyhedrons database inclusion
146 
147  \amu_define includes_required_add
148  (
149  database/geometry/polyhedra/polyhedra_all.scad
150  shapes/polyhedron_db.scad
151  )
152  \amu_include (include/amu/includes_required.amu)
153 
154  Polyhedra available through ph_db_polyhedron() may also be included
155  in the 3d shape selection options. These shapes are accessible when
156  the ph_db_polyhedron() module is included as shown under
157  `Requires:' above. As described in its documentation, a polyhedron
158  database must also be included. In this example below, the \p
159  polyhedra_all database is used; however, a smaller database may be
160  configured instead, as described in the ph_db_polyhedron()
161  documentation.
162 
163  \amu_define scope_id (example_ph_db)
164  \amu_define title (Polyhedrons database selection example)
165  \amu_define image_views (top front diag)
166  \amu_define image_size (sxga)
167 
168  \amu_include (include/amu/scope_diagrams_3d.amu)
169 
170  [cylinder()]: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Primitive_Solids#cylinder
171 *******************************************************************************/
173 (
174  type = 0,
175  argv,
176  center = false,
177  verb = 0
178 )
179 {
180  module construct_shape()
181  {
182  // polyhedron database available when modules global data table is defined
183  pd_db_available = !( is_undef(ph_db_dtc) || is_undef(ph_db_dtr) );
184 
185  // polyhedron database type offset
186  pd_db_type_offset = 99;
187 
188  // cylinder
189  if ( type == 1 )
190  let
191  (
192  h = defined_e_or(size, 0, size),
193  r1 = defined_e_or(size, 1, h),
194  r2 = defined_e_or(size, 2, r1)
195  )
196  cylinder
197  (
198  h = h,
199  r1 = r1,
200  r2 = r2,
201  center = center
202  );
203 
204  // cone
205  else if ( type == 2 )
206  cone
207  (
208  size = size,
209  vr = vr,
210  center = center
211  );
212 
213  // cuboid
214  else if ( type == 3 )
215  cuboid
216  (
217  size = size,
218  vr = vr,
219  vrm = vrm,
220  center = center
221  );
222 
223  // ellipsoid
224  else if ( type == 4 )
225  ellipsoid
226  (
227  size = size,
228  center = center
229  );
230 
231  // ellipsoid_s
232  else if ( type == 5 )
233  let
234  (
235  s = defined_e_or(size, 0, size),
236  a1 = defined_e_or(size, 1, 0),
237  a2 = defined_e_or(size, 2, 0)
238  )
240  (
241  size = s,
242  a1 = a1,
243  a2 = a2,
244  center = center
245  );
246 
247  // pyramid_t
248  else if ( type == 6 )
249  pyramid_t
250  (
251  size = size,
252  center = center
253  );
254 
255  // pyramid_q
256  else if ( type == 7 )
257  pyramid_q
258  (
259  size = size,
260  center = center
261  );
262 
263  // star3d
264  else if ( type == 8 )
265  let
266  (
267  s = defined_e_or(size, 0, size),
268  n = defined_e_or(size, 1, 5),
269  h = defined_e_or(size, 2, false)
270  )
271  star3d
272  (
273  size = s,
274  n = n,
275  half = h,
276  center = center
277  );
278 
279  //
280  // custom shapes
281  //
282  else if ( type == 50 )
283  select_common_3d_shape_50(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
284  else if ( type == 51 )
285  select_common_3d_shape_51(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
286  else if ( type == 52 )
287  select_common_3d_shape_52(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
288  else if ( type == 53 )
289  select_common_3d_shape_53(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
290  else if ( type == 54 )
291  select_common_3d_shape_54(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
292 
293  //
294  // database polyhedron
295  //
296  else if ( type > pd_db_type_offset )
297  {
298  assert
299  (
300  pd_db_available,
301  "required module not loaded; please include polyhedrons db module."
302  );
303 
304  id_number = type - pd_db_type_offset;
305  id_name = ph_db_get_id(id_number);
306 
307  if (verb > 1)
308  {
309  db_size = ph_db_get_size();
310 
311  echo(strl(["polyhedrons db_size = ", db_size, ", id_offset = ", pd_db_type_offset]));
312  echo(strl(["id_number = ", id_number, ", id_name = ", id_name]));
313  }
314 
316  (
317  id = id_name,
318  size = size,
319  align = (center==true) ? [0, 0, 0] : [1, 1, 1]
320  );
321  }
322  }
323 
324  //
325  // decode parameters
326  //
327 
328  size = defined_eon_or(argv, 0, 1); // dimensions (type dependent)
329  vr = defined_e_or (argv, 1, undef); // rounding
330  vrm = defined_e_or (argv, 2, 0); // rounding mode
331  fn = defined_e_or (argv, 3, 5); // facets
332  sm = defined_e_or (argv, 4, 0); // shape modifier
333 
334  construct_shape();
335 
336  if (verb > 0)
337  {
338  log_info(strl(["type=", type, ", argv=", argv, ", center=", center]));
339 
340  if (verb > 1)
341  echo(size=size, vr=vr, vrm=vrm, fn=fn, sm=sm);
342  }
343 }
344 
345 
346 //! @}
347 //! @}
348 
349 //----------------------------------------------------------------------------//
350 // openscad-amu auxiliary scripts
351 //----------------------------------------------------------------------------//
352 
353 /*
354 BEGIN_SCOPE example;
355  BEGIN_OPENSCAD;
356  include <omdl-base.scad>;
357  include <shapes/select_common_3d.scad>;
358 
359  r = 25;
360  h = 25;
361 
362  size = [r, h];
363  a1 = 0;
364  a2 = 225;
365 
366  type = 5;
367  argv = [[size, a1, a2]];
368 
369  select_common_3d_shape(type=type, argv=argv, center=true);
370 
371  // end_include
372  END_OPENSCAD;
373 
374  BEGIN_MFSCRIPT;
375  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
376  table_unset_all sizes;
377 
378  images name "sizes" types "sxga";
379  views name "views" views "top front diag";
380 
381  variables set_opts_combine "sizes views";
382  variables add_opts "--viewall --autocenter --view=axes";
383 
384  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
385  END_MFSCRIPT;
386 END_SCOPE;
387 */
388 
389 /*
390 BEGIN_SCOPE example_custom;
391  BEGIN_OPENSCAD;
392  include <omdl-base.scad>;
393  include <shapes/select_common_3d.scad>;
394 
395  module select_common_3d_shape_50(size, vr, vrm, fn, center)
396  {
397  cube(size, center=center);
398  }
399 
400  size = [50, 40, 30];
401  vr = undef;
402  vrm = undef;
403  fn = undef;
404 
405  type = 50;
406  argv = [size, vr, vrm, fn];
407 
408  select_common_3d_shape(type=type, argv=argv, center=true);
409 
410  // end_include
411  END_OPENSCAD;
412 
413  BEGIN_MFSCRIPT;
414  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
415  table_unset_all sizes;
416 
417  images name "sizes" types "sxga";
418  views name "views" views "top front diag";
419 
420  variables set_opts_combine "sizes views";
421  variables add_opts "--viewall --autocenter --view=axes";
422 
423  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
424  END_MFSCRIPT;
425 END_SCOPE;
426 */
427 
428 /*
429 BEGIN_SCOPE example_ph_db;
430  BEGIN_OPENSCAD;
431  include <omdl-base.scad>;
432  include <database/geometry/polyhedra/polyhedra_all.scad>;
433  include <shapes/polyhedron_db.scad>;
434  include <shapes/select_common_3d.scad>;
435 
436  size = [10, 20, 20];
437  type = 122;
438  argv = [size];
439 
440  select_common_3d_shape(type=type, argv=argv, center=true);
441 
442  // end_include
443  END_OPENSCAD;
444 
445  BEGIN_MFSCRIPT;
446  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
447  table_unset_all sizes;
448 
449  images name "sizes" types "sxga";
450  views name "views" views "top front diag";
451 
452  variables set_opts_combine "sizes views";
453  variables add_opts "--viewall --autocenter --view=axes";
454 
455  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
456  END_MFSCRIPT;
457 END_SCOPE;
458 */
459 
460 //----------------------------------------------------------------------------//
461 // end of file
462 //----------------------------------------------------------------------------//
module log_info(m)
Output information message to console.
Definition: console.scad:318
function defined_e_or(v, i, d)
Returns an element from an iterable if it exists, or a default value if not.
function defined_eon_or(v, i, d)
Returns the list element or scalar numeric value if defined, otherwise returns the default value.
function strl(v)
Convert a list of values to a concatenated string.
module pyramid_q(size=1, center=false)
A pyramid with quadrilateral base.
Definition: common_3d.scad:772
module ellipsoid_s(size=1, a1=0, a2=0, center=false)
A sector of an ellipsoid.
Definition: common_3d.scad:663
module star3d(size=1, n=5, half=false, center=false)
A three-dimensional star.
Definition: common_3d.scad:824
module cone(size=1, vr, center=false)
A cone.
Definition: common_3d.scad:461
module cuboid(size=1, vr, vrm=0, center=false)
A cuboid with edge, round, or chamfer corners.
Definition: common_3d.scad:531
module pyramid_t(size=1, center=false)
A pyramid with trilateral base formed by four equilateral triangles.
Definition: common_3d.scad:725
module ellipsoid(size=1, center=false)
An ellipsoid.
Definition: common_3d.scad:622
function ph_db_get_size(tr=ph_db_dtr)
Get the number of shape identifiers in data table.
function ph_db_get_id(n, tr=ph_db_dtr)
Get data table identifier name (or names).
module ph_db_polyhedron(id, size, align, tr=ph_db_dtr, tc=ph_db_dtc)
Construct a named polyhedron.
ph_db_dtc
<map> The default polyhedra data table columns.
ph_db_dtr
<table> The default polyhedra data table rows.
module select_common_3d_shape(type=0, argv, center=false, verb=0)
Select configure and construct one of the available 3d shapes.