How to delete a custom object by script?

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

How to delete a custom object by script?

Tagged:

Best Answer

  • Chemsdine CHEMAI
    Chemsdine CHEMAI Member, Employee Posts: 201
    100 Likes 100 Comments First 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)
    

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 Posts: 291
    25 Answers 100 Comments 25 Likes First Anniversary
    ✭✭✭✭
    def DeleteObject(obj):
        try: ID = obj.ObjectId
        except: ID = obj.InternalObject.ID
        JScmds = 'DS.Tree.DeleteObject({})'.format(ID)
        ExtAPI.Application.ScriptByName("jscript").ExecuteCommand(JScmds)