How to use PythonCode object in Mechanical? (2021R2)

Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 459
100 Answers 250 Likes 100 Comments Second Anniversary
✭✭✭✭
edited June 2023 in Structures

How to use PythonCode object in Mechanical? (2021R2)

enter image description here

Tagged:

Answers

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 459
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    Answer ✓

    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:

    enter image description here

    enter image description here

    enter image description here

    Right-click the python object and "Reload properties"

    And then you can access it in the "Script" window editor:

    enter image description here

    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!

  • Sascha Hell
    Sascha Hell Member, Employee Posts: 11
    First Anniversary 5 Likes Ansys Employee Solution Developer Community of Practice Member
    ✭✭✭
    Answer ✓

    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:

    1. 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)

    2. Scroll to the bottom of the template to find the definition of function add_props()

    3. Uncomment a few lines as indicated in the picture posted by Ayush:

      1. The property lines in the function add_props that you think you will need (e.g. all of them).

      2. 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:

    1. RMB Python Result object > Connect

    2. RMB Python Result object > Reload Properties --> then you already see your Property in the Details

    3. To evaluate the PythonResult object: RMB Python Result object > Evaluate All Results

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 107
    25 Answers Second Anniversary 10 Comments 25 Likes
    ✭✭✭✭

    I believe this will be a very useful feature in the future. Please send me feeback as you use it!

  • Rohith Patchigolla
    Rohith Patchigolla Member, Moderator, Employee Posts: 199
    100 Comments 25 Answers Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    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()

    enter image description here

    we just need to comment "return" to activate adding properties

    enter image description here

    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.

    enter image description here

    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

    enter image description here

    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
    
    

    enter image description here