How to change datatype of a Table from Bundle to Number through script?

How to change datatype of a Table from Bundle to Number through script?

TeamsTech
Not applicable
92 Views
6 Replies
Message 1 of 7

How to change datatype of a Table from Bundle to Number through script?

TeamsTech
Not applicable

[ FlexSim 23.1.2 ]

I have taken clone of Table1 in bundle datatype as Table2..

Now i want to change Table2 to Number datatype

Accepted solutions (1)
93 Views
6 Replies
Replies (6)
Message 2 of 7

carter-walch
Not applicable
Accepted solution

Hi @PVA ,

tableDataTypes.fsm

You can convert the table it to a treenode and then use the 'dataType' property of the treenode class.

See Jason's answer above for an updated solution.

treenode DataNode = Table("GlobalTable2").as(treenode);
print(DataNode.dataType); // tests to see current data type
DataNode.dataType = DATATYPE_NUMBER ; // set data type to Number
print(DataNode.dataType); // test to confirm new datatype

The datatype macros can be found here

1686932588043.png

Note: changing the datatype to certain datatypes may change your table. To reset it, simply run the code to set back to the original datatype.

This post may also be helpful in testing the datatype of table nodes

0 Likes
Message 3 of 7

TeamsTech
Not applicable

Hi Carter,

Thank you for your answer. But it hasn't completely solved my problem. I'm trying to duplicate a bundle table and convert the datatype from bundle to integer. I'm attaching the model FYR.

Thank you.tabledatatypes.fsm

0 Likes
Message 4 of 7

arunTTT2P
Participant
Participant
You are trying to make the table a bundle type or making each cell type as bundle.
0 Likes
Message 5 of 7

moehlmann_fe
Explorer
Explorer

I'm assuming you want to change the individual table cells from arrays to numbers? (A bundle is a different data structure for the entire table)

For that you can either use Table.setSize() and overwrite the previous values. But this will also remove any row and column headers.

Alternatively you can loop through all cells and change their data type individually.

Table table2 = Table("GlobalTable2");
// This also resets headers table2.setSize(table2.numRows, table2.numCols, DATATYPE_NUMBER, 1); // Loop through cells instead for(int row = 1; row <= table2.numRows; row++) {     for(int col = 1; col <= table2.numCols; col++)     {         table2.cell(row, col).dataType = DATATYPE_NUMBER;     } }
0 Likes
Message 6 of 7

jason_lightfootVL7B4
Autodesk
Autodesk

You can't change a table (a structure of related data values stored as a bundle or nested treenodes) to a single number and retain the data.

When you have bundle data in a treenode table's cell, you can simply overwrite the cell data with a new variant:

Table("GlobalTable2").cell(2,1).value=6

If anyone finding this post wishes to toggle between treenode-based and bundle table types here's some updated code showing an example data node that is a cell within a global table (first line - this was the case in the orginal poster's model)

treenode data=Table("GlobalTable2").cell(4,1);   //Global table data or other data node
int isGlobalTable=classobject(data.up)==library().find("?GlobalTable");
treenode newData;
if(getdatatype(data) != DATATYPE_BUNDLE) {
    newData = nodeinsertafter(data);
    nodeadddata(newData, DATATYPE_BUNDLE);
} 
else if (isGlobalTable) //Copy from the library so we get our TableHeader node
   newData = createcopy(library().find("?GlobalTable>variables/data"), data.up);
else {   //just a node somewhere
     newData = nodeinsertafter(data);
     newData.value="<Treenode Table>";   // so a user can see the node contains table structure
 }
data.as(Table).cloneTo(newData.as(Table));
createcopy(newData,data,1,0,0,1);  // this preserves any pointers to the table that already existed in the model
destroyobject(newData);
Message 7 of 7

natalie_white
Not applicable

Hi @PVA, was one of Jason Lightfoot's or Carter Walch's answers helpful? If so, please click the "Accept" button at the bottom of the one that best answers your question. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes