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.

Getting name of derive that imported parts

kevin_wormingtonkevin_wormington Member Posts: 2 PRO

Hi, I'm very new to featurescript and I'm making a simple cutlist table which is working. I'm deriving my parts (for example cabinets) from a configured library part. I would like to be able to get the name of the derive feature that imported the parts so I can prepend it's name to the actual part name on the body.

Any pointers would be appreciated.

    
 FeatureScript 2796;
import(path : "onshape/std/table.fs", version : "2796.0");
import(path : "onshape/std/common.fs", version : "2796.0");

annotation { "Table Type Name" : "Cut List" }
export const cutList = defineTable(function(context is Context, definition is map) returns Table
    precondition
    {
    }
    {
        var columnDefinitions = [
            tableColumnDefinition("name", "Part Name"),
            tableColumnDefinition("length", "Length"),
            tableColumnDefinition("width", "Width"),
            tableColumnDefinition("thickness", "Thickness")
        ];

        var rows = [];
        var allFeatures = evaluateQuery(context, qEverything());

        for (var part in evaluateQuery(context, qAllModifiableSolidBodies()))
        {
            const partName = getProperty(context, {
                        "entity" : part,
                        "propertyType" : PropertyType.NAME
                    });

            var derivedFeatureName = ""; // Default if no Derive/Import is found
            
            var bbox = evBox3d(context, {
                    "topology" : part,
                    "tight" : true
                });

            // Dimensions are calculated here (assumed to be L, W, T, but unsorted)
            var d1 = bbox.maxCorner[0] - bbox.minCorner[0];
            var d2 = bbox.maxCorner[1] - bbox.minCorner[1];
            var d3 = bbox.maxCorner[2] - bbox.minCorner[2];

            var dimensions = [d1, d2, d3];

            // Sort ascending: smallest (Thickness) at index 0, largest (Length) at index 2
            var sortedDims = sort(dimensions, function(a, b)
            {
                return a - b;
            });

            // Assign the sorted values to consistent names
            var thickness = sortedDims[0]; // Smallest dimension
            var width = sortedDims[1]; // Middle dimension
            var length = sortedDims[2]; // Largest dimension

            rows = append(rows, tableRow({ "name" : partName, "length" : length,
                            "width" : width, "thickness" : thickness }));

            println(derivedFeatureName ~ " " ~ partName ~ "," ~ roundToPrecision(length / millimeter, 1) ~ " mm,"
                    ~ roundToPrecision(width / millimeter, 1) ~ " mm," ~
                    roundToPrecision(thickness / millimeter, 1) ~ "mm");

        }
        return table("Cut List", columnDefinitions, rows);
    });

Tagged:
Sign In or Register to comment.