1 Reply Latest reply on Jan 13, 2018 9:28 PM by Prashant Mohan

    Help selecting Edges of a contour

    Prashant Mohan

      I have code that gets the active sketch. It then gets all the contours of the sketch and selects one contour at a time. After that it gets all edges of each contour and loops through the edges and selects them. The sketch itself consists of bunch of rectangles.

      Issue is when i select the contour it highlights the contour (of that rectangle) but when i select an edge it does not do the same neither does it give me any error. Any ideas or suggestion will be appreciated.

       

      Dim vSkContours As Variant

      Dim SelMgr As SldWorks.SelectionMgr

      Dim mySelectData As SldWorks.SelectData

      Dim swSelData As SldWorks.SelectData

      Dim mySketch As SldWorks.Sketch

      Dim skContour As SketchContour

      Dim vEdges As Variant

      Dim myEdge As SldWorks.Edge

      Dim swEntity3 As SldWorks.Entity

      Dim len1 As Integer, len2 As Integer, lenRe As Integer, counter As Integer

      Dim feature As feature

       

       

       

       

      Set SelMgr = Curr_Part.SelectionManager

      Set mySelectData = SelMgr.CreateSelectData

      Set mySketch = Curr_Part.GetActiveSketch2()

      vSkContours = mySketch.GetSketchContours()

       

        If Not mySketch Is Nothing Then

       

              vSkContours = mySketch.GetSketchContours()

              contourCount = UBound(vSkContours) - LBound(vSkContours) + 1

            

              For i = LBound(vSkContours) To UBound(vSkContours)

                  Set skContour = vSkContours(i)

                

                   bRet = skContour.Select2(False, mySelectData)

                

                  vEdges = skContour.GetEdges()

                

                  len1 = 0

                  If IsEmpty(vEdges) Then

       

       

                      Debug.Print "    No edges."

       

       

                  Else

                

                     len1 = UBound(vEdges)

                

                  End If

                

                

                   For k = LBound(vEdges) To UBound(vEdges)

       

       

                      Set myEdge = vEdges(k)

       

       

                      If Not myEdge Is Nothing Then

                    

                          Debug.Print "    Edge " & k & ": "

                     

                          Set swEntity3 = myEdge

                          bRet = swEntity3.Select4(False, mySelectData)

       

       

                      End If

       

       

                  Next k

       

       

              Next i

          End If

       

       

      Regards,

      Prashant Mohan

        • Re: Help selecting Edges of a contour
          Prashant Mohan

          I implemented a  nearest neighbor algorithm, which finds lines less than 250 microns in a sketch a does a cut. Hope this helps

           

           

           

          Public Function Find_Nearest_Nbrs(Face As SldWorks.Face2, swFeat As SldWorks.Feature, Curr_Part As Object, SketchMgr As SldWorks.SketchManager) As Boolean ' Find parallel lines and distances and draws overlapping rectangles between them

           

           

          Dim Measure As SldWorks.Measure

          Dim swSelMgr As SldWorks.SelectionMgr

          Dim swFace As SldWorks.Face2

          Dim swEnt As SldWorks.Entity, swEnt1 As SldWorks.Entity

          Dim faceArr As Variant

          Dim oneFace As Variant

          Dim status As Boolean

          Dim swSelData As SldWorks.SelectData

          Dim Area As Double, Max_Area As Double

          Dim swEdge As SldWorks.Edge

          Dim swLoop As SldWorks.Loop2, swLoop1 As SldWorks.Loop2

          Dim edgeArr As Variant, edgeArr1 As Variant

          Dim Edge_Count As Integer

          Dim swCurve As SldWorks.Curve, swCurve1 As SldWorks.Curve

          Dim swCurveParams As SldWorks.CurveParamData, swCurveParams1 As SldWorks.CurveParamData

          Dim SkLines As Variant

           

           

          Set Measure = Curr_Part.Extension.CreateMeasure

          'Set swMeasure = swModelDocExt.CreateMeasure

              'Can set this to 0

              ' 0 = center to center

              ' 1 = minimum distance

              ' 2 = maximum distanc

          Measure.ArcOption = 1

           

           

          Max_Area = 0

          Set swEnt1 = Face

           

           

          faceArr = swFeat.GetFaces: If IsEmpty(faceArr) Then Exit Function

          For Each oneFace In faceArr

              Set swFace = oneFace

              Set swEnt = swFace

           

           

              status = swEnt.Select4(False, swSelData): Debug.Assert (status)

              status = swEnt1.Select4(True, swSelData): Debug.Assert (status)

             

              'Now checking whether this face is parallel to the original face

              'Now measuring

              status = Measure.Calculate(Nothing)

              If (Measure.IsParallel) Then

                                     

                  Debug.Print "Is Parallel: " & Measure.IsParallel

                  ' If Parallel then get all its edges

                  Edge_Count = swFace.GetEdgeCount

                 

                  Set swLoop = swFace.GetFirstLoop

                  Dim len1 As Integer, len2 As Integer, len3 As Integer

           

           

                  'Getting all edges in one array

                  Do While Not swLoop Is Nothing

                 

                      edgeArr = swLoop.GetEdges()

                     

                      len1 = UBound(edgeArr)

                     

                      If IsEmpty(edgeArr1) Then

                         

                          len2 = -1

                          ReDim edgeArr1(len1)

                     

                      Else

                     

                          len2 = UBound(edgeArr1)

                          ReDim Preserve edgeArr1(len1 + (len2 + 1))

                     

                      End If

                     

                      len3 = UBound(edgeArr1)

           

           

                      For i = (len2 + 1) To UBound(edgeArr1)

                 

                          Set edgeArr1(i) = edgeArr((i - (len2 + 1)))

                 

                      Next i

                     

                      Set swLoop = swLoop.GetNext

                 

                  Loop

                  

           

           

              End If

                    

           

          Next

           

          'First checking whhich one of them are parallel and then chcking distance

          For i = 0 To UBound(edgeArr1)

           

           

              For j = i + 1 To UBound(edgeArr1)

           

           

                  Set swEdge1 = edgeArr1(i)

                  Set swEdge2 = edgeArr1(j)

           

           

           

           

                  'Now Selecting both of them

                  Set swEntity = swEdge1

                  Set swEntity1 = swEdge2

           

           

                  bRet = swEntity.Select4(False, swSelData)

                  bRet = swEntity1.Select4(True, swSelData)

           

           

                  'Now measuring

                  status = Measure.Calculate(Nothing)

                 

                  If (Measure.IsParallel) Then

                 

                      If (Not (Measure.Distance = -1)) Then

                          'Debug.Print "Length: " & Measure.Distance

                         

                          'Thin wall less than 250

                          If (Measure.Distance * 1000000) <= 250 Then

                             

                              Set swCurve = swEdge1.GetCurve

                              Set swCurve1 = swEdge2.GetCurve

                             

                              Set swCurveParams = swEdge1.GetCurveParams3

                              Set swCurveParams1 = swEdge2.GetCurveParams3

                             

                              Dim Line1_Pt() As Variant, Line2_Pt() As Variant

                              ReDim Line1_Pt(2)

                              ReDim Line2_Pt(2)

                             

                              Line1_Pt(0) = swCurveParams.StartPoint

                              Line1_Pt(1) = swCurveParams.EndPoint

                              Line2_Pt(0) = swCurveParams1.StartPoint

                              Line2_Pt(1) = swCurveParams1.EndPoint

                             

                              'Finding distance between them

                              Dim Pt_Dis() As Double, counter_chk As Integer

                              Dim Max_Dis As Double

                              Dim loc_num1 As Integer, loc_num2 As Integer

                              Dim Pt_Dis1 As Double, Pt_Dis2 As Double, Mid_X As Double, Mid_Y As Double

                             

                              ReDim Pt_Dis(4)

                             

          '                    Debug.Print "The start point x,y,z coordinates are: " & Line1_Pt(0)(0) & "," & Line1_Pt(0)(1) & "," & Line1_Pt(0)(2)

          '                    Debug.Print "The end point x,y,z coordinates are: " & Line1_Pt(1)(0) & "," & Line1_Pt(1)(1) & "," & Line1_Pt(1)(2)

          '                    Debug.Print "The start point x,y,z coordinates are: " & Line2_Pt(0)(0) & "," & Line2_Pt(0)(1) & "," & Line2_Pt(0)(2)

          '                    Debug.Print "The end point x,y,z coordinates are: " & Line2_Pt(1)(0) & "," & Line2_Pt(1)(1) & "," & Line2_Pt(1)(2)

                             

                              counter_chk = 0

                              Max_Dis = 0

                              For k = 0 To 1

                             

                                  For l = 0 To 1

                                 

                                      Pt_Dis(counter_chk) = Sqr((((Line1_Pt(k)(0) * 1000000) - (Line2_Pt(l)(0) * 1000000)) * ((Line1_Pt(k)(0) * 1000000) - (Line2_Pt(l)(0) * 1000000))) + (((Line1_Pt(k)(1) * 1000000) - (Line2_Pt(l)(1) * 1000000)) * ((Line1_Pt(k)(1) * 1000000) - (Line2_Pt(l)(1) * 1000000))))

                                     

                                      If Max_Dis < Pt_Dis(counter_chk) Then

                                 

                                          Max_Dis = Pt_Dis(counter_chk)

                                          loc_num1 = k

                                          loc_num2 = l

                                     

                                      End If

                                 

                                      counter_chk = counter_chk + 1

                                 

                                  Next l

                             

                              Next k

                             

                              loc_num1 = 1 - loc_num1

                              loc_num2 = 1 - loc_num2

                              Mid_X = ((Line1_Pt(loc_num1)(0) + Line2_Pt(loc_num2)(0)) / 2) * 1000000

                              Mid_Y = ((Line1_Pt(loc_num1)(1) + Line2_Pt(loc_num2)(1)) / 2) * 1000000

                             

                             

                              'Got the middle points now find the point closest to it

                              Pt_Dis1 = 0: Pt_Dis2 = 0

                             

                              Pt_Dis1 = Sqr(((Mid_X - (Line1_Pt(0)(0) * 1000000)) * (Mid_X - (Line1_Pt(0)(0) * 1000000))) + ((Mid_Y - (Line1_Pt(0)(1) * 1000000)) * (Mid_Y - (Line1_Pt(0)(1) * 1000000))))

                              Pt_Dis2 = Sqr(((Mid_X - (Line1_Pt(1)(0) * 1000000)) * (Mid_X - (Line1_Pt(1)(0) * 1000000))) + ((Mid_Y - (Line1_Pt(1)(1) * 1000000)) * (Mid_Y - (Line1_Pt(1)(1) * 1000000))))

                             

                              'Now checking the angle between them

                              Dim Angle As Double

                              If Pt_Dis1 < Pt_Dis2 Then

                             

                                  'A = (Mid_X - Line1_Pt(0)(0)), (Mid_Y - Line1_Pt(0)(1))

                                  'B = (Line1_Pt(1)(0) - Line1_Pt(0)(0)), (Line1_Pt(1)(1) - Line1_Pt(0)(1))

                                  'formual = A.B/|A||B|

                                 

                                  Angle = (((Mid_X - (Line1_Pt(0)(0) * 1000000)) * ((Line1_Pt(1)(0) * 1000000) - (Line1_Pt(0)(0) * 1000000))) + ((Mid_Y - (Line1_Pt(0)(1) * 1000000)) * ((Line1_Pt(1)(1) * 1000000) - (Line1_Pt(0)(1) * 1000000)))) / (Sqr(((Mid_X - (Line1_Pt(0)(0) * 1000000)) * (Mid_X - (Line1_Pt(0)(0) * 1000000))) + ((Mid_Y - (Line1_Pt(0)(1) * 1000000)) * (Mid_Y - (Line1_Pt(0)(1) * 1000000)))) * Sqr((((Line1_Pt(1)(0) * 1000000) - (Line1_Pt(0)(0) * 1000000)) * ((Line1_Pt(1)(0) * 1000000) - (Line1_Pt(0)(0) * 1000000))) + (((Line1_Pt(1)(1) * 1000000) - (Line1_Pt(0)(1) * 1000000)) * ((Line1_Pt(1)(1) * 1000000) - (Line1_Pt(0)(1) * 1000000)))))

                                 

                                 

                              Else

                             

                                  Angle = (((Mid_X - (Line1_Pt(1)(0) * 1000000)) * ((Line1_Pt(0)(0) * 1000000) - (Line1_Pt(1)(0) * 1000000))) + ((Mid_Y - (Line1_Pt(1)(1) * 1000000)) * ((Line1_Pt(0)(1) * 1000000) - (Line1_Pt(1)(1) * 1000000)))) / (Sqr(((Mid_X - (Line1_Pt(1)(0) * 1000000)) * (Mid_X - (Line1_Pt(1)(0) * 1000000))) + ((Mid_Y - (Line1_Pt(1)(1) * 1000000)) * (Mid_Y - (Line1_Pt(1)(1) * 1000000)))) * Sqr((((Line1_Pt(0)(0) * 1000000) - (Line1_Pt(1)(0) * 1000000)) * ((Line1_Pt(0)(0) * 1000000) - (Line1_Pt(1)(0) * 1000000))) + (((Line1_Pt(0)(1) * 1000000) - (Line1_Pt(1)(1) * 1000000)) * ((Line1_Pt(0)(1) * 1000000) - (Line1_Pt(1)(1) * 1000000)))))

           

           

             

                              End If

                             

                              If Angle > 1 Or Angle < 0.01 Then GoTo NextIteration

                             

                              'End If

                             

                              'Now checking if one is encompassed by other

                              'Find the length of each and mid point

                              Dim chk_len As Double, chk_len1 As Double, Angle1 As Double, Angle2 As Double

                              Dim Ax1 As Double, Ay1 As Double, Bx1 As Double, By1 As Double

                              Dim Ax2 As Double, Ay2 As Double, Bx2 As Double, By As Double

                             

                              chk_len = swCurve.GetLength3(swCurveParams.UMinValue, swCurveParams.UMaxValue)

                              chk_len1 = swCurve1.GetLength3(swCurveParams1.UMinValue, swCurveParams.UMaxValue)

                             

                              If (chk_len < chk_len1) Then

                             

                                  'This is case whre line 1 is smaller then the second

                                  'formual = A.B/|A||B|

                                  Ax1 = ((Line1_Pt(loc_num1)(0) * 1000000) - (Line2_Pt((1 - loc_num2))(0) * 1000000)): Ay1 = ((Line1_Pt(loc_num1)(1) * 1000000) - (Line2_Pt((1 - loc_num2))(1) * 1000000))

                                  Bx1 = ((Line2_Pt(loc_num2)(0) * 1000000) - (Line2_Pt((1 - loc_num2))(0) * 1000000)): By1 = ((Line2_Pt(loc_num2)(1) * 1000000) - (Line2_Pt((1 - loc_num2))(1) * 1000000))

                                  Ax2 = ((Line1_Pt((1 - loc_num1))(0) * 1000000) - (Line2_Pt(loc_num2)(0) * 1000000)): Ay2 = ((Line1_Pt((1 - loc_num1))(1) * 1000000) - (Line2_Pt(loc_num2)(1) * 1000000))

                                  Bx2 = ((Line2_Pt((1 - loc_num2))(0) * 1000000) - (Line2_Pt(loc_num2)(0) * 1000000)): By2 = ((Line2_Pt((1 - loc_num2))(1) * 1000000) - (Line2_Pt(1 - loc_num2)(1) * 1000000))

                                 

                                  Angle1 = ((Ax1 * Bx1) + (Ay1 * By1)) / (Sqr((Ax1 * Ax1) + (Ay1 * Ay1)) * Sqr((Bx1 * Bx1) + (By1 * By1)))

                                  Angle2 = ((Ax2 * Bx2) + (Ay2 * By2)) / (Sqr((Ax2 * Ax2) + (Ay2 * Ay2)) * Sqr((Bx2 * Bx2) + (By2 * By2)))

                 

           

           

                              Else

                             

                                  'This is case whre line 2 is smaller then the second

                                  'formual = A.B/|A||B|

                                  Ax1 = ((Line2_Pt(loc_num2)(0) * 1000000) - (Line1_Pt((1 - loc_num1))(0) * 1000000)): Ay1 = ((Line2_Pt(loc_num2)(1) * 1000000) - (Line1_Pt((1 - loc_num1))(1) * 1000000))

                                  Bx1 = ((Line1_Pt(loc_num1)(0) * 1000000) - (Line1_Pt((1 - loc_num1))(0) * 1000000)): By1 = ((Line1_Pt(loc_num1)(1) * 1000000) - (Line1_Pt((1 - loc_num1))(1) * 1000000))

                                  Ax2 = ((Line2_Pt((1 - loc_num2))(0) * 1000000) - (Line1_Pt(loc_num1)(0) * 1000000)): Ay2 = ((Line2_Pt((1 - loc_num2))(1) * 1000000) - (Line1_Pt(loc_num1)(1) * 1000000))

                                  Bx2 = ((Line1_Pt((1 - loc_num1))(0) * 1000000) - (Line1_Pt(loc_num1)(0) * 1000000)): By2 = ((Line1_Pt((1 - loc_num1))(1) * 1000000) - (Line1_Pt(1 - loc_num1)(1) * 1000000))

                                 

                                 

                                  Angle1 = ((Ax1 * Bx1) + (Ay1 * By1)) / (Sqr((Ax1 * Ax1) + (Ay1 * Ay1)) * Sqr((Bx1 * Bx1) + (By1 * By1)))

                                  Angle2 = ((Ax2 * Bx2) + (Ay2 * By2)) / (Sqr((Ax2 * Ax2) + (Ay2 * Ay2)) * Sqr((Bx2 * Bx2) + (By2 * By2)))

           

           

                              End If

                             

                              status = swEnt1.Select4(False, swSelData): Debug.Assert (status)

                             

                              If (Angle1 < 1 And Angle2 < 1) And (Angle1 >= 0 And Angle2 >= 0) Then

                             

                                  'Now make the full rectangle

                                  If (chk_len < chk_len1) Then

                                 

                                      If Round(Line1_Pt(0)(0), 5) = Round(Line1_Pt(1)(0), 5) Then

                                     

                                          Mid_X = ((Line1_Pt(0)(0) + Line2_Pt(0)(0)) / 2) * 1000000

                                          Mid_Y = ((Line1_Pt(0)(1) + Line1_Pt(1)(1)) / 2) * 1000000

                                     

                                      ElseIf Round(Line1_Pt(0)(1), 5) = Round(Line1_Pt(1)(1), 5) Then

                                     

                                          Mid_X = ((Line1_Pt(0)(0) + Line1_Pt(1)(0)) / 2) * 1000000

                                          Mid_Y = ((Line1_Pt(0)(1) + Line2_Pt(0)(1)) / 2) * 1000000

                                     

                                      End If

                                     

                                       SkLines = SketchMgr.CreateCenterRectangle((Mid_X / 1000000), (Mid_Y / 1000000), 0, Line1_Pt(0)(0), Line1_Pt(0)(1), 0)

                                     

                                  Else

                                 

                                      If Round(Line2_Pt(0)(0), 5) = Round(Line2_Pt(1)(0), 5) Then

                                     

                                          Mid_X = ((Line1_Pt(0)(0) + Line2_Pt(0)(0)) / 2) * 1000000

                                          Mid_Y = ((Line2_Pt(0)(1) + Line2_Pt(1)(1)) / 2) * 1000000

                                     

                                      ElseIf Round(Line2_Pt(0)(1), 5) = Round(Line2_Pt(1)(1), 5) Then

                                     

                                          Mid_X = ((Line2_Pt(0)(0) + Line2_Pt(1)(0)) / 2) * 1000000

                                          Mid_Y = ((Line1_Pt(0)(1) + Line2_Pt(0)(1)) / 2) * 1000000

                                     

                                      End If

                                     

                                       SkLines = SketchMgr.CreateCenterRectangle((Mid_X / 1000000), (Mid_Y / 1000000), 0, Line2_Pt(0)(0), Line2_Pt(0)(1), 0)

                                 

                                 

                                  End If

                             

                              Else

                             

                                  If Pt_Dis1 < Pt_Dis2 Then

                             

                                      SkLines = SketchMgr.CreateCenterRectangle((Mid_X / 1000000), (Mid_Y / 1000000), 0, Line1_Pt(0)(0), Line1_Pt(0)(1), 0)

                                 

                                  Else

                             

                                      SkLines = SketchMgr.CreateCenterRectangle((Mid_X / 1000000), (Mid_Y / 1000000), 0, Line1_Pt(1)(0), Line1_Pt(1)(1), 0)

           

           

             

                                  End If

                              End If

                             

                         

                          End If

                         

                      End If

                 

                  End If

                 

          NextIteration:

              Next j

           

           

          Next i

                 

                 

          Dim Feature As SldWorks.Feature

          Set FeatureManager = Curr_Part.FeatureManager

           

           

          boolstatus = Select_All_Contours(Curr_Part)

           

           

          ' Now Doing an extrude cut f- for now the cut will be 0.5 mm

          Set Feature = FeatureManager.FeatureCut3(True, False, False, swEndCondBlind, swEndCondBlind, 1.55 / 1000, 1.55 / 1000, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, False, True, True, False, False, False, swStartSketchPlane, 0, False)

             

          End Function