Sheldon C4 avatar image
Sheldon C4 asked Sheldon C4 commented

Global table control in C++ SDK

I'm trying to use SDK to get or set value from Global table by cell.value function, but it didn't work.

Here is my code.

        treenode RoutingData = model()->find("Tools/GlobalTables/RoutingData");
        if (RoutingData != NULL) {
        else {

        RoutingData = applicationcommand("addglobaltable", 1);

        Table* A = RoutingData->objectAs(Table);
        int row = 1;
        int colum = 1;
        print(A->cell(1, 1)->value);


FlexSim 22.1.0
5 |100000

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

1 Answer

Jason Lightfoot avatar image
Jason Lightfoot answered Sheldon C4 commented

Try this instead:

    treenode RoutingData = applicationcommand("addglobaltable", 1);
    Table A = reftable("RoutingData"); // RoutingData->objectAs(Table);
    int row = 1;
    int column = 1;
    A[1][1] = 10;
   print( A[1][1]);
· 7
5 |100000

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

Sheldon C4 avatar image Sheldon C4 commented ·

Jason Thanks, it's worked.

But I got two question didn't solve.

1. I don't know why my original code that didn't worked. and Why I got the memory error?

It's mean objectAs that is not the method to cast any flexsim class to anther?

 Table* A = RoutingData->objectAs(Table);

2. Actually, before I ask this question, I try to use the model find method to assign the file path and input into the Table(like below code).

Table A = model()->find("Tools/GlobalTables/RoutingData");

print(A->cell(1, 1)->value);

It's didn't show the memory error, but the code also didn't work, it's will jump pass the code.

Thanks for your answer, but I need to know what's wrong to get fix it.

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ Sheldon C4 commented ·
The actual data structure is held at >variables/data under the global table object - so maybe it needs that. When I tried that it complained about not being able to cast a treenode as a Table. Hopefully one of the devs can tell you more, but I'm not sure why you need that syntax - why can't you use what works?
0 Likes 0 ·
Sheldon C4 avatar image Sheldon C4 Jason Lightfoot ♦ commented ·
Thanks for your answer, because I tried for this question for a while, so I just don't know why I can't cast a treenode as a Table.

It's bother me and stuck in my head, I hope devs can give me a answer.

Your answer help me a lot, I already using your answer to solve problem.

Best regards

0 Likes 0 ·
Jordan Johnson avatar image Jordan Johnson ♦♦ Sheldon C4 commented ·

The Table class wrapper class, that points to a table structure, and knows how to access its values. So you construct an instance of the Table() class by passing the name of a global table. Alternatively, you can pass in a node that is structured like a table (either a bundle, or a node with direct subnodes for rows, and each of those has subnodes for cells). That's why you need reftable(), so you can get the actual node that holds the table structure

Table a = Table("GlobalTableName"); // correct
Table b = Table(reftable("GlobalTableName")); // correct

treenode RoutingData = applicationcommand("addglobaltable", 1);
Table c = Table(RoutingData); // incorrect
// The RoutingData node doesn't have subnodes, and it doesn't have bundle data,
// so the Table constructor sees it as a table with zero rows and columns

// do not do this, it is incorrect
Table* d = RoutingTable->objectAs(Table);
// TreeNodes cannot hold Table* data

The objectAs() is very simple, and can easily be used in an unsafe way; it's just a convience wrapper around static_cast(). It tries to cast whatever data is on the node as the type you pass in. But since TreeNodes cannot point to an instance of Table, the cast will succeed, but point to something totally invalid.

So when you say you want to use objectAs() to cast from any FlexSim class to another, that is not a correct statement. You need to know what kind of data is on the node.

0 Likes 0 ·
Sheldon C4 avatar image Sheldon C4 Jordan Johnson ♦♦ commented ·


Thank you for help me clarify the concept of objectAs(), I got it.

And I test the first line in your code, it's didn't work

Table a = Table("GlobalTableName"); // incorrect

the error message is the below picture


Am I lost something?

0 Likes 0 ·
tabletesterror.png (34.7 KiB)
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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