200 lines
6.6 KiB
Python
200 lines
6.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Test to check if ANSYS generates actual mesh files and where they are stored
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
from backend.pymechanical.session_manager import ANSYSSessionManager
|
|
import logging
|
|
|
|
# Configure logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(levelname)s:%(name)s:%(message)s'
|
|
)
|
|
|
|
def test_mesh_file_generation():
|
|
"""Test if ANSYS generates actual mesh files"""
|
|
print("Testing Mesh File Generation")
|
|
print("=" * 50)
|
|
|
|
session_manager = None
|
|
try:
|
|
# Initialize ANSYS session
|
|
session_manager = ANSYSSessionManager(simulation_mode=False)
|
|
session_manager.start_session()
|
|
print("✓ ANSYS session started")
|
|
|
|
# Get project directory
|
|
project_dir_script = '''
|
|
project_dir = ExtAPI.DataModel.Project.ProjectDirectory
|
|
print("Project directory: " + str(project_dir))
|
|
project_dir
|
|
'''
|
|
|
|
project_dir = session_manager.mechanical.run_python_script(project_dir_script)
|
|
print(f"ANSYS Project Directory: {project_dir}")
|
|
|
|
# Import geometry
|
|
geometry_file = "resource\\blade.step"
|
|
print(f"\n1. Importing geometry from {geometry_file}...")
|
|
if session_manager.import_geometry(geometry_file):
|
|
print("✓ Geometry imported successfully")
|
|
else:
|
|
print("✗ Geometry import failed")
|
|
return
|
|
|
|
# Apply basic mesh settings
|
|
print("\n2. Setting up mesh...")
|
|
mesh_setup_script = '''
|
|
mesh = Model.Mesh
|
|
mesh.ElementSize = Quantity("5.0 [mm]")
|
|
"mesh_setup_complete"
|
|
'''
|
|
|
|
setup_result = session_manager.mechanical.run_python_script(mesh_setup_script)
|
|
print(f"Mesh setup: {setup_result}")
|
|
|
|
# Generate mesh
|
|
print("\n3. Generating mesh...")
|
|
mesh_gen_script = '''
|
|
mesh = Model.Mesh
|
|
mesh.GenerateMesh()
|
|
"mesh_generated"
|
|
'''
|
|
|
|
gen_result = session_manager.mechanical.run_python_script(mesh_gen_script)
|
|
print(f"Mesh generation: {gen_result}")
|
|
|
|
# List files in project directory
|
|
print("\n4. Checking files in project directory...")
|
|
list_files_script = '''
|
|
import os
|
|
project_dir = ExtAPI.DataModel.Project.ProjectDirectory
|
|
try:
|
|
files = os.listdir(project_dir)
|
|
file_list = []
|
|
for file in files:
|
|
file_path = os.path.join(project_dir, file)
|
|
if os.path.isfile(file_path):
|
|
size = os.path.getsize(file_path)
|
|
file_list.append(f"{file} ({size} bytes)")
|
|
|
|
print("Files in project directory:")
|
|
for file_info in file_list:
|
|
print(" - " + file_info)
|
|
|
|
"files_listed"
|
|
except Exception as e:
|
|
print("Error listing files: " + str(e))
|
|
"error_listing_files"
|
|
'''
|
|
|
|
files_result = session_manager.mechanical.run_python_script(list_files_script)
|
|
print(f"Files listing result: {files_result}")
|
|
|
|
# Try to save the project
|
|
print("\n5. Saving project...")
|
|
save_project_script = '''
|
|
try:
|
|
project_dir = ExtAPI.DataModel.Project.ProjectDirectory
|
|
save_path = os.path.join(project_dir, "blade_mesh_test.mechdb")
|
|
ExtAPI.DataModel.Project.SaveAs(save_path)
|
|
|
|
# Check if file was created
|
|
if os.path.exists(save_path):
|
|
file_size = os.path.getsize(save_path)
|
|
print("Project saved successfully: " + save_path)
|
|
print("File size: " + str(file_size) + " bytes")
|
|
"project_saved:" + save_path + ":" + str(file_size)
|
|
else:
|
|
print("Project save failed - file not found")
|
|
"save_failed"
|
|
|
|
except Exception as e:
|
|
print("Error saving project: " + str(e))
|
|
"save_error:" + str(e)
|
|
'''
|
|
|
|
save_result = session_manager.mechanical.run_python_script(save_project_script)
|
|
print(f"Save result: {save_result}")
|
|
|
|
# Try to export mesh data
|
|
print("\n6. Attempting to export mesh data...")
|
|
export_mesh_script = '''
|
|
try:
|
|
mesh = Model.Mesh
|
|
project_dir = ExtAPI.DataModel.Project.ProjectDirectory
|
|
|
|
# Try to export mesh in different formats
|
|
export_results = []
|
|
|
|
# Method 1: Try to export as input file
|
|
try:
|
|
input_file_path = os.path.join(project_dir, "blade_mesh.inp")
|
|
# This might not work in all ANSYS versions
|
|
# mesh.ExportFormat = MeshExportFormat.ANSYS
|
|
# mesh.ExportToFile(input_file_path)
|
|
export_results.append("Input file export: Not available in this version")
|
|
except Exception as e:
|
|
export_results.append("Input file export error: " + str(e))
|
|
|
|
# Method 2: Check if mesh data exists
|
|
try:
|
|
elements = mesh.Elements
|
|
nodes = mesh.Nodes
|
|
element_count = len(elements) if elements else 0
|
|
node_count = len(nodes) if nodes else 0
|
|
export_results.append(f"Mesh data available: {element_count} elements, {node_count} nodes")
|
|
except Exception as e:
|
|
export_results.append("Mesh data access error: " + str(e))
|
|
|
|
for result in export_results:
|
|
print(result)
|
|
|
|
"export_attempted"
|
|
|
|
except Exception as e:
|
|
print("Export error: " + str(e))
|
|
"export_failed:" + str(e)
|
|
'''
|
|
|
|
export_result = session_manager.mechanical.run_python_script(export_mesh_script)
|
|
print(f"Export result: {export_result}")
|
|
|
|
# Check what files exist in the project directory from Python side
|
|
print("\n7. Checking project directory from Python...")
|
|
if project_dir and project_dir != "":
|
|
try:
|
|
import os
|
|
if os.path.exists(project_dir):
|
|
files = os.listdir(project_dir)
|
|
print(f"Files found in {project_dir}:")
|
|
for file in files:
|
|
file_path = os.path.join(project_dir, file)
|
|
if os.path.isfile(file_path):
|
|
size = os.path.getsize(file_path)
|
|
print(f" - {file} ({size:,} bytes)")
|
|
else:
|
|
print(f"Project directory does not exist: {project_dir}")
|
|
except Exception as e:
|
|
print(f"Error accessing project directory: {str(e)}")
|
|
else:
|
|
print("No project directory information available")
|
|
|
|
print("\n✓ Mesh file generation test completed")
|
|
|
|
except Exception as e:
|
|
print(f"\n✗ Mesh file test failed: {str(e)}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
finally:
|
|
if session_manager:
|
|
session_manager.close_session()
|
|
|
|
if __name__ == "__main__":
|
|
test_mesh_file_generation() |