-
Notifications
You must be signed in to change notification settings - Fork 115
/
screws.scad
3054 lines (2941 loc) · 178 KB
/
screws.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////
// LibFile: screws.scad
// Functions and modules for creating metric (ISO) and English (UTS) standard screws and nuts.
// Included is a function for calculating the standard dimensions of screws including the
// tolerance values that are required to make screws mate properly when they are formed
// precisely. If you can fabricate objects accurately then the modeled screws will mate
// with standard hardware without the need to introduce extra gaps for clearance.
// Includes:
// include <BOSL2/std.scad>
// include <BOSL2/screws.scad>
// FileGroup: Threaded Parts
// FileSummary: ISO (metric) and UTS screws and nuts.
//////////////////////////////////////////////////////////////////////
include <structs.scad>
include <threading.scad>
include <screw_drive.scad>
// Section: Screw and Nut Parameters
// This modules in this file create standard ISO (metric) and UTS (English) threaded screws.
// The {{screw()}} and {{nut()}} modules produce
// screws and nuts that comply with the relevant ISO and ASME standards,
// including tolerances for screw fit. You can also create screws with
// various head types and drive types that should match standard hardware.
// Subsection: Screw Naming
// You can specify screws using a string that specifies the screw.
// Metric or ISO screws are specified by a diameter in millimeters and a thread pitch in millimeters. For example,
// an M8x2 screw has a nominal diameter of 8 mm and a thread pitch of 2 mm.
// The screw specification for these screws has the form: "M`<size>`x`<pitch>`,`<length>`,
// so "M6x1,10" specifies a 6mm diameter screw with a thread pitch of 1mm and length of 10mm.
// You can omit the pitch or length, e.g. "M6x1", or "M6,10", or just "M6". If you omit the
// length then you must provide the `length` parameter. If you omit the pitch, the library
// provides a standard pitch for the specified diameter.
// .
// Imperial or UTS screws are specified by a diameter and the number of threads per inch.
// For large screws, the diameter is simply the nominal diameter in inches, so a 5/16-18 screw
// has a nominal diameter of 5/16 inches and 18 threads per inch. For diameters smaller than
// 1/4 inch, the screw diameter is given using a screw gauge, which can be from 0 up to 12.
// A common smaller size is #8-32, an 8 gauge screw with 32 threads per inch.
// For UTS screws the specification has the form `<size>`-`<threadcount>`,`<length>`, e.g.
// "#8-32,1/2", or "1/4-20,1". The units are in inches, including the length. Size can be a
// gauge number from 0 to 12 with or without a leading # to specify a screw gauge size, or any other
// value to specify a diameter in inches, either as a float or a fraction, so "0.5-13" and
// "1/2-13" are equivalent. To force interpretation of the value as inches add '' (two
// single-quotes) to the end, e.g. "1''-4" is a one inch screw and "1-80" is a very small
// 1-gauge screw. The pitch is specified using a thread count, the number of threads per inch.
// As with the ISO screws, you can omit the pitch or length and specify "#6-32", "#6,3/4", or simply #6.
// As in the metric case, if you omit the length then you must provide the `length` parameter. If you omit the pitch, the
// library provides a standard pitch for the specified diameter.
// Subsection: Standard Screw Pitch
// If you omit the pitch when specifying a screw or nut then the library supplies a standard screw pitch based
// on the screw diameter as listed in ISO 724 or ASME B1.1. For many diameters, multiple standard pitches exist.
// The available thread pitch types are different for ISO and UTS:
// .
// | ISO | UTS |
// | -------- | -------- |
// | "coarse" | "coarse" or "UNC" |
// | "fine" | "fine" or "UNF" |
// | "extrafine" or "extra fine" | "extrafine", "extra fine", or "UNEF" |
// | "superfine" or "super fine" | |
// | "none" | "none" |
// .
// The default pitch selection is "coarse". Note that this selection is case insensitive.
// To set the pitch using these pitch strings you use the `thread=` argument to the modules.
// You cannot incorporate a named pitch into the thread name. The finer pitch categories
// are defined only for larger screw diameters. You can also use the `thread=` argument to
// directly specify a pitch, so `thread=2` produces a thread pitch of 2mm. Setting the
// pitch to zero produces an unthreaded screws, the same as setting it to "none". Specifying
// a numeric value this way overrides a value given in the specification. You can also set
// `thread=true` or `thread=false` to turn threading on and off, with the same default coarse
// threading when you set it to true.
// Subsection: Screw Heads
// By default screws do not have heads.
// You can request a screw head using `head=` parameter to specify the desired head type. If you want the
// head to have a recess for driving the screw you must also specify a drive type using `drive=`.
// The table below lists the head options. Only some combinations of head and drive
// type are supported. Different sized flat heads exist for the same screw type.
// Sometimes this depends on the type of recess. If you specify "flat" then the size will be chosen
// appropriately for the recess you specify.
// .
// The `drive=` argument can be set to "none", "hex", "slot",
// "phillips", "ph0" to "ph4" (for phillips of the specified size), "torx" or
// "t<size>" (for Torx at a specified size, e.g. "t20"). If you have no head but still
// give a drive type you will get a set screw. The table below lists all of the head types and
// shows which drive type is compatible with each head types. Different head types work in ISO and UTS,
// as marked in the first column.
// .
// |ISO|UTS|Head | Drive |
// |---|---|--------------- | ----------------------------|
// |X|X|"none" | hex, torx, slot |
// |X|X|"hex" | *none*|
// |X|X|"socket" | hex, torx|
// |X|X|"button" | hex, torx|
// |X|X|"flat" | slot, phillips, hex, torx|
// |X|X|"flat sharp" | slot, phillips, hex, torx|
// | |X|"flat small" | slot, phillips|
// | |X|"flat large" | hex, torx |
// | |X|"flat undercut" | slot, phillips |
// | |X|"flat 82" | slot, phillips |
// | |X|"flat 100" | slot, phillips |
// | |X|"round" | slot, phillips |
// | |X|"fillister" | slot, phillips |
// |X|X|"pan" | slot, phillips, torx (ISO only) |
// |X| |"cheese" | slot, phillips, torx |
// .
// The drive size is specified appropriately for the drive type: drive number for phillips or torx,
// and recess width in mm or inches (as appropriate) for hex. Drive size is determined automatically
// from the screw size, but by passing the `drive_size=` argument you can override the default, or
// in cases where no default exists you can specify it. Flat head screws have variations such as 100 degree
// angle for UTS, or undercut heads. You can also request a "sharp" screw which will set the screw diameter
// the theoretical maximum and produce sharp corners instead of a flat edge on the head. For a flat head screw
// the drive specification must start with "flat", but the flat head options
// can be mixed in any order, for example, "flat sharp undercut" or "flat undercut sharp".
// Subsection: Nuts
// Nuts come in standard sizes and BOSL2 has tables to produce sizes for both Imperial and metric nuts.
// A nut for a given thread size is defined by its shape, width and thickness. The shape is either "hex"
// for hexagonal nuts or "square" for square nuts. For hexagonal Imperial nuts, you can choose from thickness values
// of "thin", "normal" or "thick", but the thin and thick nuts are defined only for thread sizes of 1/4 inch and above.
// .
// Metric nut standards are more complicated because ISO has a series of standards and DIN has a series of conflicting
// standards. Nuts from McMaster-Carr in the USA comply with DIN rather than ISO. Furthermore, ISO does not appear
// to specify dimensions for square nuts. For metric nuts you can specify "thin", "normal" and "thick" and the
// nut will be constructed to ISO standards (ISO 4035, ISO 4032, and ISO 4033 respectively). The DIN standard for thin
// nuts matches ISO, but the DIN normal thickness nuts are thinner than ISO nuts. You can request DIN nuts
// by specifying a thickness of "DIN" or "undersized". If you request a square nut it necessariliy derives from DIN
// instead of ISO. For most nut sizes, the nut widths match between ISO and DIN, but they do differ for M10, M12, M14 and M22.
// .
// You can of course specify nuts by giving an explicit numerical width and thickness in millimeters.
// Subsection: Tolerance
// Without tolerance requirements, screws would not fit together. The screw standards specify a
// nominal size, but the tolerance determines a range of allowed sizes based on that nominal size.
// So for example, an M10 screw with the default tolerance has an outside (major) diameter between 9.74 mm and 9.97 mm.
// The library will use the center point in the allowed range and create a screw with a diameter of 9.86 mm.
// A M10 nut at the default tolerance has a major diameter (which is the inside diameter) between 10 mm and 10.4 mm.
// Shrinking the major diameter of a screw makes the screw loose. Shrinking the major diameter of a nut, on the other hand,
// makes the hole smaller and hence makes the nut tighter. For this reason, we need a difference tolerance
// for a screw than for a nut. Screw tolerances shrink the diameter to make the screw looser whereas nut tolerances
// increase the diameter to make the nut looser. Screws modeled using this library will have dimensions consistent with the
// standards they are based on, so that they will interface properly if fabricated by an accurate method. The ISO and UTS
// systems use different tolerance designations.
// .
// For UTS screw threads the tolerance is one of "1A", "2A" or "3A", in
// order of increasing tightness. The default tolerance is "2A", which
// is the general standard for manufactured bolts.
// .
// For UTS nut threads, the tolerance is one of "1B", "2B" or "3B", in
// order of increasing tightness. The default tolerance is "2B", which
// is the general standard for manufactured nuts.
// .
// The ISO tolerances are more complicated. For both screws and nuts the ISO tolerance has the form of a number
// and letter. The letter specifies the "fundamental deviation", also called the "tolerance position", the gap
// from the nominal size. The number specifies the allowed range (variability) of the thread heights. For
// screws, the letter must be "e", "f", "g", or "h", where "e" is the loosest and "h" means no gap. The number
// for a screw tolerance must be a value from 3-9 for crest diameter and one of 4, 6, or 8 for pitch diameter.
// A tolerance "6g" specifies both pitch and crest diameter to be the same, but they can be different, with a
// tolerance like "5g6g" specifies a pitch diameter tolerance of "5g" and a crest diameter tolerance of "6g".
// Smaller numbers give a tighter tolerance. The default ISO screw tolerance is "6g".
// .
// For ISO nuts the letters specifying the fundamental deviation are upper case and must be "G" or "H" where "G"
// is loose and "H" means no gap. The number specifying the variability must range from 4-8. An allowed (loose)
// nut tolerance is "7G". The default ISO tolerance is "6H".
// .
// Clearance holes have a different tolerance system, described in {{screw_hole()}}.
// .
// If you wish to create screws at the nominal size you can set the tolerance to 0 or "none".
// Subsection: screw_info and nut_info structures
// When you make a screw or nut, information about the object such as the thread characteristics
// head and drive size, or nut thickness are placed into a data structure. The screw and nut
// modules can accept screw names, as described above, or they can accept screw structures.
// When you use a screw structure as a specification, computed values like head type and size and
// driver characteristics are fixed and cannot be changed, but values that are not computed
// like length can still be altered. If you want to create an unusual part you can hand
// generate the structure with your desired parameters to fill in values that would normally
// be produced automatically from the standard tables. So if your hardware is missing from the
// tables, or is sized differently, you can still create the part. For details on the
// screw_info and nut_info structures, see {{screw_info()}} and {{nut_info()}}.
// .
// All of the screw related modules set the variable `$screw_spec` to contain the specification
// for their screw. This means that child modules can make use of this variable to create
// mating (or identical) parts. Note that the `shaft_oversize` and `head_oversize` screw
// info fields are only inherited into modules that are the same as the parent module.
// This means that if you create an oversized screw hole and then make a screw as s child, the
// child screw will **not** inherit the oversize parameters. But a screw_hole will inherit
// oversize parameters from a parent screw_hole.
/*
http://mdmetric.com/thddata.htm#idx
Seems to show JIS has same nominal thread as others
https://www.nbk1560.com/~/media/Images/en/Product%20Site/en_technical/11_ISO%20General%20Purpose%20Metric%20Screw%20Threads.ashx?la=en
Various ISO standards here: https://www.fasteners.eu/standards/ISO/4026/
Torx values: https://www.stanleyengineeredfastening.com/-/media/web/sef/resources/docs/other/socket_screw_tech_manual_1.ashx
*/
// Section: Making Screws
// Module: screw()
// Synopsis: Creates a standard screw with optional tolerances.
// SynTags: Geom
// Topics: Threading, Screws
// See Also: screw_hole(), shoulder_screw()
// Usage:
// screw([spec], [head], [drive], [thread=], [drive_size=], [length=|l=], [thread_len=], [undersize=], [shaft_undersize=], [head_undersize=], [tolerance=], [blunt_start=], [details=], [anchor=], [atype=], [orient=], [spin=]) [ATTACHMENTS];
// Description:
// Create a screw. See [screw and nut parameters](#section-screw-and-nut-parameters) for details on
// the parameters that define a screw. The tolerance determines the dimensions of the screw based
// on ISO and ASME standards. Screws fabricated at those dimensions will mate properly with
// standard hardware. Note that the $slop argument does not affect the size of screws: it only
// adjusts screw holes. This will work fine if you are printing both parts, but if you need to mate
// printed screws to metal parts you may need to adjust the size of the screws, which you can do
// with the undersize arguments.
// .
// You can generate a screw specification from {{screw_info()}}, possibly create a modified version
// using {{struct_set()}}, and pass that in rather than giving the parameters.
// .
// Various anchor types refer to different parts of the screw, some of which are labeled below. The
// "screw" anchor type (the default) is simply the entire screw, so TOP and BOTTOM refer to the head
// end and tip respectively, and CENTER is the midpoint of the whole screw, including the head. The
// "head" anchor refers to the head alone. Both of these anchor types refer to the bounding
// cylinder for the specified screw part, except for hex heads, which anchor to a hexagonal prism.
// Figure(2D,Med,VPD = 140, VPT = [18.4209, 14.9821, -3.59741], VPR = [0, 0, 0],NoAxes):
// rpos=33;
// fsize=2.5;
// projection(cut=true) xrot(-90)screw("M8", head="socket", length=25, thread_len=10,anchor=BOT);
// right(rpos)projection(cut=true) xrot(-90)screw("M8", head="flat", length=25, thread_len=10,anchor=BOT);
// color("black"){
// stroke([[5,0],[5,10]],endcaps="arrow2",width=.3);
// back(5)right(6)text("threads",size=fsize,anchor=LEFT);
// stroke([[5,10],[5,25]],endcaps="arrow2",width=.3);
// back(10+15/2)right(6)text("shank",size=fsize,anchor=LEFT);
// stroke([[-5,0],[-5,25]],endcaps="arrow2",width=.3);
// back(25/2)right(-6)text("shaft",size=fsize,anchor=RIGHT);
// }
// sh=10.2841;
// right(rpos)
// color("black"){
// stroke([[5,0],[5,10]],endcaps="arrow2",width=.3);
// back(5)right(6)text("threads",size=fsize,anchor=LEFT);
// stroke([[5,10],[5,10+sh]],endcaps="arrow2",width=.3);
// back(10+sh/2)right(6)text("shank",size=fsize,anchor=LEFT);
// stroke([[-5,0],[-5,10+sh]],endcaps="arrow2",width=.3);
// back((10+sh)/2)right(-6)text("shaft",size=fsize,anchor=RIGHT);
// }
// Arguments:
// spec = screw specification, e.g. "M5x1" or "#8-32". See [screw naming](#subsection-screw-naming). This can also be a screw specification structure of the form produced by {{screw_info()}}.
// head = head type. See [screw heads](#subsection-screw-heads) Default: none
// drive = drive type. See [screw heads](#subsection-screw-heads) Default: none
// ---
// length / l = length of screw (in mm)
// thread = thread type or specification. See [screw pitch](#subsection-standard-screw-pitch). Default: "coarse"
// drive_size = size of drive recess to override computed value
// thread_len = length of threaded portion of screw (in mm), for making partly threaded screws. Default: fully threaded
// details = toggle some details in rendering. Default: true
// tolerance = screw tolerance. Determines actual screw thread geometry based on nominal sizing. See [tolerance](#subsection-tolerance). Default is "2A" for UTS and "6g" for ISO.
// undersize = amount to decrease screw diameter, a scalar to apply to all parts, or a 2-vector to control shaft and head. Replaces rather than adding to the head_oversize value in a screw specification.
// shaft_undersize = amount to decrease diameter of the shaft of screw; replaces rather than adding to the shaft_oversize value in a screw specification.
// head_undersize = amount to decrease the head diameter of the screw; replaces rather than adding to the head_oversize value in a screw specification.
// bevel1 = bevel bottom end of screw. Default: true
// bevel2 = bevel top end of threaded section. Default: true for fully threaded or unthreaded headless, false otherwise
// bevel = bevel both ends of the threaded section.
// blunt_start = if true and hole is threaded, create blunt start threads. Default: true
// blunt_start1 = if true and hole is threaded, create blunt start threads at bottom end.
// blunt_start2 = if true and hole is threaded, create blunt start threads top end.
// atype = anchor type, one of "screw", "head", "shaft", "threads", "shank"
// anchor = Translate so anchor point on the shaft is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `CENTER`
// spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#subsection-spin). Default: `0`
// orient = Vector to rotate top towards, after spin. See [orient](attachments.scad#subsection-orient). Default: `UP`
// Side Effects:
// `$screw_spec` is set to the spec specification structure.
// Anchor Types:
// screw = the entire screw (default)
// head = screw head (invalid for headless screws)
// shaft = screw shaft
// shank = unthreaded section of shaft (invalid if screw is fully threaded)
// threads = threaded section of screw
// Named Anchors:
// "top" = top of screw
// "bot" = bottom of screw
// "center" = center of screw
// "head_top" = top of head (same as top for headless screws)
// "head_bot" = bottom of head (same as top for headless screws)
// "head_center" = center of head (same as top for headless screws)
// "shaft_top" = top of shaft
// "shaft_bot" = bottom of shaft
// "shaft_center" = center of shaft
// "shank_top" = top of shank (invalid if screw is fully threaded)
// "shank_bot" = bottom of shank (invalid if screw is fully threaded)
// "shank_center" = center of shank (invalid if screw is fully threaded)
// "threads_top" = top of threaded portion of screw (invalid if thread_len=0)
// "threads_bot" = bottom of threaded portion of screw (invalid if thread_len=0)
// "threads_center" = center of threaded portion of screw (invalid if thread_len=0)
// Example(Med): Selected UTS (English) screws
// $fn=32;
// xdistribute(spacing=8){
// screw("#6", length=12);
// screw("#6-32", head="button", drive="torx",length=12);
// screw("#6-32,3/4", head="hex");
// screw("#6", thread="fine", head="fillister",length=12, drive="phillips");
// screw("#6", head="flat small",length=12,drive="slot");
// screw("#6-32", head="flat large", length=12, drive="torx");
// screw("#6-32", head="flat undercut",length=12);
// screw("#6-24", head="socket",length=12); // Non-standard threading
// screw("#6-32", drive="hex", drive_size=1.5, length=12);
// }
// Example(Med): A few examples of ISO (metric) screws
// $fn=32;
// xdistribute(spacing=8){
// screw("M3", head="flat small",length=12);
// screw("M3", head="button",drive="torx",length=12);
// screw("M3", head="pan", drive="phillips",length=12);
// screw("M3x1", head="pan", drive="slot",length=12); // Non-standard threading!
// screw("M3", head="flat large",length=12);
// screw("M3", thread="none", head="flat", drive="hex",length=12); // No threads
// screw("M3", head="socket",length=12);
// screw("M5,18", head="hex");
// }
// Example(Med): Demonstration of all head types for UTS screws (using pitch zero for fast preview)
// xdistribute(spacing=15){
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="none", drive="hex");
// screw("1/4", thread=0,length=8, anchor=TOP, head="none", drive="torx");
// screw("1/4", thread=0,length=8, anchor=TOP, head="none", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="none");
// }
// screw("1/4", thread=0, length=8, anchor=TOP, head="hex");
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="socket", drive="hex");
// screw("1/4", thread=0,length=8, anchor=TOP, head="socket", drive="torx");
// screw("1/4", thread=0,length=8, anchor=TOP, head="socket");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="socket ribbed", drive="hex",$fn=32);
// screw("1/4", thread=0,length=8, anchor=TOP, head="socket ribbed", drive="torx",$fn=32);
// screw("1/4", thread=0,length=8, anchor=TOP, head="socket ribbed",$fn=24);
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="button", drive="hex");
// screw("1/4", thread=0,length=8, anchor=TOP, head="button", drive="torx");
// screw("1/4", thread=0,length=8, anchor=TOP, head="button");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="round", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="round", drive="phillips");
// screw("1/4", thread=0,length=8, anchor=TOP, head="round");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="pan", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="pan", drive="phillips");
// screw("1/4", thread=0,length=8, anchor=TOP, head="pan");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="fillister", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="fillister", drive="phillips");
// screw("1/4", thread=0,length=8, anchor=TOP, head="fillister");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat", drive="phillips");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat", drive="hex");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat", drive="torx");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat large");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat small");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat undercut", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat undercut", drive="phillips");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat undercut");
// }
// ydistribute(spacing=15){
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat 100", drive="slot");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat 100", drive="phillips");
// screw("1/4", thread=0,length=8, anchor=TOP, head="flat 100");
// }
// }
// Example(Med): Demonstration of all head types for metric screws without threading.
// xdistribute(spacing=15){
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="none", drive="hex");
// screw("M6x0", length=8, anchor=TOP, head="none", drive="torx");
// screw("M6x0", length=8, anchor=TOP, head="none", drive="slot");
// screw("M6x0", length=8, anchor=TOP);
// }
// screw("M6x0", length=8, anchor=TOP, head="hex");
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="socket", drive="hex");
// screw("M6x0", length=8, anchor=TOP, head="socket", drive="torx");
// screw("M6x0", length=8, anchor=TOP, head="socket");
// }
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="socket ribbed", drive="hex", $fn=32);
// screw("M6x0", length=8, anchor=TOP, head="socket ribbed", drive="torx", $fn=32);
// screw("M6x0", length=8, anchor=TOP, head="socket ribbed", $fn=32);
// }
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="pan", drive="slot");
// screw("M6x0", length=8, anchor=TOP, head="pan", drive="phillips");
// screw("M6x0", length=8, anchor=TOP, head="pan", drive="torx");
// screw("M6x0", length=8, anchor=TOP, head="pan");
// screw("M6x0", length=8, anchor=TOP, head="pan flat");
// }
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="button", drive="hex");
// screw("M6x0", length=8, anchor=TOP, head="button", drive="torx");
// screw("M6x0", length=8, anchor=TOP, head="button");
// }
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="cheese", drive="slot");
// screw("M6x0", length=8, anchor=TOP, head="cheese", drive="phillips");
// screw("M6x0", length=8, anchor=TOP, head="cheese", drive="torx");
// screw("M6x0", length=8, anchor=TOP, head="cheese");
// }
// ydistribute(spacing=15){
// screw("M6x0", length=8, anchor=TOP, head="flat", drive="phillips");
// screw("M6x0", length=8, anchor=TOP, head="flat", drive="slot");
// screw("M6x0", length=8, anchor=TOP, head="flat", drive="hex");
// screw("M6x0", length=8, anchor=TOP, head="flat", drive="torx");
// screw("M6x0", length=8, anchor=TOP, head="flat small");
// screw("M6x0", length=8, anchor=TOP, head="flat large");
// }
// }
// Example: The three different English (UTS) screw tolerances (labeled on their heads)
// module label(val)
// {
// difference(){
// children();
// yflip()linear_extrude(height=.35) text(val,valign="center",halign="center",size=8);
// }
// }
// $fn=64;
// xdistribute(spacing=15){
// label("1") screw("1/4-20,5/8", head="hex",orient=DOWN,atype="head", anchor=TOP,tolerance="1A"); // Loose
// label("2") screw("1/4-20,5/8", head="hex",orient=DOWN,atype="head", anchor=TOP,tolerance="2A"); // Standard
// label("3") screw("1/4-20,5/8", head="hex",orient=DOWN,atype="head", anchor=TOP,tolerance="3A"); // Tight
// }
// Example(2D,NoAxes): This example shows the gap between nut and bolt at the loosest tolerance for UTS. This gap is what enables the parts to mesh without binding and is part of the definition for standard metal hardware. Note that this gap is part of the standard definition for the metal hardware, not the 3D printing adjustment provided by the $slop parameter.
// $fn=32;
// projection(cut=true)xrot(-90){
// screw("1/4-20,3/8", head="hex",orient=UP,anchor=BOTTOM,tolerance="1A");
// down(INCH*1/20*1.5) nut("1/4-20", thickness=8, nutwidth=0.5*INCH, tolerance="1B");
// }
// Example: Here is a screw with nonstandard threading and a weird head size, which we create by modifying the screw structure:
// spec = screw_info("M6x2,12",head="socket");
// newspec = struct_set(spec,["head_size",20,"head_height",3]);
// screw(newspec);
// Example: A bizarre custom screw with nothing standard about it. If your screw is very strange, consider setting tolerance to zero so you get exactly the screw you defined. You'll need to create your own clearance between mating threads in this case.
// spec = [["system","ISO"],
// ["type","screw_info"],
// ["pitch", 2.3],
// ["head", "flat"],
// ["head_size", 20],
// ["head_size_sharp", 22],
// ["head_angle", 60],
// ["diameter",12],
// ["length",22]];
// screw(spec,tolerance=0);
function _get_spec(spec, needtype, origin, thread, // common parameters
head, drive, drive_size, // screw parameters
shape, thickness // nut parameters
) =
assert(needtype=="screw_info" || needtype=="nut_info")
assert(is_undef(thickness) || (is_num(thickness) && thickness>0) ||
in_list(_downcase_if_str(thickness),["thin","normal","thick","undersized","din"]),
"thickness must be a positive number of one of \"thin\", \"thick\", \"normal\", \"undersized\", or \"DIN\"")
assert(!(is_undef(spec) && is_undef($screw_spec)), "No screw spec given and no parent spec available to inherit")
let(
spec=is_undef(spec) ? $screw_spec : spec,
spec_origin = is_struct(spec) ? struct_val(spec,"origin") : undef
)
assert(is_string(spec) || is_struct(spec), "Screw/nut specification must be a string or struct")
let(
specname = is_struct(spec) ? struct_val(spec,"name") : undef,
name = is_string(spec) ? spec
: struct_val(spec,"type") != needtype ? // if we switch between screw and nut we need a name
let(specname=struct_val(spec,"name"))
assert(is_string(specname),
"Parent screw_info or nut_info structure doesn't have a valid name, but a name is needed when child is of a different type")
specname
: undef,
p = is_struct(spec) ? struct_val(spec,"pitch") : undef,
thread = is_def(name) ? thread
// If the origin of the struct is a hole with pitch zero and we are making a screw, try to find a nonzero pitch
: spec_origin=="screw_hole" && origin!="screw_hole" && p==0 && is_string(specname) ?
let(temp_info = screw_info(specname,thread))
struct_val(temp_info,"pitch")
// : spec_origin=="screw_hole" && origin=="screw_hole" && all_positive([p]) ? p
// : origin=="screw_hole" && is_undef(thread) ? 0
: thread
)
is_def(name) ? (needtype=="screw_info" ? screw_info(name,_origin=origin, thread= origin=="screw_hole" ? default(thread,true) : thread,
head=head, drive=drive, drive_size=drive_size)
: nut_info(name,_origin=origin, thread=thread, shape=shape, thickness=thickness))
:
assert(in_list(struct_val(spec,"type"), ["nut_info","screw_info"]), "Screw/nut spec is invalid struct type")
assert(is_undef(thread) || thread=="none" || thread==false || thread==true || is_num(thread),
str("Thread type applied to struct specification must be numeric, \"none\" or false but got ",thread))
assert(is_undef(thickness) || is_num(thickness), str("thickness applied to struct specification must be numeric but is ",thickness))
assert(is_undef(head) || head=="none", str("The only head type allowed with struct specifications is \"none\" but got ",head))
assert(num_defined([drive,drive_size])==0, "You cannot change drive or drive_size when using a struct specification")
assert(is_undef(shape), "You cannot change nut shape when using a struct specification")
let(
spec = _struct_reset(spec,
[
["origin", origin],
if (origin=="screw") ["counterbore",0],
if (head=="none") ["head","none"],
if (head=="none") ["drive","none"],
if (thread==false || thread=="none") ["pitch",0]
else if (thread!=true) ["pitch",thread],
["thickness", thickness],
], grow=true),
inherit = is_undef(spec_origin) || spec_origin==origin
)
inherit ? spec
: struct_remove(spec, ["shaft_oversize","head_oversize"]);
function _struct_reset(s, keyval, grow=true) =
let(
good = [for(kv=keyval) (grow || is_def(struct_val(s,kv[0]))) && is_def(kv[1])]
)
struct_set(s,flatten(bselect(keyval,good)));
function _nominal_diam(spec) = struct_val(spec,"diameter")+default(struct_val(spec,"shaft_oversize"),0);
function screw(spec, head, drive, thread, drive_size,
length, l, thread_len, tolerance, details=true,
undersize, shaft_undersize, head_undersize,
atype="screw",anchor, spin=0, orient=UP,
_shoulder_diam=0, _shoulder_len=0,
bevel,bevel1,bevel2,bevelsize,
blunt_start,blunt_start1, blunt_start2,
_internal=false, _counterbore, _teardrop=false)
= no_function("screw");
module screw(spec, head, drive, thread, drive_size,
length, l, thread_len, tolerance, details=true,
undersize, shaft_undersize, head_undersize,
atype="screw",anchor, spin=0, orient=UP,
_shoulder_diam=0, _shoulder_len=0,
bevel,bevel1,bevel2,bevelsize,
blunt_start,blunt_start1, blunt_start2,
_internal=false, _counterbore, _teardrop=false)
{
tempspec = _get_spec(spec, "screw_info", _internal ? "screw_hole" : "screw",
thread=thread, head=head, drive=drive, drive_size=drive_size);
undersize = is_num(undersize) ? [undersize,undersize]
: undersize;
dummyA=assert(is_undef(undersize) || is_vector(undersize,2), "Undersize must be a scalar or 2-vector")
assert(is_undef(undersize) || num_defined([shaft_undersize, head_undersize])==0,
"Cannot combine \"undersize\" with other more specific undersize parameters")
assert(is_bool(_teardrop) ||_teardrop=="max" || all_nonnegative([_teardrop]), str("Invalid teardrop parameter",_teardrop));
_teardrop = _teardrop==true ? .05 : _teardrop; // set teardrop default
shaft_undersize = first_defined([shaft_undersize, undersize[0]]);
head_undersize = first_defined([head_undersize, undersize[1]]);
dummyB=assert(is_undef(shaft_undersize) || is_finite(shaft_undersize), "shaft_undersize must be a number")
assert(is_undef(head_undersize) || is_finite(head_undersize), "head_undersize must be a number")
assert(is_undef(_counterbore) || is_bool(_counterbore) || (is_finite(_counterbore) && _counterbore>=0),
"Counterbore must be a nonnegative number of boolean");
l = one_defined([l,length],"l,length",dflt=undef);
_counterbore = _counterbore==true ? struct_val(tempspec,"head_height")
: _counterbore==false ? undef
: _counterbore;
head = struct_val(tempspec,"head");
headless = head=="none";
flathead = is_def(head) && starts_with(head,"flat");
reset_headsize = _internal && flathead ? struct_val(tempspec,"head_size_sharp") : undef;
spec=_struct_reset(tempspec,[
["length", l],
["shaft_oversize", u_mul(-1,shaft_undersize)],
["head_oversize", u_mul(-1,head_undersize)],
["counterbore", _counterbore],
["thread_len", thread_len],
["head_size", reset_headsize],
]);
dummy = _validate_screw_spec(spec);
$screw_spec = spec;
pitch = struct_val(spec, "pitch") ;
threadspec = pitch==0 ? undef : thread_specification(spec, internal=_internal, tolerance=tolerance);
nominal_diam = _nominal_diam(spec);
d_major = pitch==0 ? nominal_diam : mean(struct_val(threadspec, "d_major"));
length = struct_val(spec,"length");
counterbore = default(struct_val(spec,"counterbore"),0);
user_thread_len = struct_val(spec,"thread_len");
dummyC = assert(in_list(atype,["shaft","head","shank","threads","screw","shoulder"]),str("Unknown anchor type: \"",atype,"\""))
assert(is_finite(length) && length>0, "Must specify positive screw length")
assert(is_finite(_shoulder_len) && _shoulder_len>=0, "Must specify a nonegative shoulder length")
assert(is_finite(_shoulder_diam) && _shoulder_diam>=0, "Must specify nonnegative shoulder diameter")
assert(is_undef(user_thread_len) || (is_finite(user_thread_len) && user_thread_len>=0), "Must specify nonnegative thread length");
sides = max(pitch==0 ? 3 : 12, segs(nominal_diam/2));
rad_scale = _internal? (1/cos(180/sides)) : 1;
islop = _internal ? 4*get_slop() : 0;
head_height = headless || flathead ? 0
: counterbore==true || is_undef(counterbore) || counterbore==0 ? struct_val(spec, "head_height")
: counterbore;
head_diam = struct_val(spec, "head_size",0) + struct_val(spec, "head_oversize",0);
flat_height = !flathead ? 0
: let( given_height = struct_val(spec, "head_height"))
all_positive(given_height) ? given_height
: (struct_val(spec,"head_size_sharp")+struct_val(spec,"head_oversize",0)-d_major*rad_scale-islop)/2/tan(struct_val(spec,"head_angle")/2);
flat_cbore_height = flathead && is_num(counterbore) ? counterbore : 0;
blunt_start1 = first_defined([blunt_start1,blunt_start,true]);
blunt_start2 = first_defined([blunt_start2,blunt_start,true]);
shoulder_adj = _shoulder_len>0 ? flat_height:0; // Adjustment because flathead height doesn't count toward shoulder length
shoulder_full = _shoulder_len==0 ? 0 : _shoulder_len + flat_height;
shank_len = is_def(user_thread_len) ? length - user_thread_len - (_shoulder_len==0?flat_height:0) : 0;
thread_len = is_def(user_thread_len) ? user_thread_len
: length - (_shoulder_len==0?flat_height:0);
dummyD = assert(!(atype=="shank" && shank_len==0), "Specified atype of \"shank\" but screw has no shank (thread_len not given or it equals shaft length)")
assert(!(atype=="shoulder" && _shoulder_len==0), "Specified atype of \"shoulder\" but screw has no shoulder")
assert(!(atype=="threads" && thread_len==0), "Specified atype of \"threads\" but screw has no threaded part (thread_len=0)")
assert(!(atype=="head" && headless), "You cannot anchor headless screws with atype=\"head\"");
eps_gen = 0.01;
eps_shoulder = headless && !_internal ? 0 : eps_gen;
eps_shank = headless && !_internal && _shoulder_len==0 ? 0 : eps_gen;
eps_thread = headless && !_internal && shank_len==0 && _shoulder_len==0 ? 0 : eps_gen;
dummyL = assert(_shoulder_len>0 || is_undef(flat_height) || flat_height < length,
str("Length of screw (",length,") is shorter than the flat head height (",flat_height,")"));
offset = atype=="head" ? (-head_height+flat_height-flat_cbore_height)/2
: atype=="shoulder" ? _shoulder_len/2 + flat_height
: atype=="shaft" ? _shoulder_len + (length+flat_height+shoulder_adj)/2
: atype=="shank" ? _shoulder_len + (length-thread_len+flat_height+shoulder_adj)/2
: atype=="threads" ? _shoulder_len + shoulder_adj + length-thread_len + thread_len/2
: atype=="screw" ? (length-head_height+_shoulder_len+shoulder_adj-flat_cbore_height)/2
: assert(false,"Unknown atype");
dummyM = //assert(!headless || !in_list(anchor,["head_top","head_bot","head_center"]), str("Anchor \"",anchor,"\" not allowed for headless screw"))
assert(shank_len>0 || !in_list(anchor,["shank_top","shank_bot","shank_center"]),
str("Screw has no unthreaded shank so anchor \"",anchor,"\" is not allowed"));
anchor_list = [
named_anchor("top", [0,0,offset+head_height+flat_cbore_height]),
named_anchor("bot", [0,0,-length-shoulder_full+offset]),
named_anchor("center", [0,0, -length/2 - shoulder_full/2 + head_height/2 + offset]),
named_anchor("head_top", [0,0,head_height+offset]),
named_anchor("head_bot", [0,0,-flat_height+offset]),
named_anchor("head_center", [0,0,(head_height-flat_height)/2+offset]),
if (_shoulder_len>0) named_anchor("shoulder_top", [0,0,offset-flat_height]),
if (_shoulder_len>0) named_anchor("shoulder_bot", [0,0,offset-shoulder_full]),
if (_shoulder_len>0) named_anchor("shoulder_center", [0,0,offset-flat_height-_shoulder_len/2]),
named_anchor("shaft_top", [0,0,-_shoulder_len-flat_height+offset]),
named_anchor("shaft_bot", [0,0,-length-shoulder_full+offset]),
named_anchor("shaft_center", [0,0,(-_shoulder_len-flat_height-length-shoulder_full)/2+offset]),
if (shank_len>0) named_anchor("shank_top", [0,0,-_shoulder_len-flat_height+offset]),
if (shank_len>0) named_anchor("shank_bot", [0,0,-shank_len-_shoulder_len-flat_height+offset]),
if (shank_len>0) named_anchor("shank_center", [0,0,-shank_len/2-_shoulder_len-flat_height+offset]),
named_anchor("threads_top", [0,0,-shank_len-_shoulder_len-flat_height+offset]),
named_anchor("threads_bot", [0,0,-length-shoulder_full+offset]),
named_anchor("threads_center", [0,0,(-shank_len-length-_shoulder_len-shoulder_full-flat_height)/2+offset])
];
vnf = head=="hex" && atype=="head" && counterbore==0 ? linear_sweep(hexagon(id=head_diam*rad_scale),height=head_height,center=true) : undef;
head_diam_full = head=="hex" ? 2*head_diam/sqrt(3) : head_diam;
attach_d = in_list(atype,["threads","shank","shaft"]) ? d_major
: atype=="screw" ? max(d_major,_shoulder_diam,default(head_diam_full,0))
: atype=="shoulder" ? _shoulder_diam
: is_def(vnf) ? undef
: head_diam_full;
attach_l = atype=="shaft" ? length-(_shoulder_len>0?0:flat_height)
: atype=="shoulder" ? _shoulder_len
: atype=="shank" ? shank_len
: atype=="threads" ? thread_len
: atype=="screw" ? length+head_height+shoulder_full + flat_cbore_height
: is_def(vnf) ? undef
: head_height+flat_height+flat_cbore_height;
bevelsize = default(bevelsize, d_major/12);
bevel1 = first_defined([bevel1,bevel,true]);
bevel2 = first_defined([bevel2,bevel,headless && _shoulder_len==0 && shank_len==0]);
attachable(
vnf = vnf,
d = u_add(u_mul(attach_d, rad_scale), islop),
l = attach_l,
orient = orient,
anchor = anchor,
spin = spin,
anchors=anchor_list)
{
up(offset)
difference(){
union(){
screw_head(spec,details,counterbore=counterbore,flat_height=flat_height,
slop=islop,teardrop=_teardrop);
if (_shoulder_len>0)
up(eps_shoulder-flat_height){
if (_teardrop!=false) //////
teardrop(d=_shoulder_diam*rad_scale+islop,cap_h=is_num(_teardrop) ? (_shoulder_diam*rad_scale+islop)/2*(1+_teardrop):undef,
h=_shoulder_len+eps_shoulder, anchor=FRONT, orient=BACK, $fn=sides);
else
cyl(d=_shoulder_diam*rad_scale+islop, h=_shoulder_len+eps_shoulder, anchor=TOP, $fn=sides, chamfer1=details ? _shoulder_diam/30:0);
}
if (shank_len>0 || pitch==0){
L = pitch==0 ? length - (_shoulder_len==0?flat_height:0) : shank_len;
bevsize = (_internal ? -1 : 1)*bevelsize;
bev1 = pitch!=0 ? 0
: bevel1==true ? bevsize
: bevel1==false ? 0
: bevel1=="reverse" ? -bevsize
: bevel1;
bev2 = pitch!=0 ? 0
: bevel2==true ? bevsize
: bevel2==false ? 0
: bevel2=="reverse" ? -bevsize
: bevel2;
down(_shoulder_len+flat_height-eps_shank)
if (_teardrop!=false) ///////
teardrop(d=d_major*rad_scale+islop, cap_h=is_num(_teardrop) ? (d_major*rad_scale+islop)/2*(1+_teardrop) : undef,
h=L+eps_shank, anchor=FRONT, orient=BACK, $fn=sides, chamfer1=bev1, chamfer2=bev2);
else
cyl(d=d_major*rad_scale+islop, h=L+eps_shank, anchor=TOP, $fn=sides, chamfer1=bev1, chamfer2=bev2);
}
if (thread_len>0 && pitch>0){
down(_shoulder_len+flat_height+shank_len-eps_thread)
threaded_rod([mean(struct_val(threadspec, "d_minor")),
mean(struct_val(threadspec, "d_pitch")),
d_major],
pitch = struct_val(threadspec, "pitch"),
l=thread_len+eps_thread, left_handed=false, internal=_internal,
bevel1=bevel1,
bevel2=bevel2,teardrop=_teardrop,
blunt_start=blunt_start, blunt_start1=blunt_start1, blunt_start2=blunt_start2,
$fn=sides, anchor=TOP);
}
}
if (!_internal) _driver(spec);
}
children();
}
}
// Module: screw_hole()
// Synopsis: Creates a screw hole.
// SynTags: Geom
// Topics: Threading, Screws
// See Also: screw()
// Usage:
// screw_hole([spec], [head], [thread=], [length=|l=], [oversize=], [hole_oversize=], [teardrop=], [head_oversize], [tolerance=], [$slop=], [blunt_start=], [anchor=], [atype=], [orient=], [spin=]) [ATTACHMENTS];
// Description:
// Create a screw hole mask. See [screw and nut parameters](#section-screw-and-nut-parameters) for details on the parameters that define a screw.
// The screw hole can be threaded to receive a screw or it can be an unthreaded clearance hole.
// The tolerance determines the dimensions of the screw
// based on ISO and ASME standards. Screws fabricated at those dimensions will mate properly with standard hardware.
// The $slop argument makes the hole larger by 4*$slop to account for printing overextrusion. It defaults to 0.
// .
// You can generate a screw specification from {{screw_info()}}, possibly create a modified version, and pass that in rather than giving the parameters.
// .
// The tolerance should be a nut tolerance for a threaded hole or a clearance hole tolerance for clearance holes.
// For clearance holes, the UTS tolerances are "normal", "loose" and "close". ASME also specifies the same naming for metric clearance holes.
// However, ISO gives "fine", "medium" and "coarse" instead. This function accepts all of these in either system. It also takes "tight" to be equivalent to "close",
// even though no standard suggests it, because it's a natural opposite of "loose". The official tolerance designations for ISO are "H12" for "fine", "H13" for "medium"
// and "H14" for "coarse". These designations will also work, but only for metric holes. You can also set tolerance to 0 or "none" to produce holes at the nominal size.
// .
// If you want to produce holes for tapping you can use a tolerance of "tap". This produces a hole of the nominal screw diameter reduced by the thread pitch. You may still
// need to adjust $slop for best results. Some people screw machine screws directly into plastic without tapping. This works better with a somewhat larger hole, so
// a tolerance of "self tap" produces such a hole. Note that this tolerance also makes the default bevel2=true to bevel the top, which makes it much easier
// to start the screw. The "self tap" tolerance subtracts `0.72 * pitch` when pitch is below 1mm, `0.6 * pitch` when the pitch is over 1.5mm, and it interpolates between.
// It was tested in PLA with a Prusa MK3S and $slop=0.05 and worked on UTS screws from #2 up to 1/2 inch.
// .
// The counterbore parameter adds a cylindrical clearance hole above the screw shaft. For flat heads it extends above the flathead and for other screw types it
// replaces the head with a cylinder large enough in diameter for the head to fit. For a flat head you must specify the length of the counterbore. For other heads you can
// set counterbore to true and it will be sized to match the head height. The counterbore will extend 0.01 above the TOP of the hole mask to ensure no
// problems with differences. Note that the counterbore defaults to true for non-flathead screws. If you want the actual head shape to appear, set counterbore to zero.
// .
// For 3d printing circular holes can be problematic. One solution is to use octagonal holes, setting $fn=8. Another option is to use a teardrop hole, which
// can be accomplished by setting `teardrop=true`. The point of the teardrop will point in the Y direction (BACK) so you will need to ensure that you orient it
// correctly in your final model.
// .
// Anchoring for screw_hole() is the same as anchoring for {{screw()}}, with all the same anchor types and named anchors. If you specify a counterbore it is treated as
// the "head", or in the case of flat heads, it becomes part of the head. If you make a teardrop hole the point is ignored for purposes of anchoring.
// Arguments:
// spec = screw specification, e.g. "M5x1" or "#8-32". See [screw naming](#subsection-screw-naming). This can also be a screw specification structure of the form produced by {{screw_info()}}.
// head = head type. See [screw heads](#subsection-screw-heads) Default: none
// ---
// thread = thread type or specification for threaded masks, true to make a threaded mask with the standard threads, or false to make an unthreaded mask. See [screw pitch](#subsection-standard-screw-pitch). Default: false
// teardrop = If true, adds a teardrop profile to the hole for 3d printability of horizontal holes. If numeric, specifies the proportional extra distance of the teardrop flat top from the screw center, or set to "max" for a pointed teardrop. Default: false
// oversize = amount to increase diameter of the screw hole (hole and countersink). A scalar or length 2 vector. Default: use computed tolerance
// hole_oversize = amount to increase diameter of the hole. Overrides the use of tolerance and replaces any settings given in the screw specification.
// head_oversize = amount to increase diameter of head. Overrides the user of tolerance and replaces any settings given in the screw specification.
// length / l= length of screw (in mm)
// counterbore = set to length of counterbore, or true to make a counterbore equal to head height. Default: false for flat heads and headless, true otherwise
// tolerance = threading or clearance hole tolerance. For internal threads, detrmines actual thread geometry based on nominal sizing. See [tolerance](#subsection-tolerance). Default is "2B" for UTS and 6H for ISO. For clearance holes, determines how much clearance to add. Default is "normal".
// bevel = if true create bevel at both ends of hole. Default: see below
// bevel1 = if true create bevel at bottom end of hole. Default: false
// bevel2 = if true create bevel at top end of hole. Default: true when tolerance="self tap", false otherwise
// blunt_start = if true and hole is threaded, create blunt start threads. Default: true
// blunt_start1 = if true and hole is threaded, create blunt start threads at bottom end.
// blunt_start2 = if true and hole is threaded, create blunt start threads top end.
// $slop = add extra gap to account for printer overextrusion. Default: 0
// atype = anchor type, one of "screw", "head", "shaft", "threads", "shank"
// anchor = Translate so anchor point on the shaft is at origin (0,0,0). See [anchor](attachments.scad#subsection-anchor). Default: `CENTER`
// spin = Rotate this many degrees around the Z axis after anchor. See [spin](attachments.scad#subsection-spin). Default: `0`
// orient = Vector to rotate top towards, after spin. See [orient](attachments.scad#subsection-orient). Default: `UP`
// Side Effects:
// `$screw_spec` is set to the spec specification structure.
// Anchor Types:
// screw = the entire screw (default)
// head = screw head (invalid for headless screws)
// shaft = screw shaft
// shank = unthreaded section of shaft (invalid if screw is fully threaded)
// threads = threaded section of screw
// Named Anchors:
// "top" = top of screw
// "bot" = bottom of screw
// "center" = center of screw
// "head_top" = top of head (invalid for headless screws)
// "head_bot" = bottom of head (invalid for headless screws)
// "head_center" = center of head (invalid for headless screws)
// "shaft_top" = top of shaft
// "shaft_bot" = bottom of shaft
// "shaft_center" = center of shaft
// "shank_top" = top of shank (invalid if screw is fully threaded)
// "shank_bot" = bottom of shank (invalid if screw is fully threaded)
// "shank_center" = center of shank (invalid if screw is fully threaded)
// "threads_top" = top of threaded portion of screw (invalid if thread_len=0)
// "threads_bot" = bottom of threaded portion of screw (invalid if thread_len=0)
// "threads_center" = center of threaded portion of screw (invalid if thread_len=0)
// Example: Counterbored clearance hole
// diff()
// cuboid(20)
// attach(TOP)
// screw_hole("1/4-20,.5",head="socket",counterbore=5,anchor=TOP);
// Example: Clearance hole for flathead
// diff()
// cuboid(20)
// attach(TOP)
// screw_hole("1/4-20,.5",head="flat",counterbore=0,anchor=TOP);
// Example: Threaded hole, with inward bevel at the base
// bottom_half()
// diff()
// cuboid(20)
// attach(FRONT)
// screw_hole("M16,15",anchor=TOP,thread=true,bevel1="reverse");
function screw_hole(spec, head, thread, oversize, hole_oversize, head_oversize,
length, l, thread_len, tolerance=undef, counterbore, teardrop=false,
bevel, bevel1, bevel2, blunt_start, blunt_start1, blunt_start2,
atype="screw",anchor=CENTER,spin=0, orient=UP)
= no_function("screw_hole");
module screw_hole(spec, head, thread, oversize, hole_oversize, head_oversize,
length, l, thread_len, tolerance=undef, counterbore, teardrop=false,
bevel, bevel1, bevel2, blunt_start, blunt_start1, blunt_start2,
atype="screw",anchor=CENTER,spin=0, orient=UP)
{
screwspec = _get_spec(spec, "screw_info", "screw_hole",
thread=thread, head=head);
bevel1 = first_defined([bevel1,bevel,false]);
bevel2 = first_defined([bevel2,bevel,tolerance=="self tap"]);
thread = default(thread,false);
checkhead = struct_val(screwspec,"head");
default_counterbore = checkhead=="none" || starts_with(checkhead,"flat") ? 0 : true;
counterbore = default(counterbore, default_counterbore);
dummy = _validate_screw_spec(screwspec);
threaded = thread==true || (is_finite(thread) && thread>0) || (is_undef(thread) && struct_val(screwspec,"pitch")>0);
oversize = force_list(oversize,2);
hole_oversize = first_defined([hole_oversize, oversize[0],struct_val(screwspec,"shaft_oversize")]);
head_oversize = first_defined([head_oversize, oversize[1],struct_val(screwspec,"head_oversize")]);
if (threaded || is_def(hole_oversize) || tolerance==0 || tolerance=="none") {
default_tag("remove")
screw(spec,head=head,thread=thread,shaft_undersize=u_mul(-1,hole_oversize), head_undersize=u_mul(-1,head_oversize),
blunt_start=blunt_start, blunt_start1=blunt_start1, blunt_start2=blunt_start2,
length=length,l=l,thread_len=thread_len, tolerance=tolerance, _counterbore=counterbore,
bevel1=bevel1, bevel2=bevel2,
atype=atype, anchor=anchor, spin=spin, orient=orient, _internal=true, _teardrop=teardrop)
children();
}
else {
tolerance = default(tolerance, "normal");
pitch = struct_val(screwspec,"pitch");
dummy3 = assert((downcase(tolerance) != "tap" && downcase(tolerance)!="self tap") || pitch!=0,
"\"tap\" clearance requires a pitch size, but pitch is set to zero");
// UTS clearances from ASME B18.2.8
UTS_clearance = [
[ // Close fit
[0.1120 * INCH,0.008*INCH],
[0.1250 * INCH, 1/64*INCH],
[7/16 * INCH, 1/64*INCH],
[1/2 * INCH, 1/32*INCH],
[1.25 * INCH, 1/32*INCH],
[1.375 * INCH, 1/16*INCH]
],
[ // Normal fit
[0.1120 * INCH, 1/64*INCH],
[0.1250 * INCH, 1/32*INCH],
[7/16 * INCH, 1/32*INCH],
[1/2 * INCH, 1/16*INCH],
[7/8 * INCH, 1/16*INCH],
[1 * INCH, 3/32*INCH],
[1.25 * INCH, 3/32*INCH],
[1.375 * INCH, 1/8*INCH],
],
[ // Loose fit
[0.1120 * INCH, 1/32*INCH],
[0.1250 * INCH, 3/64*INCH],
[7/16 * INCH, 3/64*INCH],
[1/2 * INCH, 7/64*INCH],
[5/8 * INCH, 7/64*INCH],
[3/4 * INCH, 5/32*INCH],
[1 * INCH, 5/32*INCH],
[1.125 * INCH, 3/16*INCH],
[1.25 * INCH, 3/16*INCH],
[1.375 * INCH,15/64*INCH]
]
];
// ISO clearances appear in ASME B18.2.8 and ISO 273
ISO_clearance = [
[ // Close, Fine, H12
[2.5, 0.1],
[3.5, 0.2],
[4, 0.3],
[5, 0.3],
[6, 0.4],
[8, 0.4],
[10, 0.5],
[12, 1],
[42, 1],
[48, 2],
[80, 2],
[90, 3],
[100, 4],
],
[ // Normal, Medium, H13
[1.6, 0.2],
[2, 0.4],
[3.5, 0.4],
[4, 0.5],
[5, 0.5],
[6, 0.6],
[8, 1],
[10, 1],
[12, 1.5],
[16, 1.5],
[20, 2],
[24, 2],
[30, 3],
[42, 3],
[48, 4],
[56, 6],
[90, 6],
[100, 7],
],
[ // Loose, Coarse, H14
[1.6, 0.25],
[2, 0.3],
[3, 0.6],
[3.5, 0.7],
[4, 0.8],
[5, 0.8],
[6, 1],
[8, 2],
[10, 2],
[12, 2.5],
[16, 2.5],
[20, 4],
[24, 4],
[30, 5],
[36, 6],
[42, 6],
[48, 8],
[56, 10],
[72, 10],
[80, 11],
[90, 11],
[100,12],
]
];
tol_ind = in_list(downcase(tolerance), ["close", "fine", "tight"]) ? 0
: in_list(downcase(tolerance), ["normal", "medium", "tap", "self tap"]) ? 1
: in_list(downcase(tolerance), ["loose", "coarse"]) ? 2
: in_list(tolerance, ["H12","H13","H14"]) ?
assert(struct_val(screwspec,"system")=="ISO", str("Hole tolerance ", tolerance, " only allowed with ISO screws"))
parse_int(substr(tolerance,1))-12
: assert(false,str("Unknown tolerance ",tolerance, " for unthreaded clearance hole. Use one of \"close\", \"normal\", or \"loose\""));
tol_table = struct_val(screwspec,"system")=="UTS" ? UTS_clearance[tol_ind] : ISO_clearance[tol_ind];
tol_gap = lookup(_nominal_diam(screwspec), tol_table);
// If we got here, hole_oversize is undefined and oversize is undefined
hole_oversize = downcase(tolerance)=="tap" ? -pitch
: downcase(tolerance)=="self tap" ? -pitch*lookup(pitch,[[1,0.72],[1.5,.6]])
: tol_gap;
head_oversize = default(head_oversize, tol_gap);
default_tag("remove")
screw(spec,head=head,thread=0,shaft_undersize=-hole_oversize, head_undersize=-head_oversize,
length=length,l=l,thread_len=thread_len, _counterbore=counterbore,
bevel1=bevel1, bevel2=bevel2, bevelsize=pitch>0?pitch:undef,
atype=atype, anchor=anchor, spin=spin, orient=orient, _internal=true, _teardrop=teardrop)
children();
}
}
// Module: shoulder_screw()
// Synopsis: Creates a shoulder screw.
// SynTags: Geom
// Topics: Threading, Screws
// See Also: screw(), screw_hole()
// Usage:
// shoulder_screw(s, d, length, [head=], [thread_len=], [tolerance=], [head_size=], [drive=], [drive_size=], [thread=], [undersize=], [shaft_undersize=], [head_undersize=], [shoulder_undersize=],[atype=],[anchor=],[orient=],[spin=]) [ATTACHMENTS];