Share

API example: Looping through entities

Entities in studies are available though linked lists, one for each entity type, such as node, triangle, tetrahedron (tetra), and so forth. This example implements several loops for looking at each entity of each type.

This script performs the following tasks:

  • Finds the maximum node number
  • Counts the number of beams
  • Counts the number of tetras
  • Counts the number of triangles

You can use this script when you want to read nodal data.

# %RunPerInstance
"""
DESCRIPTION:
Example of how to loop through entities using the API

SYNTAX:
python filename.py
eg. 
python loop_entities.py
python loop_entities.py -silent True

PARAMETERS:
-silent: Info Prompts, if set to True, no info will pop up [default: False].

DEPENDENCIES/LIMITATIONS:
Requires Moldflow Synergy and active project
"""

from moldflow import Synergy
from moldflow.common import SystemUnits
import tkinter as tk
from tkinter import messagebox
import argparse

def parse_arguments():
    """
    Parses command-line arguments using argparse.

    Returns:
        argparse.Namespace: Parsed arguments.
    """
    parser = argparse.ArgumentParser(
        description="Loop through entities",
        add_help=False  # Disable default -h/--help behavior
    )
    parser.add_argument(
        "-silent", "--silent",
        type=bool,        
        help="Silent mode [default: False]."
    )
    return parser.parse_args()

def main():
    """Main function to loop through entities and count them."""
    args = parse_arguments()
    silent = args.silent
    # Output string
    output = ""

    synergy = Synergy(units=SystemUnits.METRIC)
    study_doc = synergy.study_doc

    # Loop through all nodes and find the highest Node Number
    max_number = 0
    node = study_doc.get_first_node()

    while node is not None:
        node_number = study_doc.get_entity_id(node)
        if node_number > max_number:
            max_number = node_number
        node = study_doc.get_next_node(node)

    if not silent:
        output += f"Maximum Node Number in Model is: {max_number}\n"
    print("Maximum Node Number in Model is: ", max_number)

    # Count all Triangular Elements in the Model
    count = 0
    tri = study_doc.get_first_tri()

    while tri is not None:
        count += 1
        tri = study_doc.get_next_tri(tri)

    if not silent:
        output += f"Model Contains {count} Triangular Elements\n"
    print("Model Contains ", count, " Triangular Elements")

    # Count all Tet Elements in the Model
    count = 0
    tet = study_doc.get_first_tet()

    while tet is not None:
        count += 1
        tet = study_doc.get_next_tet(tet)

    if not silent:
        output += f"Model Contains {count} Tet Elements\n"
    print("Model Contains ", count, " Tet Elements")

    # Count all Beam Elements in the Model
    count = 0
    beam = study_doc.get_first_beam()

    while beam is not None:
        count += 1
        beam = study_doc.get_next_beam(beam)

    if not silent:
        output += f"Model Contains {count} Beam Elements\n"
    print("Model Contains ", count, " Beam Elements")

    if not silent:
        # Initialize tkinter for message box
        root = tk.Tk()
        root.withdraw()
        messagebox.showinfo("Script Completed", output)
        root.destroy()

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print(f"Error: {e}")

Was this information helpful?