Skip to main content

PsimReadGraphFile

Usage

psim.PsimReadGraphFile();

Python Help

py.help(psim.PsimReadGraphFile)

Python def

def PsimReadGraphFile(self, graphFilePath) -> PSIM_result:
result = PSIM_result(self)
if not self.IsValid():
result.ErrorMessage = "PSIM object was not loaded."
result.Result = 0;
result.IsError = 1;
return result;

try:
grf = PSIM.__private_OpenGraphFile(self, graphFilePath)

if(grf > 0):
result.graphHandle = grf

Graph_GetColCount = getattr(self.psimHandle, "Graph_GetColCount")
if not Graph_GetColCount:
raise AttributeError(self.VersionErrorMessage)

Graph_GetRowCount = getattr(self.psimHandle, "Graph_GetRowCount")
if not Graph_GetRowCount:
raise AttributeError(self.VersionErrorMessage)

Graph_GetColInfo = getattr(self.psimHandle, "Graph_GetColInfo")
if not Graph_GetColInfo:
raise AttributeError(self.VersionErrorMessage)


Graph_GetColCount.restype = ctypes.c_int
Graph_GetRowCount.restype = ctypes.c_int

Graph_GetColInfo.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.POINTER(ctypes.c_wchar)), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.POINTER(ctypes.c_double)), ctypes.POINTER(ctypes.POINTER(ctypes.c_byte))]
Graph_GetColInfo.restype = ctypes.c_int

pnRows = ctypes.c_int(0)
pnIncDec = ctypes.c_int(0)

# int Graph_GetColCount(int nGraphRef)
numberOfCol = Graph_GetColCount(ctypes.c_int(grf))
#print("Number of curves in this file: ", numberOfCol)

# int Graph_GetRowCount(int nGraphRef)
numberOfRows = Graph_GetRowCount(ctypes.c_int(grf))
#print("Number of rows for each curve: ", numberOfRows)


# BOOL Graph_GetColInfo(int nGraphRef, int nFlag, int nColIndex, LPTSTR * ptrName, int * pnRows, int * pnIncDec, double ** ppValues, BYTE ** ppValidState)
# ptrName : name of the curve
# ppValues: values
# ppValidState: flag for each value 0:missing 1:valid 2:invalid
# pnRows : number of rows


for col in range(0 , numberOfCol):
# read one curve at a time
#print("Reading curve number ", col+1)

ppValues1_B = (ctypes.c_double * numberOfRows)()
ppValidState1_B = (ctypes.c_byte * numberOfRows)()

ppValues = ctypes.cast(ppValues1_B, ctypes.POINTER(ctypes.c_double))
ppValidState = ctypes.cast(ppValidState1_B, ctypes.POINTER(ctypes.c_byte))

ptrName1 = (ctypes.c_wchar * 100)()
ptrName = ctypes.cast(ptrName1, ctypes.POINTER(ctypes.c_wchar))

b = Graph_GetColInfo(ctypes.c_int(grf), ctypes.c_int (0), ctypes.c_int(col), ctypes.byref(ptrName), ctypes.byref(pnRows), ctypes.byref(pnIncDec), ctypes.byref(ppValues), ctypes.byref(ppValidState))


ptrName3 = ctypes.cast(ptrName, ctypes.POINTER(ctypes.c_wchar * 100))

#print(ptrName3.contents.value)
#print(ppValues[0])
#print(ppValues[10])

crv = PSIM_curve(pnRows.value, ptrName3.contents.value, ppValues, ppValidState)
result.Graph.add_curve(crv)

result.ErrorMessage = ""
result.IsError = 0
result.Result = 1;
else:
result.IsError = 1
result.Result = 0;
result.ErrorMessage = "Unable to open following file: " + graphFilePath

except Exception as e:
print(f"Error: {e}")
return result

def __private_OpenGraphFile(self, graphFilePath):
OpenGraphFile = getattr(self.psimHandle, "Graph_ReadFile")
if not OpenGraphFile:
raise AttributeError(self.VersionErrorMessage)
OpenGraphFile.restype = ctypes.c_int
# int OpenGraphFile(const wchar_t * szGraphFilePath)
grf = OpenGraphFile(ctypes.c_wchar_p(graphFilePath)); #returns 0 on error
return grf