Script for deleting selected workbench systems.

Options
Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 813
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

Script for deleting selected workbench systems.

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 813
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    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
    Options

    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: 813
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Options

    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: 115
    First Answer First Anniversary Name Dropper Solution Developer Community of Practice Member
    Options

    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: 230
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited April 9
    Options

    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: 221
    First Answer First Comment 5 Likes First Anniversary
    Options
    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
    Options

    Thanks!