question

PVA avatar image
2 Likes"
PVA asked Natalie White commented

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

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

Now i want to change Table2 to Number datatype

FlexSim 23.1.2
using codebundle datatable datatype
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Carter Walch avatar image
0 Likes"
Carter Walch answered Carter Walch edited

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.

  1. treenode DataNode = Table("GlobalTable2").as(treenode);
  2. print(DataNode.dataType); // tests to see current data type
  3. DataNode.dataType = DATATYPE_NUMBER ; // set data type to Number
  4. 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


1686932588043.png (52.7 KiB)
tabledatatypes.fsm (25.5 KiB)
· 3
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jason Lightfoot avatar image
1 Like"
Jason Lightfoot answered Jason Lightfoot edited

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:

  1. 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)

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

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.