How to remove command snippet ?

Jean
Jean Member Posts: 25
10 Comments 5 Likes First Anniversary Name Dropper
**

Hi,

I am trying to adapt a script presented by @Pernelle Marone-Hitz : https://developer.ansys.com/blog/script-tip-friday-efficiently-combining-mapdl-and-mechanical-scripting?check_logged_in=1

Before command snippet are created which I give the name "command_snippet_joint_ID", I would like to check if a command snippet already exist with this name. If yes, I would like to delete the command and then add the new one. If no (or if a command snippet with another name exist), just add the new one.

Is it possible to do this king of check ?

Regards,
Jean

Best Answer

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 316
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓
    for existing in ExtAPI.DataModel.GetObjectsByName('delete_me') and ExtAPI.DataModel.GetObjectsByType(Ansys.ACT.Automation.Mechanical.CommandSnippet):
        existing.Delete()
    

Answers

  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 357
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭

    You can do something like this.

    In general I would recommend an easier solution to use the property "Element APDL Name" for the joints. Here you can set a custom variable name for the joints. You can automate that this property be set to "Joint_" so that it is unique and traceable back to the mechanical object. This bypasses the command snippet all together and keeps things easier if all you want is to store Ids for post.

    You lastly could implement python code objects in the post section and use the Solution.SolverData to dynamically get the Ids after the solution is run, bypassing any pre-processing required.

    SnipName = 'command_snippet_joint_ID'
    def GetSnippets(Joint, Name):
    Snippets = Joint.GetChildren(DataModelObjectCategory.CommandSnippet, True)
    Snippets=filter(lambda Obj: Obj.Name==Name, Snippets)
    return Snippets
    for Joint in ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.Joint):
    Snippets = GetSnippets(MyJoint, SnipName)
    print Joint.Name+": # of Cmd Snippets: "+str(len(Snippets))

  • Jean
    Jean Member Posts: 25
    10 Comments 5 Likes First Anniversary Name Dropper
    **

    Thanks for your answers!