Files
Associated model(s) Determine whether selected faces are parallel
Code
'Based off of exercise 11 in API Fundamentals manual
'Preconditions: two faces are selected
'Results: if faces are parallel, distance between them is displayed
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim SelType1 As Long
Dim SelType2 As Long
Dim swFace1 As SldWorks.face2
Dim swFace2 As SldWorks.face2
Dim swSurf1 As SldWorks.surface
Dim swSurf2 As SldWorks.surface
Dim varSelPt1 As Variant
Dim varSelPt2 As Variant
Dim varClosePt1 As Variant
Dim varClosePt2 As Variant
Dim Dist1 As Double
Dim Dist2 As Double
Dim swSkMan As SldWorks.SketchManager
Dim swSkSeg As SldWorks.SketchSegment
Const ParallelDistTolerance As Double = 0.0001
Sub ParallelTest1()
swSkMan.Insert3DSketch True
Set swSkSeg = swSkMan.CreateLine(varSelPt1(0), varSelPt1(1), varSelPt1(2), varClosePt2(0), varClosePt2(1), varClosePt2(2))
swModel.SetLineColor 33023
Dist1 = swSkSeg.GetLength
Set swSkSeg = swSkMan.CreateLine(varSelPt2(0), varSelPt2(1), varSelPt2(2), varClosePt1(0), varClosePt1(1), varClosePt1(2))
swModel.SetLineColor 16744448
Dist2 = swSkSeg.GetLength
swSkMan.Insert3DSketch True
swModel.ClearSelection2 True
End Sub
Sub ParallelTest2()
Dist1 = Sqr(Sq(varSelPt1(0) - varClosePt2(0)) + Sq(varSelPt1(1) - varClosePt2(1)) + Sq(varSelPt1(2) - varClosePt2(2)))
Dist2 = Sqr(Sq(varSelPt2(0) - varClosePt1(0)) + Sq(varSelPt2(1) - varClosePt1(1)) + Sq(varSelPt2(2) - varClosePt1(2)))
End Sub
Function Sq(Num As Double) As Double
Sq = Num * Num
End Function
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swSelMgr = swModel.SelectionManager
Set swSkMan = swModel.SketchManager
SelType1 = swSelMgr.GetSelectedObjectType3(1, -1)
SelType2 = swSelMgr.GetSelectedObjectType3(2, -1)
If SelType1 <> swSelFACES Or SelType2 <> swSelFACES Then
Exit Sub
End If
'get the surfaces
Set swFace1 = swSelMgr.GetSelectedObject6(1, -1)
Set swFace2 = swSelMgr.GetSelectedObject6(2, -1)
Set swSurf1 = swFace1.GetSurface
Set swSurf2 = swFace2.GetSurface
'get the user's selection pt
varSelPt1 = swSelMgr.GetSelectionPoint2(1, -1)
varSelPt2 = swSelMgr.GetSelectionPoint2(2, -1)
'for each surface, get the point on that surface closest to the point that the user selected on the other surface
varClosePt1 = swSurf1.GetClosestPointOn(varSelPt2(0), varSelPt2(1), varSelPt2(2))
varClosePt2 = swSurf2.GetClosestPointOn(varSelPt1(0), varSelPt1(1), varSelPt1(2))
'Two possible ways to determine if magnitudes of vectors are equal (in which case the surfaces are //)
Call ParallelTest1 'Get length of sketch line method, which equals vector magnitude
'Call ParallelTest2 'Calculate vector magnitude method
If Abs(Dist1 - Dist2) <= ParallelDistTolerance Then
swApp.SendMsgToUser2 "Distance = " + Str(Round(Dist1 * 1000, 2)) + " mm", swMbInformation, swMbOk
Else
swApp.SendMsgToUser2 "Faces not parallel", swMbInformation, swMbOk
End If
End Sub
'With GetClosestPointOn function, the path between the input point and the target surface is always going to be _
normal to the target surface. Also, the surface is treated as infinitely large. It does not stop at the edge _
of the face. Consequently, to determine if two surfaces are parallel we only need to compare the lengths of _
the imaginary lines between the closest point and the user's selected point. To do this we could actually _
create lines and then measure their length, or, since the line is essentially a vector, we could calculate _
the magnitude by finding the difference in vector coordinates, squaring each, adding, and then taking the _
square root. (http://www.netcomuk.co.uk/~jenolive/vect5.html)
'If you're going to get the line length using ISketchSegment::GetLength then you will need to set the line equal _
to a sketch segment beforehand.