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
-
- You must be logged in to reply to this topic.

Boost Ansys Fluent Simulations with AWS
Computational Fluid Dynamics (CFD) helps engineers design products in which the flow of fluid components is a significant challenge. These different use cases often require large complex models to solve on a traditional workstation. Click here to join this event to learn how to leverage Ansys Fluids on the cloud, thanks to Ansys Gateway powered by AWS.

Earth Rescue – An Ansys Online Series
The climate crisis is here. But so is the human ingenuity to fight it. Earth Rescue reveals what visionary companies are doing today to engineer radical new ideas in the fight against climate change. Click here to watch the first episode.

Ansys Blog
Subscribe to the Ansys Blog to get great new content about the power of simulation delivered right to your email on a weekly basis. With content from Ansys experts, partners and customers you will learn about product development advances, thought leadership and trends and tips to better use Ansys tools. Sign up here.
- Solver Pivot Warning in Beam Element Model
- Saving & sharing of Working project files in .wbpz format
- Understanding Force Convergence Solution Output
- User manual
- An Unknown error occurred during solution. Check the Solver Output…..
- What is the difference between bonded contact region and fixed joint
- The solver engine was unable to converge on a solution for the nonlinear problem as constrained.
- whether have the difference between using contact and target bodies
- Defining rigid body and contact
- Colors and Mesh Display
-
7584
-
4434
-
2951
-
1422
-
1322
© 2023 Copyright ANSYS, Inc. All rights reserved.