1 Reply Latest reply on Jan 15, 2016 2:26 AM by Andreas Killer

    How to count hole number include half hole

    Yong Ning

      Follow code, count cut whole hole is 31. not halt hole.

       

      Need calculte cut whole  hole is 36 include half hole

       

       

      10.jpg

       

      Dim swApp As SldWorks.SldWorks

      Dim swModel As SldWorks.ModelDoc2

      Dim swSelMgr As SldWorks.SelectionMgr

      Dim swFeat As SldWorks.Feature

      Dim swSketch As SldWorks.Sketch

       

      Sub main()

       

       

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          Dim swFace As SldWorks.Face2

          Set swFace = swSelMgr.GetSelectedObject5(1)

          Dim vLoops As Variant

          Dim i As Integer

          Dim totalHolesCount As Integer, vCount

       

          vCount = swFace.GetLoopCount - 1

       

       

          Dim SwLoop As Loop2

          Dim vEdges As Variant

            Set SwLoop = swFace.GetFirstLoop

            Do While Not SwLoop Is Nothing

           

              If Not SwLoop.IsOuter Then

                  vEdges = SwLoop.GetEdges

                  If UBound(vEdges) = 0 Then

                      Dim swEdge As SldWorks.Edge

                      Set swEdge = vEdges(0)

                      Dim swCurve As SldWorks.Curve

                      Set swCurve = swEdge.GetCurve

                      'If swCurve.IsCircle Then

                          totalHolesCount = totalHolesCount + 1

                      'End If

                  End If

              End If

              Set SwLoop = SwLoop.GetNext

          Loop

       

          MsgBox "Total holes count: " & totalHolesCount

       

      End Sub

       

       

      How to calculate cut hole number with API? | SOLIDWORKS Forums

      https://forum.solidworks.com/message/340498#340498

        • Re: How to count hole number include half hole
          Andreas Killer

          Based on your idea, you can count all circles on the selected face, but you have to include the outside edge, because 5 of your holes are there.

           

          Unfortunately this count includes also the circle from the base extrude. So you have to subtract 1 manually in this case.

           

          It is not possible to exclude that circle automatically, except you do a lot of calculations if one circle is inside of an other. Sounds simple, but it isn't, think of a rectangle with rounded vertices.

           

          Andreas.

           

          Sub CountCircles()
              Dim swApp As SldWorks.SldWorks
              Dim swModel As SldWorks.ModelDoc2
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swFace As SldWorks.Face2
              Dim swLoop As SldWorks.Loop2
              Dim swEdge As SldWorks.Edge
              Dim swCurve As SldWorks.Curve
              Dim i As Long, Count As Long
              Dim vEdges
             
              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swSelMgr = swModel.SelectionManager
              On Error Resume Next
              Set swFace = swSelMgr.GetSelectedObject6(1, -1)
              If swFace Is Nothing Then
                MsgBox "Select a face and try again"
                Exit Sub
              End If
              On Error GoTo 0
             
              Set swLoop = swFace.GetFirstLoop
              Do While Not swLoop Is Nothing
                vEdges = swLoop.GetEdges
                For i = LBound(vEdges) To UBound(vEdges)
                  Set swEdge = vEdges(i)
                  Set swCurve = swEdge.GetCurve
                  If swCurve.IsCircle Then
                    Count = Count + 1
                  End If
                Next
                Set swLoop = swLoop.GetNext
              Loop
              MsgBox "Circles: " & Count
          End Sub