When the user selects “evaluate” the python script will:
Read the CSV file
Identify the location of the CSV file; this is stored in the upstream result path
Convert it to a dictionary based on the element ID; each entry of the dictionary is a list of values for each column in the file
Read each line of the file
Split using the commas as the delimiter
Convert the text into numeric values
Store the values in an array
Add the array into the dictionary based on the ID
For each DAResult create a DAResultSet. Each DAResultSet will display a value for each element
Find the column to use based on the users entry
Create the DAResultSet
The value is found by looking it up in the dictionary with the given element ID
#import System
DA = DesignAssessment
#1.b.iii - Define a rountine to convert text to either a real or integer number.
def convertStr(s):
#remove the comma
s = s.translate(None,',')
#If a value exists
if len(s) > 0:
#Try to convert to an integer
try:
ret = int(s)
except ValueError:
#couldn't convert to an integer, try a real number
try:
ret = float(s)
except ValueError:
#couldn't convert to a number, set to large value
#(makes Mechanical display translucent)
ret = 1.7976931348623157e+308
return ret
#1.b - Define seperate routine to convert CSV to a dictionary for in-memory access.
def CSVToDictionary(PathAndFile):
#Define a dictionary
IDToDataDict = {};
#Open the file
CSVFile = open(PathAndFile,"r")
#1.b.i - step through the file, line by line.
for line in CSVFile:
#1.b.ii - Split into an array of words.
words = line.split(",")
#Get the first column, this is the identifier (e.g. Element or Node ID)
ID = convertStr(words[0])
#1.b.iv - All other data becomes a list of numbers
Data = []
for i in range(len(words)-1):
Data.append(convertStr(words[i+1]))
#1.b.v - Assign the list to the identifier in the dictionary
IDToDataDict[ID] = Data
#Close the file and return the dictionary.
CSVFile.close()
return IDToDataDict
def runDADemo():
#1.a - Find where the CSV fle is stored.
SolPath = DA.SolutionSelections()[0].SolutionByRow(1).ResultFilePath
CSVPath = SolPath.rstrip('file.rst') + "data.csv"
#1.b - Call the function to convert the CSV file into a dictionary
IDToDataDict = CSVToDictionary(CSVPath)
#2 - access each DA Result object in the available results
for DAResult in DA.DAResults():
#2.a - Find the column to look up in the CSV data
ColIndex = DAResult.Attribute("Column").ValueAsInt - 1
#2.b - Create a result set to display the results using.
#We know that in this case it's scalar and element based.
DAResultSet = DAResult.CreateDAResultSet("Scalar","Elemental")
#2.c - For each element set the value.
for Element in DA.MeshData.Elements():
DAResultSet.SetElementalValue(Element.Id,1,IDToDataDict[Element.Id][ColIndex])
runDADemo()