First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!
1 | initial version |
Firstly, I would like to thank the OP for posting Siemens answer back.
Secondly, I would like to point out the documentation for the 'struct' module in Python is : https://docs.python.org/2/library/struct.html
Now, though Siemens has provided an answer, but I think is not complete, for the following reasons:
From Siemens' answer, I could make some guesses. But they are guesses at best, I don't know whether they would work due to the two points mentioned above.
Here we go.
import sliderPy
import struct
# this is the format to unpack, I am gussing it from Siemens' answer
# assuming Siemens means most significant to least significant
# int (i),
# int (i),
# copy id assumed unsigned long long (Q),
# 3 bytes reserved assumed 3 pad bytes (3x),
# int (i),
# int (i),
# int (i),
# 2 bytes ID assumed a single 2-byte string (2s)
# 1 byte OPF branch flow ID (s)
# 1 byte reserved assumed 1 pad byte (x)
CONST_STR_FMT = 'iiQ3xiii2ssx'
# just a simple wrapper to make debugging easier
# you could also use decorator instead
def unpackMapStr(mapStr, str_fmt=CONST_STR_FMT):
'''
Just a simple wrapper to make debugging easier.
Parameter
---------
mapStr : byte
The map string byte returned from PSSE34.
str_fmt : string
The format for byte unpacking.
Default = CONST_STR_FMT
Return
---------
Tuple of the unpacked byte.
Example
---------
.. code:: python
# pack a float into the double format bytes
mapStr = struct.pack('d', 12.34)
print(unpackMapStr(mapStr, 'd'))
# note that the return is a tuple
# you have to get the individual elements to make sense of it
>>> (12.34,)
'''
return struct.unpack(str_fmt, mapStr)
if __name__ == '__main__':
mydoc = sliderPy.GetActiveDocument()
mydiagram = mydoc.GetDiagram()
mycomponents = mydiagram.GetComponents()
for mycomponent in mycomponents:
if mycomponent.IsSelected() == True:
try:
print('Type : ' + str(mycomponent.GetComponentType()))
# get the map string bytes
byte_temp = mycomponent.GetMapString()
str_temp = str(unpackMapStr(byte_temp))
# if I guess right and it works, then you should see a tuple
print('Map String : ' + str_temp)
# Also note that the strings returned are in bytes
# you may want to use the "decode" to convert them into
# something like 'utf-8'
except:
pass
Hope that you could get some idea from it. I don't know whether the code would work or not. But packing and unpacking bytes in Python is not a difficult thing, IF, the format is known.
2 | No.2 Revision |
Firstly, I would like to thank the OP for posting Siemens answer back.
Secondly, I would like to point out the documentation for the 'struct' module in Python is : https://docs.python.org/2/library/struct.html
Now, though Siemens has provided an answer, but I think is not complete, for the following reasons:
From Siemens' answer, I could make some guesses. But they are guesses at best, I don't know whether they would work due to the two points mentioned above.
Here we go.
import sliderPy
import struct
# this is the format to unpack, I am gussing it from Siemens' answer
# assuming Siemens means most significant to least significant
# int (i),
# int (i),
# copy id assumed unsigned long long (Q),
# 3 bytes reserved assumed 3 pad bytes (3x),
# int (i),
# int (i),
# int (i),
# 2 bytes ID assumed a single 2-byte string (2s)
# 1 byte OPF branch flow ID (s)
# 1 byte reserved assumed 1 pad byte (x)
CONST_STR_FMT = 'iiQ3xiii2ssx'
# just a simple wrapper to make debugging easier
# you could also use decorator instead
def unpackMapStr(mapStr, str_fmt=CONST_STR_FMT):
'''
Just a simple wrapper to make debugging easier.
Parameter
---------
mapStr : byte
The map string byte returned from PSSE34.
str_fmt : string
The format for byte unpacking.
Default = CONST_STR_FMT
Return
---------
Tuple of the unpacked byte.
Example
---------
.. code:: python
# pack a float into the double format bytes
mapStr = struct.pack('d', 12.34)
print(unpackMapStr(mapStr, 'd'))
# note that the return is a tuple
# you have to get the individual elements to make sense of it
>>> (12.34,)
'''
return struct.unpack(str_fmt, mapStr)
if __name__ == '__main__':
mydoc = sliderPy.GetActiveDocument()
mydiagram = mydoc.GetDiagram()
mycomponents = mydiagram.GetComponents()
for mycomponent in mycomponents:
if mycomponent.IsSelected() == True:
try:
print('Type : ' + str(mycomponent.GetComponentType()))
# get the map string bytes
byte_temp = mycomponent.GetMapString()
str_temp = str(unpackMapStr(byte_temp))
# if I guess right and it works, then you should see a tuple
print('Map String : ' + str_temp)
# Also note that the strings returned are in bytes
# you may want to use the "decode" to convert them into
# something like 'utf-8'
except:
pass
Hope that you could get some idea from it. I don't know whether the code would work or not. But packing and unpacking bytes in Python is not a difficult thing, IF, the format is known.