Preprocessing

Preprocessing

Topics related to geometry, meshing, and CAD.

Spaceclaim API – Select faces at given coordinates

    • PPosabella
      Subscriber

      Hello everybody!

      I am generating multiple geometries with an own-written code. I am struggling to identify the commands to select faces of my geometries of which I know the coordinates (x, y, z).

      I'll be more specific. In the attached picture is depicted a cube of dimensions L = 10;  W = 10; H = 20 [mm]: I want to select the face at the top of the structure (highlighted in orange) of which I know the coordinates, then use the NamedSelection.Create() command. One of my ideas was to create planes at those coordinates and then find the intersections with my geometry, but I couldn't develop my code.

      I tried to use the split body tool but with no luck. Same with filter by bounding box (also described here /forum/forums/topic/spaceclaim-script-selection-by-filterbyboundingbox/)

      I already made searches on the forum and the web but with poor results. Any kind of help is appreciated, thanks in advance :)

       

    • mjmiddle
      Ansys Employee

      Yes, it looks like the FilterByBoundingBox(mbox) under the face selection is not doing any filtering at all.

      There are a number of ways you can get the face. If you know this face is planar to the XZ plane, you could loop through all faces of the body, and get each face's vertices (need to get the face's edges first, then get the edge's vertices), then check that the Y value for all it's vertices are at 20 mm.

      You could check the Y value of a centroid. But you can't get the centroid of a face, only bodies. So you would have to internally convert each face to a surface body and then delete after checking the centroid.

      For large models, we can create a partition of all the faces (or bodies, edges, vertices), and then search within a bounding box.

      Since you are asking this question, this may not be a problem if you choose a different script design. You should use bottom-up methods of creation to track items you created. So that means: create curves, then faces, then bodies. And get the returned item each time for the next operation. Use functions after the return of creation such as these in SpaceClaim:
      result.CreatedPlanes
      result.CreatedCurves
      result.CreatedBodies
      result.CreatedFaces

      See the example below to create a box. I did choose to use "Repair > Missing Faces" to make face6, and this does not return the face created, so I just got last face in the list. This is not the best means of creation if you wanted to track face6 to place in a named selection. It may be better to create all the wireframe edges of the body, and Fill or Blend each surface instead of pulling the 4 edges like I did so that you can track more explicitly all 6 faces made from the fill or blend operation.

      learAll()    # good to do this at the beginning so you always start from the same state, and entity indices don't continue from some previously defined indices
      extrudeDepth = MM(40)
      BaseLength = MM(20)
      BaseWidth = MM(16)

      # Set Sketch Plane
      sectionPlane = Plane.PlaneXY
      result = ViewHelper.SetSketchPlane(sectionPlane, None)

      # Set Layout Curves
      SketchHelper.StartConstraintSketching()
      SketchHelper.LayoutCurves = True    # So that it doesn't try to create a fill surface automatically after it goes from 2D sketch mode to 3D

      # Sketch Line
      start = Point2D.Create(MM(0), MM(0))
      end = Point2D.Create(BaseLength, MM(0))
      result = SketchLine.Create(start, end)
      crv1 = result.CreatedCurves[0]

      # Sketch Line
      start = Point2D.Create(BaseLength, MM(0))
      end = Point2D.Create(BaseLength, BaseWidth)
      result = SketchLine.Create(start, end)
      crv2 = result.CreatedCurves[0]

      # Sketch Line
      start = Point2D.Create(BaseLength, BaseWidth)
      end = Point2D.Create(MM(0), BaseWidth)
      result = SketchLine.Create(start, end)
      crv3 = result.CreatedCurves[0]

      # Sketch Line
      start = Point2D.Create(MM(0), BaseWidth)
      end = Point2D.Create(MM(0), MM(0))
      result = SketchLine.Create(start, end)
      crv4 = result.CreatedCurves[0]

      # Solidify Sketch
      mode = InteractionMode.Solid
      result = ViewHelper.SetViewMode(mode, None)

      # Extrude 1 Edge
      selection = Selection.Create(crv1)
      options = ExtrudeEdgeOptions()
      pnt1 = crv1.Shape.StartPoint
      result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
      face1 = result.CreatedFaces[0]
      body1 = face1.Parent

      # Extrude 1 Edge
      selection = Selection.Create(crv2)
      pnt1 = crv2.Shape.StartPoint
      result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
      face2 = result.CreatedFaces[0]
      body2 = face2.Parent

      # Extrude 1 Edge
      selection = Selection.Create(crv3)
      pnt1 = crv3.Shape.StartPoint
      result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
      face3 = result.CreatedFaces[0]
      body3 = face3.Parent

      # Extrude 1 Edge
      selection = Selection.Create(crv4)
      pnt1 = crv4.Shape.StartPoint
      result = ExtrudeEdges.Execute(selection, pnt1, Direction.DirZ, extrudeDepth, options)
      face4 = result.CreatedFaces[0]
      body4 = face4.Parent

      # Fill
      selection = Selection.Create([crv1,crv2,crv3,crv4])
      secondarySelection = Selection.Empty()
      options = FillOptions()
      result = Fill.Execute(selection, secondarySelection, options, FillMode.ThreeD, None)
      face5 = result.CreatedFaces[0]
      body5 = face5.Parent

      # Merge Bodies
      targets = BodySelection.Create(body1)
      tools = BodySelection.Create(body2, body3, body4, body5)
      result = Combine.Merge(targets, tools)

      # Fix Missing Face
      options = FixMissingFacesOptions()
      result = FixMissingFaces.FindAndFix(options, None)
      #FixMissingFaces.FindAndFix does not return the faces creates. Knowing only one hole will be filled, get the last face in the list:
      face6 = body1.Faces[-1]    # Only body1 is left since all the other bodies were tool bodies to merge to body1

      # Create Named Selection Group
      primarySelection = BodySelection.Create(body1)
      secondarySelection = Selection.Empty()
      result = NamedSelection.Create(primarySelection, secondarySelection)
      result = NamedSelection.Rename("Group1", "My Body1")

      # Create Named Selection Group
      primarySelection = FaceSelection.Create(face1)
      secondarySelection = Selection.Empty()
      result = NamedSelection.Create(primarySelection, secondarySelection)
      result = NamedSelection.Rename("Group1", "My face1")

       

    • PPosabella
      Subscriber

      Thank you for your answer!

       

      However, I am working on more complex structures than the one I described above. I managed to work on the normal vectors of each face, here I attach the code I developed (it may require adjustments, but for now it's doing its job)

       

      numFaces = len(GetRootPart().Bodies[0].Faces) # get number of total faces
      selectedFaces = [] # initialise vector to include required faces

      for i in range(0, numFaces - 1): # select faces knowing their normal vector
          if str((GetRootPart().Bodies[0].Faces[i].GetFaceNormal(0,0)))  == 'Direction: (0, 0, -1)': # choose normal vector. Still don't know how GetFaceNormal(0,0) is affecting the code
                  selectedFaces.append(GetRootPart().Bodies[0].Faces[i]) # append face

      print(selectedFaces)

      # Define Named Selection
      primarySelection = FaceSelection.Create(selectedFaces)
      secondarySelection = Selection.Empty()
      result = NamedSelection.Create(primarySelection, secondarySelection)
      # Rename Named Selection
      result = NamedSelection.Rename("Group1", "myNamedSelection")
    • mjmiddle
      Ansys Employee

      Python range(value) command already subtracts one from the value. Do not do numFaces-1.

      range(4) or range(0,4) returns [0,1,2,3]

Viewing 3 reply threads
  • The topic ‘Spaceclaim API – Select faces at given coordinates’ is closed to new replies.