Get Angle of Hole Not Normal to a Face Example (VBA)
This example shows how to get the angle of a hole not normal to a face.
'----------------------------------------------
'
' Preconditions:
' (1)
Part is open.
' (2)
Edge of a hole is selected.
' (3)
Hole enters part on a flat face.
'
' Postconditions: None
'
' NOTE: Angle is returned in first quadrant; it should
' be
the angle measured between hole axis and face
' normal.
'
'-----------------------------------------------
Option Explicit
Const PI As
Double = 3.14159265358979
Function Arccos _
( _
X
As Double _
) As Double
If
Abs(1# - X) < 0.00000001 Then Arccos = PI / 2#: Exit Function
Arccos
= Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
End Function
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swMathUtil As
SldWorks.MathUtility
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swEdge As
SldWorks.Edge
Dim
vFace As
Variant
Dim
swFace(1) As
SldWorks.face2
Dim
swSurf(1) As
SldWorks.surface
Dim
swCylSurf As
SldWorks.surface
Dim
swFlatSurf As
SldWorks.surface
Dim
vCylinder As
Variant
Dim
vPlane As
Variant
Dim
nVector(2) As
Double
Dim
vVector As
Variant
Dim
i As
Long
Dim
swCylAxis As
SldWorks.MathVector
Dim
swFlatNorm As
SldWorks.MathVector
Dim
nDotProd As
Double
Dim
nAngle As
Double
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swMathUtil = swApp.GetMathUtility
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swEdge = swSelMgr.GetSelectedObject5(1)
vFace
= swEdge.GetTwoAdjacentFaces2
Set
swFace(0) = vFace(0)
Set
swFace(1) = vFace(1)
Set
swSurf(0) = swFace(0).GetSurface
Set
swSurf(1) = swFace(1).GetSurface
If
swSurf(0).IsCylinder Then
Set
swCylSurf = swSurf(0)
Set
swFlatSurf = swSurf(1)
Else
Debug.Assert
swSurf(0).IsPlane
Debug.Assert
swSurf(1).IsCylinder
Set
swCylSurf = swSurf(1)
Set
swFlatSurf = swSurf(0)
End
If
vCylinder
= swCylSurf.CylinderParams
vPlane
= swFlatSurf.PlaneParams
nVector(0)
= vCylinder(3): nVector(1)
= vCylinder(4): nVector(2)
= vCylinder(5)
vVector
= nVector
Set
swCylAxis = swMathUtil.CreateVector((vVector))
nVector(0)
= vPlane(0): nVector(1)
= vPlane(1): nVector(2)
= vPlane(2)
vVector
= nVector
Set
swFlatNorm = swMathUtil.CreateVector((vVector))
nDotProd
= swCylAxis.Dot(swFlatNorm)
'
Ensure angle is in first quadrant
nAngle
= Arccos(Abs(nDotProd)) * 180# / PI
Debug.Print
"File = " & swModel.GetPathName
Debug.Print
" cyl
origin
=
(" & vCylinder(0) * 1000# & ", " & vCylinder(1)
* 1000# & ", " & vCylinder(2) * 1000# & ")
mm"
Debug.Print
" cyl
axis =
(" & vCylinder(3) & ", " & vCylinder(4) &
", " & vCylinder(5) & ")"
Debug.Print
" cyl
radius
=
" & vCylinder(6) * 1000# & " mm"
Debug.Print
" flat
normal =
(" & vPlane(0) & ", " & vPlane(1) & ",
" & vPlane(2) & ")"
Debug.Print
" flat
root =
(" & vPlane(3) * 1000# & ", " & vPlane(4) *
1000# & ", " & vPlane(5) * 1000# & ") mm"
Debug.Print
" Angle
=
" & nAngle & " deg"
End Sub
'----------------------------------------------