get the face on a plane

I have a body in Solidworks and have a plane defined where I cut the body at the plane. This leaves an exposed face. There are several things I want to do with this face such as compute the section properties and write the results to a text file, I also want to save the face as a step file.

In order to achieve this, I started by recording a macro of me performing the functions manually. However, some of the functions the macro recorded won't work consistently.

I was looking through the API object model and cannot find a method for programmatically selecting the face on the currently selected plane.

Pre-condition:

Define a plane at a distance of x.not (named CutPlane)

Define a sketch 

Draw a rectangle larger than the part

extrude the reactangle leaving only a partial body

Then, for example, my VBA code does the following:

   calculate a starting x position of the plane

   select CutPlane

   change position of CutPlane

   Update model

   'Here is the problem

   select face on selected plane "CutPlane"

   calculate section properties from face

   write to text file

   save face as step file

   update x position

   update step file name

loop

As I understand, there are numerous ways I could have gotten the section face but I needed the cutplane for other reasons and thought I would make use of it for this macro as well.

Is there a simple way to select the CutPlane by name and get the face on that plane?

SolidworksApi/macros
Answers 2
ST
ST 2021-04-08 Best Answer

To get the plane. use the PartDoc.FeatureByName method

Assuming it is a simple cut with surface feature, then you can get the face that was created by the cut by getting the Feature for the cut (using FeatureByName again) and call Feature.GetFaces. That will return an array of faces, but most likely there will only be one face in the array, which is the one you want.

Here is a simple macro which selects the face created by a surface cut:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2

Sub main()
    Dim swPart As PartDoc
    Dim swFeat As Feature
    Dim sa As Variant
    Dim i As Integer
    Dim swFace As Face2
    Dim swEnt As Entity
    
    Set swApp = Application.SldWorks
    Set Part = swApp.ActiveDoc
    Part.ClearSelection2 True
    
    Set swPart = Part
    Set swFeat = swPart.FeatureByName("SurfaceCut1")
    sa = swFeat.GetFaces
    If Not IsEmpty(sa) Then
        For i = 0 To UBound(sa)
            Set swFace = sa(i)
            Set swEnt = myFace
            myEnt.Select4 True, Nothing
        Next
    End If
End Sub

KK
KK 2021-04-08

Thank you Simon,

This worked perfect. I noticed the little type with myFace and myEnt rather than swFace and swEnt but that is a trivial detail. This worked awesome and already added into my existing code.