> Get Angle of Hole Not Normal to a Face Example (VBA)
 Print |  Feedback on this topic
Welcome
Getting Started
SolidWorks API Help
FeatureWorks API Help
SolidWorks Document Manager API Help
eDrawings API Help
SolidWorks Routing API Help
SolidWorks Simulation API Help
SolidWorks Utilities API Help
SolidWorks Workgroup PDM API Help

# 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)

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

'----------------------------------------------

Related SolidWorks Forum Content

Provide feedback on this topic

* Required

 *Email: Subject: Feedback on Help Topics Page: Get Angle of Hole Not Normal to a Face Example (VBA) *Comment:
x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

Never show this message again
x

Web Help Content Version: API Help (English only) 2012 SP05

The search functionality within the web help is in a beta test phase and you may experience periodic delays or interruptions in its performance. These are the normal and ordinary features of a beta test and shall not under any circumstances give rise to any liability on the part of DS SolidWorks or its licensors. The topics within the Web-based help are not beta topics; they document API Help (English only) 2012 SP05.

To disable Web help from within SolidWorks and use local help instead, click Help > Use SolidWorks Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.