Exchange of Multi-Dimensional Arrays between FlexSim and Python

Exchange of Multi-Dimensional Arrays between FlexSim and Python

ulrich_b2
Not applicable
25 Views
3 Replies
Message 1 of 4

Exchange of Multi-Dimensional Arrays between FlexSim and Python

ulrich_b2
Not applicable

[ FlexSim 22.2.1 ]

Hello,

is there a way to exchange of Multi-Dimensional Arrays between FlexSim and Python?

I have only managed to pass 1-Dim Arrays to my py script, returning a list greater then 1-Dim back to FlexSim results in a Null-Object.


Thank you!


Uli

0 Likes
Accepted solutions (1)
26 Views
3 Replies
Replies (3)
Message 2 of 4

ulrich_b2
Not applicable
Accepted solution

Hello,

this seems to work:

Table tab = Table("MyGlobalTable");
Array arr = tab.clone();
var outVarArray = node("MODEL:/Tools/UserCommands/callPyFnc_getTable/code").evaluate(arr);
print(arr, " --> Return from Python --> ", outVarArray);

The only thing to consider: integers arrive as floats on python side, therefore, if used for e.g. indexing, these have to be casted to integer's


Thanks,


Uli


Message 3 of 4

ralf_gruber
Collaborator
Collaborator

Uli,

per class definition in FlexSim an array is one dimension only:

https://docs.flexsim.com/en/22.2/Reference/CodingInFlexSim/FlexScriptAPIReference/Data/Array.html

You can use arrays or variant type variables as the elements of your one dimensional array to use it in multiple dimensions, but you will have to consider that when reading elements of your array.

Good luck

0 Likes
Message 4 of 4

ulrich_b2
Not applicable

It seems like if FlexSim <-> Python conversions are "string" centric.

Passing back a dictionary with a Two-Dimensional Array as value is possible by setting the value as a string representation of the array:

def getTable(arrayFromFlexSim):  
    arr = np.array(arrayFromFlexSim)
    seq = dict()     
    for i in range(0, 3):            
        seq = np.array2string(arr)
    
    return seq

The Dictionary get converted correctly to FlexSim:

Map: {
0: Array[3]: {Array[5]: {1,1,0,1,1},Array[5]: {1,0,0,1,1},Array[5]: {1,1,1,1,2}}, 
1: Array[3]: {Array[5]: {1,1,0,1,1},Array[5]: {1,0,0,1,1},Array[5]: {1,1,1,1,2}}, 
2: Array[3]: {Array[5]: {1,1,0,1,1},Array[5]: {1,0,0,1,1},Array[5]: {1,1,1,1,2}}
}


If the 2-Dim Array is not converted to its string representation, the conversion result on FlexSim side will fail:

Map: {0: NULL, 1: NULL, 2: NULL}


Just in case someone might get trapped here too. 🙂