-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
1017 lines (784 loc) · 32.4 KB
/
Copy pathscript.js
File metadata and controls
1017 lines (784 loc) · 32.4 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
/**
*
* Gridfinity Generator for Onshape
* ================================
*
* Author: Jose Galarza (@igalarzab)
* <https://x.com/igalarzab>
*
* You can submit bugs and feature requests in:
* <https://github.com/igalarzab/gridfinity-generator-onshape>
*
* Gridfinity is an original idea developed by Zach Freedman
* <https://www.youtube.com/@ZackFreedman>
*
* Gridfinity Specifications thanks to @Stu142
* <https://github.com/Stu142/Gridfinity-Documentation>
*
* This work is licensed under Creative Commons Attribution-ShareAlike 4.0
* <https://creativecommons.org/licenses/by-sa/4.0/>
*
*/
FeatureScript 2656;
import(path: 'onshape/std/common.fs', version: '2656.0');
import(path: 'onshape/std/geometry.fs', version: '2656.0');
icon::import(path: 'ee93c9c076a700a661adcd6f', version: 'cc6e67e2b4cf4e6ce87b92f5');
/**
*
* Dimensions.
*
* You can change any of these dimensions to modify your bin, but doing so will make
* your part to be not-Gridfinity compliant
*
*/
const Dims = {
unitHeight: 7 * millimeter,
unitSeparator: 0.25 * millimeter,
baseFillet: 0.8 * millimeter,
baseHoleClearance: 4.8 * millimeter,
baseHoleMinimumGap: 0.5 * millimeter,
baseHoleMinimumWall: 0.5 * millimeter,
baseHoleRemoverRadius: 1.3 * millimeter,
baseDraftAngle: 45 * degree,
baseLayer1Height: 0.8 * millimeter,
baseLayer2Height: 1.8 * millimeter,
baseLayer3Height: 2.15 * millimeter,
baseLayer4Height: 2.25 * millimeter,
bodyFillet: 3.75 * millimeter,
bodyInternalFillet: 2.55 * millimeter,
topHeight: 4.4 * millimeter,
topStackableLipWidth: 2.6 * millimeter,
topStackableLipHeight: 5.8 * millimeter,
topStackableLipRoundedFillet: 0.5 * millimeter,
};
/**
*
* Global Variables.
*
*/
export enum TopLipShape {
SHARP, ROUNDED
}
export enum FillType {
COMPLETE, UNTIL_LIP
}
export enum FingerSlideType {
CHAMFER, ROUNDED
}
enum Orientation {
TOP, BOTTOM, LEFT, RIGHT, FRONT, BACK
}
const Planes = {
top: qCreatedBy(makeId('Top'), EntityType.FACE),
right: qCreatedBy(makeId('Right'), EntityType.FACE),
front: qCreatedBy(makeId('Front'), EntityType.FACE),
};
// Ranges -> (min, default, max)
const UNIT_HEIGHT_RANGE = [2, 6, 50];
const MAGNETS_DIAMETER_RANGE = [1, 6.5, 8.5];
const MAGNETS_DEPTH_RANGE = [0.5, 2.4, 10];
const MAGNET_LEAD_IN_SIZE_RANGE = [0.1, 0.2, 2];
const SCREWS_DIAMETER_RANGE = [1, 3, 8.5];
const SCREWS_DEPTH_RANGE = [0.5, 6, 10];
const LABEL_WIDTH_RANGE = [1, 13, 100];
const LABEL_OFFSET_RANGE = [0, 0.5, 100];
const BODY_WALL_THICKNESS_RANGE = [1.2, 1.6, 10];
const UNIT_SIZE_RANGE = [12, 42, 72];
const FINGER_SLIDE_HEIGHT_RANGE = [2, 10, 15];
// Sweep contour for each lid type (in mm)
const LID_SWEEP = {
TopLipShape.SHARP: {
x: [-2.6, 4.4, 2.5, 0.7, 0.0, -2.6],
y: [ 0.0, 0.0, 1.9, 1.9, 2.6, 0.0],
},
TopLipShape.ROUNDED: {
x: [-2.6, 4.4, 4.4, 3.05, 1.25, 0.55, 0.0, -2.6],
y: [ 0.0, 0.0, 0.55, 1.9, 1.9, 2.6, 2.6, 0.0],
}
};
/**
*
* Feature Definition
*
*/
annotation { 'Feature Type Name' : 'Gridfinity Bin', 'Feature Type Description' : 'Create a gridfinity bin', 'Icon': icon::BLOB_DATA }
export const gridfinityBin = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation { 'Group Name' : 'Dimensions', 'Collapsed By Default' : false }
{
annotation { 'Name' : 'Rows', 'UIHint' : [UIHint.REMEMBER_PREVIOUS_VALUE] }
isInteger(definition.rows, POSITIVE_COUNT_BOUNDS);
annotation { 'Name' : 'Columns', 'UIHint' : [UIHint.REMEMBER_PREVIOUS_VALUE] }
isInteger(definition.columns, POSITIVE_COUNT_BOUNDS);
annotation { 'Name' : 'Height', 'UIHint' : [UIHint.REMEMBER_PREVIOUS_VALUE] }
isInteger(definition.height, { (unitless) : UNIT_HEIGHT_RANGE } as IntegerBoundSpec);
}
annotation { 'Name' : 'Add Magnets', 'Default': true }
definition.magnets is boolean;
if (definition.magnets) {
annotation { 'Group Name' : '', 'Collapsed By Default' : true, 'Driving Parameter' : 'magnets' }
{
annotation { 'Name' : 'Easy Remover', 'Default': true }
definition.baseMagnetEasyRemover is boolean;
annotation { 'Name' : 'Magnet Lead-In', 'Default': false }
definition.baseMagnetLeadIn is boolean;
annotation { 'Name' : 'Diameter' }
isLength(definition.baseMagnetDiameter, { (millimeter): MAGNETS_DIAMETER_RANGE } as LengthBoundSpec);
annotation { 'Name' : 'Depth' }
isLength(definition.baseMagnetDepth, { (millimeter): MAGNETS_DEPTH_RANGE } as LengthBoundSpec);
if (definition.baseMagnetLeadIn) {
annotation { 'Name' : 'Lead-In Size' }
isLength(definition.baseMagnetLeadInSize, { (millimeter): MAGNET_LEAD_IN_SIZE_RANGE } as LengthBoundSpec);
}
}
}
annotation { 'Name' : 'Add Screws', 'Default': false }
definition.screws is boolean;
if (definition.screws) {
annotation { 'Group Name' : '', 'Collapsed By Default' : true, 'Driving Parameter' : 'screws' }
{
annotation { 'Name' : 'Diameter' }
isLength(definition.baseScrewDiameter, { (millimeter): SCREWS_DIAMETER_RANGE } as LengthBoundSpec);
annotation { 'Name' : 'Depth' }
isLength(definition.baseScrewDepth, { (millimeter): SCREWS_DEPTH_RANGE } as LengthBoundSpec);
}
}
annotation { 'Name' : 'Fill the bin' }
definition.filled is boolean;
if (definition.filled) {
annotation { 'Group Name' : '', 'Collapsed By Default' : false, 'Driving Parameter' : 'filled' }
{
annotation { 'Name' : 'Type', 'Default': FillType.UNTIL_LIP }
definition.fillType is FillType;
}
}
if (!definition.filled || (definition.filled && definition.fillType == FillType.UNTIL_LIP)) {
annotation { 'Name' : 'Add Stackable Lip', 'Default': true }
definition.stackableLip is boolean;
if (definition.stackableLip) {
annotation { 'Group Name' : '', 'Collapsed By Default' : true, 'Driving Parameter' : 'stackableLip' }
{
annotation { 'Name' : 'Shape', 'Default': TopLipShape.SHARP }
definition.lipShape is TopLipShape;
}
}
}
if (!definition.filled) {
annotation { 'Name' : 'Add Label', 'Default': true }
definition.label is boolean;
if (definition.label) {
annotation { 'Group Name' : '', 'Collapsed By Default' : true, 'Driving Parameter' : 'label' }
{
annotation { 'Name' : 'Width' }
isLength(definition.labelWidth, { (millimeter): LABEL_WIDTH_RANGE } as LengthBoundSpec);
annotation { 'Name' : 'Offset' }
isLength(definition.labelOffset, { (millimeter): LABEL_OFFSET_RANGE } as LengthBoundSpec);
}
}
annotation { 'Name' : 'Add Finger Slide', 'Default': true }
definition.fingerSlide is boolean;
if (definition.fingerSlide) {
annotation { 'Group Name' : '', 'Collapsed By Default' : true, 'Driving Parameter' : 'fingerSlide' }
{
annotation { 'Name' : 'Shape', 'Default': FingerSlideType.ROUNDED }
definition.fingerSlideType is FingerSlideType;
annotation { 'Name' : 'Height' }
isLength(definition.fingerSlideHeight, { (millimeter): FINGER_SLIDE_HEIGHT_RANGE } as LengthBoundSpec);
}
}
}
annotation { 'Group Name' : 'Advanced Config', 'Collapsed By Default' : true }
{
annotation { 'Name' : 'Wall Thicknes', 'UIHint' : [UIHint.REMEMBER_PREVIOUS_VALUE] }
isLength(definition.bodyWallThicknes, { (millimeter): BODY_WALL_THICKNESS_RANGE } as LengthBoundSpec);
annotation { 'Name' : 'Unit Size', 'UIHint' : [UIHint.REMEMBER_PREVIOUS_VALUE] }
isLength(definition.unitSize, { (millimeter): UNIT_SIZE_RANGE } as LengthBoundSpec);
}
}
{
// Create the base parts
const base = baseCreate(context, definition, id + 'Base');
const body = bodyCreate(context, definition, id + 'Body', base);
const top = topCreate(context, definition, id + 'Top', body);
// Merge the base parts in one part
mergeParts(context, id + 'BaseBin', [base, body, top]);
// Create the finger slide if needed
if (!definition.filled && definition.fingerSlide) {
const fingerSlide = fingerSlideCreate(context, definition, id + 'FingerSlide', base);
}
// Create the label if needed (min height is 3 units)
if (!definition.filled && definition.label && definition.height > 2) {
const label = labelCreate(context, definition, id + 'Label', base);
mergeParts(context, id + 'BaseBinWithLabel', [base, label]);
}
// Center the bin in the origin
centerPart(context, id + 'Center', base.id);
// Rename the bin
renamePart(context, base.id, 'Gridfinity Bin ' ~ definition.rows ~ 'x' ~ definition.columns);
}
);
/**
*
* Functions to create the base of the bin
*
*/
function baseCreate(context is Context, definition is map, id is Id) {
const baseSketch = baseSketch(context, definition, id + 'Sketch');
// Create the 3 layers of the base
const layer1Extrude = wallExtrude(context, id + 'Layer1', baseSketch.region, {
depth: Dims.baseLayer1Height,
filletRadius: Dims.baseFillet,
draftAngle: Dims.baseDraftAngle,
});
const layer2Extrude = wallExtrude(context, id + 'Layer2', findFace(context, layer1Extrude.id, Orientation.TOP), {
depth: Dims.baseLayer2Height,
});
const layer3Extrude = wallExtrude(context, id + 'Layer3', findFace(context, layer2Extrude.id, Orientation.TOP), {
depth: Dims.baseLayer3Height,
draftAngle: Dims.baseDraftAngle,
});
// Merge the three layers into a single part
const basePart = mergeParts(context, id + 'BasePart', [
layer1Extrude,
layer2Extrude,
layer3Extrude,
]);
// Create the magnet holes if needed
if (definition.magnets) {
const magnetRadius = definition.baseMagnetDiameter / 2;
const enoughSizeForMagnets = baseHasEnoughSizeForHoles(
definition,
magnetRadius,
definition.baseMagnetEasyRemover
);
if (enoughSizeForMagnets) {
const magnetHolesSketch = baseHolesSketch(
context,
definition,
id + 'Magnets',
basePart,
magnetRadius,
definition.baseMagnetEasyRemover
);
const magnets = wallExtrude(context, id + 'MagnetsExtrude', magnetHolesSketch.region, {
depth: definition.baseMagnetDepth,
});
const magnetsHoleId = id + 'MagnetsHole';
substractParts(context, magnetsHoleId, basePart.id, [magnets.id]);
if (definition.baseMagnetLeadIn) {
baseCreateMagnetLeadIn(context, id, basePart, magnetsHoleId, magnetRadius, definition.baseMagnetLeadInSize);
}
removeBodies(context, id + 'DeleteMagnetsSketch', [magnetHolesSketch.id]);
}
}
// Create the screw holes if needed
if (definition.screws) {
const screwRadius = definition.baseScrewDiameter / 2;
const enoughSizeForScrews = baseHasEnoughSizeForHoles(
definition,
screwRadius,
false
);
if (enoughSizeForScrews) {
const screwHolesSketch = baseHolesSketch(
context,
definition,
id + 'Screws',
basePart,
screwRadius,
false
);
const screws = wallExtrude(context, id + 'ScrewsExtrude', screwHolesSketch.region, {
depth: definition.baseScrewDepth,
});
substractParts(context, id + 'ScrewsHole', basePart.id, [screws.id]);
removeBodies(context, id + 'DeleteScrewsSketch', [screwHolesSketch.id]);
}
}
// Replicate the base for rows * columns
var linearPatternId = undefined;
if (definition.rows > 1 || definition.columns > 1) {
linearPatternId = id + 'ReplicateBases';
linearPattern(context, linearPatternId, {
'patternType': PatternType.PART,
'entities': qCreatedBy(basePart.id, EntityType.BODY),
'hasSecondDir': true,
'oppositeDirectionTwo': true,
'directionOne': Planes.right,
'directionTwo': Planes.front,
'distance': definition.unitSize,
'distanceTwo': definition.unitSize,
'instanceCount': definition.columns,
'instanceCountTwo': definition.rows,
});
}
// Layer 4 is common to all the bases, that's why we do it after the linearPattern
const layer4Sketch = baseLayer4Sketch(
context,
definition,
id + 'Layer4Sketch',
layer3Extrude
);
const layer4Extrude = wallExtrude(context, id + 'Layer4Extrude', layer4Sketch.region, {
depth: Dims.baseLayer4Height,
filletRadius: Dims.bodyFillet,
});
// Merge all the bases into a single part
mergeParts(context, id + 'AllBases', [
basePart,
{ 'id': linearPatternId },
layer4Extrude,
]);
// Remove sketches, they are not needed anymore
removeBodies(context, id + 'DeleteBaseSketches', [baseSketch.id, layer4Sketch.id]);
return { 'id': basePart.id, 'layer4Id': layer4Extrude.id };
}
function baseSketch(context is Context, definition is map, id is Id) {
const sketchId = id + 'Sketch';
const sketch = newSketch(context, sketchId, {
'sketchPlane' : Planes.top,
});
const bottomSize = baseCalculateBottomSize(definition);
skRectangle(sketch, 'bottomSketchRectangle', {
'firstCorner': vector(0, 0) * millimeter,
'secondCorner': vector(bottomSize, bottomSize)
});
skSolve(sketch);
return { 'id': sketchId, 'region': qSketchRegion(sketchId, false) };
}
function baseHolesSketch(context is Context, definition is map, id is Id, base is map, radius is ValueWithUnits, easyRemover is boolean) {
const sketchId = id + 'Sketch';
// Create a plane in the center of the base to make maths for the magnets simpler
const tangentPlane = evFaceTangentPlane(context, {
'face': findFace(context, base.id, Orientation.BOTTOM),
'parameter': vector(0.5, 0.5),
});
const sketch = newSketchOnPlane(context, sketchId, {
'sketchPlane' : tangentPlane,
});
const bottomSize = baseCalculateBottomSize(definition);
const x = (bottomSize / 2) - Dims.baseHoleClearance;
const y = -(bottomSize / 2) + Dims.baseHoleClearance;
skCircle(sketch, 'topRight', { 'center': vector(x, x), 'radius': radius });
skCircle(sketch, 'bottomRight', { 'center': vector(x, y), 'radius': radius });
skCircle(sketch, 'topLeft', { 'center': vector(y, x), 'radius': radius });
skCircle(sketch, 'bottomLeft', { 'center': vector(y, y), 'radius': radius });
if (easyRemover) {
const p = radius * (sqrt(2) / 2);
const r = Dims.baseHoleRemoverRadius;
skCircle(sketch, 'topRightRemover', { 'center': vector(x-p, x-p), 'radius': r });
skCircle(sketch, 'bottomRightRemover', { 'center': vector(x-p, y+p), 'radius': r });
skCircle(sketch, 'topLeftRemover', { 'center': vector(y+p, x-p), 'radius': r });
skCircle(sketch, 'bottomLeftRemover', { 'center': vector(y+p, y+p), 'radius': r });
}
skSolve(sketch);
return { 'id': sketchId, 'region': qSketchRegion(sketchId, false) };
}
function baseCreateMagnetLeadIn(context is Context, id is Id, base is map, magnetHoleId is Id, magnetRadius is ValueWithUnits, leadInSize is ValueWithUnits) {
const bottomLoop = qLoopEdges(findFace(context, base.id, Orientation.BOTTOM));
const magnetHoleEdges = qCreatedBy(magnetHoleId + 'Substract', EntityType.EDGE);
const magnetEntryEdges = qIntersection(bottomLoop, magnetHoleEdges);
opChamfer(context, id + 'MagnetsLeadIn', {
'entities' : magnetEntryEdges,
'chamferType' : ChamferType.EQUAL_OFFSETS,
'width' : min(leadInSize, magnetRadius / 2),
});
}
function baseLayer4Sketch(context is Context, definition is map, id is Id, layer3Extrude is map) {
const sketchId = id + 'Sketch';
const sketch = newSketch(context, sketchId, {
'sketchPlane' : findFace(context, layer3Extrude.id, Orientation.TOP)
});
const initXY = -baseCalculateOffset();
const endX = initXY + (definition.unitSize * definition.columns) - (Dims.unitSeparator * 2);
const endY = initXY + (definition.unitSize * definition.rows) - (Dims.unitSeparator * 2);
skRectangle(sketch, 'rectangle', {
'firstCorner': vector(initXY, initXY),
'secondCorner': vector(endX, endY)
});
skSolve(sketch);
return { 'id': sketchId, 'region': qSketchRegion(sketchId, false) };
}
function baseCalculateBottomSize(definition is map) {
return definition.unitSize - ((Dims.baseLayer1Height + Dims.baseLayer3Height + Dims.unitSeparator) * 2);
}
function baseHasEnoughSizeForHoles(definition is map, radius is ValueWithUnits, easyRemover is boolean) {
const bottomSize = baseCalculateBottomSize(definition);
const minimumInset = radius + Dims.baseHoleMinimumWall;
const minimumCenterDistance = baseHoleMinimumCenterDistance(radius, easyRemover);
const centerDistance = bottomSize - (Dims.baseHoleClearance * 2);
return Dims.baseHoleClearance >= minimumInset && centerDistance >= minimumCenterDistance;
}
function baseHoleMinimumCenterDistance(radius is ValueWithUnits, easyRemover is boolean) {
var minimumDistance = (radius * 2) + Dims.baseHoleMinimumGap;
if (easyRemover) {
const removerOffset = radius * (sqrt(2) / 2);
minimumDistance = max(
minimumDistance,
((removerOffset + Dims.baseHoleRemoverRadius) * 2) + Dims.baseHoleMinimumGap
);
}
return minimumDistance;
}
function baseCalculateOffset() {
return Dims.baseLayer1Height + Dims.baseLayer3Height;
}
/**
*
* Functions to create the body of the bin
*
*/
function bodyCreate(context is Context, definition is map, id is Id, base is map) {
const topFace = findFace(context, base.layer4Id, Orientation.TOP);
const bodyExtrude = wallExtrude(context, id + 'Body', topFace, {
depth: Dims.unitHeight * (definition.height - 1),
});
if (!definition.filled) {
const sketch = bodyHollowSketch(context, definition, id + 'Body', base);
const hollowExtrude = wallExtrude(context, id + 'Hollow', sketch.region, {
depth: Dims.unitHeight * (definition.height - 1),
filletRadius: Dims.bodyInternalFillet,
});
substractParts(context, id, bodyExtrude.id, [hollowExtrude.id]);
removeBodies(context, id + 'DeleteBodyHollowSketch', [sketch.id]);
}
return { 'id': bodyExtrude.id };
}
function bodyHollowSketch(context is Context, definition is map, id is Id, base is map) {
const sketchId = id + 'Sketch';
const sketch = newSketch(context, sketchId, {
'sketchPlane' : findFace(context, base.layer4Id, Orientation.TOP)
});
var fingerSlideOffset = 0 * millimeter;
if (definition.fingerSlide) {
fingerSlideOffset = max(0 * millimeter, Dims.topStackableLipWidth - definition.bodyWallThicknes);
}
const offset = -baseCalculateOffset();
const initX = offset + definition.bodyWallThicknes;
const initY = offset + definition.bodyWallThicknes + fingerSlideOffset;
const endX = offset - definition.bodyWallThicknes + (definition.unitSize * definition.columns) - (Dims.unitSeparator * 2);
const endY = offset - definition.bodyWallThicknes + (definition.unitSize * definition.rows) - (Dims.unitSeparator * 2);
skRectangle(sketch, 'rectangle', {
'firstCorner': vector(initX, initY),
'secondCorner': vector(endX, endY)
});
skSolve(sketch);
return { 'id': sketchId, 'region': qSketchRegion(sketchId, false) };
}
/**
*
* Functions to create the top of the bin
*
*/
function topCreate(context is Context, definition is map, id is Id, body is map) {
const topFace = findFace(context, body.id, Orientation.TOP);
const topId = id + 'Top';
// If the bin should be filled completely, we extrude the top (stackable lip is implicit)
if (definition.filled && definition.fillType == FillType.COMPLETE) {
wallExtrude(context, topId, topFace, {
'depth': Dims.topHeight,
});
return { 'id': topId };
}
// If there is no stackable lip, total height is 7 * nU (no extra topHeight needed)
if (!definition.stackableLip) {
return { 'id': undefined };
}
// Otherwise we prepare the sweep to create the lid
const lipSketch = topLipSketch(context, definition, id + 'Lip', body);
const topLoop = qIntersection(qCreatedBy(body.id, EntityType.EDGE), qLoopEdges(topFace));
opSweep(context, topId, {
'profiles': lipSketch.region,
'path': topLoop,
});
// The rounded shape needs a fillet
if (definition.lipShape == TopLipShape.ROUNDED) {
const lipLoop = qLoopEdges(findFace(context, topId, Orientation.TOP));
const leftFaceLoop = qLoopEdges(findFace(context, topId, Orientation.LEFT));
const lipLeftEdge = qIntersection(lipLoop, leftFaceLoop);
opFillet(context, id + 'TopFillet', {
'entities': lipLeftEdge,
'radius' : Dims.topStackableLipRoundedFillet,
'tangentPropagation': true,
});
}
removeBodies(context, id + 'DeleteLipSketch', [lipSketch.id]);
return { 'id': topId };
}
function topLipSketch(context is Context, definition is map, id is Id, top is map) {
const sketchId = id + 'Sketch';
const tangentPlane = evFaceTangentPlane(context, {
'face': findFace(context, top.id, Orientation.TOP),
'parameter': vector(0.5, 0)
});
// We need a plane perpendicular to the lid to create the sketch for the sweep
const perpendicularPlane = plane(
tangentPlane.origin,
-tangentPlane.x,
tangentPlane.normal
);
const sketch = newSketchOnPlane(context, sketchId, {
'sketchPlane' : perpendicularPlane
});
const x = LID_SWEEP[definition.lipShape]['x'];
const y = LID_SWEEP[definition.lipShape]['y'];
for (var i = 0; i != size(x)-1; i += 1) {
skLineSegment(sketch, 'Line' ~ i, {
'start' : vector(x[i], y[i]) * millimeter,
'end' : vector(x[i+1], y[i+1]) * millimeter
});
}
skSolve(sketch);
return { 'id': sketchId, 'region': qSketchRegion(sketchId, false) };
}
/**
*
* Functions to create the label
*
*/
function labelCreate(context is Context, definition is map, id is Id, base is map) {
const extrudeId = id + 'LabelExtrude';
const labelSketch = labelSketch(context, definition, id + 'LabelSketch', base);
const rightFace = findFace(context, base.layer4Id, Orientation.RIGHT);
opExtrude(context, extrudeId, {
'entities' : labelSketch.region,
'direction' : evPlane(context, {'face' : Planes.right}).normal,
'endBound' : BoundingType.UP_TO_FACE,
'endBoundEntity' : rightFace
});
removeBodies(context, id + 'DeleteLabelSketch', [labelSketch.id]);
return { 'id': extrudeId };
}
function labelSketch(context is Context, definition is map, id is Id, base is map) {
const sketchId = id + 'Sketch';
const internalLoop = qLoopEdges(findFace(context, base.layer4Id, Orientation.TOP));
const frontAndBackEdges = qParallelEdges(
internalLoop,
evPlane(context, {'face' : Planes.front}).x
);
const backEdge = findExtremeEdge(context, frontAndBackEdges, Orientation.BACK);
const backLine = evEdgeTangentLine(context, { 'edge': backEdge, 'parameter': 0 });
const sketch = newSketchOnPlane(context, sketchId, {
'sketchPlane' : plane(
backLine.origin - vector(Dims.bodyInternalFillet, 0 * millimeter, 0 * millimeter),
evPlane(context, {'face' : Planes.right}).normal
)
});
var heightToTop = (definition.height - 1) * Dims.unitHeight;
if (definition.stackableLip) {
heightToTop = heightToTop + Dims.topHeight - Dims.topStackableLipHeight;
}
skSafeOverhangTriangle(
sketch,
0 * millimeter,
heightToTop - definition.labelOffset,
-definition.labelWidth
);
skSolve(sketch);
return { 'id': sketchId, 'region': qSketchRegion(sketchId, false) };
}
/**
*
* Functions to create the finger slide
*
*/
function fingerSlideCreate(context is Context, definition is map, id is Id, base is map) {
const internalLoop = qLoopEdges(findFace(context, base.layer4Id, Orientation.TOP));
const frontAndBackEdges = qParallelEdges(
internalLoop,
evPlane(context, {'face' : Planes.front}).x
);
const frontEdge = findExtremeEdge(context, frontAndBackEdges, Orientation.FRONT);
if (definition.fingerSlideType == FingerSlideType.ROUNDED) {
opFillet(context, id + 'TopFillet', {
'entities': frontEdge,
'radius' : definition.fingerSlideHeight,
'tangentPropagation': false,
});
} else if (definition.fingerSlideType == FingerSlideType.CHAMFER) {
opChamfer(context, id + 'To', {
'entities' : frontEdge,
'chamferType' : ChamferType.EQUAL_OFFSETS,
'width' : definition.fingerSlideHeight,
});
} else {
throw 'FingerSlideType not implemented: ' ~ definition.fingerSlideType;
}
}
/**
*
* Helper functions
*
*/
function skSafeOverhangTriangle(sketch is Sketch, point0 is ValueWithUnits, point1 is ValueWithUnits, point2 is ValueWithUnits) {
skLineSegment(sketch, 'Line' ~ 1, {
'start' : vector(point0, point1),
'end' : vector(point2, point1)
});
skLineSegment(sketch, 'Line' ~ 2, {
'start' : vector(point2, point1),
'end' : vector(point0, point0 + point1 + point2)
});
skLineSegment(sketch, 'Line' ~ 3, {
'start' : vector(point0, point0 + point1 + point2),
'end' : vector(point0, point1)
});
}
function wallExtrude(context is Context, id is Id, face is Query, config is map) {
const extrudeId = id + 'Extrude';
opExtrude(context, extrudeId, {
'entities' : face,
'direction' : evPlane(context, {'face' : Planes.top}).normal,
'endBound' : BoundingType.BLIND,
'endDepth' : config.depth
});
if (config.filletRadius != undefined) {
const edges = qParallelEdges(
qCreatedBy(id, EntityType.EDGE),
evPlane(context, {'face' : Planes.top}).normal
);
opFillet(context, id + 'Fillet', {
'entities' : edges,
'radius' : config.filletRadius
});
}
if (config.draftAngle != undefined) {
const rightFaces = qParallelPlanes(
qCreatedBy(extrudeId, EntityType.FACE),
evPlane(context, {'face' : Planes.right})
);
const frontFaces = qParallelPlanes(
qCreatedBy(extrudeId, EntityType.FACE),
evPlane(context, {'face' : Planes.front})
);
opDraft(context, id + 'Draft', {
'draftType' : DraftType.REFERENCE_SURFACE,
'draftFaces' : qUnion(rightFaces, frontFaces),
'referenceSurface': face,
'pullVec' : vector(0, 0, -1),
'angle' : config.draftAngle
});
}
return { 'id': extrudeId };
}
function mergeParts(context is Context, id is Id, parts is array) {
var finalParts = [];
var firstPartId = undefined;
for (var part in parts) {
if (part != undefined && part.id != undefined) {
finalParts = append(finalParts, qCreatedBy(part.id, EntityType.BODY));
if (firstPartId == undefined) {
firstPartId = part.id;
}
}
}
opBoolean(context, id + 'Union', {
operationType: BooleanOperationType.UNION,
tools: qUnion(finalParts)
});
// In an union, no new part is created, so we return the first part that's defined
return { 'id': firstPartId };
}
function substractParts(context is Context, id is Id, targetId is Id, partIds is array) {
var parts = [];
var firstPartId = undefined;
for (var partId in partIds) {
if (partId != undefined) {
parts = append(parts, qCreatedBy(partId, EntityType.BODY));
if (firstPartId == undefined) {
firstPartId = partId;
}
}
}
opBoolean(context, id + 'Substract', {
operationType: BooleanOperationType.SUBTRACTION,
targets: qCreatedBy(targetId, EntityType.BODY),
tools: qUnion(parts)
});
// In a removal, no new part is created, so we return the first part that's defined
return { 'id': firstPartId };
}
function findFace(context is Context, id is Id, face is Orientation) {
const allFaces = evaluateQuery(context, qCreatedBy(id, EntityType.FACE));
var vectorValue = undefined;
if (face == Orientation.TOP) {
vectorValue = vector(0, 0, 1);
} else if (face == Orientation.BOTTOM) {
vectorValue = vector(0, 0, -1);
} else if (face == Orientation.LEFT) {
vectorValue = vector(-1, 0, 0);
} else if (face == Orientation.RIGHT) {
vectorValue = vector(1, 0, 0);
} else {
throw 'Orientation not implemented: ' ~ face;
}
for (var f in allFaces) {
const plane = evFaceTangentPlane(context, { 'face': f, parameter: vector(0.5, 0.5) });
if (plane.normal == vectorValue) {
return f;
}
}
debug(context, 'No ' ~ face ~ ' face found for ' ~ id, DebugColor.RED);
debug(context, allFaces);
return undefined;
}
function findExtremeEdge(context is Context, edgeQuery is Query, direction is Orientation) returns Query {
const edges = evaluateQuery(context, edgeQuery);
const directionMap = {
Orientation.LEFT: { 'dimension': 0, 'minimize': false },
Orientation.RIGHT: { 'dimension': 0, 'minimize': true },
Orientation.BACK: { 'dimension': 1, 'minimize': false },
Orientation.FRONT: { 'dimension': 1, 'minimize': true },
Orientation.BOTTOM: { 'dimension': 2, 'minimize': false },
Orientation.TOP: { 'dimension': 2, 'minimize': true }
};
const dim = directionMap[direction].dimension;
const minimize = directionMap[direction].minimize;
if (dim == undefined || minimize == undefined) {
throw 'Direction not implemented ' ~ direction;
}
var bestPoint = (minimize ? inf : -inf) * millimeter;
var bestEdge = undefined;
for (var edge in edges) {
const midPoint = evEdgeTangentLine(context, {
'edge': edge,
'parameter': 0.5
}).origin[dim];
if ((minimize && midPoint < bestPoint) || (!minimize && midPoint > bestPoint)) {
bestPoint = midPoint;
bestEdge = edge;
}
}
return bestEdge == undefined ? qNothing() : qUnion([bestEdge]);
}
function removeBodies(context is Context, id is Id, idsToRemove is array) {
var finalIds = [];
for (var idToRemove in idsToRemove) {
if (idToRemove != undefined) {
finalIds = append(finalIds, qCreatedBy(idToRemove, EntityType.BODY));
}
}
opDeleteBodies(context, id + 'DeleteBodies', {
entities: qUnion(finalIds)
});
}
function centerPart(context is Context, id is Id, partId is Id) {
const part = qCreatedBy(partId, EntityType.BODY);
const boxPart = evBox3d(context, {
'topology': qCreatedBy(partId, EntityType.BODY)
});