Note
Go to the end to download the full example code.
Display properties for an object#
Using supplied files, this example shows how to display the properties that you would see in an object’s details view in the Mechanical GUI.
Download required files#
Download the required files. Print the file path for the MECHDAT file.
import os
from ansys.mechanical.core import launch_mechanical
from ansys.mechanical.core.examples import download_file
mechdat_path = download_file(
"example_03_simple_bolt_new.mechdat", "pymechanical", "00_basic"
)
print(f"Downloaded the MECHDAT file to: {mechdat_path}")
Downloaded the MECHDAT file to: /home/runner/.local/share/ansys_mechanical_core/examples/example_03_simple_bolt_new.mechdat
Launch Mechanical#
Launch a new Mechanical session in batch, setting the cleanup_on_exit
argument to False
. To close this Mechanical session when finished,
this example must call the mechanical.exit()
method.
mechanical = launch_mechanical(batch=True, cleanup_on_exit=False)
print(mechanical)
Ansys Mechanical [Ansys Mechanical Enterprise]
Product Version:242
Software build date: 06/03/2024 09:35:09
Initialize the variable needed for this workflow#
Set the path for the mechdat_path
variable for later use.
Make this variable compatible for Windows, Linux, and Docker containers.
project_directory = mechanical.project_directory
print(f"project directory = {project_directory}")
# Upload the file to the project directory.
mechanical.upload(file_name=mechdat_path, file_location_destination=project_directory)
# Build the path relative to project directory.
base_name = os.path.basename(mechdat_path)
combined_path = os.path.join(project_directory, base_name)
mechdat_path_modified = combined_path.replace("\\", "\\\\")
mechanical.run_python_script(f"mechdat_path='{mechdat_path_modified}'")
# Verify the path.
result = mechanical.run_python_script(f"mechdat_path")
print(f"MECHDATA file is stored on the server at: {result}")
project directory = /tmp/ANSYS.root.1/AnsysMech3D4E/Project_Mech_Files/
Uploading example_03_simple_bolt_new.mechdat to dns:///127.0.0.1:10000:/tmp/ANSYS.root.1/AnsysMech3D4E/Project_Mech_Files/.: 0%| | 0.00/7.06M [00:00<?, ?B/s]
Uploading example_03_simple_bolt_new.mechdat to dns:///127.0.0.1:10000:/tmp/ANSYS.root.1/AnsysMech3D4E/Project_Mech_Files/.: 100%|██████████| 7.06M/7.06M [00:00<00:00, 248MB/s]
MECHDATA file is stored on the server at: /tmp/ANSYS.root.1/AnsysMech3D4E/Project_Mech_Files/example_03_simple_bolt_new.mechdat
Run the script#
Run the Mechanical script to display the properties and their current values for the analysis object.
result = mechanical.run_python_script(
"""
import json
ExtAPI.DataModel.Project.Open(mechdat_path)
analysisSettings = Model.Analyses[0].AnalysisSettings
props = {}
if hasattr(analysisSettings,'VisibleProperties') != False:
for prop in analysisSettings.VisibleProperties:
props[prop.Caption] = prop.StringValue
json.dumps(props, indent=1)
"""
)
print(f"AnalysisSettings properties:\n{result}")
AnalysisSettings properties:
{
"Contact Summary": "Program Controlled",
"Delete Unneeded Files": "Yes",
"Volume and Energy": "Yes",
"Nonlinear Data": "No",
"Combine Restart Files": "Program Controlled",
"Force Convergence": "Program Controlled",
"Step End Time": "1. s",
"Rotation Convergence": "Program Controlled",
"Solver Unit System": "mks",
"Output Selection": "None",
"Generate Restart Points": "Program Controlled",
"Coriolis Effect": "Off",
"Number Of Steps": "10.",
"Contact Data": "Yes",
"Inverse Option": "No",
"Auto Time Stepping": "Program Controlled",
"Stress": "Yes",
"Strain": "Yes",
"Result File Compression": "Program Controlled",
"Future Analysis": "None",
"Solver Units": "Active System",
"Large Deflection": "Off",
"Back Stress": "No",
"General Miscellaneous": "No",
"Scratch Solver Files Directory": "",
"Nodal Forces": "No",
"Save MAPDL db": "No",
"Nonlinear Solution": "No",
"Current Step Number": "1.",
"Euler Angles": "Yes",
"Store Results At": "All Time Points",
"Newton-Raphson Option": "Program Controlled",
"Moment Convergence": "Program Controlled",
"Solver Type": "Program Controlled",
"Inertia Relief": "Off",
"Weak Springs": "Off",
"Solver Pivot Checking": "Program Controlled",
"Contact Miscellaneous": "No",
"Solver Files Directory": "/tmp/ANSYS.root.1/AnsysMech3D4E/Project_Mech_Files/example_03_simple_bolt_new_Mech_Files/Static Structural (2)/",
"Stabilization": "Program Controlled",
"Contact Split (DMP)": "Off",
"Line Search": "Program Controlled",
"Quasi-Static Solution": "Off",
"Retain Files After Full Solve": "No",
"Displacement Convergence": "Program Controlled"
}
Clear the data#
Clear the data so it isn’t saved to the project.
mechanical.clear()
Close Mechanical#
Close the Mechanical instance.
mechanical.exit()
Total running time of the script: (0 minutes 1.112 seconds)