Ansys Learning Forum Forums Discuss Simulation Preprocessing Spaceclaim API – Select faces at given coordinates Reply To: Spaceclaim API – Select faces at given coordinates

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")