TraverseExtension

The examples in this section address programming calls that query geometry, mesh, simulation, and results properties. Each of the code samples are taken from the TraverseExtension extension.


Note:  This extension is included in the package ACT Developer's Guide Examples, which you can download from the ACT Resources page on the ANSYS Customer Portal. To display the page, select Downloads > ACT Resources. To download the package, expand the Help & Support section and click ACT Developer's Guide Examples under Documentation.


Traversing the Geometry

The API for geometry data is organized to match the underlying hierarchical data model. The API offers a variety of property queries through which the connectivity of the geometry entities can be determined. For instance, it is possible to query for the faces upon which an edge is defined by using the property Faces exposed by the edge interface. See the ANSYS ACT API Reference Guide for a comprehensive list of these interfaces and their properties.

The basic hierarchy of the geometry is:

- Geometry
  - Assembly
    - Part
      - Body
        - Shell
        - Face
        - Edge
        - Vertex

The IronPython script function traversegeometry() demonstrates how an extension could traverse the geometry data. In this example, an object of type IGeoData is obtained from the object Analysis using the property GeoData. This object GeoData is then used to query for a list of IGeoAssembly objects by calling the property Assembly. For each of the IGeoAssembly objects in the returned list, the property Parts is called and a list of IGeoPart objects is returned. This pattern is repeated through the hierarchy of the geometry down to the vertices of each edge.

The content of the function traversegeometry() follows:

