How to delete a custom object by script?

Chemsdine CHEMAI
Chemsdine CHEMAI Member, Employee Posts: 201
100 Likes 100 Comments Second Anniversary Ansys Employee
✭✭✭✭
edited September 2023 in Structures

How to delete a custom object by script?

Tagged:

Best Answers

  • Chemsdine CHEMAI
    Chemsdine CHEMAI Member, Employee Posts: 201
    100 Likes 100 Comments Second Anniversary Ansys Employee
    ✭✭✭✭
    Answer ✓

    By using the customization API to retrieve the object, i.e :

    currentobj=ExtAPI.DataModel.GetUserObjects(ExtAPI.ExtensionManager.CurrentExtension)[0]
    

    One will not see any Delete() function accessible.

    The trick is to use the Automation API, for example :

    automcurrentobj=ExtAPI.DataModel.GetObjectById(currentobj.ObjectId) automcurrentobj.Delete()

    from the 2021R1, you can use instead of "automcurrentobj.Delete()" :

    DS = ExtAPI.DataModel.InternalObject["ds"]
    DS.Tree.DeleteObject(automcurrentobj.ObjectId)
    
  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 319
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    @Jean Use the DeleteObject function that I posted previously.

    Alternatively, add parentheses to DS.Tree for 24.2:
    DS.Tree().DeleteObject(automcurrentobj.ObjectId)

Answers

  • dafedin
    dafedin Member Posts: 21
    10 Comments First Anniversary Name Dropper
    **

    Hello Chemsdine,

    The code works:

    currentobj=ExtAPI.DataModel.GetUserObjects(ExtAPI.ExtensionManager.Extensions[0])[0]
    automcurrentobj=ExtAPI.DataModel.GetObjectById(currentobj.ObjectId)
    DS = ExtAPI.DataModel.InternalObject["ds"]
    DS.Tree.DeleteObject(automcurrentobj.ObjectId)
    

    Here, the current extension is selected by its index in the list of loaded extensions. Then, the target user object is selected and deleted. The only thing that causes doubt is the use of InternalObject. As I know it is recommended only as a last resort.

    Thanks

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 319
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    def DeleteObject(obj):
        try: ID = obj.ObjectId
        except: ID = obj.InternalObject.ID
        JScmds = 'DS.Tree.DeleteObject({})'.format(ID)
        ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(JScmds)
    
  • Jean
    Jean Member Posts: 25
    10 Comments 5 Likes First Anniversary Name Dropper
    **

    @dafedin said:
    Hello Chemsdine,

    The code works:

    currentobj=ExtAPI.DataModel.GetUserObjects(ExtAPI.ExtensionManager.Extensions[0])[0]
    automcurrentobj=ExtAPI.DataModel.GetObjectById(currentobj.ObjectId)
    DS = ExtAPI.DataModel.InternalObject["ds"]
    DS.Tree.DeleteObject(automcurrentobj.ObjectId)
    

    Here, the current extension is selected by its index in the list of loaded extensions. Then, the target user object is selected and deleted. The only thing that causes doubt is the use of InternalObject. As I know it is recommended only as a last resort.

    Thanks

    Hi,

    This code works perfectly in Ansys 22R2, but in Ansys 24R2, there is an error :
    'DispCallable' object has no attribute 'DeleteObject'

    Is there a new way to do that ?

    Regards,
    Jean