How to use PythonCode object in Mechanical? (2021R2)
Answers
-
Mechanical 2021R2 has Python-Code object to execute ACT callbacks directly within Mechanical, without the need of creating an extension. If added under "Solution" the code will be executed after Solve. You can use Mechanical ExtAPI commands in the object and also create custom properties.
To create a custom property, you can add properties to an already provided function template, in the "Property Provider" window:
Right-click the python object and "Reload properties"
And then you can access it in the "Script" window editor:
prop_val = this.PropertyProvider.GetPropertyByName("MyProperty").Value geom_sel_val = this.PropertyProvider.GetPropertyByName("Scoping Property/Geometry Selection").Value.Ids[0] ExtAPI.Log.WriteMessage("The value of MyProperty is %s" % prop_val) ExtAPI.Log.WriteMessage("Scoped Geometry %s" % geom_sel_val) mesh = ExtAPI.DataModel.AnalysisList[0].MeshData ec = mesh.ElementCount ExtAPI.Log.WriteMessage("Element count %s" % ec)
At the end, (Re)Solve!
2 -
Just to add to Ayush: What Ayush mentioned is not only true for the PythonCode object but also true for the PythonResult object, where you can e.g. insert a DPF evaluation tailored to your needs.
If you have access to the dev version of the documentation, you can find the PropertyProvider documented at
https://ansysproducthelpqa.win.ansys.com/account/secured?returnurl=/Views/Secured/corp/v212/en/wb_sim/ds_python_code_Property_Provider.html
Just note that this documentation is at the time of this post still beta and subject to potentially substantial changes.Also adding a quick step by step guide to add properties to your PythonCode or PythonResult object:
Click the property provider tab where you can already find a template
(further examples can be found at {YOUR_INSTALL_DIR}/aisol/DesignSpace/DSPages/Python/mech_templates)Scroll to the bottom of the template to find the definition of function add_props()
Uncomment a few lines as indicated in the picture posted by Ayush:
The property lines in the function add_props that you think you will need (e.g. all of them).
Also uncomment line "this.PropertyProvider = None" (directly below the add_props() function)
The code in the PythonCode object is executed after a solve/resolve.
The execution is different for the PythonResult object:
RMB Python Result object > Connect
RMB Python Result object > Reload Properties --> then you already see your Property in the Details
To evaluate the PythonResult object: RMB Python Result object > Evaluate All Results
0 -
I believe this will be a very useful feature in the future. Please send me feeback as you use it!
2 -
@Pierre Thieffry Indeed, excited about the further development of this feature, looks very promising!
6 -
Please note that there have been some changes in the Property Provider structure in the newer build of v2021R2. The function template to add custom properties has been moved to the top and is modified. The new function name is reload_props()
we just need to comment "return" to activate adding properties
Right-click the python object and "Reload properties". The function adds by default different types of properties (double, expression, geometry scoping and options). One can comment these properties if not needed in the function in the Property Provider.
The API to access the custom property defined is, this.GetCustomPropertyByPath("your_property_group_name/your_property_name").Value
instead of the old version, this.PropertyProvider.GetPropertyByName("your_property_group_name/your_property_name").Value
Example script to parametrize all the bolt pretension loads in the tree.
""" Called before solving the parent analysis. Keyword Arguments : this -- the datamodel object instance of the python code object you are currently editing in the tree analysis -- Static Structural """ # To access properties created using the Property Provider, please use the following command. # this.GetCustomPropertyByPath("your_property_group_name/your_property_name") # To access scoping properties use the following to access geometry scoping and named selection respectively: # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Geometry Selection") # this.GetCustomPropertyByPath("your_property_group_name/your_property_name/Named Selection") preload = this.GetCustomPropertyByPath("Parameters/Preload (N)").Value all_prets = DataModel.GetObjectsByName('Bolt Pretension') for prets in all_prets: times = range(1,prets.Parent.AnalysisSettings.NumberOfSteps+1) preload_vals=[] time_vals=[] for time in times: time_vals.append(Quantity(float(time), "sec")) preload_vals.append(Quantity(float(preload), "N")) prets.Preload.Inputs[0].DiscreteValues = time_vals prets.Preload.Output.DiscreteValues=preload_vals
2