-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotate.scad
More file actions
1793 lines (1693 loc) · 83.7 KB
/
annotate.scad
File metadata and controls
1793 lines (1693 loc) · 83.7 KB
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: annotate.scad
// Functions and modules for annotating OpenSCAD models
// Annotations are in-scene flyout blocks of information on specific models.
//
// FileSummary: Functions and modules for annotating models
// Includes:
// include <openscad_annotations/annotate.scad>
// MECH_NUMBER = "EX";
// Continues:
// *(Some aspects of annotation depend on the use of a file-wide global value
// `MECH_NUMBER`, which uniquely identifies the entire mechanism being
// modeled; eg, "002". OpenSCAD doesn't offer the facility to query the filename
// being operated on for reasons beyond knowing, and this variable must be set somehow. Ideally
// the `MECH_NUMBER` value is set in your model just after where `openscad_annotations/annotate.scad`
// is included, as shown here. You should set it to whatever best makes sense to your project; within
// this documentation, the value `"EX"` is used, to signal it is an example.)*
//
include <openscad_annotations/common.scad>
include <openscad_annotations/flyout.scad>
/// Section: Global Constants
///
/// Constant: MECH_NUMBER
/// Description:
/// The global `MECH_NUMBER` identifies the model-id of the mechanism in a given .scad file. You **should** set this constant within your
/// model file somewhere for part-numbers to work well. You may only set `MECH_NUMBER` once.
/// .
/// Throughout the annotation.scad documentation below, and in the examples, we use `EX` as a `MECH_NUMBER`
/// (to indicate that it is an "example").
/// .
/// Currently: `false`, so as to indicate not-set.
MECH_NUMBER = false;
/// Constant: EXPAND_PARTS
/// Constant: $_EXPAND_PARTS
/// Description:
/// Boolean variable that can instruct models using `partno()` to
/// "part-out" their models, by expanding deliniated parts away
/// from their modeled position.
/// Currently: `false`
/// See Also: partno(), partno_attach()
EXPAND_PARTS = false;
$_EXPAND_PARTS = false;
/// Constant: ISOLATED_PART
/// Constant: $_ISOLATED_PART
/// Description:
/// A scalar value meant to hold a string representing a
/// full part number. When set, only shapes with that
/// part are meant to be shown in-scene.
/// Currently: `undef`
/// See Also: partno(), partno_attach(), _is_shown()
ISOLATED_PART = undef;
$_ISOLATED_PART = undef;
/// Constant: LIST_PARTS
/// Description:
/// Boolean variable ethat can instruct models using `partno()`
/// emit part numbers to OpenSCAD's console - or STDOUT when
/// run non-interactively - as they are discovered.
/// Currently: `false`
/// See Also: partno(), partno_attach()
LIST_PARTS = false;
/// Section: Scope-level Constants
///
/// Constant: $_anno_labelname
/// Description:
/// Holds the `label` annotation string within the model.
$_anno_labelname = undef;
/// Constant: $_anno_partno
/// Description:
/// Holds the `partno` annotation list within the model.
$_anno_partno = [];
/// Constant: $_anno_desc
/// Description:
/// Holds the `desc` annotation string within the model.
$_anno_desc = undef;
/// Constant: $_anno_spec
/// Description:
/// Holds the `spec` annotation list-of-lists within the model.
$_anno_spec = undef;
/// Constant: $_anno_obj
/// Description:
/// Holds the `obj` annotation object within the model.
$_anno_obj = undef;
/// Constant: $_anno_obj_measure
/// Description:
/// Holds the `obj-measure` annotation list-of-lists within the model.
$_anno_obj_measure = [[],[]];
// Section: Applying Annotations to Shapes and Modules
// Labeling and describing shapes and models with Annotations happens in two steps. The first step applies the annotation,
// be it a simple label ("tab-A"), a descriptive block of text ("fits into slot B"), a part-number ("A-001-X"), or
// whatever; the second step produces the in-scene textual elements applied to the shape, the actual act of annotation.
// .
// The modules in this section do the first step: they apply the notes and annotation to child shapes and models, to be
// rendered into an annotation in the next step.
//
// Function&Module: label()
// Synopsis: Return or apply a label annotation to a hierarchy of children modules
// Usage: as a function:
// str = label();
// Usage: as a module:
// label(name) [CHILDREN];
// Description:
// Applies `name` as a label. A label is a simple, discrete name useful in directions or
// explanations, such as "tab A" or "slot B". When annotated, labels have the largest and
// most prominent display.
// .
// Label assignment is hierarchical, in that all children beneath the `label()` call will
// have the same label. Label assignment is singular, in that there is only ever
// one label assigned to a 3D element when annotated.
// .
// When invoked as a function, `label()` returns the current hierarchical label `str`.
//
// Arguments:
// name = A string to set as the label for all child elements.
//
// Continues:
// Invoking `label()` as a module with no `name` argument clears the presence of a label annotation
// for subsequent children. Invoking `label()` as a function with a `name` argument makes no changes.
//
// Example: assigning a label: the cuboid is assigned label "A", and when annotated, that label appears:
// label("A")
// cuboid(10)
// annotate(show=["label"]);
//
// Example: labels apply to all models within the child hierarchy: the cuboid is assigned label "A" and that shows when annotated as in Example 1; the child sphere inherits that same label, and shows that when annotated:
// label("A")
// cuboid(10) {
// annotate(show=["label"]);
// up(10)
// sphere(6)
// annotate(show=["label"]);
// }
//
// Example: Changing labels in the hirearchy: the cuboid is assigned label "A", and later in the hirearchy the sphere is assigned an `undef` label, clearing its assignment of "A". The child to that sphere, the smaller cuboid, is assigned label "B". All three elements show the correct label (or lack of label) when annotated:
// label("A")
// cuboid(10) {
// annotate(show=["label"]);
// up(10)
// label(undef)
// sphere(6) {
// annotate(show=["label"]);
// label("B")
// back(12)
// cuboid(6)
// annotate(show=["label"]);
// }
// }
//
function label(name=undef) = $_anno_labelname;
module label(name) {
req_children($children);
$_anno_labelname = name;
children();
}
// Function&Module: desc()
// Synopsis: Return or apply a description annotation to a hierarchy of children modules
// Usage: as a function:
// str = desc();
// Usage: as a module:
// desc(desc) [CHILDREN];
// Description:
// Applies `desc` as a description. A description is additional context, a few short words.
// .
// Descriptions are hierarchical, in that all children beneath the `desc()` call will
// have the same description. Description assignment is singular, in that there is only ever
// one description assigned to a model when annotated.
// .
// This is a convienence module for setting the description without calling `annotate()`,
// and should be used sparingly.
// Ideally, descriptions are specified at annotation time, to provide additional
// context to the model part, alongside the label, part-number, specification, and object
// details. Using `desc()` allows context to be created within models without forcing
// an `annotate()` call in the model.
// .
// When invoked as a function, `desc()` returns the current hierarchical description `str`.
//
// Arguments:
// desc = A string to set as the desc for all child elements.
//
// Continues:
// Invoking `desc()` as a module with no `desc` argument clears the presence of a label annotation
// for subsequent children. Invoking `desc()` as a function with a `desc` argument makes no changes.
//
// Example: assigning a description:
// desc("A very special cuboid")
// cuboid(10)
// annotate(show=["desc"]);
//
// Example: a description applies to all models within the child hierarchy: a description is assigned above the cuboid, and it is present for all elements below that hirearchy:
// desc("Special cube")
// cuboid(10) {
// annotate(show=["desc"]);
// up(10)
// sphere(6)
// annotate(show=["desc"]);
// }
//
// Example: Precedence is given to `annotate()` for descriptions: when `desc()` is used in the same hirearchy as an `annotate()` call with a description, the description provided to `annotate()` will be shown:
// desc("A good cuboid")
// cuboid(10)
// annotate("Not such a great cube, actually", show=["desc"]);
//
function desc(desc=undef) = $_anno_desc;
module desc(desc) {
req_children($children);
$_anno_desc = desc;
children();
}
// Function&Module: spec()
// Synopsis: Return or apply a specification annotation to a hierarchy of children modules
// Usage: as a function:
// list = spec();
// Usage: as a module:
// spec(list) [CHILDREN];
// Description:
// Applies given `list` as a series of `[key, value]` pair specifications to all children hirearchially beneath the `spec()` call.
// When annotated, the specification will have its pairs displayed in a *key=value* layout.
// .
// When invoked as a function, `spec()` returns the current specification `list`.
//
// Arguments:
// list = A list of specifications. Specifications are *assumed* to be lists, of the `[key, value]` format.
//
// Continues:
// Invoking `spec()` as a module with no `list` argument clears the presensce of the specification annotation for all
// subsequent children. Invoking `spec()` as a function with a `list` argument makes no changes.
//
// Todo:
// Currently, `spec()` and `obj()` conflict with each other, and until that gets resolved, don't use them both on the same annotation.
//
// Example: a basic specification:
// spec([["cuboid"], ["s", 10]])
// cuboid(10)
// annotate(show=["spec"]);
//
// Example: explicit argument specification:
// spec([
// ["cuboid"],
// ["x", 10], ["y", 20], ["z", 8],
// ["anchor", CENTER]
// ])
// cuboid([10, 20, 8], anchor=CENTER)
// annotate(show=["spec"]);
//
function spec(list=undef) = $_anno_spec;
module spec(list) {
req_children($children);
$_anno_spec = list;
children();
}
// Function&Module: obj()
// Synopsis: Apply an Object annotation to a hierarchy of children modules
// Usage: as a function:
// object = obj();
// Usage: as a module:
// obj(object) [CHILDREN];
// Description:
// Applies given `object` as a model specification to all children hirearchically beneath the `obj()` call.
// When annotated, the object will have its attributes and values displayed in a *key=value* layout.
// .
// When invoked as a function, `obj()` returns the current hierarchical object `object`.
// .
// A openscad_object Object of any type can be used. You'll want to review
// [the documentation on Objects](https://github.com/jon-gilbert/openscad_objects/wiki) for
// more information on how these work.
//
// Arguments:
// object = An object of any type.
//
// Continues:
// Object modules *should* call `obj()` with their own object entries immediately before calling `attachable()`,
// to prevent accidental hierarchical transfer of objects when it's not desired.
// .
// Invoking `obj()` as a module with no `object` argument clears the presence of the object annotation
// for subsequent children. Invoking `obj()` as a function with an `object` argument makes no changes.
//
// Todo:
// Double-check that `spec()` and `obj()` no longer conflict with each other
// `obj()` example documentation is lacking
//
function obj(obj=undef, dimensions=undef, flyouts=undef) = $_anno_obj;
module obj(obj=[], dimensions=[], flyouts=[]) {
req_children($children);
log_info_if(( !_defined(obj) && !_defined(dimensions) && !_defined(flyouts) ),
"obj(): no Object, dimensions, or flyouts specified. Obj, dimension settings for subsequent children will be emptied.");
$_anno_obj = obj;
$_anno_obj_measure = [ dimensions, flyouts ];
children();
}
// Section: Annotating Part Numbers, and Parting Out Modules
// A part-number is an identifier for discrete sub-sections of a model. Part-numbers are
// hirearchical and cumulatively collected, implying a chain of parentage. Specifying part
// numbers for models works roughly the same as labels and descriptions, in that a module
// sets a value for the hierarchy underneath it. What differes is that part numbers stack
// hierarchically, and allow parts of models to be arranged apart from each other to ease
// inspection of complex pieces.
// Figure(3D,NoAxes,NoScales): A relatively simple-complex model with individually-parted models within it: a plate with a hole, in which a screw with a nut is inserted. In the scene, neither the screw's nut, nor the plate's hole, can be easily examined.
// include <BOSL2/screws.scad>
// partno(1)
// diff()
// cuboid([20, 20, 3])
// tag("remove") attach(CENTER) cyl(d=5, h=4);
// partno(2)
// screw("M5,8", thread_len=5, head="button", drive="torx", anchor="shank_center")
// partno_attach(BOTTOM, overlap=5, partno=1)
// nut("M5");
//
// Continues:
// A fully-collected part-number has a mechanical number, an optional label, and one or more part numbers,
// all separated with hyphens. It's akin to the following format:
// ```
// partno = mech-number, [ sep, label ], sep, part-number, { sep, part-number } ;
// sep = "-" ;
// chars = a .. z, A .. Z, 0 .. 9, _ ;
// mech-number = { chars } ;
// label = { chars } ;
// part-number = { chars } ;
// ```
// Examples: `EX-1`, `EX-B-1`, `EX-B-1-02-2a`
// .
// Part-numbers are predicated on the existance of `MECH_NUMBER`, that uniquely identifies the entire
// mechanism being modeled (eg, `002`). *(OpenSCAD doesn't offer the facility to query the filename
// being operated on for reasons beyond knowing, and this variable must be set somehow.) Ideally
// the `MECH_NUMBER` value is set in your model just after where `openscad_annotations/annotate.scad` is included.)*
// .
// When annotated, a model or shape with a part-number assigned will have the part-number in-scene as a flyout to the respective
// model.
// Figure(3D,NoAxes,NoScales): The model above, but with its part numbers annotated:
// include <BOSL2/screws.scad>
// partno(1)
// diff()
// cuboid([20, 20, 3]) {
// annotate(show=["partno"], anchor=BACK);
// tag("remove") attach(CENTER) cyl(d=5, h=4);
// }
// partno(2)
// screw("M5,8", thread_len=5, head="button", drive="torx", anchor="shank_center") {
// annotate(show=["partno"]);
// partno_attach(BOTTOM, TOP, overlap=5, partno=1)
// nut("M5")
// annotate(show=["partno"]);
// }
//
// Continues:
// .
// **Parting out:** Both `partno()` and `partno_attach()` below provide
// a deliniation of parts within the model. Parts within the model that are delineated
// can be automatically expanded, parted-out, for examination or construction. This is especially
// useful for visualizing the internal aspects of models with that have moving parts not easily seen.
// The distances between parts is adjusted by the current value of `$t`, and animating the scene
// within OpenSCAD will produce parts that coalesce into place over time.
// .
// Part expansion applies to the entire scene, by setting `EXPAND_PARTS` within the .scad file to `true`;
// or within a partial subset of the scene by using `expand_parts()`.
// Figure(3D,NoAxes,NoScales): The above example model, but with part expansion enabled:
// include <BOSL2/screws.scad>
// EXPAND_PARTS = true;
// partno(1)
// diff()
// cuboid([20, 20, 3])
// tag("remove") attach(CENTER) cyl(d=5, h=4);
// partno(2)
// screw("M5,8", thread_len=5, head="button", drive="torx", anchor="shank_center")
// partno_attach(BOTTOM, overlap=5, partno=1)
// nut("M5");
//
// Continues:
// .
// **Highlighting Parts:** Individual parts can be isolated and selectively displayed by setting
// the `ISOLATED_PART` global variable, or by calling the `isolated_part()` module somewhere in the
// hirearchy. This will exclude all but the specified part number when producing models.
// .
// If you're unsure what parts are available for isolating, or if you're running openscad
// non-interactively, you can set `LIST_PARTS` to `true`: it will emit all the parts found
// as it produces the scene to STDOUT (or to the console, if run within the GUI).
// Figure(3D,NoAxes,NoScales): The above example model again, but this time with only part `EX-2` (the screw) shown:
// include <BOSL2/screws.scad>
// ISOLATED_PART = "EX-2";
// partno(1)
// diff()
// cuboid([20, 20, 3])
// tag("remove") attach(CENTER) cyl(d=5, h=4);
// partno(2)
// screw("M5,8", thread_len=5, head="button", drive="torx", anchor="shank_center")
// partno_attach(BOTTOM, overlap=5, partno=1)
// nut("M5");
//
//
// Function&Module: partno()
// Synopsis: Return or apply a part-number annotation to a hierarchy of children modules
// Usage: as a function:
// partno_str = partno();
// partno_lst = partno(as_string=false);
// Usage: as a module:
// partno(partno) [CHILDREN];
// Description:
// When invoked as a function, `partno()` returns the current active part-number string `partno_str`
// from the child hierarchy. When the option `as_string` is set to `false`, the value returned is
// a list of all the part number elements `partno_lst`.
// .
// When invoked as a module, `partno()` sets or extends the model's part-numbers by appending
// `partno` to the existing part-numbers to all children hirearchically beneath the `partno()` call.
// Calling `partno()` multiple times in a child hirearchy will add each call's `partno` to the part-number
// element list.
// .
// When `EXPAND_PARTS` is set to `true`, calls to `partno()` will translate its children to a
// new position in the scene. The position is derived *from* the part-number, and should reasonably
// relocate deliniated parts via `move()` so that inspection or construction is eased. The `distance`
// argument controls how far each step away from a part's origin to move the part; the number of steps
// is derived in part by how many part number sequences there are (so, part `1-1` would probably be closer to
// the part's origin than `1-1-1-1-1` would be).
// .
// When `LIST_PARTS` is set to `true`, `partno()` will emit part numbers to the console, or to STDOUT, as
// it is called throughout the hirearchy.
//
// Arguments: when invoked as a function:
// ---
// as_string = A boolean flag that, when set, instructs `partno()` to return the part number as a string. Default: `true`
//
// Arguments: when invoked as a module:
// partno = A string to append to the current part numbers.
// start_new = A boolean which, if set to `true`, clears previous part numbers from the hirearchy before applying `partno`. Default: `false`
// distance = When parting out, use `distance` to specify how far each step away from their origin to place elements. Default: `20`
//
// Continues:
// It is an error to invoke `partno()` as a function with any of the module-specific arguments; and, it is an
// error to invoke `partno()` as a module with any of the function-specific arguments.
// .
// There is no way for `partno()` to be aware of duplicate part numbers; and, there is every possibility that
// modules' echos will be called multiple times; therefore when using `LIST_PARTS`-triggered part listings, you
// will probably want to de-duplicate part numbers before programmatically iterating through them.
//
// See Also: partno_attach(), expand_parts(), collapse_parts(), isolate_part(), isolated_part()
//
// Example(NORENDER): setting a chain of part-numbers, and then echoing it to the console:
// partno(1)
// partno(1)
// partno(2)
// partno("4a")
// partno(5)
// echo(partno());
// // yields: ECHO: "EX-1-1-2-4a-5"
//
// Example(NORENDER): setting a chain of part-numbers, resetting it in the middle, and then echoing it to the console as a list, not a string:
// partno(1)
// partno(1)
// partno(2)
// partno("4a", start_new=true)
// partno(5)
// echo(partno(as_string=false));
// // yields: ECHO: ["EX", "4a", 5]
//
// Example: a simple `partno()` module example: the cube is part number `1`, and is annotated to show the same:
// partno(1)
// cuboid(30)
// annotate(show=["partno"]);
//
// Example: hirearchical part-number use: children inherit the part-number of their ancestors; this single child tree yields parts `EX-30`, `EX-30-16`, and `EX-30-16-5`
// partno(30)
// cuboid(30) {
// annotate(show=["partno"]);
// attach(TOP, BOTTOM)
// partno(16)
// cuboid(16) {
// annotate(show=["partno"]);
// attach(TOP, BOTTOM)
// partno(5)
// cuboid(5)
// annotate(show=["partno"]);
// }
// }
//
// Example: setting, then clearing, a part-number, to start a new part-number inheritance; the cube at part-number `5` has its inheritance reset, yielding parts `EX-30`, `EX-30-16`, and `EX-5`, even though they are all within the same child tree
// partno(30)
// cuboid(30) {
// annotate(show=["partno"]);
// attach(TOP, BOTTOM)
// partno(16)
// cuboid(16) {
// annotate(show=["partno"]);
// attach(TOP, BOTTOM)
// partno(5, start_new=true)
// cuboid(5)
// annotate(show=["partno"]);
// }
// }
//
// Example: part-number values can be strings:
// partno("r2")
// cuboid(30)
// annotate(show=["partno"]);
//
// Example: if present, part-numbers incorporate labels when annotated:
// label("A")
// partno(1)
// cuboid(30)
// annotate(show=["partno"]);
//
// Example: if you're working with a BOSL2 distributor that sets `$idx` as a side-effect, naturally you can leverage that within `partno()`:
// partno(1)
// zrot_copies(n=5, r=20, subrot=false)
// partno($idx)
// sphere(r=3)
// annotate(show=["partno"], anchor=TOP, leader_len=3);
//
// Example: un-parted sphere-within-sphere: a sphere (`EX-1-2`) exists within another, hollow sphere (`EX-1`), but isn't visible by design:
// partno(1)
// diff()
// sphere(d=20) {
// attach(CENTER)
// tag("remove")
// sphere(d=19);
//
// annotate(show="ALL");
//
// attach(CENTER)
// tag("keep")
// partno(2)
// sphere(d=10)
// annotate(show="ALL");
// }
//
// Example: parted-out sphere-within-sphere: same example as above, a sphere-within-a-sphere, but expanded: `EX-1-2` and `EX-1` are relocated via `partno()`, and both parts of the model are shown:
// EXPAND_PARTS = true;
// partno(1)
// diff()
// sphere(d=20) {
// attach(CENTER)
// tag("remove")
// sphere(d=19);
//
// annotate(show="ALL");
//
// attach(CENTER)
// tag("keep")
// partno(2)
// sphere(d=10)
// annotate(show="ALL");
// }
function partno(partno=undef, start_new=undef, distance=undef, anno=Annotation(), as_string=true) =
let(
_ = log_error_if((_defined(partno) || _defined(start_new) || _defined(distance)),
"When invoked as a function, partno() does not accept the `partno`, `start_new`, or `distance` arguments"),
partno_list = anno_partno_list()
)
(as_string)
? str_join(partno_list, "-")
: partno_list;
module partno(partno, start_new=false, distance=20, anno=undef, as_string=undef) {
log_error_if((_defined(as_string) || _defined(anno)),
"When invoked as a module, partno() does not accept the `as_string` argument");
req_children($children);
$_anno_partno = anno_partno_attach_partno_or_idx(partno, start_new=start_new);
if (LIST_PARTS)
echo(str("PART:", partno()));
if (expand_parts()) {
move(anno_partno_translate(d=distance))
isolate_attachable_part()
children();
} else {
isolate_attachable_part()
children();
}
}
// Module: partno_attach()
// Synopsis: attachable-aware partno() module
// Usage:
// [ATTACHABLE] partno_attach([attach args...], <partno=undef>, <start_new=false>, <distance=20>) [CHILDREN];
// Description:
// `partno_attach()` combines the functionality of BOSL2's `attach()` and openscad_annotation's `partno()`.
// Individual models are attached to each other as normal, and in the same attach call can be assigned
// part numbers. When modeling two functionally distinct shapes (say, two gears) together, `partno_attach()`
// gives you the attachability of the pieces, while later making it easy to separate for examination,
// annotation, slicing, whatever.
// .
// The `partno_attach()` module differs from `attach()` in the following specific four ways:
// .
// 1. part-number assignent: when the `partno` argument is set to something, that value (a number or string) will be
// appended to the sequence of part numbering in the current hierarchy, as `partno()` does.
// When parent anchors are specified as multiple vectors, using the literal `"idx"` as a `partno` will tell `partno_attach()` to use the internally
// generated `$idx` as the partno (but see the caveats below).
// .
// 2. part expansion: if the `EXPAND_PARTS` global is set to `true`, `partno_attach()` will place
// models `distance` length away from their parents' attachment points. Unlike `partno()` which
// flings parts in a determinstic but unstructured direction, this relocation is determined by
// the attachment points. The distances are
// `$t`-time modified to space models away from their parents; if animated, the parts will
// move towards their attachments.
// Because attachments are specific vectors between the parent and child, `partno_attach()` can describe
// that connection with a dashed line between the parted models; unlike `partno()`, a visual relationship
// of how the child connects to the parent is made obvious.
// .
// 3. part listing: if the `LIST_PARTS` global is set to `true`, `partno_attach()` will
// emit part numbers to the console as it finds them. If run from the command-line non-interactively,
// the part numbers will be emitted to STDOUT. Part numbers are emitted whether or not the
// child is modeled in-scene.
// .
// 4. singular part isolating: if `ISOLATED_PART` is set to a partno-string value, `partno_attach()`
// works with `attachable()` to _only_ that part will be modeled in-scene. This is
// contingent on a re-implemention of the `_is_shown()` function, which is queried within `attachable()`.
// The result is similar to how `show_only()` works, but without conflicting with BOSL2's tags functions.
// .
// The changes were kept to an absolute minimum; BOSL2's `attach()` functionality is otherwise as-is, as of
// 2025-02-13. You'll need to review the
// [documentation for `attach()`](https://github.com/BelfrySCAD/BOSL2/wiki/attachments.scad#module-attach)
// as well as [the Attachments tutorial](https://github.com/BelfrySCAD/BOSL2/wiki/Tutorial-Attachments)
// for a full understanding of how attachments work;
// explaining this is outside the scope of the `partno_attach()` documentation here. You'll also need to review
// the documentation for `partno()` above for a full understanding of how part numbers are assigned and displayed.
//
// Arguments:
// parent = The parent anchor point to attach to or a list of parent anchor points.
// child = Optional child anchor point. If given, orients the child to connect this anchor point to the parent anchor.
// ---
// align = If `child` is given you can specify alignment or list of alistnments to shift the child to an edge or corner of the parent.
// inset = Shift aligned children away from their alignment edge/corner by this amount. Default: 0
// overlap = Amount to sink child into the parent. Equivalent to `down(X)` after the attach. This defaults to the value in `$overlap`, which is `0` by default.
// inside = If `child` is given you can set `inside=true` to attach the child to the inside of the parent for diff() operations. Default: false
// shiftout = Shift an inside object outward so that it overlaps all the aligned faces. Default: 0
// spin = Amount to rotate the parent around the axis of the parent anchor. Can set to "align" to align the child's BACK with the parent aligned edge. (Only permitted in 3D.)
// partno = A string to append to the current part numbers. No default.
// start_new = A boolean which, if set to `true`, clears previous part numbers from the hirearchy before applying `partno`. Default: `false`
// distance = When parting out, use `distance` to specify how far away to place attached elements. Default: `60`
//
// Side Effects:
// `$anchor` set to the parent anchor value used for the child.
// `$align` set to the align value used for the child.
// `$idx` set to a unique index for each child, increasing by alignment first.
// `$attach_anchor` for each anchor given, this is set to the `[ANCHOR, POSITION, ORIENT, SPIN]` information for that anchor.
// if `inside` is true and no `partno` was set then set default tag to "remove"
// `$attach_to` is set to the value of the `child` argument, if given. Otherwise, `undef`
// `$edge_angle` is set to the angle of the edge if the anchor is on an edge and the parent is a prismoid or vnf with "hull" anchoring
// `$edge_length` is set to the length of the edge if the anchor is on an edge and the parent is a prismoid or vnf with "hull" anchoring
// `$_anno_partno` is set to the combined value of previously set `$_anno_partno` plus the new `partno`.
//
// Continues:
// Note that because `partno_attach()` can process and apply multiple parental anchors, it can behave like a
// BOSL2 distributor, attaching multiple identical children to various anchors on the parent. Each of
// those distributed model copies is assigned an index via a `$idx` scoped variable. `partno_attach()`
// recognizes a `partno` of `"idx"` as the signal to use `$idx` as the new part-number value. Naturally, this
// precludes the future ability to use the literal string `"idx"` as a part number element (which is
// a trade-off that I'm OK with).
// .
// Note also there is no way for `partno_attach()` to be aware of duplicate part numbers; and, there is every possibility that
// modules' echos will be called multiple times; therefore when using `LIST_PARTS`-triggered part listings, you
// will probably want to de-duplicate part numbers before programmatically iterating through them.
//
// See Also: partno(), expand_parts(), collapse_parts(), isolate_part(), isolated_part()
//
// Example(3D): `partno()`'s Example 4, above, but with using `partno_attach()`: a hirearchical part-number use, showing inheritance of the part-numbers within a tree:
// partno(30)
// cuboid(30) {
// annotate(show=["partno"]);
// partno_attach(TOP, BOTTOM, partno=16)
// cuboid(16) {
// annotate(show=["partno"]);
// partno_attach(TOP, BOTTOM, partno=5)
// cuboid(5)
// annotate(show=["partno"]);
// }
// }
//
// Example(3D): `partno()`'s Example 8, above, but with `partno_attach()`: using `$idx` as a value doesn't work with `partno_attach()` (because at the time of invocation, `$idx` isn't set correctly), but you *can* use the literal string `"idx"` as a value, and `partno_attach()` will do the right thing. This works in a distributor, and also when specifying multiple attachable anchor points in a single attach call:
// partno(1)
// cuboid(30)
// partno_attach([TOP,BOTTOM,LEFT,RIGHT,FWD,BACK], BOTTOM, partno="idx")
// sphere(r=3)
// annotate(show=["partno"], anchor=TOP, leader_len=3);
//
//
// Example(3D,NoAxes): a simple `partno_attach()` relation, with `EXPAND_PARTS` set as `false` (which is the default):
// EXPAND_PARTS = false;
// label("A")
// tube(id=4, od=8, h=5) {
// annotate(show=["label", "partno"]);
// partno_attach(CENTER, undef, partno=1)
// cyl(d=4, h=10)
// annotate(show=["label", "partno"]);
// }
//
// Example(3D,NoAxes): the same `partno_attach()` relation, with expansion enabled:
// EXPAND_PARTS = true;
// label("A")
// tube(id=4, od=8, h=5) {
// annotate(show=["label", "partno"]);
// partno_attach(CENTER, undef, partno=1)
// cyl(d=4, h=10)
// annotate(show=["label", "partno"]);
// }
//
// Example(3D): A sphere attached to the top of a cube, but with `ISOLATED_PART` set to limit only the sphere:
// ISOLATED_PART = "EX-1-1";
// partno(1)
// cuboid(5)
// partno_attach(TOP, BOTTOM, partno=1)
// sphere(3);
module partno_attach(parent, child, overlap, align, spin=0, norot, inset=0, shiftout=0, inside=false, from, to, partno=undef, start_new=false, distance=20)
{
// ## 1 ## partno_attach() args: the partno, start_new, and distance arguments are added above
dummy3=
assert(num_defined([to,child])<2, "Cannot combine deprecated 'to' argument with 'child' parameter")
assert(num_defined([from,parent])<2, "Cannot combine deprecated 'from' argument with 'parent' parameter")
assert(spin!="align" || is_def(align), "Can only set spin to \"align\" when the 'align' parameter is given")
assert(is_finite(spin) || spin=="align", "Spin must be a number (unless align is given)")
assert((is_undef(overlap) || is_finite(overlap)) && (is_def(overlap) || is_undef($overlap) || is_finite($overlap)),
str("Provided ",is_def(overlap)?"":"$","overlap is not valid."));
if (is_def(to))
echo("The 'to' option to attach() is deprecated and will be removed in the future. Use 'child' instead.");
if (is_def(from))
echo("The 'from' option to attach(0 is deprecated and will be removed in the future. Use 'parent' instead");
if (norot)
echo("The 'norot' option to attach() is deprecated and will be removed in the future. Use position() instead.");
req_children($children);
dummy=assert($parent_geom != undef, "No object to attach to!")
assert(is_undef(child) || is_string(child) || (is_vector(child) && (len(child)==2 || len(child)==3)),
"child must be a named anchor (a string) or a 2-vector or 3-vector")
assert(is_undef(align) || !is_string(child), "child is a named anchor. Named anchors are not supported with align=");
two_d = _attach_geom_2d($parent_geom);
basegeom = $parent_geom[0]=="conoid" ? attach_geom(r=2,h=2,axis=$parent_geom[5])
: $parent_geom[0]=="prismoid" ? attach_geom(size=[2,2,2],axis=$parent_geom[4])
: attach_geom(size=[2,2,2]);
childgeom = attach_geom([2,2,2]);
child_abstract_anchor = is_vector(child) && !two_d ? _find_anchor(_make_anchor_legal(child,childgeom), childgeom) : undef;
// ## 2 ## partno_t_offset: set an offset value based on the distance argument and the value of $t
partno_t_offset = -1 * (distance - (distance * ($t + 0.0999)));
// ## 3 ## overlap: conditionally modify the overlap to the offset if EXPAND_PARTS and ok_to_annotate() are
// both true; otherwise, leave the existing specified value of overlap alone
overlap = ((overlap!=undef)? overlap : $overlap)
+ ((expand_parts() && !is_undef(partno))
? partno_t_offset
: 0);
parent = first_defined([parent,from]);
anchors = is_vector(parent) || is_string(parent) ? [parent] : parent;
align_list = is_undef(align) ? [undef]
: is_vector(align) || is_string(align) ? [align] : align;
dummy4 = assert(is_string(parent) || is_list(parent), "Invalid parent anchor or anchor list")
assert(spin==0 || (!two_d || is_undef(child)), "spin is not allowed for 2d objects when 'child' is given");
child_temp = first_defined([child,to]);
child = two_d ? _force_anchor_2d(child_temp) : child_temp;
dummy2=assert(align_list==[undef] || is_def(child), "Cannot use 'align' without 'child'")
assert(!inside || is_def(child), "Cannot use 'inside' without 'child'")
assert(inset==0 || is_def(child), "Cannot specify 'inset' without 'child'")
assert(inset==0 || is_def(align), "Cannot specify 'inset' without 'align'")
assert(shiftout==0 || is_def(child), "Cannot specify 'shiftout' without 'child'");
factor = inside?-1:1;
$attach_to = child;
for (anch_ind = idx(anchors)) {
dummy=assert(is_string(anchors[anch_ind]) || (is_vector(anchors[anch_ind]) && (len(anchors[anch_ind])==2 || len(anchors[anch_ind])==3)),
str("parent[",anch_ind,"] is ",anchors[anch_ind]," but it must be a named anchor (string) or a 2-vector or 3-vector"))
assert(align_list==[undef] || !is_string(anchors[anch_ind]),
str("parent[",anch_ind,"] is a named anchor (",anchors[anch_ind],"), but named anchors are not supported with align="));
anchor = is_string(anchors[anch_ind])? anchors[anch_ind]
: two_d?_force_anchor_2d(anchors[anch_ind])
: point3d(anchors[anch_ind]);
$anchor=anchor;
anchor_data = _find_anchor(anchor, $parent_geom);
$edge_angle = len(anchor_data)==5 ? struct_val(anchor_data[4],"edge_angle") : undef;
$edge_length = len(anchor_data)==5 ? struct_val(anchor_data[4],"edge_length") : undef;
$edge_end1 = len(anchor_data)==5 ? struct_val(anchor_data[4],"vec") : undef;
anchor_pos = anchor_data[1];
anchor_dir = factor*anchor_data[2];
anchor_spin = two_d || !inside || anchor==TOP || anchor==BOT ? anchor_data[3]
: let(spin_dir = rot(anchor_data[3],from=UP, to=-anchor_dir, p=BACK))
_compute_spin(anchor_dir,spin_dir);
parent_abstract_anchor = is_vector(anchor) && !two_d ? _find_anchor(_make_anchor_legal(anchor,basegeom),basegeom) : undef;
for(align_ind = idx(align_list)){
align = is_undef(align_list[align_ind]) ? undef
: assert(is_vector(align_list[align_ind],2) || is_vector(align_list[align_ind],3), "align direction must be a 2-vector or 3-vector")
two_d ? _force_anchor_2d(align_list[align_ind])
: point3d(align_list[align_ind]);
spin = is_num(spin) ? spin
: align==CENTER ? 0
: sum(v_abs(anchor))==1 ? // parent anchor is a face
let(
spindir = in_list(anchor,[TOP,BOT]) ? BACK : UP,
proj = project_plane(point4d(anchor),[spindir,align]),
ang = v_theta(proj[1])-v_theta(proj[0])
)
ang
: // parent anchor is not a face, so must be an edge (corners not allowed)
let(
nativeback = apply(rot(to=parent_abstract_anchor[2],from=UP)
*affine3d_zrot(parent_abstract_anchor[3]), BACK)
)
nativeback*align<0 ? -180:0;
$idx = align_ind+len(align_list)*anch_ind;
$align=align;
goodcyl = $parent_geom[0] != "conoid" || is_undef(align) || align==CTR ? true
: let(
align=rot(from=$parent_geom[5],to=UP,p=align),
anchor=rot(from=$parent_geom[5],to=UP,p=anchor)
)
anchor==TOP || anchor==BOT || align==TOP || align==BOT;
badcorner = !in_list($parent_geom[0],["conoid","spheroid"]) && !is_undef(align) && align!=CTR && sum(v_abs(anchor))==3;
badsphere = $parent_geom[0]=="spheroid" && !is_undef(align) && align!=CTR;
dummy=assert(is_undef(align) || all_zero(v_mul(anchor,align)),
str("Invalid alignment: align value (",align,") includes component parallel to parent anchor (",anchor,")"))
assert(goodcyl, str("Cannot use align with an anchor on a curved edge or surface of a cylinder at parent anchor (",anchor,")"))
assert(!badcorner, str("Cannot use align at a corner anchor (",anchor,")"))
assert(!badsphere, "Cannot use align on spheres.");
// Now compute position on the parent (including alignment but not inset) where the child will be anchored
pos = is_undef(align) ? anchor_data[1] : _find_anchor(anchor+align, $parent_geom)[1];
$attach_anchor = list_set(anchor_data, 1, pos); // Never used; For user informational use? Should this be set at all?
// Compute adjustment to the child anchor for position purposes. This adjustment
// accounts for the change in the anchor needed to to alignment.
child_adjustment = is_undef(align)? CTR
: two_d ? rot(to=child,from=-factor*anchor,p=align)
: apply( rot(to=child_abstract_anchor[2],from=UP)
* affine3d_zrot(child_abstract_anchor[3])
* affine3d_yrot(inside?0:180)
* affine3d_zrot(-parent_abstract_anchor[3])
* rot(from=parent_abstract_anchor[2],to=UP)
* rot(v=anchor,-spin),
align);
// The $anchor_override anchor value forces an override of the *position* only for the anchor
// used when attachable() places the child
$anchor_override = all_zero(child_adjustment)? inside?child:undef
: child+child_adjustment;
reference = two_d? BACK : UP;
// inset_dir is the direction for insetting when alignment is in effect
inset_dir = is_undef(align) ? CTR
: two_d ? rot(to=reference, from=anchor,p=align)
: apply(affine3d_yrot(inside?180:0)
* affine3d_zrot(-parent_abstract_anchor[3])
* rot(from=parent_abstract_anchor[2],to=UP)
* rot(v=anchor,-spin),
align);
spinaxis = two_d? UP : anchor_dir;
olap = - overlap * reference - inset*inset_dir + shiftout * (inset_dir + factor*reference);
// ## 4 ## anno_partno: establish the partno for the upcoming part. This may
// involve the $idx value, so we cannot do that until we reach this point.
$_anno_partno = anno_partno_attach_partno_or_idx(partno, $idx, start_new);
partno_str = partno();
// ## 5 ## LIST_PARTS: conditionally emit the full partno here to the console if
// the LIST_PARTS global is true
if (LIST_PARTS)
echo(str("PART:", partno_str));
if (norot || (approx(anchor_dir,reference) && anchor_spin==0))
translate(pos) rot(v=spinaxis,a=factor*spin) translate(olap) default_tag("remove",inside) children();
else
translate(pos)
rot(v=spinaxis,a=factor*spin)
rot(anchor_spin,from=reference,to=anchor_dir)
translate(olap)
default_tag("remove",inside) children();
// ## 6 ## anchor lines: conditionally draw annotation extension lines to
// the specified anchor.
if (!isolated_part() && expand_parts() && ok_to_annotate())
attach(anchor)
anno_dashed_line(partno_t_offset, anchor=TOP);
}
}
}
// Function&Module: expand_parts()
// Synopsis: Determines or gates the expansion of parts
// Usage: as a function:
// bool = expand_parts();
// Usage: as a module:
// expand_parts() [CHILDREN];
// Description:
// When called as a function, `expand_parts()` returns a
// boolean `bool`. A `true` value indicating that models capale of
// being parted out should do so; `false` otherwise.
// .
// When called as a module, `expand_parts()` instructs
// children to expand their parts if possible.
// See Also: partno(), partno_attach(), collapse_parts()
function expand_parts() = (EXPAND_PARTS || $_EXPAND_PARTS);
module expand_parts() {
let($_EXPAND_PARTS = true)
children();
}
// Function&Module: collapse_parts()
// Synopsis: Determines or un-gates the expansion of parts
// Usage: as a function:
// bool = collapse_parts();
// Usage: as a module:
// collapse_parts() [CHILDREN];
// Description:
// When called as a function, `collapse_parts()` returns a
// boolean `bool`. A `true` value indicating that models capale of
// being parted out should no longer do so; `false` otherwise.
// .
// When called as a module, `collapse_parts()` instructs
// children to expand their parts if possible.
// See Also: partno(), partno_attach(), expand_parts()
function collapse_parts() = !expand_parts();
module collapse_parts() {
let(EXPAND_PARTS = false, $_EXPAND_PARTS = false)
children();
}
// Function&Module: isolate_part()
// Synopsis: Determines or starts the isolation of a single part
// Usage: as a function:
// bool = isolate_part();
// bool = isolate_part(<partno=partno()>, <isolated_part=isolated_part()>);
// Usage: as a module:
// isolate_part() [CHILDREN];
// isolate_part(<partno=undef>) [CHILDREN];
// Description:
// When called as a function when a part is to be isolated in the
// current hierarchy, returns a boolean `bool` as whether the
// part number is to be isolated and displayed, indicated with a `true` value; or to be ignored and not
// shown in-scene, indicated with a `false` value.
// If optionally called with a part number string `partno`,
// that part number will be used for consideration instead of the
// currently active part-number gleaned from the child hirearchy.
// The option `isolated_part` will take precedence
// over either globally set via `ISOLATED_PART` or previous
// calls of `isolate_part()`.
// .
// When called as a module, `isolate_part()` instructs the
// hirearchy to restrict producing models in-scene that do not
// match the value of `ISOLATED_PART`. If optionally called with a
// part number string `partno`, that part number will be used for consideration
// over any set in the hirearchy.
// .
// In both function and module modes, `isolate_part()` will examine
// an optional `partno` argument, the locally scoped `$_ISOLATED_PART`,
// and finally the global `ISOLATED_PART`, in that order, to make its
// decisions.
//
// Arguments:
// partno = An optional part-number; if specified, will take precedence over the locally scoped and global values. Default: `undef`
// isolated_part = When used as a function, will use this as the comparison over a globally set value.
//
// Continues:
// Generally speaking, *you need not call `isolate_part()` manually.* The isolation activity that
// that `isolate_part()` provides as a module happens automatically within `attachable()`, and bases
// its decisions on the globally set `ISOLATED_PART`. Invoking `isolate_part()` at the top of a
// child hierarchy duplicates that effort; and, inserting an `isolate_part()` module call in the
// middle of a hierarchy won't ensure that models above it, or in another hirearchy, won't also
// be displayed (thus negating the notion of "isolation"). Use of `isolate_part()` as a module
// should be done sparingly, perhaps only while in testing.
//
// Example(NORENDER): simple function use:
// ISOLATED_PART = "EX-1";
// echo(isolate_part());
// // yields: false
// partno(1)
// cuboid(10)
// echo(isolate_part());
// // yields: true
//
// Example(3D): simple module use: tell a child hirearchy to isolate part `EX-1-2-3`, without the use of a globally defined variable:
// isolate_part("EX-1-2-3")
// partno(1)
// recolor("red") cuboid(30)
// partno_attach(TOP, BOTTOM, partno=2)
// recolor("blue") cuboid(20)
// partno_attach(TOP, BOTTOM, partno=3)
// recolor("yellow") cuboid(10)
// partno_attach(TOP, BOTTOM, partno=4)
// recolor("green") cuboid(5);
//
// Example(3D): same example as above, but the `isolate_part()` call is inserted in the middle of the hirearchy. It still shows `EX-1-2-3`, but also all the parts that came before it:
// partno(1)
// recolor("red") cuboid(30)
// partno_attach(TOP, BOTTOM, partno=2)
// recolor("blue") cuboid(20)
// isolate_part("EX-1-2-3")