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.

How can I use FeatureScript to get the longest edge of a part as a computed property

elias_bonauerelias_bonauer Member Posts: 4 ✭✭
I try to create a feature script that lets me get the longest edge of a part as a computed property in a user defined table.
So far I had no success other than get the compuedVolume as shown in an example somewhere.

Can yomebody give me a hint or an example how to get all edges, find the logest and rturn that as a computed propery?

Any help is highly appreciated!

Best Answer

  • elias_bonauerelias_bonauer Member Posts: 4 ✭✭
    Answer ✓
    Ok, i figured it out, I needed to use evaluateQuery and then return the value at index 0

    annotation { "Property Function Name" : "longestEdge" }
    export const longestEdge = defineComputedPartProperty(
                 function(context is Context, part is Query, definition is map) returns ValueWithUnits
        {
           var longestEdge = evaluateQuery(context, qLargest(qOwnedByBody(part, EntityType.EDGE)));
           return evLength(context, {
                   "entities" : longestEdge[0]
           });       
        });

Answers

  • Jacob_CorderJacob_Corder Member Posts: 134 PRO
    annotation { "Feature Type Name" : "Longest Edge" }
    export const longestEdge = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
             annotation { "Name" : "Allow Wire Bodies","UIHint":UIHint.REMEMBER_PREVIOUS_VALUE }
             definition.allowWireBodyEdges is boolean;         
        }
        {
            
            var attributeName = "longestEdge";//set this to whatever name you want
            var edgeQuery=qEverything(EntityType.EDGE) ;
            if(!definition.allowWireBodyEdges) edgeQuery =  qSubtraction(edgeQuery,qEdgeTopologyFilter(edgeQuery, EdgeTopology.WIRE));         
            edgeQuery = qUnion(evaluateQuery(context, qLargest(edgeQuery)));//this executes the longest portion which is qLargest and wraps it in qUnion so evaluation does not happen again.
            setAttribute(context, {
                    "entities" : edgeQuery,
                    "name" : attributeName,
                    "attribute" : {}
            });
            //Then later call qHasAttribute to get the edge in the table
            var attributedEdge = qHasAttribute(attributeName);
            addDebugEntities(context, attributedEdge, DebugColor.GREEN);
        });
    
    that should do it
  • elias_bonauerelias_bonauer Member Posts: 4 ✭✭
    Thanks for your answer, but this does not give me computed properties in the custom table.

    The example with the volume looks like this:

    annotation { "Property Function Name" : "longestEdge" }
    export const longestEdge = defineComputedPartProperty(
                 function(context is Context, part is Query, definition is map) returns ValueWithUnits
        {
            return evVolume(context, { "entities" : part });
        });


    How can I use this as a computed property?

  • Caden_ArmstrongCaden_Armstrong Member Posts: 152 PRO
    @elias_bonauer

    Jacob wrote a feature to find the longest edge.
    For a computed property, the query for part is given already so the code is a bit shorter:


    annotation { "Property Function Name" : "longestEdge" }
    export const longestEdge = defineComputedPartProperty(
                 function(context is Context, part is Query, definition is map) returns ValueWithUnits
        {
           var longestEdge = qLargest(qOwnedByBody(part, EntityType.EDGE));
           return evLength(context, {
                   "entities" : longestEdge
           });
        });
    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
  • elias_bonauerelias_bonauer Member Posts: 4 ✭✭
    edited March 5
    Hi Caden,

    this works as expected, thank you so much!

    I just realized that this gives me the sum of all longest edges, not only the length of 1 longest  :|
  • elias_bonauerelias_bonauer Member Posts: 4 ✭✭
    Answer ✓
    Ok, i figured it out, I needed to use evaluateQuery and then return the value at index 0

    annotation { "Property Function Name" : "longestEdge" }
    export const longestEdge = defineComputedPartProperty(
                 function(context is Context, part is Query, definition is map) returns ValueWithUnits
        {
           var longestEdge = evaluateQuery(context, qLargest(qOwnedByBody(part, EntityType.EDGE)));
           return evLength(context, {
                   "entities" : longestEdge[0]
           });       
        });

  • Caden_ArmstrongCaden_Armstrong Member Posts: 152 PRO
    Right, I forgot that qLargest can return multiples if there is a tie. You could also just do qNthElement instead of evaluating.
    www.smartbenchsoftware.com --- fs.place --- Renaissance
    Custom FeatureScript and Onshape Integrated Applications
Sign In or Register to comment.