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.

I'm stuck on something I'm sure is simple

EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
Hi everyone. I've got essentially zero coding experience, so I hope I'm missing something that will be obvious to people. I'm trying to write a script that will find all of the sharp edges of a body and highlight them with the debug function. This will help me evaluate models before I send them off for injection molding since no edges should be sharp except shutoffs. Here's what I have so far, but I'm not getting any red edges:
<div>FeatureScript 961;</div><div>import(path : "onshape/std/geometry.fs", version : "961.0");</div><div><br></div><div>annotation { "Feature Type Name" : "Find Sharp Edges" }</div><div>export const findSharp = defineFeature(function(context is Context, id is Id, definition is map)</div><div>&nbsp; &nbsp; precondition</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; annotation { "Name" : "Body to Search", "Filter" : EntityType.BODY && BodyType.SOLID, "MaxNumberOfPicks" : 1 }</div><div>&nbsp; &nbsp; definition.body is Query;</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; var smooth is Query = qOwnedByBody(definition.body, EntityType.EDGE && EdgeConvexityType.SMOOTH);</div><div>&nbsp; &nbsp; var all is Query = qOwnedByBody(definition.body, EntityType.EDGE);</div><div>&nbsp; &nbsp; var sharp is Query = qSubtraction(all, smooth);</div><div>&nbsp; &nbsp; debug(context, sharp);</div><div>&nbsp; &nbsp; });</div>
Evan Reese

Comments

  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    edited December 2018
    for some reason the formatting above looks wrong, so here's the code without the "code" formatting in case that helps:
    FeatureScript 961;
    import(path : "onshape/std/geometry.fs", version : "961.0");

    annotation { "Feature Type Name" : "Find Sharp Edges" }
    export const findSharp = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
        annotation { "Name" : "Body to Search", "Filter" : EntityType.BODY && BodyType.SOLID, "MaxNumberOfPicks" : 1 }
        definition.body is Query;
        }
        {
        var smooth is Query = qOwnedByBody(definition.body, EntityType.EDGE && EdgeConvexityType.SMOOTH);
        var all is Query = qOwnedByBody(definition.body, EntityType.EDGE);
        var sharp is Query = qSubtraction(all, smooth);
        debug(context, sharp);
        });
    Evan Reese
  • pmdpmd Member, Developers Posts: 63 PRO
    I do not think you can use EdgeConvexityType like that (&&-ed in). You probably need to loop over all edges for a body and call evEdgeConvexity()  on each edge individually.

  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    pmd said:
    I do not think you can use EdgeConvexityType like that (&&-ed in). You probably need to loop over all edges for a body and call evEdgeConvexity()  on each edge individually.

    I think that makes sense. Is it right to think of the query is kind of like the container of the edges, so it isn't convex, or smooth? If so I need to unpack the container and look at each edge. By "loop" do you mean a "for loop"? Not sure how to approach that. I was able to query all edges and get that to show up red.
    Evan Reese
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    as @pmd said you need to pass a query of edges to filter laminar edges, not the body query. and you can't use logical argument combinations inside.
    var all is Query = qOwnedByBody(definition.body, EntityType.EDGE);
    var smooth is Query = qOwnedByBody(all,  EdgeConvexityType.SMOOTH);


  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    Not sure if this is the most efficient way, but it works:

    annotation { "Feature Type Name" : "Find Sharp Edges" }
    export const findSharp = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Body to Search", "Filter" : EntityType.BODY && BodyType.SOLID, "MaxNumberOfPicks" : 1 }
            definition.body is Query;
        }
        {
            var sharp = [];
            var allEdges = qOwnedByBody(definition.body, EntityType.EDGE);
            
            for (var i = 0; i < size(evaluateQuery(context, allEdges)); i += 1)
            {
                if (evEdgeConvexity(context, { "edge" : qNthElement(allEdges, i) }) != EdgeConvexityType.SMOOTH)
                {
                    sharp = append(sharp, qNthElement(allEdges, i));
                }
            }
            debug(context, qUnion(sharp));
        });
    By the way, to get the code block to work in a post like this, set it to code first then paste your code into the block.
    Senior Director, Technical Services, EMEAI
  • Jake_RosenfeldJake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646
    @Evan_Reese

    To get the code to paste correctly, you should hit the "code" formatting option, then paste your code into the blank yellow box that appears (not paste the code then highlight and press the "code" button).

    This is the code that you want:
    var sharpArray = [];
    // This is a query that represents all the edges of the body
    const allEdges = qOwnedByBody(definition.body, EntityType.EDGE);
    // This is an array of queries.  Each query in the array represents one of the edges of the body (the array is
    // in an arbitrary, but deterministic, order)
    const allEdgesArray = evaluateQuery(context, allEdges);
    // Now that you have an array of each edge, you can loop over it
    for (var edge in allEdgesArray)
    {
        const convexity = evEdgeConvexity(context, { "edge" : edge });
        if (convexity != EdgeConvexityType.SMOOTH)
        {
            sharpArray = append(sharpArray, edge);
        }
    }
    // at this point sharpArray is an array of queries for sharp edges.  To combine it into one overarching query, use qUnion:
    const sharpQuery = qUnion(sharpArray);
    debug(context, sharpQuery);
    
    
    
    // In shorthand:
    var sharpArray = [];
    for (var edge in evaluateQuery(context, qOwnedByBody(definition.body, EntityType.EDGE)))
    {
        if (evEdgeConvexity(context, { "edge" : edge }) != EdgeConvexityType.SMOOTH)
        {
            sharpArray = append(sharpArray, edge);
        }
    }
    debug(context, qUnion(sharpArray));
    <br>

    Jake Rosenfeld - Modeling Team
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    as @pmd said you need to pass a query of edges to filter laminar edges, not the body query. and you can't use logical argument combinations inside.
    var all is Query = qOwnedByBody(definition.body, EntityType.EDGE);
    var smooth is Query = qOwnedByBody(all,  EdgeConvexityType.SMOOTH);


    That's definitely getting me closer. Thanks! with this code, I should be able to view all of the smooth edges by debugging, but I'm not having any luck. Here's what it looks like

        var all is Query = qOwnedByBody(definition.body, EntityType.EDGE);
        var smooth is Query = qOwnedByBody(all, EdgeConvexityType.SMOOTH);
        debug(context, smooth);
    Evan Reese
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    edited December 2018
    sorry, i copied your code unattentively, and thought query for convext edges exist in standart library, but I was wrong, so you really need to iterate through a list of edges and check each with evEdgeConvexityType()
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    @konstantin_shiriazdanov no worries, and thanks for the help!
    @Jake_Rosenfeld this looks like a good solution, but I don't have time to dig into it right now. Thanks for the help!
    @NeilCooke Thanks! I'll look into this sometime soon, and compare your and Jake's solutions to see what I can learn. For now, I've got actual deadlines on some other stuff.
    Evan Reese
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,683
    Jake’s solution is better  :) 
    Senior Director, Technical Services, EMEAI
Sign In or Register to comment.