-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextend.fs
456 lines (410 loc) · 17.5 KB
/
extend.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
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.
// Imports used in interface
export import(path : "onshape/std/query.fs", version : "✨");
export import(path : "onshape/std/tool.fs", version : "✨");
// Features using manipulators must export manipulator.fs.
export import(path : "onshape/std/manipulator.fs", version : "✨");
// Imports used internally
import(path : "onshape/std/containers.fs", version : "✨");
import(path : "onshape/std/evaluate.fs", version : "✨");
import(path : "onshape/std/feature.fs", version : "✨");
import(path : "onshape/std/primitives.fs", version : "✨");
import(path : "onshape/std/surfaceGeometry.fs", version : "✨");
import(path : "onshape/std/valueBounds.fs", version : "✨");
import(path : "onshape/std/vector.fs", version : "✨");
export import(path : "onshape/std/extendendtype.gen.fs", version : "✨");
export import(path : "onshape/std/extendsheetshapetype.gen.fs", version : "✨");
/**
* Bounding type used with extend.
*/
export enum ExtendBoundingType
{
annotation { "Name" : "Blind" }
BLIND,
annotation { "Name" : "Up to face" }
UP_TO_FACE,
annotation { "Name" : "Up to part/surface" }
UP_TO_BODY,
annotation { "Name" : "Up to vertex" }
UP_TO_VERTEX
}
const targetBounds = 100 * meter;
/**
* Extends a surface body by calling [opExtendSheetBody].
*/
annotation { "Feature Type Name" : "Move boundary", "Manipulator Change Function" : "extendManipulatorChange" }
export const extendSurface = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation { "Name" : "Surface or boundary edges", "Filter" : ((EntityType.BODY && BodyType.SHEET) ||
(EntityType.EDGE && EdgeTopology.ONE_SIDED)) &&
ConstructionObject.NO && SketchObject.NO && ModifiableEntityOnly.YES }
definition.entities is Query;
annotation { "Name" : "Tangent propagation", "Default" : true }
definition.tangentPropagation is boolean;
annotation { "Name" : "Move end condition" }
definition.endCondition is ExtendBoundingType;
if (definition.endCondition == ExtendBoundingType.BLIND)
{
annotation { "Name" : "Opposite direction", "UIHint" : UIHint.OPPOSITE_DIRECTION }
definition.oppositeDirection is boolean;
annotation { "Name" : "Distance" }
isLength(definition.extendDistance, LENGTH_BOUNDS);
}
else if (definition.endCondition == ExtendBoundingType.UP_TO_BODY)
{
annotation { "Name" : "Target", "Filter" : EntityType.BODY && (BodyType.SOLID || BodyType.SHEET) && SketchObject.NO && AllowMeshGeometry.YES, "MaxNumberOfPicks" : 1 }
definition.targetPart is Query;
}
else if (definition.endCondition == ExtendBoundingType.UP_TO_FACE)
{
annotation { "Name" : "Target", "Filter" : (EntityType.FACE && SketchObject.NO) || BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 1 }
definition.targetFace is Query;
}
else if (definition.endCondition == ExtendBoundingType.UP_TO_VERTEX)
{
annotation { "Name" : "Target", "Filter" : QueryFilterCompound.ALLOWS_VERTEX, "MaxNumberOfPicks" : 1 }
definition.targetVertex is Query;
}
annotation {"Name" : "Maintain curvature"}
definition.maintainCurvature is boolean;
}
{
verifyNoMesh(context, definition, "entities");
if (definition.maintainCurvature)
definition.extensionShape = ExtendSheetShapeType.SOFT;
else
definition.extensionShape = ExtendSheetShapeType.LINEAR;
if (definition.endCondition == ExtendBoundingType.BLIND)
{
definition.endCondition = ExtendEndType.EXTEND_BLIND;
if (definition.oppositeDirection)
{
definition.extendDistance *= -1;
}
try(addExtendManipulator(context, id, definition));
if (definition.extendDistance > 0)
{
opExtendSheetBody(context, id, definition);
}
else //use edge change for trimming back
{
const trackedEdges = getTrackedEdges(context, definition);
var edgeChangeOptions = [];
for (var i = 0; i < size(trackedEdges); i += 1)
{
var edge = trackedEdges[i];
edgeChangeOptions = append(edgeChangeOptions, { "edge" : edge,
"face" : qAdjacent(edge, AdjacencyType.EDGE, EntityType.FACE),
"offset" : definition.extendDistance });
}
if (edgeChangeOptions == [])
{
throw regenError(ErrorStringEnum.EXTEND_SHEET_BODY_NO_BODY, ["entities"]);
}
trimEdges(context, id + "edgeChange", edgeChangeOptions);
}
}
else //up to target => up to face,part,vertex
{
verifyNonemptyQuery(context, definition, "entities", ErrorStringEnum.EXTEND_SHEET_BODY_NO_BODY);
var toDelete = [];
definition.target = definition.targetPart;
var errorEntityString = "targetPart";
if (definition.endCondition == ExtendBoundingType.UP_TO_FACE)
{
verifyNonemptyQuery(context, definition, "targetFace", ErrorStringEnum.EXTEND_SHEET_BODY_NO_TARGET);
definition.target = getTargetFromFace(context, id, definition);
toDelete = append(toDelete, definition.target);
errorEntityString = "targetFace";
}
else if (definition.endCondition == ExtendBoundingType.UP_TO_VERTEX)
{
verifyNonemptyQuery(context, definition, "targetVertex", ErrorStringEnum.EXTEND_SHEET_BODY_NO_TARGET);
definition.target = getTargetFromVertex(context, id, definition);
toDelete = append(toDelete, definition.target);
errorEntityString = "targetVertex";
}
if (isQueryEmpty(context, definition.target))
{
throw regenError(ErrorStringEnum.EXTEND_SHEET_BODY_NO_TARGET, [errorEntityString]);
}
definition.endCondition = ExtendEndType.EXTEND_TO_TARGET;
try
{
extendToTarget(context, id, definition);
if (!isQueryEmpty(context, qUnion(toDelete)))
{
opDeleteBodies(context, id + "deleteBodiesCleanup", { "entities" : qUnion(toDelete) });
}
}
catch(e)
{
if (!isQueryEmpty(context, qUnion(toDelete)))
{
opDeleteBodies(context, id + "deleteBodiesCleanup", { "entities" : qUnion(toDelete) });
}
throw e;
}
}
}, { oppositeDirection : false, tangentPropagation : true, endCondition : ExtendBoundingType.BLIND, maintainCurvature : false });
function extendToTarget(context is Context, id is Id, definition is map)
{
var edgesToExtend = [];
var edgeChangeOptions = [];
const trackedEdges = getTrackedEdges(context, definition);
var bodyToCollision = {}; //keep track to avoid calling evCollusion more than once per body
if (trackedEdges == [])
{
throw regenError(ErrorStringEnum.EXTEND_SHEET_BODY_NO_BODY, ["entities"]);
}
for (var i = 0; i < size(trackedEdges); i += 1)
{
var edge = trackedEdges[i];
const ownerBody = evaluateQuery(context, qOwnerBody(edge))[0];
if (bodyToCollision[ownerBody] == false ||
(bodyToCollision[ownerBody] == undefined && evCollision(context, { "tools" : ownerBody, "targets" : qOwnerBody(definition.target) }) == []))
{
bodyToCollision[ownerBody] = false;
//no collision, use extend
edgesToExtend = append(edgesToExtend, edge);
}
else //it's a trim
{
bodyToCollision[ownerBody] = true;
var targetFace = qOwnedByBody(definition.target, EntityType.FACE);
if (size(evaluateQuery(context, targetFace)) > 1)
{
setErrorEntities(context, id, { "entities" : definition.target });
throw regenError(ErrorStringEnum.TRIM_TO_MULTI_FAILED); //cannot trim to multi-face targets
}
edgeChangeOptions = append(edgeChangeOptions, { "edge" : edge,
"face" : qAdjacent(edge, AdjacencyType.EDGE, EntityType.FACE),
"replaceFace" : targetFace });
}
}
if (size(edgeChangeOptions) > 0)
{
trimEdges(context, id + "edgeChange", edgeChangeOptions);
}
else if (size(edgesToExtend) > 0)
{
definition.entities = qUnion(edgesToExtend);
opExtendSheetBody(context, id + "extend", definition);
}
}
function trimEdges(context is Context, id is Id, edgeChangeOptions is array)
{
try silent
{
opEdgeChange(context, id + "edgeChange", { "edgeChangeOptions" : edgeChangeOptions });
}
catch
{
var edgesToTrim = [];
for (var i = 0; i < size(edgeChangeOptions); i += 1)
{
edgesToTrim = append(edgesToTrim, edgeChangeOptions[i].edge);
}
setErrorEntities(context, id, { "entities" : qUnion(edgesToTrim) });
throw regenError(ErrorStringEnum.TRIM_FAILED);
}
}
function extendTarget(context is Context, id is Id, surfaceDefinition is map) returns Query
{
var newTarget;
if (surfaceDefinition is Plane)
{
opPlane(context, id + "plane", { "plane" : surfaceDefinition,
"width" : targetBounds, "height" : targetBounds });
newTarget = qCreatedBy(id + "plane", EntityType.BODY);
}
else if (surfaceDefinition is Cylinder)
{
const cyl = surfaceDefinition;
fCylinder(context, id + "cylinder", { "topCenter" : cyl.coordSystem.origin + targetBounds * cyl.coordSystem.zAxis,
"bottomCenter" : cyl.coordSystem.origin - targetBounds * cyl.coordSystem.zAxis,
"radius" : cyl.radius });
const capFaces = qUnion([qCapEntity(id + "cylinder", CapType.START, EntityType.FACE),
qCapEntity(id + "cylinder", CapType.END, EntityType.FACE)]);
opDeleteFace(context, id + "deleteCaps", {"deleteFaces": capFaces, "leaveOpen" : true, "includeFillet" :false, "capVoid" :false});
newTarget = qCreatedBy(id + "cylinder", EntityType.BODY);
}
return newTarget;
}
function getTrackedEdges(context is Context, definition is map) returns array
{
var selectedEdges = qEntityFilter(definition.entities, EntityType.EDGE);
if (definition.tangentPropagation)
{
selectedEdges = qUnion([selectedEdges, qTangentConnectedEdges(selectedEdges)]);
}
var allEdges = qEdgeTopologyFilter(qUnion([selectedEdges, qOwnedByBody(definition.entities, EntityType.EDGE)]), EdgeTopology.ONE_SIDED);
var trackedEdges = [];
for (var edge in evaluateQuery(context, allEdges))
{
trackedEdges = append(trackedEdges, qUnion([edge, startTracking(context, edge)]));
}
return trackedEdges;
}
function getTargetFromFace(context is Context, id is Id, definition is map) returns Query
{
try
{
const mateConnectorCSys = try silent(evMateConnector(context, { "mateConnector" : definition.targetFace }));
if (mateConnectorCSys != undefined)
{
opPlane(context, id + "plane", { "plane" : plane(mateConnectorCSys), "width" : targetBounds, "height" : targetBounds });
return qCreatedBy(id + "plane", EntityType.BODY);
}
else
{
var targetFace = definition.targetFace;
var surface = evSurfaceDefinition(context, { "face" : targetFace });
if (surface is Plane || surface is Cylinder)
{
return extendTarget(context, id, surface);
}
else
{
opExtractSurface(context, id + "extractFace", { "faces" : definition.targetFace });
return qCreatedBy(id + "extractFace", EntityType.BODY);
}
}
}
catch
{
setErrorEntities(context, id, { "entities" : definition.targetFace });
throw regenError(ErrorStringEnum.EXTEND_TO_FACE_FAILED);
}
}
function getTargetFromVertex(context is Context, id is Id, definition is map) returns Query
{
try
{
const returnMap = getExtendDirection(context, definition.entities, false); //use edge with min detId
if (returnMap.extendDirection != undefined)
{
const vertexPoint = evVertexPoint(context, { "vertex" : definition.targetVertex });
opPlane(context, id + "plane", { "plane" : plane(vertexPoint, returnMap.extendDirection),
"width" : targetBounds, "height" : targetBounds });
return qCreatedBy(id + "plane", EntityType.BODY);
}
else
{
throw regenError(ErrorStringEnum.EXTEND_TO_VERTEX_FAILED);
}
}
catch
{
setErrorEntities(context, id, { "entities" : definition.targetVertex });
throw regenError(ErrorStringEnum.EXTEND_TO_VERTEX_FAILED);
}
}
//manipulator related:
const EXTEND_MANIPULATOR = "extendManipulator";
function addExtendManipulator(context is Context, id is Id, definition is map)
{
const useLastSelectedEdge = true;
const returnMap = try silent(getExtendDirection(context, definition.entities, useLastSelectedEdge));
if (returnMap != undefined && returnMap.extendDirection != undefined)
{
addManipulators(context, id, { (EXTEND_MANIPULATOR) :
linearManipulator({ "base" : returnMap.origin,
"direction" : returnMap.extendDirection,
"offset" : definition.extendDistance,
"primaryParameterId" : "extendDistance" }) });
}
}
function getFirstLaminarEdge(context is Context, query is Query)
{
const laminarEdges = qEdgeTopologyFilter(query, EdgeTopology.ONE_SIDED);
const laminarQ = evaluateQuery(context, laminarEdges);
if (laminarQ == [])
{
throw regenError(ErrorStringEnum.EXTEND_NON_LAMINAR, ["entities"]);
}
return laminarQ[0];
}
function getEdgeToUse(context is Context, entities is Query, useLastSelected is boolean)
{
const resolvedEntities = evaluateQuery(context, entities);
var edgeToUse = undefined;
if (@size(resolvedEntities) > 0)
{
if (useLastSelected) //used in manipulator attachment
{
edgeToUse = resolvedEntities[@size(resolvedEntities) - 1];
if (!isQueryEmpty(context, qEntityFilter(edgeToUse, EntityType.BODY)))
{
edgeToUse = getFirstLaminarEdge(context, qOwnedByBody(edgeToUse, EntityType.EDGE));
}
}
else //use edge with min deterministic id, used in feature regen
{
const edges = evaluateQuery(context, qEntityFilter(entities, EntityType.EDGE));
if (size(edges) > 0)
{
edgeToUse = edges[0];
}
else
{
//if there are no edges, use the laminar edge with min det id
edgeToUse = getFirstLaminarEdge(context, qOwnedByBody(qEntityFilter(entities, EntityType.BODY), EntityType.EDGE));
}
}
}
return edgeToUse;
}
function getExtendDirection(context is Context, entities is Query, useLastSelectedEdge is boolean)
{
var returnMap = {};
const edgeToUse = getEdgeToUse(context, entities, useLastSelectedEdge);
if (edgeToUse != undefined)
{
var faceNormal;
const faces = evaluateQuery(context, qAdjacent(edgeToUse, AdjacencyType.EDGE, EntityType.FACE));
if (size(faces) == 1)
{
const midParam = .5;
const tangentLine = evEdgeTangentLine(context, { "edge" : edgeToUse, "face" : faces[0], "parameter" : midParam });
returnMap.origin = tangentLine.origin;
const edgeDirection = tangentLine.direction;
const faceTangentPlane = evFaceTangentPlaneAtEdge(context, {
"face" : faces[0],
"edge" : edgeToUse,
"parameter" : midParam,
"usingFaceOrientation" : true
});
faceNormal = faceTangentPlane.normal;
if (faceNormal != undefined)
{
returnMap.extendDirection = cross(edgeDirection, faceNormal);
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V2083_EXTEND_FS_FIX))
{
returnMap.extendDirection = normalize(returnMap.extendDirection);
}
}
}
}
return returnMap;
}
/**
* @internal
*/
export function extendManipulatorChange(context is Context, definition is map, newManipulators is map) returns map
{
try
{
if (newManipulators[EXTEND_MANIPULATOR] is map)
{
const newValue = newManipulators[EXTEND_MANIPULATOR].offset;
definition.extendDistance = abs(newValue);
definition.oppositeDirection = newValue.value < 0;
}
}
return definition;
}