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

elias_bonauer
elias_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_bonauer
    elias_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_Corder
    Jacob_Corder Member Posts: 141 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_bonauer
    elias_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_Armstrong
    Caden_Armstrong Member Posts: 472 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 --- Renaissance --- Kestrel -- SmartLink
    Experts in Onshape Automation - Custom Features and Integrated Applications
  • elias_bonauer
    elias_bonauer Member Posts: 4 ✭✭
    edited March 2024
    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_bonauer
    elias_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_Armstrong
    Caden_Armstrong Member Posts: 472 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 --- Renaissance --- Kestrel -- SmartLink
    Experts in Onshape Automation - Custom Features and Integrated Applications