Skip to content
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

Potential workflow : pymechanical + pydynamicreporting #23

Open
dipinknair opened this issue Jul 18, 2024 · 1 comment
Open

Potential workflow : pymechanical + pydynamicreporting #23

dipinknair opened this issue Jul 18, 2024 · 1 comment

Comments

@dipinknair
Copy link
Contributor

dipinknair commented Jul 18, 2024

for more information ref

@dipinknair
Copy link
Contributor Author

##Prerequisites
##User Inputs

print("User Inputs")
mech_db_file_path = r"D:\No_Backup\embedded_mech_to_ADR\test_case1.mechdat"
working_dir = r"D:\Temp"
print(" Section Completed")

##Import Python Modules

print("Import Python Modules")
import ansys.dynamicreporting.core as adr
from ansys.mechanical.core import App
from ansys.mechanical.core import global_variables
from ansys.mechanical.core.embedding import add_mechanical_python_libraries
import os
import shutil
from datetime import datetime
print(" Section Completed")

##Define constants and create subfolders

print("Define constants and create subfolders")
version = 241
ansys_ins = os.getenv('AWP_ROOT'+str(version))
adr_db_directory = os.path.join(working_dir,"Nexus")
export_directory = os.path.join(working_dir,"Exports")

if os.path.exists(adr_db_directory):
try: shutil.rmtree(adr_db_directory)
except: print(f"Cannot delete adr_db directory: {adr_db_directory}")

try: os.mkdir(adr_db_directory)
except: print(f"Cannot mkdir adr_db directory: {adr_db_directory}")
else: print(f" Directory created: {adr_db_directory}")

if os.path.exists(export_directory):
try:shutil.rmtree(export_directory)
except: print(f"Cannot delete export directory: {export_directory}")

try: os.mkdir(export_directory)
except: print(f"Cannot mkdir export directory: {export_directory}")
else: print(f" Directory created: {export_directory}")

print(" Section Completed")

##Start ADR service

print("Start ADR service")
adr_service = adr.Service(ansys_installation=ansys_ins, db_directory=adr_db_directory)

try: session_guid = adr_service.start(create_db=True)
except:
print("ADR server not started properly. Please ensure that the 'Nexus' folder in your working directory is empty.")
print("If an old Nexus server is still running, use Task Manager to end any nginx.exe processes.")
print("Or create a new, empty working directory.")

server=adr_service.serverobj
print(adr_service.url)
print(" Section Completed")

##Launch Mechanical and import Mechanical scripting variables

print("Launch Mechanical and import Mechanical scripting variables")
app = App(version=version)
globals().update(global_variables(app))
add_mechanical_python_libraries(version)
print(f"Reading Mechanical Database: {mech_db_file_path}..")
app.open(mech_db_file_path)
print(f"Mechanical details:\n{app}")
print(" Section Completed")

##Define Mechanical helper functions

print("Define Mechanical helper functions")
def ExportAVZ(obj,export_directory):
obj.Activate()

export_directory = wbjn.ExecuteCommand(ExtAPI,'returnValue(GetUserFilesDirectory())')

filename = os.path.join(export_directory,'{}.avz'.format(obj.Name))
ExtAPI.Graphics.Export3D(filename)
return filename

def ExportPNG(obj,export_directory):
obj.Activate()
filename = os.path.join(export_directory,'{}.png'.format(obj.Name))
image_export_format = Ansys.Mechanical.DataModel.Enums.GraphicsImageExportFormat.PNG
settings_720p = Ansys.Mechanical.Graphics.GraphicsImageExportSettings()
ExtAPI.Graphics.Camera.SetFit()
ExtAPI.Graphics.ExportImage(filename, image_export_format, settings_720p)
return filename

print(" Mechanical helper functions created.")

##Define ADR helper functions

print("Define ADR helper functions")
def pushTree(tree,adr_service,tags=''):
item = adr_service.create_item()
item.item_tree = tree
item.set_tags(tags)

def PushAVZ(avz,adr_service,tags='',name='Scene_from_AVZ'):
a = adr_service.create_item()
a.item_scene = avz
a.add_tag(tags)

def PushPNG(file,adr_service,tags='',name='Image_from_PNG'):
item = adr_service.create_item(obj_name = name)
item.item_image = file
item.add_tag(tags)

def ExportPDF(adr_service,report,filename,version=241):
ansys_ins = os.getenv('AWP_ROOT'+str(version))
cei_directory= os.path.join(ansys_ins,'CEI')
adr_service.serverobj.export_report_as_pdf(report_guid=report.report.guid, file_name=filename, exec_basis=cei_directory, ansys_version=version)

