Skip to content

FEAT: Add an App method to print project tree for embedding scenario #779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 21, 2024
Merged
1 change: 1 addition & 0 deletions doc/changelog.d/779.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FEAT: Add an App method to print project tree for embedding scenario
50 changes: 50 additions & 0 deletions src/ansys/mechanical/core/embedding/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,56 @@ def plot(self) -> None:

plot_model(self)

def print_tree(self, node, indentation=""):
"""
Recursively prints a tree structure starting from the given node.

Each object in the tree is expected to have the following attributes:
- Name: The name of the object.
- Suppressed : Print as suppressed, if object is suppressed.
- Children: Checks if object have children.
Each child node is expected to have the all these attributes.

Parameters
----------
node: ExtAPI object
The starting object of the tree.

Raises
------
AttributeError
If the node does not have the required attributes.

Examples
--------
>>> import ansys.mechanical.core as mech
>>> app = mech.App()
>>> app.update_globals(globals())
>>> app.print_tree(DataModel.Project)
... ├── Project
... | ├── Model
... | | ├── Geometry Imports
... | | ├── Geometry
... | | ├── Materials
... | | ├── Coordinate Systems
... | | | ├── Global Coordinate System
... | | ├── Remote Points
... | | ├── Mesh

Prints a hierarchical tree representation of the Mechanical project structure.
"""
if not hasattr(node, "Name"):
raise AttributeError("Object must have a 'Name' attribute")

if hasattr(node, "Suppressed") and node.Suppressed is True:
print(f"{indentation}├── {node.Name} (Suppressed)")
else:
print(f"{indentation}├── {node.Name}")

if hasattr(node, "Children") and node.Children is not None and node.Children.Count > 0:
for child in node.Children:
self.print_tree(child, indentation + "| ")

@property
def poster(self) -> Poster:
"""Returns an instance of Poster."""
Expand Down
18 changes: 18 additions & 0 deletions tests/embedding/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ def test_nonblock_sleep(embedded_app):
assert (t2 - t1) >= 1


@pytest.mark.embedding
def test_app_print_tree(embedded_app, capsys, assets):
"""Test printing hierarchical tree of Mechanical ExtAPI object"""
embedded_app.update_globals(globals())
geometry_file = os.path.join(assets, "Eng157.x_t")
geometry_import = Model.GeometryImportGroup.AddGeometryImport()
geometry_import.Import(geometry_file)
allbodies = Model.GetChildren(DataModelObjectCategory.Body, True)
allbodies[0].Suppressed = True
embedded_app.print_tree(DataModel.Project)
captured = capsys.readouterr()
printed_output = captured.out.strip()
assert "├── Project" in printed_output

with pytest.raises(AttributeError):
embedded_app.print_tree(DataModel)


@pytest.mark.embedding
def test_app_poster(embedded_app):
"""The getters of app should be usable after a new().
Expand Down