def traversegeometry(analysis):
    now = datetime.datetime.now()
    outFile = SetUserOutput(analysis, "SolutionDetails.log")
    f = open(outFile,'a')    
    f.write("*.*.*.*.*.*.*.*\n")
    f.write(str(now)+"\n")
    # --- IGeometry Interface
    # +++ Properties and Methods
    # +++    Assemblies
    # +++    CellFromRefId
    # +++    SelectedRefIds
    geometry = analysis.GeoData
    assemblies = geometry.Assemblies
    assemblies_count = assemblies.Count
    # --- IGeoAssembly Interface
    # +++ Properties and Methods
    # +++    Name
    # +++    Parts
    for assembly in assemblies:
        assembly_name = assembly.Name
        parts = assembly.Parts
        parts_count = parts.Count
        # --- IGeoPart Interface
        # +++ Properties and Methods
        # +++    Name
        # +++    Bodies
        for part in parts:
            part_name = part.Name
            bodies = part.Bodies
            bodies_count = bodies.Count
            # --- IGeoBody Interface
            # +++ Properties and Methods
            # +++    Name
            # +++    Vertices
            # +++    Edges
            # +++    Faces
            # +++    Shells
            # +++    Material
            for body in bodies:
                faces = body.Faces
                faces_count = faces.Count
                # --- IGeoFace Interface
                # +++ Properties and Methods
                # +++    Body
                # +++    Shell
                # +++    Vertices
                # +++    Edges
                # +++    Loops
                # +++    Area
                # +++    SurfaceType
                # +++    PointAtParam
                # +++    PointsAtParams
                for face in faces:
                    edges = face.Edges
                    edges_count = edges.Count
                    # --- IGeoEdge Interface
                    # +++ Properties and Methods
                    # +++    Faces
                    # +++    Vertices
                    # +++    StartVertex
                    # +++    EndVertex
                    # +++    Length
                    # +++    CurveType
                    # +++    Extents
                    # +++    IsParamReversed
                    # +++    ParamAtPoint
                    # +++    PointAtParam
                    # +++    PointsAtParams
                    for edge in edges:
                        vertices = edge.Vertices
                        vertices_count = vertices.Count
                        # --- IGeoVertex Interface
                        # +++ Properties and Methods
                        # +++ Edges   
                        # +++ Faces   
                        # +++ Bodies   
                        # +++ X   
                        # +++ Y   
                        # +++ Z
                        for vertex in vertices:
                            xcoord = vertex.X
                            ycoord = vertex.Y
                            zcoord = vertex.Z
                            try:
                                f.write("      Vertex: "+vertex.ToString()+", X = "+xcoord.ToString()+", Y = 
"+ycoord.ToString()+", Z = "+zcoord.ToString()+"\n")
                            except:
                                continue
    f.close()
    return

Traversing the Mesh

The API Mesh offers a variety of property queries through which the connectivity of the mesh entities can be determined. For comprehensive information on the interfaces and properties for this API, see the ANSYS ACT API Reference Guide.

The following IronPython script function traversemesh() demonstrates how an extension could traverse the mesh data. In this example, an object of type IMeshData is obtained from the object IAnalysis using the property MeshData. This mesh object is then used to query for a list of element IDs with the property Elements. For each of the element IDs in the returned list, the method Element is called and the corresponding object IElement is returned. This pattern is repeated for all the nodes for each element. Finally, the coordinates of the nodes are queried. Comments are included in the content of the function traversemesh() to clarify script functionality.

def traversemesh(analysis):
    now = datetime.datetime.now()
    outFile = SetUserOutput(analysis, "SolutionDetails.log")
    f = open(outFile,'a')    
    f.write("*.*.*.*.*.*.*.*\n")
    f.write(str(now)+"\n")
    # --- IMesh Interface
    # +++ Properties and Methods
    # +++ MeshRegion   
    # +++ Node   
    # +++ Element   
    # +++ Nodes   
    # +++ Elements   
    # +++ NumNodes   
    # +++ NumElements   
    mesh = analysis.MeshData
    elementids = mesh.ElementIds
    # --- IElement Interface
    # +++ Properties and Methods
    # +++ Id   
    # +++ Type   
    # +++ Nodes
    for elementid in elementids:
        element = mesh.ElementById(elementid)
        nodeids = element.NodeIds
        # --- INode Interface
        # +++ Properties and Methods
        # +++ Id   
        # +++ X   
        # +++ Y   
        # +++ Z   
        # +++ Elements
        for nodeid in nodeids:
            node = mesh.NodeById(nodeid)
            nodex = node.X
            nodey = node.Y
            nodez = node.Z
            try:
                f.write("      Element: "+elementid.ToString()+"  Node: "+nodeid.ToString()+", X = 
"+nodex.ToString()+", Y = "+nodey.ToString()+", Z = "+nodez.ToString()+"\n")
            except:
                continue
    f.close()
    return

The script function elementcounter() can also be used to access the mesh data. In this example, only the elements of user-selected geometry entities are considered. First, the script obtains the objects IGeoData and IMeshData. The property CurrentSelection.Ids then queries the IDs of the selected geometry entities using the object ISelectionMgr. If no IDs are returned, a message box displays the message "Nothing Selected." Otherwise, the methods GeoEntityById and MeshRegionById obtain the objects IGeoEntity and IMeshRegion corresponding to each selected entity. These two objects are used inside the try-except block to query for the type of entity selected and the number of elements in each entity's mesh. The property Type of the interface IGeoEntity and the property NumElements of the interface IMeshRegion are used here and the results are displayed in a message box.

def elementcounter(analysis):
    geometry = analysis.GeoData
    mesh = analysis.MeshData
    selectedids = ExtAPI.SelectionManager.CurrentSelection.Ids
    if selectedids.Count == 0:
        MessageBox.Show("Nothing Selected!")
    else:
        for selectedid in selectedids:
            entity = geometry.GeoEntityById(selectedid)
            meshregion = mesh.MeshRegionById(selectedid)
            try:
                numelem = meshregion.ElementCount
                MessageBox.Show("Entity of type: "+entity.Type.ToString()+
                                " contains "+numelem.ToString()+
                                " elements.")                
            except:
                MessageBox.Show("The mesh is empty!")
                return
    return

Traversing Results

The script function minmaxresults() computes the minimum and maximum component values of the nodal displacement and the SXX stress component. It begins by instantiating a result reader using the method analysis.ResultsData. Results are retrieved relative to the finite element model and queried using either the elementID (elemental result) or the nodeID (nodal result). The displacement result "U" is a nodal result, whereas the stress result "S" is a result on nodes of the elements. The displacement result stores a set of component values for each node, where the component names are X, Y, and Z.

The script minmaxresults first iterates over the nodeIDs to compute the minimum and maximum values. Then, the script iterates over the elementIDs and the nodes of each element to compute the minimum and maximum values.


Note:  The second loop over the nodes is filtered to the primary nodes of the elements because stress results are available only on these primary nodes.


Finally, the results are written to the output file.

The contents of the function minmaxresults() follow.

def minmaxresults(analysis):
    now = datetime.datetime.now()
    outFile = SetUserOutput(analysis, "SolutionDetails.log")
    f = open(outFile,'a')    
    f.write("*.*.*.*.*.*.*.*\n")
    f.write(str(now)+"\n")
    #
    # Get the element ids 
    #
    meshObj = analysis.MeshData
    elementids = meshObj.ElementIds
    nodeids = meshObj.NodeIds
    #  
    # Get the results reader
    #
    reader = analysis.GetResultsData()
    reader.CurrentResultSet = int(1) 
    #
    # Get the displacement result object
    displacement = reader.GetResult("U")
    
    num = 0
    for nodeid in nodeids: 
        #
        # Get the component displacements (X Y Z) for this node
        #     
        dispvals = displacement.GetNodeValues(nodeid)
        #
        # Determine if the component diplacement (X Y Z) is min or max
        #
        if num == 0:
            maxdispx = dispvals[0]
            mindispx = dispvals[0]
            maxdispy = dispvals[1]
            mindispy = dispvals[1]
            maxdispz = dispvals[2]
            mindispz = dispvals[2]
        
        num += 1
        
        if dispvals[0] > maxdispx:
            maxdispx = dispvals[0]
        if dispvals[1] > maxdispy:
            maxdispy = dispvals[1]
        if dispvals[2] > maxdispz:
            maxdispz = dispvals[2]
        if dispvals[0] < mindispx:
            mindispx = dispvals[0]
        if dispvals[1] < mindispy:
            mindispy = dispvals[1]
        if dispvals[2] < mindispz:
            mindispz = dispvals[2]
    
    # Get the stress result object
    stress = reader.GetResult("S")
    
    num = 0
    for elementid in elementids:  
        element = meshObj.ElementById(elementid)
        #
        # Get the SXX stress component
        #
        stressval = stress.GetElementValues(elementid)
        #
        # Get the primary node ids for this element
        #
        nodeids = element.CornerNodeIds
        for i in range(nodeids.Count): 
            #
            # Get the SXX stress component at node "nodeid"
            #
            SXX = stressval[i]
            #
            # Determine if the SXX stress component is min or max
            #
            if num == 0:
                maxsxx = SXX
                minsxx = SXX
            
            if SXX > maxsxx:
                maxsxx = SXX
            if SXX < minsxx:
                minsxx = SXX
                
            num += 1
    #
    # Write the results to the output
    #
    f.write("Max U,X:Y:Z = "+maxdispx.ToString()+" : "+maxdispy.ToString()+" : "+maxdispz.ToString()+"\n")
    f.write("Min U,X:Y:Z = "+mindispx.ToString()+" : "+mindispy.ToString()+" : "+mindispz.ToString()+"\n")
    f.write("Max SXX = "+maxsxx.ToString()+"\n")
    f.write("Min SXX = "+minsxx.ToString()+"\n")
    f.close()

Release 18.2 - © ANSYS, Inc. All rights reserved.