3 Replies Latest reply on May 19, 2017 2:48 AM by Ivana Kolin

    NEAREST VERTEX( for exemple FACE1) FROM A FACE 2

    Korbi Anis

      Hi brothers , I am beginner in API solidworks and I have a request I hope you will help me to solve it . I have two faces FACE 1 and FACE2 and I would like to select the nearest vertex of FACE1 that near more from FACE 2 . I need a macro to do it . thank you very much

          • Re: NEAREST VERTEX( for exemple FACE1) FROM A FACE 2
            Korbi Anis

            Thank you drear Ivana fro your aswer , however , I need just to select all vertex wich are near from a specific face !

              • Re: NEAREST VERTEX( for exemple FACE1) FROM A FACE 2
                Ivana Kolin
                Option Explicit
                Sub main()
                    Dim swApp                 As SldWorks.SldWorks
                    Dim swModel               As SldWorks.ModelDoc2
                    Dim swSelMgr              As SldWorks.SelectionMgr
                    Dim swFace1               As SldWorks.Face2
                    Dim swFace2               As SldWorks.Face2
                    Dim vPoint1               As Variant
                    Dim vPoint2               As Variant
                    Dim nDist                 As Double
                    Dim minDist               As Double
                    Dim edgeList              As Variant
                    Dim edgeCount             As Long
                    Dim edgeObj               As Object
                    Dim startVertexObj        As Object
                    Dim endVertexObj          As Object
                    Dim i                     As Integer
                    Dim e                     As Entity
                    Set swApp = Application.SldWorks
                
                
                    Set swModel = swApp.ActiveDoc
                    Set swSelMgr = swModel.SelectionManager
                    Set swFace1 = swSelMgr.GetSelectedObject6(1, -1)
                    Set swFace2 = swSelMgr.GetSelectedObject6(2, -1)
                    swModel.ClearSelection2 True
                
                
                    nDist = swModel.ClosestDistance(swFace1, swFace2, vPoint1, vPoint2)
                    If nDist < 0 Then Exit Sub
                    minDist = nDist
                    edgeCount = swFace2.GetEdgeCount
                    edgeList = swFace2.GetEdges
                    For i = 0 To (edgeCount - 1)
                        Set edgeObj = edgeList(i)
                        Set startVertexObj = edgeObj.GetStartVertex
                        Set endVertexObj = edgeObj.GetEndVertex
                
                
                        If (Not startVertexObj Is Nothing) Then
                            nDist = swModel.ClosestDistance(swFace1, startVertexObj, vPoint1, vPoint2)
                            If nDist >= 0 And nDist = minDist Then
                                Set e = startVertexObj
                                e.Select4 True, Nothing
                            End If
                        End If
                
                
                        If (Not endVertexObj Is Nothing) Then
                            nDist = swModel.ClosestDistance(swFace1, endVertexObj, vPoint1, vPoint2)
                            If nDist >= 0 And nDist = minDist Then
                                Set e = endVertexObj
                                e.Select4 True, Nothing
                            End If
                        End If
                    Next
                End Sub