pyNastran: Nastran File Reader/Editor

1.4.1 · active · verified Thu Apr 16

pyNastran is an open-source Python library for reading, editing, and writing Nastran BDF, OP2, OP4, and F06 files, commonly used in finite element analysis (FEA). It supports various Nastran versions and provides tools for visualizing and manipulating FEA data. The library is actively maintained, with version 1.4.1 being the latest stable release and a consistent release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a dummy BDF file, read it using the `BDF` class, and access basic model information like nodes and elements. The `xref=True` argument in `read_bdf` is highlighted as essential for properly populating the model with cross-referenced data.

import os
from pyNastran.bdf.bdf import BDF

# Create a dummy BDF file for demonstration
bdf_content = """
SOL 101
CEND
BEGIN BULK
GRID,1,,0.0,0.0,0.0
GRID,2,,1.0,0.0,0.0
CQUAD4,1,1,1,2,3,4
ENDDATA
"""
dummy_bdf_path = "dummy.bdf"
with open(dummy_bdf_path, "w") as f:
    f.write(bdf_content)

# Read the BDF file
model = BDF()
try:
    # xref=True is crucial for resolving cross-references and building a complete model
    model.read_bdf(dummy_bdf_path, xref=True)
    print(f"Successfully read {dummy_bdf_path}")
    print(f"Number of GRIDs: {len(model.nodes)}")
    print(f"Number of CQUAD4 elements: {len(model.elements)}")

    # Accessing specific data (example)
    if 1 in model.nodes:
        print(f"Node 1 coordinates: {model.nodes[1].xyz}")
except Exception as e:
    print(f"Error reading BDF file: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(dummy_bdf_path):
        os.remove(dummy_bdf_path)

view raw JSON →