Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

FeatureScript Arrow Operator Improvements

Alex_KempenAlex_Kempen Member Posts: 221 EDU
edited June 2021 in FeatureScript
In August of last year, Onshape added support for a new syntax operator, the arrow operator (->). It allows users to chain FeatureScript arguments together by passing the argument on the left of the operator into the function on the right as the first argument. For example, the following two statements are equivalent:
const mateConnectorQuery = myQuery->qEntityType(EntityType.BODY)->qBodyType(BodyType.MATE_CONNECTOR);
const mateConnectorQuery = qBodyType(qEntityType(myQuery, EntityType.BODY), BodyType.MATE_CONNECTOR);
The arrow operator is obviously a great inclusion, but it can only can be used to pass arguments into the first (i.e. leftmost) argument of a function. Thus, the arrow operator is not useful for functions like evaluateQuery and toWorld since the part of the argument which tends to change or be calculated in-line is the second argument rather than the first:
evaluateQuery(context, query);
toWorld(cSys, pointInCSys);

Thus, I propose the addition of a variant of the arrow operator which allows passing the argument into a function as the second argument of a function instead of the first. A logical form for this new operator would be a double arrow, either (-->) or (->>). The addition of this operator would thus enable the following statements:
if (evaluateQuery(context, myQuery->qEntityType(EntityType.BODY)->qBodyType(BodyType.MATE_CONNECTOR)) != [])// checks if query contains a mate connector
if (myQuery->qEntityType(EntityType.BODY)->qBodyType(BodyType.MATE_CONNECTOR)->>evaluateQuery(context) != [])
// projects a point onto a plane
const projectedPoint = evVertexPoint(context, { "vertex" : myVertexQuery })->>project(myPlane);
const projectedPoint = project(myPlane, evVertexPoint(context, { "vertex" : myVertexQuery }));
// turns the first sketchpoint of a sketch into a world point
const worldPoint = qCreatedBy(context, id + "mySketch", EntityType.VERTEX)->qNthElement(0)->>planeToWorld(sketchPlane);
const worldPoint = planeToWorld(sketchPlane, qCreatedBy(context, id + "mySketch", EntityType.VERTEX)->qNthElement(0));
This would enhance FeatureScript by making it easier to use certain functions in-line with each other and thus make code easier to write, understand, and troubleshoot.

For the sake of being thorough, here's a list of some commonly used functions which I believe would benefit from the addition of this operator:
evaluateQuery(context, query);
debug(context, value);
addDebugEntities(context, entities);
addDebugPoint(context, point);
toWorld(cSys, pointInCSys);
fromWorld(cSys, worldPoint);
project(plane, point);
getVariable(context, name);
planeToWorld(plane, planePoint);
worldToPlane3D(plane, worldPoint);
worldToPlane(plane, worldPoint);
scaleUniformly(scale, pointToScaleAbout);
lastModifyingOperationId(context, query);
startTracking(context, subQuery);
makeRobustQuery(context, subQuery);
makeRobustQueriesBatched(context, subQuery);
adjustAngle(context, angle);
constructPath(context, edgesQuery);
evPathLength(context, path);
isClosed(context, edge);
extractDirection(context, entity);
Tagged:

Comments

Sign In or Register to comment.