Move Rollback bar to the top of the Feature Manager

I want to add reference geometry to a large number of parts via a macro. I want to add this above any features, so I am trying to move the Rollback bar to the very top of the feature manager.

There is a nice option in the API that allows you to move the rollback bar all of the way to the end.

bRetVal = featMgr.EditRollback(swMoveRollbackBarToEnd, 0)

What I don’t see is an easy way to roll it up to the start. It looks like the only way to do this is to get the list of features, and then roll back to before the first feature using: swMoveRollbackBarToBeforeFeature

Does anyone know of an easier way to do this? I tried moving to after the feature “Origin” but that didn’t work.

EditRollback doesn’t seem to understand the origin. You can use TreeControlItem to quickly traverse the top of the tree until you get to the origin, move one feature forward and then use EditRollback with the swMoveRollbackBarToBeforeFeature parameter. It’s a little hokey but it works:

Dim swApp As SldWorks.SldWorks
Dim mDoc As ModelDoc2
Dim featMgr As FeatureManager
Dim ti As TreeControlItem
Dim origin As Feature
Dim firstFeat As Feature

Sub main()
    Set swApp = Application.SldWorks
    Set mDoc = swApp.ActiveDoc
    Set featMgr = mDoc.FeatureManager
    Set ti = featMgr.GetFeatureTreeRootItem2(swFeatMgrPaneTop)
    Set ti = ti.GetFirstChild
    Do While ti.Text <> "Origin"
        Set ti = ti.GetNext
    Loop
    Set firstFeat = ti.GetNext.Object ' This is the first feature after the origin
    featMgr.EditRollback swMoveRollbackBarTo_e.swMoveRollbackBarToBeforeFeature, firstFeat.Name
End Sub
3 Likes

Can I trust that the Origin will always be the 17th feature in the feature manager? I know the proper way to do this would be to parse the list, but I’m looking for the easy button. :slight_smile:

No, you can’t. You also can’t trust that it will be named “Origin”. It can be renamed by the user, also it will definitely be a different name if the file was created in an install using a different language. However, you can trust that the feature type name will be “OriginProfileFeature”.

But how dang easy do you want? This is only a handful of lines of code…

Dim swApp As Object
Sub main()

  Set swApp = Application.SldWorks
  Dim swFeat As SldWorks.Feature
  Dim swDoc As SldWorks.ModelDoc2
  
  Set swDoc = swApp.ActiveDoc
  Set swFeat = swDoc.FirstFeature
  While Not swFeat Is Nothing And Not swFeat.GetTypeName2 = "OriginProfileFeature"
      Debug.Print swFeat.Name, swFeat.GetTypeName2
      Set swFeat = swFeat.GetNextFeature
  Wend
  Set swFeat = swFeat.GetNextFeature
  If Not swFeat Is Nothing Then
      MsgBox "The feature after the origin is " & swFeat.Name
      swDoc.FeatureManager.EditRollback swMoveRollbackBarToBeforeFeature, swFeat.Name
  End If

End Sub

2 Likes

I was typing my reply and hit send before seeing Jim’s answer.

Sub RollbackFirst()
    Dim d As ModelDoc2, f As Feature
    Set d = Application.SldWorks.ActiveDoc: If d Is Nothing Then Exit Sub
    Set f = d.FirstFeature: Do While f.GetTypeName2 <> "OriginProfileFeature": Set f = f.GetNextFeature: Loop
    Set f = f.GetNextFeature: If Not f Is Nothing Then d.FeatureManager.EditRollback swMoveRollbackBarToBeforeFeature, f.Name
End Sub
2 Likes

It might be best for you not to play with the Tree Back and Forth. But to create your features at the end of the tree (if there is a FlatPattern, it will be more at the end of everything) and pass them (Feture Object) to the move function, which will move them Up without searching for this place.

' Demonstration procedure
Sub Demo_CreateAndMove()
    Dim d As ModelDoc2, feat As Feature
    Set d = Application.SldWorks.ActiveDoc: If d Is Nothing Then Exit Sub
    
    Set feat = CreateBossOnTop(d, 0.1, 0.01) ' D100 mm, Extrude 10 mm (in m)
    If Not feat Is Nothing Then
        If Not MoveFeatureToTop(d, feat) Then Debug.Print "MoveFeatureToTop failed for: "; feat.Name
    End If
End Sub

' Move before Origin (to the very top of the tree)
Function MoveFeatureToTop(d As ModelDoc2, f As Feature) As Boolean
    If f Is Nothing Then Exit Function
    MoveFeatureToTop = d.Extension.ReorderFeature(f.Name, "", swMoveToTop) ' swMoveToTop = 4
