Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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.
Help with determining if a selected input is a sketch vertex or a mate connector please. - FIXED
owen_sparks
Member, Developers Posts: 2,660 PRO
Hi folks.
I'm having a struggle to get a FS working and could use a hand if anyone's free to help.
Given the new implied mate connectors added in the last Onshape update it seemed a shame not to use them, so I'm trying to have a FS that's happy to accept sketch points or MC's
The precondition is fine and happy:-
annotation { "Name" : "Target Sketch Points or MC", "Filter" : EntityType.VERTEX && SketchObject.YES || BodyType.MATE_CONNECTOR }<br> definition.TargetsketchPoints is Query;Later we have a for loop to work through each selection in turn.I'm using a transform which needs to be different depending on what was selected so am trying to test to determine if it's a vertex or a MC.
var TargetPoints = evaluateQuery(context, definition.TargetsketchPoints);<br> var TargetPointCount = size(TargetPoints);<br><br> debug(context, TargetPointCount ~ " TargetPoints");<br><br> var transform; //Needs setting as a transform and then initialising?<br><br> for (var i = 0; i < TargetPointCount; i += 1)<br> {<br> <br> //Evaluate Targetpoint to see if it's a sketch vertex or a mate connector so we can apply the correct transform method<br> <br> //If using a Mate Connector as an input<br> if (definition.TargetsketchPoints[i] == BodyType.MATE_CONNECTOR)<br> {<br> debug(context, "LoopCounter:- " ~ i);<br> debug(context, "is a Mate Connector");<br> <br> transform = toWorld(evMateConnector(context, {<br> "mateConnector" : TargetPoints[i] <br> }));<br> } <br> else //It must be a sketch point as an input<br> {<br> debug(context, "LoopCounter:- " ~ i);<br> debug(context, "is a Sketch Vertex");<br> <br> var skPlane = evOwnerSketchPlane(context, {<br> "entity" : TargetPoints[i]<br> });<br> <br> skPlane.origin = evVertexPoint(context, {<br> "vertex" : TargetPoints[i]<br> });<br> <br> // if (definition.isFlipped)<br> // {<br> // skPlane.normal *= -1;<br> // skPlane.x *= -1;<br> // }<br> <br> transform = toWorld(coordSystem(skPlane));<br> }<br><br> <br> const instantiator = newInstantiator(id + i + "instantiator");<br> <br> addInstance(instantiator, build, <br> {<br> "configuration" :<br> {<br> "BoltLength" : definition.LengthSelected,<br> },<br> <br> // "transform" : toWorld(coordSystem(skPlane))<br> "transform" : transform<br> });<br> <br> instantiate(context, instantiator);<br> <br> }<br> }<br>(Question 1)
From the debug I can see the if statement isn't yielding what we're after. The total count is correct but only the "else" code is ever executed so my condition
if (definition.TargetsketchPoints[i] == BodyType.MATE_CONNECTOR)must be incorrect. Am I going about this completely wrong or have I just got the syntax wrong? Is a MC still a MC after it's put in the array or is it translated into something else?
(Question2)
I'm using a variable to store the transform. If set as a "transform type" it needs to be initialized. The snipped above doesn't set the type and instead initializes the variable (without type) within the loop. Is this acceptable? It seems to work OK, the transform executes fine. Should I instead initialize it to a do nothing transform; if so what's the syntax for that?
Sorry for newbie questions.
Cheers, Owen S.
Business Systems and Configuration Controller
HWM-Water Ltd
HWM-Water Ltd
0
Comments
-
look at this function from Transform pattern FS
function getCoordSys(context is Context, query is Query, keepOrientation is boolean) returns CoordSystem { try silent { return evMateConnector(context, { "mateConnector" : query }); } if (!keepOrientation) { try silent { const zAxis = evOwnerSketchPlane(context, { "entity" : query }).normal; const origin = evVertexPoint(context, { "vertex" : query }); return coordSystem(origin, perpendicularVector(zAxis), zAxis); } } try silent { const origin = evVertexPoint(context, { "vertex" : query }); return coordSystem(origin, vector(1, 0, 0), vector(0, 0, 1)); } }
1 -
Hi.You're the man, thank you!Happy feature now:-.Sketch vertex, Explicit MC and Implicit MC all good.
It appears it'll abort the "try" at the first failed instruction so by moving the debug calls to the bottom they work as intended too.Most excellent.Thanks for your help,Owen S.
Business Systems and Configuration Controller
HWM-Water Ltd0 -
yes, a saw this try silent {} trick in standart library, it is helpful when you need to check and process some unknown data1
-
Try silent should work well.
For future reference, definition.TargetsketchPoints is a Query, not an array. So the real equivalent of "definition.TargetsketchPoints[i] == BodyType.MATE_CONNECTOR" would beconst mateConnectorAtI is Query = qBodyType(qNthElement(definition.TargetsketchPoints, i), BodyType.MATE_CONNECTOR); if (evaluateQuery(context, mateConnectorAtI) != []) { // Do something with mateConnectorAtI
1 -
Thanks @kevin_o_toole_1 it's always good to have a solution and an understanding of what you were doing incorrectly.Thanks for the explanation and the code
Cheers,Owen S.Business Systems and Configuration Controller
HWM-Water Ltd0


