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.

multisort an array/custom table with featurescript

rooiejorisrooiejoris OS Professional Posts: 16 PRO
Hi,

As I start always: I am not a programmer, but I think I want something fairly simple:

I want a 'multisort' in an array or custom table.
At the moment I postprocess the tabel in an php script and use:
    array_multisort(array_column($arrRondjesGrid, 'y'), SORT_ASC, array_column($arrRondjesGrid, 'x'), SORT_DESC, $arrRondjesGrid);

But I think this should be possible within onshape as well. Current script:

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


annotation { "Table Type Name" : "Get Center Coordinates" }
export const partVolumes = defineTable(function(context is Context, definition is map) returns Table
    precondition
    {
    }
    {
/*
        var columns = [tableColumnDefinition("name", "Part name"),
                       tableColumnDefinition("coord", "Coordinates"),
                       tableColumnDefinition("xcoord", "X"),
                       tableColumnDefinition("ycoord", "Y"),
                       tableColumnDefinition("xycoord", "XY")];
*/
        var columns = [tableColumnDefinition("xycoord", "XY")];
                       

        var rows = [];
        for (var part in evaluateQuery(context, qAllModifiableSolidBodies()))
        {
            var partName = getProperty(context, { "entity" : part, "propertyType" : PropertyType.NAME } );
            //var volume = evVolume(context, { "entities" : part });
            var coord = box3dCenter(evBox3d(context, {
                    "topology" : part,
                    "tight" : true
            }));

            var xcoord = roundToPrecision(coord[0]/millimeter,3);
            var ycoord = roundToPrecision(coord[1]/millimeter,3);
            var xycoord = "" ~ xcoord ~ "," ~ ycoord;


/*
            for (var key, value in coord)
            {
                //println("Key: " ~ key ~ ", Value: " ~ value);
                var xcoord = key;
                var ycoord = value;
            rows = append(rows, tableRow({ "name" : partName, "coord" : xcoord,  "coord" : ycoord }));
            }
*/
            rows = append(rows, tableRow({"xycoord" : xycoord }));
            //rows = append(rows, tableRow({ "name" : partName, "coord" : coord, "xcoord" : xcoord, "ycoord" : ycoord, "xycoord" : xycoord }));
            //rows = append(rows, tableRow({ "name" : partName, "coord" : coord }));
            //rows = append(rows, tableRow({ "name" : partName, "coord" : coord["a"] }));
        }

        //sort(coord,function(a, b) { return a - b; });

        return table("Part coordinates", columns, rows);
    });

Maybe an extra for next loop is needed to append the rows and use the current for next loop to fill an array...?!
So then you get: 1- loop through bodies to fill array, 2- multisort that array first Y ascending then X descending, 3- loop through array to append the custom table.


thanks in advance...!

cheers / joris




Sign In or Register to comment.