Get Angle Between Plane and Line Example (VBA)
This example shows how to get the angle between a selected plane and
a selected sketch line.
'-----------------------------------------------
'
' Preconditions: Sketch containing a Line1@3DSketch1 exists.
'
' Postconditions: None
'
'-----------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim boolstatus As Boolean
Sub main()
Dim swMath As SldWorks.MathUtility
Set swApp = Application.SldWorks
Set swMath = swApp.GetMathUtility
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swModelDocExt = swModel.Extension
swModel.ClearSelection2
True
boolstatus = swModelDocExt.SelectByID2("Top
Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swModelDocExt.SelectByID2("Line1@3DSketch1",
"EXTSKETCHSEGMENT", 0.07751735119544, 0.02644237600659, -0.0634797539437,
True, 0, Nothing, 0)
Dim swFeat As SldWorks.Feature
Dim swSkSeg As SldWorks.SketchSegment
Set swFeat = swSelMgr.GetSelectedObject6(1,
0)
Set swSkSeg = swSelMgr.GetSelectedObject6(2,
0)
Dim swRefPlane As SldWorks.RefPlane
Set swRefPlane = swFeat.GetSpecificFeature2
Dim normVec As MathVector
Dim dirArr(2) As Double
Dim params As Variant
dirArr(0) = 0#
dirArr(1) = 0#
dirArr(2) = 1#
Set normVec = swMath.CreateVector((dirArr))
Set normVec = normVec.MultiplyTransform(swRefPlane.Transform)
Debug.Print normVec.ArrayData(0),
normVec.ArrayData(1), normVec.ArrayData(2)
Dim curveVec As SldWorks.MathVector
Dim swCurve As SldWorks.Curve
Set swCurve = swSkSeg.GetCurve
params = swCurve.LineParams
dirArr(0) = params(3)
dirArr(1) = params(4)
dirArr(2) = params(5)
Set curveVec = swMath.CreateVector((dirArr))
Debug.Print curveVec.ArrayData(0),
curveVec.ArrayData(1), curveVec.ArrayData(2)
Dim crossVec As SldWorks.MathVector
Set crossVec = curveVec.Cross(normVec)
Dim dot As Double
Dim vecLen As Double
Dim angle As Double
dot = curveVec.dot(normVec)
vecLen = crossVec.GetLength()
angle = Atn(dot / vecLen)
Debug.Print angle
End Sub