How to delete a custom object by script?

Chemsdine CHEMAI
Chemsdine CHEMAI Member, Employee Posts: 201
First Anniversary Ansys Employee Solution Developer Community of Practice Member 5 Likes
edited September 2023 in Structures

How to delete a custom object by script?

Tagged:

Best Answer

  • Chemsdine CHEMAI
    Chemsdine CHEMAI Member, Employee Posts: 201
    First Anniversary Ansys Employee Solution Developer Community of Practice Member 5 Likes
    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
    First Comment 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