2 Replies Latest reply on Dec 7, 2018 4:46 AM by Ronny Engmann

    API: expand selected sketch in FM tree

    Ronny Engmann



      I'm selecting a sketch via API and I want to have it expanded in the FM tree just like SOLIDWORKS does when you select the sketch via breadcrumbs:



      You can see that the parent features incl. folders and subfolders will be expanded.


      Unfortunately that doesn't happen via API (ModelDocExtension::SelectByID2). I tried to use FeatureManager::ExpandFeature but depending on the "WhichPane" parameter it will do that either in the wrong FM pane or only expand the "History" folder.


      Is there any workaround? The only thing that I have in mind is to work with nodes (FeatureManager::GetFeatureTreeRootItem2 and TreeControlItem::Expanded). Do you have any better solution / suggestion?


      Attached you'll find the test macro and an example part.


      Thanks in advance.


      Kind regards,


        • Re: API: expand selected sketch in FM tree
          Fifi Riri

          Hello, Try this


          Option Explicit

          Dim traverseLevel As Integer

          Dim mySketchName As String

          Sub main()

              Dim i As Integer

              Dim swApp As SldWorks.SldWorks

              Dim myModel As SldWorks.ModelDoc2

              Dim featureMgr As SldWorks.FeatureManager

              Dim rootNode As SldWorks.TreeControlItem

              Dim swSelMgr As SldWorks.SelectionMgr


              Dim boolstatus As Boolean

              Set swApp = Application.SldWorks

              Set myModel = swApp.ActiveDoc

              Set swSelMgr = myModel.SelectionManager

              boolstatus = myModel.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)


              Dim swFeat As SldWorks.Feature

              Set swFeat = swSelMgr.GetSelectedObject5(1)

              mySketchName = swFeat.Name


              Set featureMgr = myModel.FeatureManager

              Set rootNode = featureMgr.GetFeatureTreeRootItem2(swFeatMgrPaneBottom)

              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 parentNode As SldWorks.TreeControlItem

          Dim i As Integer

          If node.ObjectType = SwConst.swTreeControlItemType_e.swFeatureManagerItem_Feature Then

              If Not node.Object Is Nothing Then

                  Set featureNode = node.Object

                  Debug.Print featureNode.Name & "  " & featureNode.GetTypeName2

                  If featureNode.GetTypeName2 = "HistoryFolder" Then Exit Sub

                  If featureNode.Name = mySketchName Then

                      Set parentNode = node.GetParent

                      For i = 0 To traverseLevel - 2

                          parentNode.Expanded = True

                          Set parentNode = parentNode.GetParent


                  End If

              End If

          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: API: expand selected sketch in FM tree
              Ronny Engmann



              thank you very much. That works fine for my purpose even if more nodes will be expanded hen necessary while expanding the parent nodes. I just had to temporarily disable the option "Scroll selected item into view" (swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swFeatureManagerEnsureVisible, bExpFMTree) because the history folder will be expanded when selecting the sketch sometimes.


              Thanks for your help.

              1 person found this helpful