Script for deleting selected workbench systems.

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭

Script for deleting selected workbench systems.

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    The below script works at the project page level.

    #Enter the systems to be deleted
    systems2Delete = ["C","D"]
    
    from collections import defaultdict
    systemsData = defaultdict(list)
    systems = GetAllSystems()
    for system in systems:
        component1 = system.Components[0]
        systemsData[ACT.InitializeAndGenerateTaskCoordinates(component1)[0]] = system
    #Sorting is required since after every delete systems get re-named
    #Therefore deleting the systems in reverse order
    sortedValues = systemsData.keys()
    sortedValues.sort()
    sortedValues.reverse()
    from collections import OrderedDict
    OrderedData  = OrderedDict()
    for value in sortedValues:
        OrderedData[value] = systemsData[value]
    
    for key in OrderedData:
        if key in systems2Delete:
            OrderedData[key].Delete()
    
  • lgospar
    lgospar Member Posts: 3
    First Comment
    **

    Is it possible to apply the same code based not on a systems2Delete list but based on the actual selected SYSs in Workbench?

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    I don't think we can retrieve selected SYS from the WB project page, but maybe someone from the @AKD-Scripting-Team knows how.

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

    I don't think there is a way to retrieve selected Sys from WB Project Page. I would suggest making a python list containing strings of names of all the systems, loop through all systems, and use an if condition based on Sys.Display text to filter the systems one would need to operate on.

  • M
    M Member, Employee Posts: 241
    50 Answers 100 Comments 100 Likes Second Anniversary
    ✭✭✭✭
    edited April 9

    You can GetAllSystems and then use the [n].Name to delete a system by name.

    systems = GetAllSystems()
    
    system1 = GetSystem(Name=systems[0].Name)
    system1.Delete()
    

    If you need to limit or filter, you can use the system.DisplayText() which is the name of the component in WB.

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee Posts: 299
    25 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    import System
    import clr
    clr.AddReference("Ans.UI")
    import Ansys.UI
    uiMgr = Ansys.UI.UIManager.Instance
    psView = uiMgr.GetActiveWorkspace().GetView("ProjectSchematic")
    d = psView.Control
    selectedCells = []
    for taskGroupBlock in d.Blocks:
        for taskBlock in taskGroupBlock.Cells:
            if taskBlock.Selected:
                selectedCells.append(taskBlock)
    
    for selectedCell in selectedCells:
        tag = selectedCell.Tag
        t = tag.GetType()
        entityProp = t.GetProperty("Entity", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public)
        component = entityProp.GetValue(tag)
        for system in GetAllSystems():
            if component in system.Components:
                system.Delete()
    
  • lgospar
    lgospar Member Posts: 3
    First Comment
    **

    Thanks!