3D Design

3D Design

Topics related to Ansys Discovery and Ansys SpaceClaim.

Select all objects in Layer

    • David Hills
      Subscriber

      Hello, I'd like to enumerate through all layers, select all objects on a layer, then perform a custom function on the selected objects. I'm not able to identify if the objects i have selected are part of the active layer. i would expect it to work similar to this:

      def selectObjinLayer():
          doc = Window.ActiveWindow.Document
       
          for layer in Layers.GetAllLayers():
              #print(layer.GetName())
              Layers.Activate(layer.GetName())
              selection = Selection.Clear()
              selection = Selection.Create(Layers.GetActive())
              secondarySelection = Selection.Empty()
              
              #this is the point where the script fails
              # the GetAllBodies returns empty, so no iteration occurs
              for design_body in doc.MainPart.GetAllBodies():
                  print("check if ", design_body," is in layer ", layer)
      #this is a nonsense getter and comparison:
                 if design_body.Layer == layer:
                      selection.AddToActive()
              return selection
       
      selectedOBJS = selectObjinLayer()
      customFunction(selectedOBJS)
       
      the customFunction fills all objects, then renames the associated Surfaces based on the layer name.
    • Atharv Joshi
      Forum Moderator

      Hi David,

      Let me check and get back to you. 

      Thanks and Regards
      Atharv

    • David Hills
      Subscriber

      Hi Atharv,

      Thanks for investigating this, I'm excited to see what you come up with.  

      Perhaps this is helpful: when right-clicking on a layer, the "select all objects" function does exactly what I need, but the macro recorder cannot record its action.

      BEst,

      Dave 

    • David Hills
      Subscriber

      An Ansys engineer has helped me to close this issue. The resulting code is:

       

      def fillSelected(layer_curves):
          #fill the curves in the layer. this creates a new surf body
          selection = Selection.Create(layer_curves)
          secondarySelection = Selection.Empty()
          options = FillOptions()
          result = Fill.Execute(selection, secondarySelection, options, FillMode.ThreeD)

      allCurves = GetRootPart().GetDescendants[IDesignCurve]()
      for layer in Layers.GetAllLayers():
          layer_curves=[i for i in allCurves if i.Layer==layer]
          if not len(layer_curves)==0:
              Layers.Activate(layer.GetName())
              fillSelected(layer_curves)

Viewing 3 reply threads
  • The topic ‘Select all objects in Layer’ is closed to new replies.