print("ADR helper functions created.")

##Create Report Items and push them to ADR

print("Create Report Items and push them to ADR")
geometry = ExtAPI.DataModel.Project.Model.Geometry
geom_avz = ExportAVZ(geometry,export_directory)
PushAVZ(geom_avz,adr_service,'Geom')
geom_png = ExportPNG(geometry,export_directory)
PushPNG(geom_png,adr_service,'Geom Static')

mesh = ExtAPI.DataModel.Project.Model.Mesh
mesh_avz = ExportAVZ(mesh,export_directory)
PushAVZ(mesh_avz,adr_service,'Mesh')
mesh_png = ExportPNG(mesh,export_directory)
PushPNG(mesh_png,adr_service,'Mesh Static')

results =ExtAPI.DataModel.GetObjectsByType(Ansys.Mechanical.DataModel.Enums.DataModelObjectCategory.Result)
for result in results:
avz = ExportAVZ(result,export_directory)
PushAVZ(avz,adr_service,'Result '+result.Name)
png = ExportPNG(result,export_directory)
PushPNG(png,adr_service,'Result Static'+result.Name)

print(" Section Completed")

adr_service.visualize_report()

print("Create tree items and push them to ADR")
partList = []
for part in ExtAPI.DataModel.GeoData.Assemblies[0].Parts:
bodyList = []
for body in part.Bodies:
if body.Suppressed:
continue
mat = body.Material
bodyList.append(dict(key='child', name=body.Name, value=mat.DisplayName, state="collapsed"))
partList.append(dict(key='child', name='Part: '+part.Name, value=None, children=bodyList,state="collapsed"))

MaterialTree = [(dict(key='root', name='Material Data', value=None, children=partList,state="collapsed"))]
pushTree(MaterialTree,adr_service,tags='MaterialProperties')

timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
RunDetails = [['CAD File',geometry.Source],['Report Time',timestamp]]
RunDetailsDict = [dict(key='child', name=x, value=y) for x, y in RunDetails]
RunDetailsTree = [dict(key='root', name='FEA Details', children=RunDetailsDict, value=None)]
pushTree(RunDetailsTree,adr_service,tags='RunDetails')
print(" Section Completed")

adr_service.visualize_report()

print(adr_service.url)

##Create the report templates

print("Create the dynamic report template")
template_00=server.create_template(name="Mechanical Basic Report", parent=None, report_type="Layout:basic")
server.put_objects(template_00)

template_13=server.create_template(name="Logo", parent=template_00, report_type="Layout:basic")
template_13.set_filter("A|i_type|cont|image;A|i_tags|cont|img=AnsysLogo;")
server.put_objects(template_13)
server.put_objects(template_00)

template_02=server.create_template(name="TOC", parent=template_00, report_type="Layout:toc")
template_02.params='{"TOCitems": 1, "properties": {"TOCItem": "0"}, "HTML": "

Table of Contents
"}'
template_02.set_filter("A|i_name|eq|NonexistantName;")
server.put_objects(template_02)
server.put_objects(template_00)
template_geom=server.create_template(name="Geom", parent=template_00, report_type="Layout:panel")
template_geom.params='{"HTML": "

Geometry
", "properties": {"TOCLevel": "0", "TOCItem": "1"}}'
template_geom.set_filter("A|i_tags|cont|Geom;")
server.put_objects(template_geom)
server.put_objects(template_00)
template_geom_scene =server.create_template(name="3Dscene", parent=template_geom, report_type="Layout:basic")
template_geom_scene.params='{"properties": {"TOCItem": "0"}}'
template_geom_scene.set_filter("A|i_type|cont|scene;")
server.put_objects(template_geom_scene)
server.put_objects(template_geom)
server.put_objects(template_00)

template_03=server.create_template(name="Mesh", parent=template_00, report_type="Layout:panel")
template_03.params='{"HTML": "

Mesh
", "properties": {"TOCLevel": "0", "TOCItem": "1"}}'
template_03.set_filter("A|i_tags|cont|Mesh;")
server.put_objects(template_03)
server.put_objects(template_00)
template_04=server.create_template(name="3Dscene", parent=template_03, report_type="Layout:basic")
template_04.params='{"properties": {"TOCItem": "0"}}'
template_04.set_filter("A|i_type|cont|scene;")
server.put_objects(template_04)
server.put_objects(template_03)
server.put_objects(template_00)

