Get Plane or Face for Sketch Example (VBA)
This example shows how to get the plane of the face on which a selected
sketch was drawn.
'--------------------------------------
'
' Preconditions:
' (1)
Part or assembly is open.
' (2)
Sketch is selected.
'
' Postconditions: Plane or face on which selected sketch
was drawn is selected.
'
'--------------------------------------
Option Explicit
Sub main()
Dim
pSWApp As
SldWorks.SldWorks
Dim
pModel As
SldWorks.ModelDoc2
Dim
pSelMgr As
SldWorks.SelectionMgr
Dim
pFeat As
SldWorks.Feature
Dim
pSketch As
SldWorks.sketch
Dim
pEntity As
SldWorks.entity
Dim
pPlaneFeat As
SldWorks.Feature
Dim
pFace As
SldWorks.face2
Dim
pRefPlane As
SldWorks.refPlane
Dim
vPlaneParams As
Variant
Dim
vNormal As
Variant
Dim
bRet As
Boolean
Dim
nEntType As
Long
Set
pSWApp = CreateObject("SldWorks.Application")
Set
pModel = pSWApp.ActiveDoc
Set
pSelMgr = pModel.SelectionManager
Set
pFeat = pSelMgr.GetSelectedObject5(1)
Set
pSketch = pFeat.GetSpecificFeature2
Set
pEntity = pSketch.GetReferenceEntity(nEntType)
Debug.Print
"Sketch Name = " + pFeat.Name
If
nEntType = swSelDATUMPLANES Then
Set
pPlaneFeat = pEntity
Set
pRefPlane = pEntity
vPlaneParams
= pRefPlane.GetRefPlaneParams
Debug.Print
"Plane Selected!"
Debug.Print
" Origin
=
(" + _
Str(vPlaneParams(0)
* 1000#) + "," + _
Str(vPlaneParams(1)
* 1000#) + "," + _
Str(vPlaneParams(2)
* 1000#) + ") mm"
Debug.Print
" Xvector
= ("
+ _
Str(vPlaneParams(3))
+ "," + _
Str(vPlaneParams(4))
+ "," + _
Str(vPlaneParams(5))
+ ")"
Debug.Print
" Normal
=
(" + _
Str(vPlaneParams(6))
+ "," + _
Str(vPlaneParams(7))
+ "," + _
Str(vPlaneParams(8))
+ ")"
'
Cannot select through Entity; must
'
select through Feature
bRet
= pPlaneFeat.Select2(False, 0)
End
If
If
nEntType = swSelFACES Then
Set
pFace = pEntity
vNormal
= pFace.Normal
Debug.Print
"Face Selected!"
Debug.Print
" Normal
=
(" + _
Str(vNormal(0))
+ "," + _
Str(vNormal(1))
+ "," + _
Str(vNormal(2))
+ ")"
bRet
= pEntity.Select2(False, 0)
End
If
End Sub
'--------------------------------------------------