3 Replies Latest reply on May 31, 2016 9:05 AM by Evan Dlugopolski

    Get length of bend line with VBA

    Evan Dlugopolski

      Hello All,


      I already have a post about this but I am re-posting in hopes to reach more people.


      Am trying to create a macro that will retrieve the length of each bend line (as shown below) in a sheet metal part.



      Would anyone know how to achieve this?

      Thank you,


        • Re: Get length of bend line with VBA
          Amen Allah Jlili

          For some reason, I couldn't get this to wokr using IsBendLine.

          I think this following approach will work fine. I wonder though if the sketch segments are ordered in the same the bends are.
          Let me know if this helps!

          ' jliliamen@gmail.com
          Dim swApp As Object
          Dim swModelDoc As SldWorks.ModelDoc2
          Dim swSelectionManager As SelectionMgr
          Dim swFeatureManager As FeatureManager
          Dim Features As Variant
          Dim swFeature As Feature
          Sub main()
          Set swApp = Application.SldWorks
          Set swModelDoc = swApp.ActiveDoc
          Set swFeatureManager = swModelDoc.FeatureManager
           Features = swFeatureManager.GetFeatures(False)
          For i = LBound(Features) To UBound(Features)
          Set swFeature = Features(i)
          If swFeature.GetTypeName2 = "ProfileFeature" Then
          Dim swSketch As Sketch
          Set swSketch = swFeature.GetSpecificFeature2
          Dim SketchLines As Variant
          SketchSegs = swSketch.GetSketchSegments
          Dim swSketchSeg As SketchSegment
          For j = LBound(SketchSegs) To UBound(SketchSegs)
          Set swSketchSeg = SketchSegs(j)
          If swSketchSeg.ConstructionGeometry = False Then
          Debug.Print "Length of bend #" & j + 1 & " : " & Round(swSketchSeg.GetLength * 1000, 2) & " mm"
          End If
          Next j
          Exit For
          End If
          Next 'i
          End Sub




          It's working on my end.

          Happy weekend!

          • Re: Get length of bend line with VBA
            Ivana Kolin

            Here is it using isBendLine

            Option Explicit
            Sub processSketch(swSketch As SldWorks.Sketch)
                Dim vSketchSeg                                As Variant
                Dim swSketchSeg As SldWorks.SketchSegment
                Dim vSketchSegID                              As Variant
                Dim i                                         As Long
                Dim bRet                                      As Boolean
                vSketchSeg = swSketch.GetSketchSegments
                For i = 0 To UBound(vSketchSeg)
                    Set swSketchSeg = vSketchSeg(i)
                    If swSketchSeg.IsBendLine Then
                        vSketchSegID = swSketchSeg.GetID
                        Debug.Print "    SketchSegID(" & i & ") = [" & vSketchSegID(0) & ", " & vSketchSegID(1) & "]"
                        Debug.Print "      Type       = " & swSketchSeg.GetType
                        Debug.Print "      Length     = " & swSketchSeg.GetLength
                    End If
            End Sub
            Sub TraverseFeatureFeatures(swFeat As SldWorks.Feature, nLevel As Long)
                Dim swSubFeat                                 As SldWorks.Feature
                Dim sPadStr                                   As String
                Dim i                                         As Long
                For i = 0 To nLevel
                    sPadStr = sPadStr + "  "
                Next i
                While Not swFeat Is Nothing
                    If swFeat.GetTypeName2 = "FlatPattern" Then
                        Set swSubFeat = swFeat.GetFirstSubFeature
                        While Not swSubFeat Is Nothing
                            If swSubFeat.GetTypeName = "ProfileFeature" Then
                                Dim swSketch                  As SldWorks.Sketch
                                Set swSketch = swSubFeat.GetSpecificFeature2
                                processSketch swSketch
                            End If
                            Set swSubFeat = swSubFeat.GetNextSubFeature()
                    End If
                    Set swFeat = swFeat.GetNextFeature
            End Sub
            Sub TraverseModelFeatures(swModel As SldWorks.ModelDoc2, nLevel As Long)
                Dim swFeat                                    As SldWorks.Feature
                Set swFeat = swModel.FirstFeature
                TraverseFeatureFeatures swFeat, nLevel
            End Sub
            Sub main()
                Dim swApp                                     As SldWorks.SldWorks
                Dim swModel                                   As SldWorks.ModelDoc2
                Dim swConf                                    As SldWorks.Configuration
                Dim swRootComp                                As SldWorks.Component2
                Dim nStart                                    As Long
                Set swApp = CreateObject("SldWorks.Application")
                ' Open assembly
                Set swModel = swApp.ActiveDoc
                Set swConf = swModel.GetActiveConfiguration
                Set swRootComp = swConf.GetRootComponent3(True)
                nStart = Timer
                ' Traverse components and features
                TraverseModelFeatures swModel, 1
            End Sub
            • Re: Get length of bend line with VBA
              Evan Dlugopolski

              Both of these codes work very well.  My next challenge is to use the lengths of each bend line to do a calculation and change the bend radius info.  Radius and k-factor to be specific.  The only problem I see is that the bend lines do not correspond to the bend features.  Do either of you see a way to update the bend radius & kfactor based on the length of its bend line?