template_05=server.create_template(name="FEA Details", parent=template_00, report_type="Layout:panel")
template_05.params='{"HTML": "

FEA Details
\nDetails of the model", "properties": {"TOCItem": "1"}}'
template_05.set_filter("A|i_type|cont|tree;A|i_tags|cont|RunDetails;")
server.put_objects(template_05)
server.put_objects(template_00)
template_06=server.create_template(name="Materials", parent=template_00, report_type="Layout:panel")
template_06.params='{"HTML": "

Materials
", "properties": {"TOCItem": "1"}}'
template_06.set_filter("A|i_type|cont|tree;A|i_tags|cont|MaterialProperties;")
server.put_objects(template_06)
server.put_objects(template_00)
template_10=server.create_template(name="Static Structural", parent=template_00, report_type="Layout:panel")
template_10.params='{"HTML": "

Static Structural
", "properties": {"TOCItem": "1"}}'
template_10.set_filter("A|i_tags|cont|Result;")
server.put_objects(template_10)
server.put_objects(template_00)
template_14=server.create_template(name="Total Deformation", parent=template_10, report_type="Layout:basic")
template_14.params='{"HTML": "

Total Deformation
", "properties": {"TOCLevel": "1"}}'
template_14.set_filter("A|i_tags|cont|Deformation;")
server.put_objects(template_14)
server.put_objects(template_10)
server.put_objects(template_00)
template_07=server.create_template(name="3Dscene", parent=template_14, report_type="Layout:basic")
template_07.params='{"properties": {"TOCItem": "0"}}'
template_07.set_filter("A|i_type|cont|scene;")
server.put_objects(template_07)
server.put_objects(template_14)
server.put_objects(template_10)
server.put_objects(template_00)

template_08=server.create_template(name="table", parent=template_14, report_type="Layout:basic")
template_08.params='{"properties": {"TOCItem": "0"}}'
template_08.set_filter("A|i_type|cont|table;")
server.put_objects(template_08)
server.put_objects(template_14)
server.put_objects(template_10)
server.put_objects(template_00)

template_11=server.create_template(name="Equivalent Stress", parent=template_10, report_type="Layout:basic")
template_11.params='{"HTML": "

Equivalent Stress
", "properties": {"TOCLevel": "1"}}'
template_11.set_filter("A|i_tags|cont|Stress;")
server.put_objects(template_11)
server.put_objects(template_10)
server.put_objects(template_00)
template_12=server.create_template(name="3Dscene", parent=template_11, report_type="Layout:basic")
template_12.params='{"properties": {"TOCItem": "0"}}'
template_12.set_filter("A|i_type|cont|scene;")
server.put_objects(template_12)
server.put_objects(template_11)
server.put_objects(template_10)
server.put_objects(template_00)

template_09=server.create_template(name="table", parent=template_11, report_type="Layout:basic")
template_09.params='{"properties": {"TOCItem": "0"}}'
template_09.set_filter("A|i_type|cont|table;")
server.put_objects(template_09)
server.put_objects(template_11)
server.put_objects(template_10)
server.put_objects(template_00)
print(f" Template Created: {template_00}")

print("Create the static report template")
template_00=server.create_template(name="Mechanical Basic Report (Static)", parent=None, report_type="Layout:basic")
server.put_objects(template_00)

template_13=server.create_template(name="Logo", parent=template_00, report_type="Layout:basic")
template_13.set_filter("A|i_type|cont|image;A|i_tags|cont|img=AnsysLogo;")
server.put_objects(template_13)
server.put_objects(template_00)

template_02=server.create_template(name="TOC", parent=template_00, report_type="Layout:toc")
template_02.params='{"TOCitems": 1, "properties": {"TOCItem": "0"}, "HTML": "

Table of Contents
"}'
template_02.set_filter("A|i_name|eq|NonexistantName;")
server.put_objects(template_02)
server.put_objects(template_00)
template_geom=server.create_template(name="Geom", parent=template_00, report_type="Layout:panel")
template_geom.params='{"HTML": "

Geometry
", "properties": {"TOCLevel": "0", "TOCItem": "1"}}'
template_geom.set_filter("A|i_tags|cont|Geom;")
server.put_objects(template_geom)
server.put_objects(template_00)
template_geom_scene =server.create_template(name="Static Image", parent=template_geom, report_type="Layout:basic")
template_geom_scene.params='{"properties": {"TOCItem": "0"}}'
template_geom_scene.set_filter("A|i_type|ncont|scene;")
server.put_objects(template_geom_scene)
server.put_objects(template_geom)
server.put_objects(template_00)

