How to select face(s) of planar section view in macro?
How to select face(s) of planar section view in macro?
(OP)
How can I select all faces created by a planar section view?
I need to obtain the total cross-sectional area of face(s) (there may be more than one), which I know is available via the GetSectionProperties() function, but I haven't been able to determine how to select the faces on the section plane. Selection of the faces prior to invoking the macro is not an option, because the macro needs to be able to move the section plane.
Thanks,
R. Main.
I need to obtain the total cross-sectional area of face(s) (there may be more than one), which I know is available via the GetSectionProperties() function, but I haven't been able to determine how to select the faces on the section plane. Selection of the faces prior to invoking the macro is not an option, because the macro needs to be able to move the section plane.
Thanks,
R. Main.






RE: How to select face(s) of planar section view in macro?
1. Get the ModelView object for the section
2. Use either GetSectionedBody or GetSectionedBodies (depending on Part or Assembly context) to get all of the bodies sectioned in the current view.
3. Iterate through all the faces of these bodies using GetFirstFace and GetNextFace
4. Compare GetFeature on each face to Nothing. Faces that were created by cutting the section view are not real faces, so they have no parent feature. Any face that returns Nothing for the GetFeature method should be faces that were created by the section.
Here is a quick macro that will return the sectioned and non-sectioned areas along with the sectioned face count and non-sectioned face count. It only works in part files, and it only works on one sectioned body. If there are multiple bodies sectioned or multiple bodies created by the sectioning process it will not work correctly.
CODE
Dim swDoc As SldWorks.ModelDoc2
Dim selMgr As SldWorks.SelectionMgr
Dim mView As SldWorks.ModelView
Dim myBody As SldWorks.Body2
Dim myFace As SldWorks.Face2
Dim myFaultEnt As SldWorks.FaultEntity
Dim myFeat As SldWorks.Feature
Dim MviewMgr As SldWorks.ModelViewManager
Dim mySecArea As Double
Dim myNonSecArea As Double
Dim SecCount As Long
Dim NonSecCount As Long
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set selMgr = swApp.ActiveDoc.SelectionManager
Set MviewMgr = swDoc.ModelViewManager
Set mView = swDoc.ActiveView
Set myBody = swDoc.GetSectionedBody(mView)
Set myFace = myBody.GetFirstFace
mySecArea = 0
myNonSecArea = 0
SecCount = 0
NonSecCount = 0
While Not (myFace Is Nothing)
If (myFace.GetFeature Is Nothing) Then
mySecArea = mySecArea + myFace.GetArea
SecCount = SecCount + 1
Else
myNonSecArea = myNonSecArea + myFace.GetArea
NonSecCount = NonSecCount + 1
End If
Wend
MsgBox "Sectioned Area: " & mySecArea & vbCrLf & _
"Sectioned Faces: " & SecCount & vbCrLf & _
"Non-Sectioned Area: " & myNonSecArea & vbCrLf & _
"Non-Sectioned Faces: " & NonSecCount
End Sub
RE: How to select face(s) of planar section view in macro?
Sum area of all sectioned faces
Count of sectioned faces
Sum area of all non-sectioned faces
Count of non-sectioned faces
Total component count in assembly
Count of components that have been sectioned
Total bodies count
Sectioned bodies count
It is not limited to only seeing a single body as it is in part files. If you need to analyze a single part you can still put it into an empty assembly and then run your macro on the assembly.
CODE
Dim swDoc As SldWorks.ModelDoc2
Dim selMgr As SldWorks.SelectionMgr
Dim mView As SldWorks.ModelView
Dim myBody As SldWorks.Body2
Dim myFace As SldWorks.Face2
Dim myFaultEnt As SldWorks.FaultEntity
Dim myFeat As SldWorks.Feature
Dim MviewMgr As SldWorks.ModelViewManager
Dim myComps As Variant
Dim myComp As SldWorks.Component2
Dim myBods As Variant
Dim mySecArea As Double
Dim myNonSecArea As Double
Dim SecCount As Long
Dim BodyCount As Long
Dim SectionedBodyCount As Long
Dim SectionedCompCount As Long
Dim BodySectioned As Boolean
Dim CompSectioned As Boolean
Dim NonSecCount As Long
Dim i As Long
Dim j As Long
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set selMgr = swApp.ActiveDoc.SelectionManager
Set MviewMgr = swDoc.ModelViewManager
Set mView = swDoc.ActiveView
myComps = swDoc.GetComponents(False)
mySecArea = 0
myNonSecArea = 0
SecCount = 0
NonSecCount = 0
SectionedCompCount = 0
SectionedBodyCount = 0
BodyCount = 0
For j = 0 To UBound(myComps)
Set myComp = myComps(j)
CompSectioned = False
myBods = myComp.GetSectionedBodies(mView)
For i = 0 To UBound(myBods)
Set myBody = myBods(i)
Set myFace = myBody.GetFirstFace
BodySectioned = False
While Not (myFace Is Nothing)
If (myFace.GetFeature Is Nothing) Then
mySecArea = mySecArea + myFace.GetArea
SecCount = SecCount + 1
BodySectioned = True
CompSectioned = True
Else
myNonSecArea = myNonSecArea + myFace.GetArea
NonSecCount = NonSecCount + 1
End If
Set myFace = myFace.GetNextFace
Wend
If BodySectioned Then
SectionedBodyCount = SectionedBodyCount + 1
End If
BodyCount = BodyCount + 1
Next i
If CompSectioned Then
SectionedCompCount = SectionedCompCount + 1
End If
Next j
MsgBox "Sectioned Area: " & mySecArea & vbCrLf & _
"Sectioned Faces: " & SecCount & vbCrLf & _
"Non-Sectioned Area: " & myNonSecArea & vbCrLf & _
"Non-Sectioned Faces: " & NonSecCount & vbCrLf & _
"Components: " & UBound(myComps) + 1 & vbCrLf & _
"Sectioned Components: " & SectionedCompCount & vbCrLf & _
"Bodies: " & BodyCount & vbCrLf & _
"Sectioned Bodies: " & SectionedBodyCount
End Sub