TAGGED: apdl, python-apdl
-
-
May 29, 2023 at 9:15 pm
Anirudh Thantry
SubscriberI have created a parameterized wing using Design Modeler. And I have created the setup for modal analysis for several wing configurations and now I want to export the Nastran input files for all these configurations (without manually exporting using GUI). Is there a possible way to automate this exporting using APDL commands or Python scripting?
-
May 30, 2023 at 8:50 pm
mjmiddle
Ansys EmployeeWithin Mechcanical you can do:
analysis = ExtAPI.DataModel.Project.Model.Analyses[0] # 1st analysis
from Ansys.ACT.Automation.Mechanical import NastranExportOptions
nas = NastranExportOptions()
nas.NastranFilename = r"D:\Myfile.nas"
analysis.ExportNastranFile(nas)If you have multiple analyses in the same Mechanical session, then use a python loop instead of using the first index.
If you have multiple unlinked systems in the workbench project schematic, you’ll need to loop through them and send commands to Mechanical:
loop = 1
for system in GetAllSystems():
cmdMech = '''
analysis = ExtAPI.DataModel.Project.Model.Analyses[0] # 1st analysis
from Ansys.ACT.Automation.Mechanical import NastranExportOptions
nas = NastranExportOptions()
nas.NastranFilename = r"D:\\Myfile''' + str(loop) + '''.nas"
analysis.ExportNastranFile(nas)
'''
setup = system.GetContainer(ComponentName="Setup")
setup.Edit(Interactive=False) # set Interactive=True for debugging
setup.SendCommand(Language="Python", Command=cmdMech)
setup.Exit()
loop += 1 -
May 31, 2023 at 9:50 pm
mjmiddle
Ansys EmployeeUnfortunately, I didn’t catch the word “parameterized” in the original post, so I didn’t think you were using design points. It gets a lot more complicated to have something run automatically with that. One way is to insert code into a “python code” object in the Outline and send calls up to workbench level to get the design point number. Witihin Mechanical, such as in “Python Code” object:
cmd = ”'
dp = Parameters.GetActiveDesignPoint()
dpNum = dp.Name
”'
ret_dict = ExtAPI.Application.ScriptByName(‘journaling’).ExecuteCommand(cmd, None, ‘dpNum’)
dpNum = ret_dict[‘dpNum’]You can append this number (as a string) to the filename.
Turn on "Tools > Options > Mechanical > Connect/Run Python Code Objects when Mechanical is Launched" from workbench project schematic.
-
May 31, 2023 at 10:32 pm
mjmiddle
Ansys EmployeeAnother way is to send commands from workbench that work with the native design point update process. You need to use an ACT extension. The XML can look like this:
And the main.py could look like this:
setup_handled = False
def write_my_data(task):
global setup_handled
if not task.Name == “Setup”: return
if setup_handled == True: return # callback is done many times for each task. Need to block all but one attempt
dir = GetUserFilesDirectory()
dir = dir.replace(“\\”,”/”)
cmdprefix = ”’ExtAPI.ExtensionManager.GetExtensionByName(“My Extension”).ScriptScope.”'
# when this function is called automatically during DP updates, through workflow callback, Mechanical will not have an extension loaded.
dp = Parameters.GetActiveDesignPoint()
for system in GetAllSystems():
comp = sys.GetComponent(Name=’Setup’)
cmdMech = cmdprefix + “write_nastran(\”” + dir + “\”,” + dp.Name + “,\”” + comp.DirectoryName + “\”)”
setup = system.GetContainer(ComponentName=”Setup”)
setup.Edit(Interactive=False)
setup.SendCommand(Language=”Python”, Command=cmdMech)
setup.Exit()
setup_handled = Truedef dp_after_change():
global setup_handled
setup_handled = Falsedef write_nastran(dir, dp, solDir):
# executed within Mechanical
import os
for analysis in ExtAPI.DataModel.Project.Model.Analyses:
path = analysis.WorkingDir # solution directory
d = os.path.dirname(path)
d2 = os.path.dirname(d) # need to do dirname() twice since the path ends in a backslash
sysDir = os.path.basename(d2)
if solDir == sysDir:
from Ansys.ACT.Automation.Mechanical import NastranExportOptions
nas = NastranExportOptions()
nas.NastranFilename = dir + “/” + “dp” + str(dp) + “_” + solDir + “.txt”
analysis.ExportNastranFile(nas)
break
-
- The topic ‘Export Nastran input file’ is closed to new replies.
-
6279
-
1906
-
1457
-
1308
-
1022
© 2026 Copyright ANSYS, Inc. All rights reserved.