<p>Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.</p></p><strong>First time visiting? Here are some places to start:</strong></p><ol style="list-style:inherit;margin-left:25px;"><li>Looking for a certain topic? Check out the <a href="https://forum.onshape.com/categories">categories filter</a> or use Search (upper right).</li><li>Need support? <a href="https://forum.onshape.com/post/question/community-support">Ask a question</a> to our <a href="https://forum.onshape.com/categories/community-support">Community Support</a> category.</li><li>Please submit support tickets for bugs but you can request improvements in the <a href="https://forum.onshape.com/categories/product-feedback">Product Feedback category</a>.</li><li>Be respectful, on topic and if you see a problem, Flag it.</li></ol><p>If you would like to contact our Community Manager personally, feel free to <a href="https://forum.onshape.com/profile/6/LouGallo">send a private message</a> or <a href="mailto:lgallo@onshape.com">an email</a>.
</p>

Can you help me create a feature script

Aly_k
Aly_k Member Posts: 17 ✭

Can you help me create a feature script to build a custom table with part name, length, width, thickness?

Best Answer

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,948 PRO
    edited October 2025 Answer ✓

    getProperty() can only be called from within Edit Logic or from within a table definition. It can not be called from within the feature body of a feature. This is one of the limitations of the way features are built in the tree atm.

    Looks like your rounding functions may be off as well. And the table definition.

    Here is a very basic example of how to create a custom feature + table + getProperty():

    • Note that tableDefinitions are not the same as featureDefinitions.
    • In this example, I'm using the feature to tag parts with an attribute, then the table is searching for all parts with that attribute.
    • Since the table is running outside the tree, it is able to access the getProperty function.


    https://cad.onshape.com/documents/052438916dad30498b0bc515/w/f5e9251178fd774e38d2b108/e/5137db7718ea44ffbb8d1…

    image.png

    .

    And here is a more advanced example where the table interacts with the custom feature and has sorting and even some editing logic:
    Table - Custom Feature

    Here is an example where the table has sorting options and table UI:
    Measure Cut List - Custom Feature

    .


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚

Answers

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,948 PRO
    edited October 2025

    Here is a custom feature for creating tables. It also is a good example of how to make custom tables via FeatureScript.

    Table - Custom Feature

    Or if you want a step by step video tutorial of how to do it, CADSharp shows how to do this in FS Lesson 16 which is a paid lesson. If you do end up following the lessons, it's important to do them in order because each one teaches the fundamentals for the next lesson, ensuring you have the skills required by the later lessons.


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • Aly_k
    Aly_k Member Posts: 17 ✭

    can i rather show you what i have and why its not working and maybe you can guide me?

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,948 PRO

    Sure thing!


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • Aly_k
    Aly_k Member Posts: 17 ✭

    FeatureScript 2780;
    import(path : "onshape/std/common.fs", version : "2780.0");

    // Feature name and description
    annotation { "Feature Type Name" : "Part Size Table" }
    export const partSizeTable = defineFeature(function(context is Context, id is Id, definition is map)

    precondition
    {
        annotation { "Name" : "Parts to include", "Filter" : EntityType.BODY, "MaxNumberOfPicks" : 100, "MinNumberOfPicks" : 1 }
        definition.parts is Query;
    }
    {
        var tableRows = [];
    
        // Header row
        tableRows = append(tableRows, ["Part Name", "Length (mm)", "Width (mm)", "Thickness (mm)"]);
    
        // Loop through selected parts
        for (var part in evaluateQuery(context, definition.parts))
        {
            const partName = getProperty(context, {
                        "entity" : part,
                        "propertyType" : PropertyType.NAME
                    });
    
            // Compute bounding box
            var bbox = evBox3d(context, { "topology" : part });
            var size = vector(bbox.maxCorner[0] - bbox.minCorner[0],
            bbox.maxCorner[1] - bbox.minCorner[1],
            bbox.maxCorner[2] - bbox.minCorner[2]);
    
            // Sort dimensions largest to smallest
            var sorted = sortDescending([size[0], size[1], size[2]]);
    
            // Convert meters to mm and round
            tableRows = append(tableRows, [
                        partName,
                        round(sorted[0] * 1000, 2),
                    
                        round(sorted[1] * 1000, 2),
                        round(sorted[2] * 1000, 2)
                    ]);
        }
    
        // Create the table
        table(
            id + "table",
            tableRows,
            { "name" : "Part Dimensions" }
            );
    });
    

    // Helper function to sort descending
    function sortDescending(list)
    {
    return sort(list, function(a, b)
    {
    return b - a;
    });
    }

    image.png

    so this is what i have so far but i dont seem to understand what im doing wrong

  • MichaelPascoe
    MichaelPascoe Member Posts: 2,948 PRO
    edited October 2025 Answer ✓

    getProperty() can only be called from within Edit Logic or from within a table definition. It can not be called from within the feature body of a feature. This is one of the limitations of the way features are built in the tree atm.

    Looks like your rounding functions may be off as well. And the table definition.

    Here is a very basic example of how to create a custom feature + table + getProperty():

    • Note that tableDefinitions are not the same as featureDefinitions.
    • In this example, I'm using the feature to tag parts with an attribute, then the table is searching for all parts with that attribute.
    • Since the table is running outside the tree, it is able to access the getProperty function.


    https://cad.onshape.com/documents/052438916dad30498b0bc515/w/f5e9251178fd774e38d2b108/e/5137db7718ea44ffbb8d1…

    image.png

    .

    And here is a more advanced example where the table interacts with the custom feature and has sorting and even some editing logic:
    Table - Custom Feature

    Here is an example where the table has sorting options and table UI:
    Measure Cut List - Custom Feature

    .


    RENDERCAD - Photorealistic product rendering.
    CADSharp - Professional CAD + software consulting.

    ▚▞▚▞▚▞▚▞▚
  • Konst_Sh
    Konst_Sh Member Posts: 170 PRO
    edited October 2025

    You using defineFeature template function while for table you need defineTable()

    CADSharp - Onshape custom programming services + Onshape mechanical design services

  • Aly_k
    Aly_k Member Posts: 17 ✭
    edited October 2025

    This one works for me so well thank you so much🤗