omdl  v0.9.6
OpenSCAD Mechanical Design Library
screws.scad
Go to the documentation of this file.
1 //! Screws and screw bores.
2 /***************************************************************************//**
3  \file
4  \author Roy Allen Sutton
5  \date 2025
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 (Screws)
31  \amu_define group_brief (Screws and screw bores.)
32 
33  \amu_include (include/amu/pgid_path_pstem_pg.amu)
34 *******************************************************************************/
35 
36 //----------------------------------------------------------------------------//
37 // group and macros.
38 //----------------------------------------------------------------------------//
39 
40 /***************************************************************************//**
41  \amu_include (include/amu/group_in_parent_start.amu)
42  \amu_include (include/amu/includes_required.amu)
43 *******************************************************************************/
44 
45 //----------------------------------------------------------------------------//
46 
47 //! Flat and beveled-head screw bore with nut, nut slot, and bore tolerance.
48 /***************************************************************************//**
49  \param d <decimal> bore diameter.
50 
51  \param l <decimal> bore length.
52 
53  \param h <decimal-list-5> screw head; a list [hs, hf, hb, hg, hr],
54  the head size, flat-height, bevel-height, side geometry
55  (flat side count), and rotation. The head size is
56  measured flat-to-flat when \p hg is specified.
57 
58  \param n <decimal-list-5> screw nut; a list [ns, nf, nb, ng, nr, no],
59  the nut size, flat-height, bevel-height, side geometry
60  (flat side count), rotation, and bottom offset. The nut
61  size is measured flat-to-flat.
62 
63  \param t <decimal-list-2> bore tolerance; a list [tx, ty], the
64  tolerance along the x and/or y axis.
65 
66  \param s <decimal-list-list-3> nut slot cutout; a list of lists
67  [sx, sy, sz], the list of slot sizes along the x, y,
68  and/or z axis. Each of sx, sy, or sz can be a decimal
69  list, such as [lower, upper], or a single decimal value,
70  to cover [0, value].
71 
72  \param f <decimal-list-2 | decimal> bore scale factor; a list [bf, hf],
73  the bore diameter and bore height scale factors, or a
74  single decimal to specify \p bf only. The default value
75  for \p hf = 1 and it scales only the screw head and nut
76  heights.
77 
78  \param a <integer> z-alignment index; one of eight preset alignments.
79 
80  \details
81 
82  Construct a bore for a screw hole, screw head, and/or screw nut.
83  Both the screw head and screw nut are optional. A tolerance can be
84  specified along the bore x and y axis. A nut slot cutout can be
85  specified along the x, y, or z axis. The following example uses
86  both tolerance and a nut slot along the y axis. For convenience,
87  exact fastener dimensions can be specified along with the an
88  appropriately selected scale factor \p f to slightly increase the
89  bore for acceptable fastener fit.
90 
91  \amu_define scope_id (example)
92  \amu_define title (Screw bore example)
93  \amu_define image_views (top front diag)
94  \amu_define image_size (sxga)
95 
96  \amu_include (include/amu/scope_diagrams_3d.amu)
97 *******************************************************************************/
98 
99 module screw_bore
100 (
101  d = 1,
102  l = 1,
103 
104  h,
105  n,
106 
107  t,
108  s,
109 
110  f = 1,
111  a = 0
112 )
113 {
114  function cdc(s, n, m=0) = (m == 0) ? s * fd : s * fd / cos(180/n);
115 
116  // diameter and height scale factors
117  fd = defined_e_or(f, 0, f);
118  fh = defined_e_or(f, 1, 1);
119 
120  // screw bore
121  bd = cdc(d);
122 
123  // screw head
124  hs = defined_e_or(h, 0, 0);
125  hf = defined_e_or(h, 1, 0) * fh;
126  hb = defined_e_or(h, 2, 0) * fh;
127  hg = defined_e_or(h, 3, $fn);
128  hr = defined_e_or(h, 4, 0);
129 
130  hd = is_undef(h[3]) ? cdc(hs) : cdc(hs, hg, 1);
131 
132  // nut
133  ns = defined_e_or(n, 0, 0);
134  nf = defined_e_or(n, 1, 0) * fh;
135  nb = defined_e_or(n, 2, 0) * fh;
136  ng = defined_e_or(n, 3, 6);
137  nr = defined_e_or(n, 4, 0);
138  no = defined_e_or(n, 5, 0);
139 
140  nd = cdc(ns, ng, 1);
141 
142  // tolerance
143  tx = defined_e_or(t, 0, 0);
144  ty = defined_e_or(t, 1, 0);
145 
146  // slot
147  sx = defined_e_or(s, 0, 0);
148  sy = defined_e_or(s, 1, 0);
149  sz = defined_e_or(s, 2, 0);
150 
151  az =
152  [
153  0, -l/2, -l/2+hf/2, -l/2+hf, -l/2+hf+hb,
154  +l/2-nb-nf-no, +l/2-nf-no, +l/2-nf/2-no, +l/2-no, +l/2
155  ];
156 
157  translate([0, 0, select_ci(az, a, false)])
158  union()
159  {
160  hfo = [0, 0, +l/2 - hf/2 + eps*2];
161  hbo = [0, 0, +l/2 - hf - hb/2 + eps*4];
162  nbo = [0, 0, -l/2 + nf + nb/2 + no - eps*4];
163  nfo = [0, 0, -l/2 + nf/2 + no - eps*2];
164 
165  if ( is_undef(t) && is_undef(s) )
166  {
167  // screw hole
168  cylinder(d=bd, h=l, center=true);
169 
170  // head flat height
171  translate(hfo)
172  rotate([0, 0, hr])
173  cylinder(d=hd, h=hf, center=true, $fn=hg);
174 
175  // head bevel height
176  translate(hbo)
177  cylinder(d1=bd, d2=cdc(hs), h=hb, center=true);
178 
179  // nut bevel height
180  translate(nbo)
181  cylinder(d2=bd, d1=cdc(ns), h=nb, center=true);
182 
183  // nut flat height
184  translate(nfo)
185  rotate([0, 0, nr])
186  cylinder(d=nd, h=nf, center=true, $fn=ng);
187  }
188  else
189  { // slower equivalent with support for tolerance and nut slot
190  hull() for( v=[-1, 1], w=[-1, 1] )
191  translate([tx/2*v, ty/2*w, 0])
192  cylinder(d=bd, h=l, center=true);
193 
194  hull() for( v=[-1, 1], w=[-1, 1] )
195  translate(hfo + [tx/2*v, ty/2*w, 0])
196  rotate([0, 0, hr])
197  cylinder(d=hd, h=hf, center=true, $fn=hg);
198 
199  hull() for( v=[-1, 1], w=[-1, 1] )
200  translate(hbo + [tx/2*v, ty/2*w, 0])
201  cylinder(d1=bd, d2=cdc(hs), h=hb, center=true);
202 
203  // start slot from origin
204  ix = is_list(sx) ? sx : [0, sx];
205  iy = is_list(sy) ? sy : [0, sy];
206  iz = is_list(sz) ? sz : [0, sz];
207 
208  hull() for( v=[-1, 1], w=[-1, 1], x=ix, y=iy, z=iz )
209  translate(nbo + [tx/2*v + x, ty/2*w + y, z])
210  cylinder(d2=bd, d1=cdc(ns), h=nb, center=true);
211 
212  hull() for( v=[-1, 1], w=[-1, 1], x=ix, y=iy, z=iz )
213  translate(nfo + [tx/2*v + x, ty/2*w + y, z])
214  rotate([0, 0, nr])
215  cylinder(d=nd, h=nf, center=true, $fn=ng);
216  }
217  }
218 }
219 
220 //! @}
221 //! @}
222 
223 
224 //----------------------------------------------------------------------------//
225 // openscad-amu auxiliary scripts
226 //----------------------------------------------------------------------------//
227 
228 /*
229 BEGIN_SCOPE example;
230  BEGIN_OPENSCAD;
231  include <omdl-base.scad>;
232  include <models/3d/fastener/screws.scad>;
233 
234  $fn = 36;
235 
236  // screw bore with tolerance and nut-slot from front to back
237  %difference()
238  {
239  cube([10, 15, 18], center=true);
240  screw_bore(2.75, 18+eps*8, h=[6,1,3], n=[6,2,0,6,30,3], t=[0,5], s=[0,[-6,6],0], f=1.15);
241  }
242 
243  // show actual minimal space required
244  screw_bore(2.75, 18, h=[6,1,3], n=[6,2,0,6,30,3]);
245 
246  // end_include
247  END_OPENSCAD;
248 
249  BEGIN_MFSCRIPT;
250  include --path "${INCLUDE_PATH}" {var_init,var_gen_png2eps}.mfs;
251  table_unset_all sizes;
252 
253  images name "sizes" types "sxga";
254  views name "views" views "top front diag";
255 
256  variables set_opts_combine "sizes views";
257  variables add_opts "--viewall --autocenter --view=axes";
258 
259  include --path "${INCLUDE_PATH}" scr_make_mf.mfs;
260  END_MFSCRIPT;
261 END_SCOPE;
262 */
263 
264 //----------------------------------------------------------------------------//
265 // end of file
266 //----------------------------------------------------------------------------//
267 
eps
<decimal> Epsilon, small distance to deal with overlapping shapes.
Definition: constants.scad:195
function defined_e_or(v, i, d)
Return an element of an iterable when it exists or a default value otherwise.
function select_ci(v, i, l=true)
Select specified element from list or return a default.
module screw_bore(d=1, l=1, h, n, t, s, f=1, a=0)
Flat and beveled-head screw bore with nut, nut slot, and bore tolerance.
Definition: screws.scad:359