omdl  v1.0
OpenSCAD Mechanical Design Library
select_common_2d.scad
Go to the documentation of this file.
1 //! A standard selection and configuration scheme for common 2D 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 2d)
31  \amu_define group_brief (Selection and configuration for common 2D 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 2d 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 2D shapes available in the
61  library, offering a consistent and flexible way to include,
62  configure, and modify 2D 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 | 1 | \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 | circle | r | [circle()]
95  2 | ngon | r, n | pg_ngon()
96  3 | rectangle | size | pg_rectangle()
97  4 | rectangle sr | size | pg_rectangle_rs()
98  5 | rhombus | size | pg_rhombus()
99  6 | elliptical sector | r, v1, v2 | pg_elliptical_sector()
100  7 | triangle vertices | v1, v2, v3 | pg_triangle_ppp()
101  8 | triangle sides | s1, s2, s3 | pg_triangle_sss()
102  9 | star | size, n | star2d()
103  10 | corner round | r, m, v1, v2 | pg_corner_round()
104 
105  \amu_define scope_id (example)
106  \amu_define title (Selection example)
107  \amu_define image_views (top)
108  \amu_define image_size (sxga)
109 
110  \amu_include (include/amu/scope_diagrams_3d.amu)
111 
112  #### custom shape types
113 
114  t | shapes | parameters | function invocation name
115  :----:|:---------------------:|:---------------:|:----------------------------
116  50 | custom shape 1 | size | \p select_common_2d_shape_50()
117  51 | custom shape 2 | size | \p select_common_2d_shape_51()
118  52 | custom shape 3 | size | \p select_common_2d_shape_52()
119  53 | custom shape 4 | size | \p select_common_2d_shape_53()
120  54 | custom shape 5 | size | \p select_common_2d_shape_54()
121 
122  User-defined custom shape functions can be selected by defining a
123  shape function using one of the reserved custom shape function
124  names, and then referencing the corresponding shape type listed in
125  the table above. The custom function prototype and an example is
126  provided below.
127 
128  <b>Prototype:</b>
129 
130  \code{.C}
131  // custom shape module, type 50
132 
133  module select_common_2d_shape_50(size, vr, vrm, fn, center)
134  {
135 
136  };
137  \endcode
138 
139  \amu_define scope_id (example_custom)
140  \amu_define title (Custom shape example)
141  \amu_define image_views (top)
142  \amu_define image_size (sxga)
143 
144  \amu_include (include/amu/scope_diagrams_3d.amu)
145 
146  [circle()]: https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Using_the_2D_Subsystem#circle
147 *******************************************************************************/
149 (
150  type = 0,
151  argv,
152  center = false,
153  verb = 0
154 )
155 {
156  module construct_shape()
157  {
158  // circle
159  if ( type == 1 )
160  circle
161  (
162  r = size
163  );
164 
165  // ngon
166  else if ( type == 2 )
167  pg_ngon
168  (
169  r = defined_e_or(size, 0, size),
170  n = defined_e_or(size, 1, 5),
171  vr = vr,
172  vrm = vrm,
173  vfn = fn,
174  center = center
175  );
176 
177  // rectangle
178  else if ( type == 3 )
180  (
181  size = size,
182  vr = vr,
183  vrm = vrm,
184  vfn = fn,
185  center = center
186  );
187 
188  // rectangle_rs
189  else if ( type == 4 )
191  (
192  size = size,
193  sr = vr,
194  center = center
195  );
196 
197  // rhombus
198  else if ( type == 5 )
199  pg_rhombus
200  (
201  size = size,
202  vr = vr,
203  vrm = vrm,
204  vfn = fn,
205  center = center
206  );
207 
208  // elliptical sector
209  else if ( type == 6 )
211  (
212  r = defined_e_or(size, 0, size),
213  v1 = defined_e_or(size, 1, x_axis2d_uv),
214  v2 = defined_e_or(size, 2, x_axis2d_uv)
215  );
216 
217  // triangle_ppp
218  else if ( type == 7 )
219  let
220  (
221  v1 = defined_e_or(size, 0, size * origin2d),
222  v2 = defined_e_or(size, 1, size * y_axis2d_uv),
223  v3 = defined_e_or(size, 2, size * x_axis2d_uv)
224  )
226  (
227  c = [v1, v2, v3],
228  vr = vr,
229  vrm = vrm,
230  vfn = fn,
231  cm = center ? 1 : 0
232  );
233 
234  // triangle_sss
235  else if ( type == 8 )
236  let
237  (
238  s1 = defined_e_or(size, 0, size),
239  s2 = defined_e_or(size, 1, s1),
240  s3 = defined_e_or(size, 2, s2)
241  )
243  (
244  v = [s1, s2, s3],
245  vr = vr,
246  vrm = vrm,
247  vfn = fn,
248  cm = center ? 1 : 0
249  );
250 
251  // star2d
252  else if ( type == 9 )
253  star2d
254  (
255  size = defined_e_or(size, 0, size),
256  n = defined_e_or(size, 1, 5),
257  vr = vr
258  );
259 
260  // corner_round
261  else if ( type == 10 )
263  (
264  r = defined_e_or(size, 0, size),
265  m = defined_e_or(size, 1, 1),
266  v1 = defined_e_or(size, 2, x_axis2d_uv),
267  v2 = defined_e_or(size, 3, y_axis2d_uv)
268  );
269 
270  //
271  // custom shapes
272  //
273  else if ( type == 50 )
274  select_common_2d_shape_50(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
275  else if ( type == 51 )
276  select_common_2d_shape_51(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
277  else if ( type == 52 )
278  select_common_2d_shape_52(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
279  else if ( type == 53 )
280  select_common_2d_shape_53(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
281  else if ( type == 54 )
282  select_common_2d_shape_54(size=size, vr=vr, vrm=vrm, fn=fn, center=center);
283  }
284 
285  //
286  // decode parameters
287  //
288 
289  size = defined_eon_or(argv, 0, 1); // dimensions (type dependent)
290  vr = defined_e_or (argv, 1, undef); // rounding
291  vrm = defined_e_or (argv, 2, 1); // rounding mode
292  fn = defined_e_or (argv, 3, 5); // facets
293  sm = defined_e_or (argv, 4, 0); // shape modifier
294 
295  construct_shape();
296 
297  if (verb > 0)
298  {
299  log_info(strl(["type=", type, ", argv=", argv, ", center=", center]));
300 
301  if (verb > 1)
302  echo(size=size, vr=vr, vrm=vrm, fn=fn, sm=sm);
303  }
304 }
305 
306 
307 //! @}
308 //! @}
309 
310 //----------------------------------------------------------------------------//
311 // openscad-amu auxiliary scripts
312 //----------------------------------------------------------------------------//
313 
314 /*
315 BEGIN_SCOPE example;
316  BEGIN_OPENSCAD;
317  include <omdl-base.scad>;
318  include <shapes/select_common_2d.scad>;
319 
320  size = [50, 40, 30];
321  vr = [1, 5, 5];
322  vrm = [1, 1, 5];
323  fn = 9;
324 
325  type = 8;
326  argv = [size, vr, vrm, fn];
327 
328  select_common_2d_shape(type=type, argv=argv, center=true);
329 
330  // end_include
331  END_OPENSCAD;
332 
333  BEGIN_MFSCRIPT;
334  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
335  table_unset_all sizes;
336 
337  images name "sizes" types "sxga";
338  views name "views" views "top";
339 
340  variables set_opts_combine "sizes views";
341  variables add_opts "--viewall --autocenter --view=axes";
342 
343  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
344  END_MFSCRIPT;
345 END_SCOPE;
346 */
347 
348 /*
349 BEGIN_SCOPE example_custom;
350  BEGIN_OPENSCAD;
351  include <omdl-base.scad>;
352  include <shapes/select_common_2d.scad>;
353 
354  module select_common_2d_shape_50(size, vr, vrm, fn, center)
355  {
356  square(size, center=center);
357  }
358 
359  size = [50, 40];
360  vr = undef;
361  vrm = undef;
362  fn = undef;
363 
364  type = 50;
365  argv = [size, vr, vrm, fn];
366 
367  select_common_2d_shape(type=type, argv=argv, center=true);
368 
369  // end_include
370  END_OPENSCAD;
371 
372  BEGIN_MFSCRIPT;
373  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
374  table_unset_all sizes;
375 
376  images name "sizes" types "sxga";
377  views name "views" views "top";
378 
379  variables set_opts_combine "sizes views";
380  variables add_opts "--viewall --autocenter --view=axes";
381 
382  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
383  END_MFSCRIPT;
384 END_SCOPE;
385 */
386 
387 //----------------------------------------------------------------------------//
388 // end of file
389 //----------------------------------------------------------------------------//
module log_info(m)
Output information message to console.
Definition: console.scad:318
origin2d
<point-2d> The origin point coordinate in 2d Euclidean space.
Definition: constants.scad:407
x_axis2d_uv
<vector-2d> The unit vector of the positive x-axis in 2d Euclidean space.
Definition: constants.scad:410
y_axis2d_uv
<vector-2d> The unit vector of the positive y-axis in 2d Euclidean space.
Definition: constants.scad:413
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 star2d(size=1, n=5, vr)
A two-dimensional star.
module pg_rectangle_rs(size=1, o, sr, center=false)
A polygon rectangle with side rounding.
Definition: polygon.scad:616
module pg_corner_round(r=1, m=1, o=origin2d, v1=x_axis2d_uv, v2=y_axis2d_uv)
A polygon edge round with constant radius between two vectors.
Definition: polygon.scad:564
module pg_rhombus(size=1, o, vr, vrm=1, vfn, center=false)
A polygon rhombus with vertex rounding.
Definition: polygon.scad:824
module pg_ngon(n=5, r=1, o, vr, vrm=1, vfn, center=true)
A n-sided regular polygon with vertex rounding.
Definition: polygon.scad:874
module pg_elliptical_sector(r=1, o=origin2d, v1=x_axis2d_uv, v2=x_axis2d_uv, s=true)
A polygon elliptical sector.
Definition: polygon.scad:685
module pg_rectangle(size=1, o, vr, vrm=1, vfn, center=false)
A polygon rectangle with vertex rounding.
Definition: polygon.scad:773
module pg_triangle_sss(v, a=x_axis_ci, o, vr, vrm=1, vfn, cm=0)
A polygon triangle specified by three side lengths with vertex rounding.
Definition: polygon.scad:1047
module pg_triangle_ppp(c, o, vr, vrm=1, vfn, cm=0)
A polygon triangle specified by three coordinate points with vertex rounding.
Definition: polygon.scad:975
module select_common_2d_shape(type=0, argv, center=false, verb=0)
Select configure and construct one of the available 2d shapes.