End Function


' ========== AUXILIARY ===============
'Auxiliary functions that for a specific example create an elongated round boss on the plane Above.
Function CreateBossOnTop(d As ModelDoc2, ByVal dia As Double, ByVal depth As Double) As Feature
    Const swEndCondBlind As Long = 0
    Dim sm As SketchManager, fm As FeatureManager
    Dim ok As Boolean, r As Double
    Set sm = d.SketchManager: Set fm = d.FeatureManager
    r = dia / 2#
    
    d.ClearSelection2 True
    ok = SelectTopPlaneOrFirst(d)
    If Not ok Then Exit Function
    
    sm.InsertSketch True
    sm.CreateCircleByRadius 0#, 0#, 0#, r
    sm.InsertSketch True
    
    ' Simple Blind Draw, Merge Bodies = True
    Set CreateBossOnTop = fm.FeatureExtrusion2(True, False, False, swEndCondBlind, swEndCondBlind, _
                                               depth, 0#, False, False, False, False, 0#, 0#, _
                                               False, False, False, False, True, True, True, 0, 0, False)
End Function

' Select Top Plane for example
Private Function SelectTopPlaneOrFirst(d As ModelDoc2) As Boolean
    Dim ext As ModelDocExtension: Set ext = d.Extension
    Dim names As Variant, i As Long
    names = Array("Top", "Up") 'Or your super original name
    For i = LBound(names) To UBound(names)
        If ext.SelectByID2(names(i), "PLANE", 0, 0, 0, False, 0, Nothing, 0) Then
            SelectTopPlaneOrFirst = True
            Exit Function
        End If
    Next i
End Function

That is how my macro works now, but it isn’t always successful moving the new folder to the top of the tree.

1 Like

There is a new complication. For this part (which is a fastener from McMaster Carr) there is another feature which isn’t recognized by the EditRollBack call after the origin.

Ok, try this. I think it might also be the “easiest” :smiley: :

    Sub main()
        Dim swFeat As SldWorks.Feature
        Dim swFmgr As SldWorks.FeatureManager
        Set swFeat = Application.SldWorks.ActiveDoc.FirstFeature
        Set swFmgr = Application.SldWorks.ActiveDoc.FeatureManager
        Do While Not (swFeat Is Nothing)
            If swFmgr.EditRollback(swMoveRollbackBarToBeforeFeature, swFeat.Name) Then Exit Do
            Set swFeat = swFeat.GetNextFeature
        Loop
        If swFeat Is Nothing Then
            MsgBox "Failed to move Rollback Bar before reaching end of feature tree."
        Else
            MsgBox "Moved rollback bar before " & swFeat.Name
        End If
    End Sub
2 Likes

So… You mentioned McMaster-Carr parts, so I tried downloading one. The code above actually reports that it moved the bar before the “Lights and Cameras” folder. But it doesn’t. In trying to examine the folder, it seems to be somewhat corrupt. Or maybe SW has a bug? I can’t seem to show the folder on any model using “Hide/Show Tree Items”. However, with this McMaster part, I added a light and now Lights/Cameras doesn’t even show up in the “Hidden Tree Items”.

But anyway… All that to say that I added a check to verify that the bar really did get moved.

    Sub main()
        Dim swFeat As SldWorks.Feature
        Dim swFmgr As SldWorks.FeatureManager
        Set swFeat = Application.SldWorks.ActiveDoc.FirstFeature
        Set swFmgr = Application.SldWorks.ActiveDoc.FeatureManager
        Do While Not (swFeat Is Nothing)
            If swFmgr.EditRollback(swMoveRollbackBarToBeforeFeature, swFeat.Name) Then
                If swFeat.IsRolledBack Then Exit Do
            End If
            Set swFeat = swFeat.GetNextFeature
        Loop
        If swFeat Is Nothing Then
            MsgBox "Failed to move Rollback Bar before reaching end of feature tree."
        Else
            MsgBox "Moved rollback bar before " & swFeat.Name
        End If
    End Sub
1 Like

I am seeing the same behavior. It jumps out of the loop on “lights and cameras”, but doesn’t move the rollback bar.

It does seem to work on parts created with our template. I can live with the McMaster parts not getting upgraded.

Thanks for your assistance with this. I thought I was going to spend 30 minutes writing a quick macro. I didn’t anticipate it being quite so involved.

The updated code I posted adds a check to verify that the bar got moved. It’s just a couple extra lines and it fixes the “lights and cameras” thing, at least for the one part I downloaded.

2 Likes