General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

ACT Script: Assign items to a named selection

    • Mike3000
      Subscriber


      What is the best way to assign items to a named selection in ACT?

      I know how to browse through items (parts, bodies, etc.) in the structure tree. But it seems to me that it is not possible to assign an item, for example a body directly to a named selection. An intermediate step seems to be to gather items in a selection and then assign the contents of the selection to a named selection.

      However, I do not manage to add the items to a selection programmatically. The methods NewSelection as well as AddSelection need an input argument of type ISelectionInfo but I do not know how to obtain this from the Items I am browsing.


      Below is an example where I have tried to assign each part to a named selection.



      #Model
      model = ExtAPI.DataModel.Project.Model

      # Get parts
      Parts = model.Geometry.GetChildren(DataModelObjectCategory.Part,True)

      # The procedure will be included in a loop later. Just using first part in test.
      MyPart = Parts[0]

      # Selection
      SlMn = ExtAPI.SelectionManager
      SlMn.ClearSelection()

      #
      >>> Missing part: Adding "MyPart" to current selection <<<

      # SlMn.NewSelection()

      # Create named selection
      NSn = model.NamedSelections
      MyNS = NSn.AddNamedSelection()
      MyNS.Name = "Part0"
      MyNS.Location = SlMn.CurrentSelection

    • Mike3000
      Subscriber

      Meanwhile, I created a solution myself. It creates named selections for every part and assigns the part name to the corresponding named selection.


      # Selection
      SlMn = ExtAPI.SelectionManager
      SlMn.ClearSelection()
      Sel = SlMn.CreateSelectionInfo(SelectionTypeEnum.GeometryEntities)

      #Model
      model = ExtAPI.DataModel.Project.Model

      # Get parts
      Parts = model.Geometry.GetChildren(DataModelObjectCategory.Part,True)

      print("Listing properties of Items:")
      with Transaction():             # Suppress GUI update until finish to improve speed
          for Part in Parts:
              PName = Part.Name
              # print("Part name: ",PName)
             
              tmp_ls = []
              for Body in Part.Children:
                  BName = Body.Name
                  BId = Body.GetGeoBody().Id
                  #print("Body name: "+BName+" ,BId: "+str(Id))
                  tmp_ls.append(BId)                                    # Collect all body Ids in temporary list

              # ExtAPI.SelectionManager.NewSelection(Sel)
              Sel.Ids = tmp_ls
              print("Sel.Ids: ",Sel.Ids)
              # Sel.Entities = [Body.GetGeoBody()]
              # Create named selection
              NSn = model.NamedSelections
              MyNS = NSn.AddNamedSelection()
              MyNS.Name = Part.Name
              MyNS.Location = Sel

      • a_l_o
        Subscriber

        Hi Mike,

        Thank you so much for your script! It helped me a lot, cheers to you for posting it.

        Greets! a-l-o

    • Rob
      Forum Moderator

      Thanks for posting the solution: we're very limited in how much coding support we're allowed to provide so always useful when users (and the wider community) can post answers. 

    • Erik Kostson
      Ansys Employee

      Thank you - closing this post.

      Erik

Viewing 3 reply threads
  • The topic ‘ACT Script: Assign items to a named selection’ is closed to new replies.