question

Ulrich B2 avatar image
0 Likes"
Ulrich B2 asked Ulrich B2 published

Exchange of Multi-Dimensional Arrays between FlexSim and Python

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

FlexSim 22.2.1
pythonarrays
5 |100000

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

Ulrich B2 avatar image
2 Likes"
Ulrich B2 answered Ralf Gruber commented

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


· 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.

Ralf Gruber avatar image Ralf Gruber ♦ commented ·

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 0 ·
Ulrich B2 avatar image
1 Like"
Ulrich B2 answered Ulrich B2 published

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[i] = 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. :)

5 |100000

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