template_03=server.create_template(name="Mesh", parent=template_00, report_type="Layout:panel")
template_03.params='{"HTML": "

Mesh
", "properties": {"TOCLevel": "0", "TOCItem": "1"}}'
template_03.set_filter("A|i_tags|cont|Mesh;")
server.put_objects(template_03)
server.put_objects(template_00)
template_04=server.create_template(name="Static Image", parent=template_03, report_type="Layout:basic")
template_04.params='{"properties": {"TOCItem": "0"}}'
template_04.set_filter("A|i_type|ncont|scene;")
server.put_objects(template_04)
server.put_objects(template_03)
server.put_objects(template_00)

template_05=server.create_template(name="FEA Details", parent=template_00, report_type="Layout:panel")
template_05.params='{"HTML": "

FEA Details
\nDetails of the model", "properties": {"TOCItem": "1"}}'
template_05.set_filter("A|i_type|cont|tree;A|i_tags|cont|RunDetails;")
server.put_objects(template_05)
server.put_objects(template_00)
template_06=server.create_template(name="Materials", parent=template_00, report_type="Layout:panel")
template_06.params='{"HTML": "

Materials
", "properties": {"TOCItem": "1"}}'
template_06.set_filter("A|i_type|cont|tree;A|i_tags|cont|MaterialProperties;")
server.put_objects(template_06)
server.put_objects(template_00)
template_10=server.create_template(name="Static Structural", parent=template_00, report_type="Layout:panel")
template_10.params='{"HTML": "

Static Structural
", "properties": {"TOCItem": "1"}}'
template_10.set_filter("A|i_tags|cont|Result;")
server.put_objects(template_10)
server.put_objects(template_00)
template_14=server.create_template(name="Total Deformation", parent=template_10, report_type="Layout:basic")
template_14.params='{"HTML": "

Total Deformation
", "properties": {"TOCLevel": "1"}}'
template_14.set_filter("A|i_tags|cont|Deformation;")
server.put_objects(template_14)
server.put_objects(template_10)
server.put_objects(template_00)
template_07=server.create_template(name="Static Image", parent=template_14, report_type="Layout:basic")
template_07.params='{"properties": {"TOCItem": "0"}}'
template_07.set_filter("A|i_type|ncont|scene;")
server.put_objects(template_07)
server.put_objects(template_14)
server.put_objects(template_10)
server.put_objects(template_00)

template_08=server.create_template(name="table", parent=template_14, report_type="Layout:basic")
template_08.params='{"properties": {"TOCItem": "0"}}'
template_08.set_filter("A|i_type|cont|table;")
server.put_objects(template_08)
server.put_objects(template_14)
server.put_objects(template_10)
server.put_objects(template_00)

template_11=server.create_template(name="Equivalent Stress", parent=template_10, report_type="Layout:basic")
template_11.params='{"HTML": "

Equivalent Stress
", "properties": {"TOCLevel": "1"}}'
template_11.set_filter("A|i_tags|cont|Stress;")
server.put_objects(template_11)
server.put_objects(template_10)
server.put_objects(template_00)
template_12=server.create_template(name="Static Image", parent=template_11, report_type="Layout:basic")
template_12.params='{"properties": {"TOCItem": "0"}}'
template_12.set_filter("A|i_type|ncont|scene;")
server.put_objects(template_12)
server.put_objects(template_11)
server.put_objects(template_10)
server.put_objects(template_00)

template_09=server.create_template(name="table", parent=template_11, report_type="Layout:basic")
template_09.params='{"properties": {"TOCItem": "0"}}'
template_09.set_filter("A|i_type|cont|table;")
server.put_objects(template_09)
server.put_objects(template_11)
server.put_objects(template_10)
server.put_objects(template_00)
print(f" Template Created: {template_00}")
print(" Section Completed")

##View the reports

print("View the reports")
adr_service.visualize_report('Mechanical Basic Report',new_tab = True)

static_report = adr_service.get_report(report_name='Mechanical Basic Report (Static)')
pdf_file = os.path.join(export_directory,'Report.pdf')
ExportPDF(adr_service,static_report,pdf_file)
print(" Section Completed")

##Clean up

print("Stop the Nexus server and close Mechanical")
adr_service.stop()
print(" Nexus server stopped.")
app.close()
print(" Mechanical closed.")

@RobPasMue RobPasMue changed the title Potential workflow : pyechanical + pydynamicreporting Potential workflow : pymechanical + pydynamicreporting Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant