-
Notifications
You must be signed in to change notification settings - Fork 2
/
cylinderCast.fs
534 lines (472 loc) · 19.2 KB
/
cylinderCast.fs
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
FeatureScript ✨; /* Automatically generated version */
// This module is part of the FeatureScript Standard Library and is distributed under the MIT License.
// See the LICENSE tab for the license text.
// Copyright (c) 2013-Present PTC Inc.
/*
* Utility functions used by hole feature
* Finds the projection of a cylinder against a part.
*/
import(path : "onshape/std/boolean.fs", version : "✨");
import(path : "onshape/std/boundingtype.gen.fs", version : "✨");
import(path : "onshape/std/box.fs", version : "✨");
import(path : "onshape/std/clashtype.gen.fs", version : "✨");
import(path : "onshape/std/containers.fs", version : "✨");
import(path : "onshape/std/coordSystem.fs", version : "✨");
import(path : "onshape/std/evaluate.fs", version : "✨");
import(path : "onshape/std/extrude.fs", version : "✨");
import(path : "onshape/std/feature.fs", version : "✨");
import(path : "onshape/std/mathUtils.fs", version : "✨");
import(path : "onshape/std/sketch.fs", version : "✨");
import(path : "onshape/std/surfaceGeometry.fs", version : "✨");
import(path : "onshape/std/tool.fs", version : "✨");
import(path : "onshape/std/string.fs", version : "✨");
import(path : "onshape/std/units.fs", version : "✨");
/**
* @internal
*/
export function cylinderCast(context is Context, idIn is Id, arg is map)
precondition
{
isLength(arg.distance);
isLength(arg.diameter);
arg.cSys is CoordSystem;
arg.isFront is boolean;
arg.findClosest is boolean;
arg.scope is Query;
}
{
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V362_HOLE_IMPROVED_DEPTH_FINDING))
{
return cylinderCast_rev_2(context, idIn, arg);
}
else if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V299_HOLE_FEATURE_FIX_BLIND_IN_LAST_FLIP))
{
return cylinderCast_rev_1(context, idIn, arg);
}
else
{
return cylinderCast_rev_0(context, idIn, arg);
}
}
/*
* Deprecated, too many repairs, starting new function
*/
function cylinderCast_rev_0(context is Context, idIn is Id, arg is map) returns map
{
const shotName = arg.isFront ? "shot_front" : "shot_back";
const id = idIn + shotName;
const distance = arg.distance;
var direction = arg.cSys.zAxis;
if (!arg.isFront)
direction = -direction;
const startingCSys = coordSystem(arg.cSys.origin - distance * direction, arg.cSys.xAxis, direction);
const sketchPlane = plane(startingCSys.origin, direction); // plane is rotated
//------------- Create profile ----------------
const sketchName = "raySketch";
const sketch = newSketchOnPlane(context, id + sketchName, { "sketchPlane" : sketchPlane });
skCircle(sketch, "circle", { "center" : vector(0, 0) * meter, "radius" : arg.diameter / 2 });
skSolve(sketch);
var bestDist = undefined;
var foundTarget = undefined;
const AOL_HOLE_FEATURE_HANDLE_FAILED_EXTRUDE = isAtVersionOrLater(context, FeatureScriptVersionNumber.V208_HOLE_FEATURE_HANDLE_FAILED_EXTRUDE);
const AOL_HOLE_FEATURE_FIX_BROKEN_DEPTH_CALC = isAtVersionOrLater(context, FeatureScriptVersionNumber.V225_HOLE_FEATURE_FIX_BROKEN_DEPTH_CALC);
// Fix for 208 was too broad, don't fix it here, fix later
if (AOL_HOLE_FEATURE_HANDLE_FAILED_EXTRUDE && !AOL_HOLE_FEATURE_FIX_BROKEN_DEPTH_CALC)
{
bestDist = 0 * meter;
}
const targets = evaluateQuery(context, arg.scope);
if (size(targets) == 0)
{
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V286_HOLE_ADD_STANDARDS))
return { "distance" : 0 * meter };
else
throw "No targets";
}
var shotNum = -1;
const sketchEntityQuery = sketchEntityQuery(id + (sketchName ~ ".wireOp"), EntityType.EDGE, "circle");
const curves = evaluateQuery(context, sketchEntityQuery);
for (var targetQuery in targets)
{
shotNum += 1;
const operationId = id + shotNum;
try {
extrude(context, operationId, {
"bodyType" : ExtendedToolBodyType.SURFACE,
"operationType" : NewBodyOperationType.NEW,
"surfaceEntities" : qUnion([sketchEntityQuery]),
"endBound" : BoundingType.UP_TO_BODY,
"endBoundEntityBody" : qUnion([targetQuery])
});
} catch (e) {
if (AOL_HOLE_FEATURE_HANDLE_FAILED_EXTRUDE)
{
continue;
}
else
{
throw e;
}
}
const bodyQuery = qCreatedBy(operationId, EntityType.BODY);
const cylBox = evBox3d(context, { "topology" : bodyQuery, "cSys" : arg.cSys } );
var d = arg.isFront ? cylBox.maxCorner[2] : cylBox.minCorner[2];
// This is the front plane clamp to zero
const isMinimumCase = (arg.findClosest && arg.isFront) || (!arg.findClosest && !arg.isFront);
if (AOL_HOLE_FEATURE_FIX_BROKEN_DEPTH_CALC && isMinimumCase && d < 0) {
d = 0 * meter;
}
if (AOL_HOLE_FEATURE_FIX_BROKEN_DEPTH_CALC) {
const collisions = evCollision(context, {'targets' : targetQuery, 'tools' : bodyQuery});
for (var collision in collisions) {
if (collision['type'] == ClashType.INTERFERE) {
// If the shot cylinder interferes with the target, then we began the shot on the interior,
// fall back to starting from the sketch plane.
d = 0 * meter;
break;
}
}
}
if (bestDist == undefined)
{
bestDist = d;
foundTarget = targetQuery;
}
else if (isMinimumCase)
{
if (d < bestDist)
{
bestDist = d;
foundTarget = targetQuery;
}
}
else
{
if (d > bestDist) {
foundTarget = targetQuery;
bestDist = d;
}
}
opDeleteBodies(context, id + ("delete_" ~ shotNum), { "entities" : bodyQuery });
}
opDeleteBodies(context, id + "delete", { "entities" : qCreatedBy(id + sketchName, EntityType.BODY) });
if (AOL_HOLE_FEATURE_FIX_BROKEN_DEPTH_CALC) {
if (bestDist == undefined) {
bestDist = 0 * meter;
}
} else if (bestDist == undefined) {
throw "Hole not entirely on one body.";
}
return { "distance" : bestDist, "target" : foundTarget };
}
/*
* Deprecated, new version returns all results in an array
*/
function cylinderCast_rev_1(context is Context, idIn is Id, arg is map) returns map
{
const targets = evaluateQuery(context, arg.scope);
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V314_HOLE_FIX_CYL_CAST_EARLY_RETURN) && size(targets) == 0)
{
return { "distance" : 0 * meter };
}
const shotName = arg.isFront ? "shot_front" : "shot_back";
const id = idIn + shotName;
var direction = arg.cSys.zAxis;
if (!arg.isFront)
{
direction = -direction;
}
const cSys = coordSystem(arg.cSys.origin - arg.distance * direction, arg.cSys.xAxis, direction);
// Small but finite distance.
// A large distance does not work when trying to drill out from the inside of a box.
// A zero distance produces no surface for the cast if the the target is a planar face parallel to the sketch
const smallOffset = 0.01 * millimeter;
const sketchPlane = plane(cSys.origin - smallOffset * direction, direction); // plane is rotated
var bestDist = undefined;
var foundTarget = undefined;
//------------- Create profile ----------------
startFeature(context, id, {});
const sketchName = "raySketch";
const sketch = newSketchOnPlane(context, id + sketchName, { "sketchPlane" : sketchPlane });
skCircle(sketch, "circle", { "center" : vector(0, 0) * meter, "radius" : arg.diameter / 2 });
skSolve(sketch);
// Legacy code, left for older versions. Never hit in later versions because return call above
if (size(targets) == 0)
{
return { "distance" : 0 * meter };
}
const sketchEntityQuery = qCreatedBy(id + (sketchName ~ ".wireOp"), EntityType.EDGE);
var shotNum = 0;
for (var targetQuery in targets)
{
const extrudeDefinition = {
"bodyType" : ExtendedToolBodyType.SURFACE,
"operationType" : NewBodyOperationType.NEW,
"surfaceEntities" : sketchEntityQuery,
"endBound" : BoundingType.UP_TO_BODY,
"endBoundEntityBody" : targetQuery
};
const extrudeId = id + ("extrude_" ~ shotNum);
shotNum += 1;
var d;
var hasCollision is boolean = false;
try
{
extrude(context, extrudeId, extrudeDefinition);
const bodyQuery = qCreatedBy(extrudeId, EntityType.BODY);
const cylBox = evBox3d(context, { "topology" : bodyQuery, "cSys" : cSys } );
d = cylBox.maxCorner[2];
const collisions = evCollision(context, {'targets' : targetQuery, 'tools' : bodyQuery});
for (var collision in collisions) {
if (collision['type'] == ClashType.INTERFERE) {
// If the shot cylinder interferes with the target, then we began the shot on the interior,
// fall back to starting from the sketch plane.
hasCollision = true;
}
}
}
if (hasCollision)
{
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V306_HOLE_FEATURE_FIX_UNDETERMINED_TARGET_BODY))
{
// Fix, targets with collisions indicate the hole started inside the body and should be ignored
continue;
}
else
{
bestDist = 0 * meter;
foundTarget = targetQuery;
break;
}
}
if ((d != undefined) && ((bestDist == undefined) || (arg.findClosest ? d < bestDist : d > bestDist)))
{
bestDist = d;
foundTarget = targetQuery;
}
}
abortFeature(context, id);
if (bestDist == undefined || bestDist < 0) {
bestDist = 0 * meter;
}
if (!arg.isFront)
{
const p = cSys.origin + bestDist * cSys.zAxis;
bestDist = (p - arg.cSys.origin).dot(arg.cSys.zAxis);
}
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V306_HOLE_FEATURE_FIX_UNDETERMINED_TARGET_BODY) && foundTarget == undefined)
{
if (size(targets) == 1)
{
foundTarget = targets[0];
}
else
{
throw regenError(ErrorStringEnum.HOLE_CANNOT_DETERMINE_LAST_BODY);
}
}
return { "distance" : bestDist, "target" : foundTarget };
}
function cylinderCast_rev_2(context is Context, idIn is Id, arg is map) returns array
{
const targets = evaluateQuery(context, arg.scope);
const numberOfTargets = size(targets);
if (numberOfTargets == 0)
{
return [];
}
const shotName = arg.isFront ? "shot_front" : "shot_back";
const id = idIn + shotName;
var direction = arg.cSys.zAxis;
if (!arg.isFront)
{
direction = -direction;
}
const cSys = coordSystem(arg.cSys.origin - arg.distance * direction, arg.cSys.xAxis, direction);
// Small but finite distance.
// A large distance does not work when trying to drill out from the inside of a box.
// A zero distance produces no surface for the cast if the the target is a planar face parallel to the sketch
const smallOffset = 0.01 * millimeter;
const sketchPlane = plane(cSys.origin - smallOffset * direction, direction); // plane is rotated
startFeature(context, id, {});
const sketchName = "raySketch";
const sketch = newSketchOnPlane(context, id + sketchName, { "sketchPlane" : sketchPlane });
var doRecalculateFirstBodyDistance is boolean = false;
if (numberOfTargets == 1 && arg.firstBodyCastDiameter != undefined)
{
// only 1 body, use the specified first body cast diameter for the one and only cast entity needed
skCircle(sketch, "circle1", { "center" : vector(0, 0) * meter, "radius" : arg.firstBodyCastDiameter / 2 });
}
else
{
// the first and maybe only cast entity
skCircle(sketch, "circle1", { "center" : vector(0, 0) * meter, "radius" : arg.diameter / 2 });
}
if (numberOfTargets > 1 && arg.firstBodyCastDiameter != undefined)
{
// will need a second cast to recalculate the first body starting distance, sketch the circle that will be used for that
skCircle(sketch, "circle2", { "center" : vector(0, 0) * meter, "radius" : arg.firstBodyCastDiameter / 2 });
doRecalculateFirstBodyDistance = true;
}
skSolve(sketch);
var query = sketchEntityQuery(id + (sketchName ~ ".wireOp"), EntityType.EDGE, "circle1");
var result = getBodyHitDistances(context, id, cSys, query, direction, targets);
// did we need a second cast to recalculate the first body starting distance?
if (doRecalculateFirstBodyDistance && size(result) > 0)
{
query = sketchEntityQuery(id + (sketchName ~ ".wireOp"), EntityType.EDGE, "circle2");
var result2 = getBodyHitDistances(context, id + "2", cSys, query, direction, [result[0].target]);
// got it?
if (size(result2) > 0 && result[0].target == result2[0].target)
{
// update the new hit point distance on the first body
result[0].distance = result2[0].distance;
}
}
abortFeature(context, id);
return result;
}
/**
* @internal
* Extrudes the specified entities query up to all the specified body targets and returns the distance from the
* specified coordinate system to the farthest hit point of each body. It is assumed a start feature operation
* has already been started.
* @param context {Context} : The target context.
* @param id {Id}: identifier of the feature
* @param cSys {Coord} : the coordinate system used to define the bounding box of the extrude to determine the hit
* point distances
* @param entitiesToExtrude {Query} : the entities query used for the extrude
* @param extrudeDirection {Vector} : the direction of the extrude
* @param targets {array} : array of bodies to determine the hit point distances for
* @returns {array} : array of maps containing the body target ("target") and the farthest hit point distance to that
* target ("distance"). The array will be returned sorted from closest target body to farthest target body.
*/
function getBodyHitDistances(context is Context, id is Id, cSys is CoordSystem, entitiesToExtrude is Query, extrudeDirection is Vector, targets is array) returns array
{
var shotNum = 0;
var result = [];
for (var targetQuery in targets)
{
const extrudeId = id + ("extrude_" ~ shotNum);
shotNum += 1;
var d;
var hasCollision is boolean = false;
try silent
{
var extrudeDefinition;
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V364_HOLE_FIX_FEATURE_MIRROR))
{
// New version using opextrude so as to use correct normal vector
extrudeDefinition = {
"entities" : entitiesToExtrude,
"direction" : extrudeDirection,
"endBound" : BoundingType.UP_TO_BODY,
"endBoundEntity" : targetQuery
};
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V429_HOLE_SAFE_SKETCH_CLEANUP))
try silent(opExtrude(context, extrudeId, extrudeDefinition));
else
opExtrude(context, extrudeId, extrudeDefinition);
}
else
{
// Old version calling feature extrude.
extrudeDefinition = {
"bodyType" : ExtendedToolBodyType.SURFACE,
"operationType" : NewBodyOperationType.NEW,
"surfaceEntities" : entitiesToExtrude,
"endBound" : BoundingType.UP_TO_BODY,
"endBoundEntityBody" : targetQuery
};
extrude(context, extrudeId, extrudeDefinition);
}
const bodyQuery = qCreatedBy(extrudeId, EntityType.BODY);
const cylBox = evBox3d(context, { "topology" : bodyQuery, "cSys" : cSys } );
d = cylBox.maxCorner[2];
const collisions = evCollision(context, {'targets' : targetQuery, 'tools' : bodyQuery});
for (var collision in collisions) {
if (collision['type'] == ClashType.INTERFERE) {
// If the shot cylinder interferes with the target, then we began the shot on the interior,
// fall back to starting from the sketch plane.
hasCollision = true;
}
}
}
if (hasCollision)
{
// Fix, targets with collisions indicate the hole started inside the body and should be ignored
continue;
}
if (d != undefined)
{
result = append(result, { "distance" : d, "target" : targetQuery });
}
}
if (size(result) > 1) {
result = sort(result, function(a, b) {
return a.distance - b.distance;
});
}
return result;
}
/**
* @internal
* Returns the result of casting a cylinder from front and or back of the scope.
*/
export function cylinderCastBiDirectional(context is Context, id is Id, arg is map) returns map
precondition
{
arg.cSys is CoordSystem;
isLength(arg.diameter);
arg.scope is Query;
arg.needFront is boolean || arg.needFront is undefined; // default true
arg.needBack is boolean || arg.needBack is undefined; // default true
}
{
const smallFrontOffset = isAtVersionOrLater(context, FeatureScriptVersionNumber.V299_HOLE_FEATURE_FIX_BLIND_IN_LAST_FLIP) ? 0 * meter : 0.01 * millimeter;
var resultFront = [];
if (arg.needFront != false)
{
try {
resultFront = cylinderCast(context, id, {
"distance" : smallFrontOffset,
"cSys" : arg.cSys,
"isFront" : true,
"findClosest" : true,
"diameter" : arg.diameter,
"firstBodyCastDiameter" : arg.firstBodyCastDiameter,
"scope" : arg.scope });
}
}
// The back of the part uses a large distance until we can find a better method.
var resultBack = [];
if (arg.needBack != false)
{
try {
resultBack = cylinderCast(context, id, {
"distance" : arg.scopeSize,
"cSys" : arg.cSys,
"isFront" : false,
"findClosest" : true,
"diameter" : arg.diameter,
"scope" : arg.scope });
}
}
var result = {};
result.resultFront = constructResult(resultFront);
result.resultBack = constructResult(resultBack);
return result;
}
function constructResult(data) returns array
{
if (data is array)
{
return data;
}
else
{
if (data.distance == undefined)
data.distance = 0 * meter;
return [data];
}
}