我想比較兩個 Ansys 機械模型并總結差異。在某些情況下,只需將兩個 ds.dat 檔案與例如 Notepad 進行比較就足夠了。但是不同的網格使這種比較很快變得混亂。
我的想法是將兩個模型的樹從
ExtAPI.DataModel.Project.Model兩個字典匯出,然后比較它們。但是我很難保持結構。當我嘗試遍歷所有受此鏈接啟發的孩子時
#recursive function to iterate a nested dictionary
def iterate(dictionary, indent=4):
print '{'
for child in dictionary.Children:
#recurse if the value is a dictionary
if len(child.Children) != 0:
print ' '*indent child.Name ": " ,
iterate(child, indent 4)
else:
print ' '*indent child.Name
print ' '*(indent-4) '}'
我得到了可用格式的結構:
iterate(Model)
{
Geometry: {
Part: {
Body1
}
...
}
Materials: {
Structural Steel
}
Cross Sections: {
Extracted Profile1
}
Coordinate Systems: {
Global Coordinate System
} ... }
但是現在我正在努力替換遞回函式中的列印陳述句并保留模型樹中的結構。
uj5u.com熱心網友回復:
將其移動到字典理解中:
def iterate(dictionary):
return {
child.Name: (iterate(child) if child.Children else None)
for child in dictionary.Children
}
輸出將是:
{
"Geometry": {
"Part": {
"Body1": None
},
...
},
"Materials": {
"Structural Steel": None,
}
"Cross Sections": {
"Extracted Profile1": None,
}
"Coordinate Systems": {
"Global Coordinate System": None
} ... }
pprint.pprint()如果您希望將其格式化以進行演示,則可以使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415926.html
標籤:
