python-2.7ansys

Comparing two Ansys Mechanical Model Trees


I want to compare two Ansys Mechanical Models and summarize the differences. For some cases it is sufficient to just compare the two ds.dat files with e.g. Notepad++. But different meshes make this comparison quickly confusing.

My idea is to export the tree of the two Models from ExtAPI.DataModel.Project.Model to two dictionaries and than compare those. But I have difficulties keeping the structure. When I try to iterate through all Children inspiried by this link with

#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)+ '}'

I get the structure in a usable format:

iterate(Model) 
{
    Geometry:  {
        Part:  {
            Body1
        }
    ...
    }

    Materials:  {
        Structural Steel
    }
    Cross Sections:  {
        Extracted Profile1
    }
    Coordinate Systems:  {
        Global Coordinate System
    } ... }

But now I am strugling with replacing the print statement in the recursive function and keeping the structure from the model tree.


Solution

  • Move it into a dictionary comprehension:

    def iterate(dictionary):
        return {
            child.Name: (iterate(child) if child.Children else None)
            for child in dictionary.Children
        }
    

    The output will be:

    {
        "Geometry":  {
            "Part":  {
                "Body1": None
            },
        ...
        },
    
        "Materials":  {
            "Structural Steel": None,
        }
        "Cross Sections":  {
            "Extracted Profile1": None,
        }
        "Coordinate Systems":  {
            "Global Coordinate System": None
        } ... }
    

    You can use pprint.pprint() if you wish to format it nicely for presentation.