4 Replies Latest reply on Apr 9, 2018 9:59 PM by Vincent Li

    Traverse drawing feature tree problem

    Vincent Li

      Hi Guys,


      when I use VBA Macro to traverse the drawing feature tree, I found the anchor feature that under the "sheet format" could not be read.


      what are these object called in SW Drawing API ?


      as the picture shows, we know the feature, subfeature, then how to call the anchor ones below "Sheet foramt"??



          • Re: Traverse drawing feature tree problem
            Vincent Li

            Hi Artem,


            thank you , and actually, I want to rename all the features in the drawing feature manager. and I add a line to rename, however, it pop out an error message,  I really don't how it work , pls help to point out the problem of the code after I adding a that line in red marked below.


            --------------------------The Code--------------------------------------------------


            Option Explicit



            Dim traverseLevel As Integer



            Sub main()

                Dim swApp As SldWorks.SldWorks

                Dim myModel As SldWorks.ModelDoc2

                Dim featureMgr As SldWorks.FeatureManager

                Dim rootNode As SldWorks.TreeControlItem


                Set swApp = Application.SldWorks

                Set myModel = swApp.ActiveDoc

                Set featureMgr = myModel.FeatureManager

                Set rootNode = featureMgr.GetFeatureTreeRootItem()


                If Not rootNode Is Nothing Then


                    traverseLevel = 0

                    traverse_node rootNode

                End If

            End Sub

            Private Sub traverse_node(node As SldWorks.TreeControlItem)


                Dim childNode As SldWorks.TreeControlItem

                Dim featureNode As SldWorks.Feature

                Dim componentNode As SldWorks.Component2

                Dim nodeObjectType As Long

                Dim nodeObject As Object

                Dim restOfString As String

                Dim indent As String

                Dim i As Integer

                Dim displayNodeInfo As Boolean

                Dim compName As String

                Dim suppr As Long, supprString As String

                Dim vis As Long, visString As String

                Dim fixed As Boolean, fixedString As String

                Dim componentDoc As Object, docString As String

                Dim refConfigName As String


                displayNodeInfo = False

                nodeObjectType = node.ObjectType

              nodeObject = node.Object


                Select Case nodeObjectType


                Case SwConst.swTreeControlItemType_e.swFeatureManagerItem_Feature:



                    displayNodeInfo = True



                    If Not nodeObject Is Nothing Then



                        Set featureNode = nodeObject



                        restOfString = "[FEATURE: " & featureNode.Name & "]"


                        'Try to rename the feature under the sheetforamt.

                        featureNode.Name = "ABC-" & Str(Int(Rnd * 100))






                        restOfString = "[FEATURE: object Null?!]"



                    End If





                Case Else:



                    displayNodeInfo = True



                    If Not nodeObject Is Nothing Then



                        restOfString = "[object type not handled]"






                        restOfString = "[object Null?!]"



                    End If



                End Select





                For i = 1 To traverseLevel



                    indent = indent & "  "



                Next i



                If (displayNodeInfo) Then



                    Debug.Print indent & node.Text & " : " & restOfString



                End If




                traverseLevel = traverseLevel + 1



                Set childNode = node.GetFirstChild()



                While Not childNode Is Nothing



                    traverse_node childNode



                    Set childNode = childNode.GetNext






                traverseLevel = traverseLevel - 1



            End Sub




              • Re: Traverse drawing feature tree problem
                Artem Taturevych

                I see the issue. You cannot modify the node while traversing as it breaks the iterator so you need to collect the features a and later modify them. Please take a look at the sample below:


                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swFeatMgr As SldWorks.FeatureManager




                Sub main()



                    Set swApp = Application.SldWorks


                    Set swModel = swApp.ActiveDoc


                    Set swFeatMgr = swModel.FeatureManager



                    Dim swRootNode As SldWorks.TreeControlItem

                    Set swRootNode = swFeatMgr.GetFeatureTreeRootItem()


                    Dim swFeatsColl As Collection

                    Set swFeatsColl = New Collection


                    TraverseNode swRootNode, swFeatsColl


                    Dim i As Integer


                    For i = 1 To swFeatsColl.Count

                        Dim swFeat As SldWorks.Feature

                        Set swFeat = swFeatsColl.Item(i)

                        swFeat.Name = swFeat.Name & CStr(CInt(Rnd * 100))



                End Sub




                Sub TraverseNode(node As SldWorks.TreeControlItem, featsColl As Collection)


                    Dim swChildNode As SldWorks.TreeControlItem


                    Set swChildNode = node.GetFirstChild


                    While Not swChildNode Is Nothing


                        Dim swNextChildNode As SldWorks.TreeControlItem

                        Set swNextChildNode = swChildNode.GetNext


                        TraverseNode swChildNode, featsColl


                        Set swChildNode = swNextChildNode



                    If node.ObjectType = swTreeControlItemType_e.swFeatureManagerItem_Feature Then

                        Dim swFeat As SldWorks.Feature

                        Set swFeat = node.Object

                        featsColl.Add swFeat

                    End If


                End Sub