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:

  1. Table tab = Table("MyGlobalTable");
  2. Array arr = tab.clone();
  3. var outVarArray = node("MODEL:/Tools/UserCommands/callPyFnc_getTable/code").evaluate(arr);
  4. 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.

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:

  1. def getTable(arrayFromFlexSim):
  2.     arr = np.array(arrayFromFlexSim)
  3.     seq = dict()     
  4.     for i in range(0, 3):            
  5.         seq[i] = np.array2string(arr)
  6.     
  7.     return seq

The Dictionary get converted correctly to FlexSim:

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


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

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