Get Section Properties for Faces from Section Planes Example (VBA)
This example shows how to get the section properties of the selected
items.
'-----------------------------------------------
' Problem: Through the user interface, it is possible
to extract the
' section
properties from various combinations of
' selected
entities. This is accessible through the SolidWorks
' API
with:
'
' IModelDocExtension::GetSectionProperties
'
' This
method has only one input parameter, an array of entities
' to
consider with the current selection set.
' The
entity array must only contain the following types of
' objects:
'
' *
IFace2
' *
ISketch
'
' For
planar faces and reference surfaces, this is the
' IFace2
object. A sketch corresponds to a
' ISketch
object. However, for a face on a section plane,
' there
is no API object.
'
' This
sample code shows how to use this method
' to
allow for faces obtained from section planes.
'
' Preconditions:
' (1)
Part, assembly, or drawing is open
' (2)
Assembly is fully resolved
' (3)
At least one of the following is selected:
' *
Sketch
' *
Planar model face in any document
' *
Face on a section plane
' *
Crosshatch section face in a section view
' in
a drawing or a sketch
'
' Postconditions:
' (1)
Selection set is maintained (see NOTES)
' (2)
If successful, section properties are output to the debugger
'
' NOTES:
' (1)
Array of objects passed in to this method are added to
' the
selection set
' (2)
Outputted values are the same as those obtained through
' the
user interface
' (3)
Reasons for failure are the same as those
' in
the user interface
'
'-----------------------------------------------
Option Explicit
Public Enum swSelectType_e
swSelFACES
= 2 '
"FACE"
swSelSKETCHES
= 9 '
"SKETCH"
swSelREFSURFACES
= 27 '
"REFSURFACE"
swSelMANIPULATORS
= 79 '
"MANIPULATOR"
End Enum
Sub main()
Const
PI As
Double = 3.14159
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swModelExt As
SldWorks.ModelDocExtension
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swFace As
SldWorks.face2
Dim
swFeat() As
SldWorks.feature
Dim
swFaceEnt() As
SldWorks.entity
Dim
swSketch As
SldWorks.sketch
Dim
nSelType As
Long
Dim
swSelObj() As
Object
Dim
vSelObj As
Variant
Dim
vSectionProp As
Variant
Dim
nSelCount As
Long
Dim
nNumObj As
Long
Dim
i As
Long
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swModelExt = swModel.Extension
Set
swSelMgr = swModel.SelectionManager
Debug.Print
"File = " & swModel.GetPathName
nSelCount
= swSelMgr.GetSelectedObjectCount
For
i = 1 To nSelCount
nSelType
= swSelMgr.GetSelectedObjectType2(i)
Debug.Print
" SelType("
& i & ") = " & nSelType
Select
Case nSelType
Case
swSelFACES, swSelREFSURFACES
nNumObj
= nNumObj + 1
ReDim
Preserve swSelObj(nNumObj - 1)
ReDim
Preserve swFaceEnt(nNumObj - 1)
Set
swFace = swSelMgr.GetSelectedObject5(i)
Set
swFaceEnt(nNumObj - 1) = swFace
Set
swSelObj(nNumObj - 1) = swFace
Case
swSelSKETCHES
nNumObj
= nNumObj + 1
ReDim
Preserve swSelObj(nNumObj - 1)
ReDim
Preserve swFeat(nNumObj - 1)
Set
swFeat(nNumObj - 1) = swSelMgr.GetSelectedObject5(i)
Set
swSketch = swFeat(nNumObj - 1).GetSpecificFeature
Set
swSelObj(nNumObj - 1) = swSketch
Debug.Print
" "
& swFeat(nNumObj - 1).Name
Case
swSelMANIPULATORS
'
Section face in a part or assembly when in a section view
'
There
is no corresponding API object for this,
'
so
leave it selected
End
Select
Next
i
Debug.Print
""
'
Deselect faces and sketches otherwise user-interface selections
'
are added to array parameter. Leave section faces
'
selected.
If
Not IsEmpty(swFaceEnt) Then
For
i = 0 To UBound(swFaceEnt)
swFaceEnt(i).DeSelect
Next
i
End
If
If
Not IsEmpty(swFeat) Then
For
i = 0 To UBound(swFeat)
swFeat(i).DeSelect
Next
i
End
If
vSelObj
= swSelObj
'
This adds the array of faces or sketches to the selection set.
'
Because the faces or sketches have been deselected, this
'
preserves the selection set.
vSectionProp
= swModelExt.GetSectionProperties((vSelObj))
'
Return code from:
'
'
IModelDocExtension::GetSectionProperties
'
'
0
= success
'
1
= invalid input
'
2
= selected faces are not in the same or parallel planes
'
3
= unable to compute section properties
'
Debug.Print
" Return
code =
" & vSectionProp(0)
Debug.Print
""
Debug.Print
" Area
=
" & vSectionProp(1) * 1000000# & " mm^2"
Debug.Print
" Centroid
=
(" & vSectionProp(2) * 1000# & ", " & vSectionProp(3)
* 1000# & ", " & vSectionProp(4) * 1000# & ")
mm"
Debug.Print
" Ixx
=
" & vSectionProp(5) * 1000000000000# & " mm^4"
Debug.Print
" Iyy
=
" & vSectionProp(6) * 1000000000000# & " mm^4"
Debug.Print
" Izz
=
" & vSectionProp(7) * 1000000000000# & " mm^4"
Debug.Print
" Ixy
=
" & vSectionProp(8) * 1000000000000# & " mm^4"
Debug.Print
" Izx
=
" & vSectionProp(9) * 1000000000000# & " mm^4"
Debug.Print
" Iyz
=
" & vSectionProp(10) * 1000000000000# & " mm^4"
Debug.Print
" Polar
MOI =
" & vSectionProp(11) * 1000000000000# & " mm^4"
Debug.Print
" Angle
princ & part axes =
"
& vSectionProp(12) * 180# / PI & " deg"
Debug.Print
" Ix
=
" & vSectionProp(13) * 1000000000000# & " mm^4"
Debug.Print
" Iy
=
" & vSectionProp(14) * 1000000000000# & " mm^4"
End Sub
'-------------------------------------------------