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.

Help with determining if a selected input is a sketch vertex or a mate connector please. - FIXED

owen_sparksowen_sparks Member, Developers Posts: 2,436 PRO
edited March 7 in FeatureScript
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 }
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);
var TargetPointCount = size(TargetPoints);

debug(context, TargetPointCount ~ " TargetPoints");

var transform; //Needs setting as a transform and then initialising?

for (var i = 0; i < TargetPointCount; i += 1)
{

//Evaluate Targetpoint to see if it's a sketch vertex or a mate connector so we can apply the correct transform method

//If using a Mate Connector as an input
if (definition.TargetsketchPoints[i] == BodyType.MATE_CONNECTOR)
{
debug(context, "LoopCounter:- " ~ i);
debug(context, "is a Mate Connector");

transform = toWorld(evMateConnector(context, {
"mateConnector" : TargetPoints[i]
}));
}
else //It must be a sketch point as an input
{
debug(context, "LoopCounter:- " ~ i);
debug(context, "is a Sketch Vertex");

var skPlane = evOwnerSketchPlane(context, {
"entity" : TargetPoints[i]
});

skPlane.origin = evVertexPoint(context, {
"vertex" : TargetPoints[i]
});

// if (definition.isFlipped)
// {
// skPlane.normal *= -1;
// skPlane.x *= -1;
// }

transform = toWorld(coordSystem(skPlane));
}


const instantiator = newInstantiator(id + i + "instantiator");

addInstance(instantiator, build,
{
"configuration" :
{
"BoltLength" : definition.LengthSelected,
},

// "transform" : toWorld(coordSystem(skPlane))
"transform" : transform
});

instantiate(context, instantiator);

}
}

(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.
Production Engineer
HWM-Water Ltd

Comments

  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 931 ✭✭✭✭✭
    edited March 7
    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));
        }
    }

  • owen_sparksowen_sparks Member, Developers Posts: 2,436 PRO
    edited March 7
    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.


    Production Engineer
    HWM-Water Ltd
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 931 ✭✭✭✭✭
    yes, a saw this try silent {} trick in standart library, it is helpful when you need to check and process some unknown data
  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 450
    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 be
    const mateConnectorAtI is Query = qBodyType(qNthElement(definition.TargetsketchPoints, i), BodyType.MATE_CONNECTOR);
    if (evaluateQuery(context, mateConnectorAtI) != []) {
        // Do something with mateConnectorAtI
    


  • owen_sparksowen_sparks Member, Developers Posts: 2,436 PRO
    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 :+1: 
    Cheers,
    Owen S.
    Production Engineer
    HWM-Water Ltd
Sign In or Register to comment.