I am having trouble selecting the first Component in an Assembly Feature Tree. I have an assembly with a handful of parts in it. The following code selects a component but it is often not the first component in the tree, that is it’s not the first part directly under “origin” but is usually a different component lower down the tree.
The first feature could not necessarily be a component, but any folder like comments, history, etc.
To select the first component, get the array of all the components in the assembly, and compare each feature with the component name. And once it matches, then select the desired component.
You can traverse the tree in order using FeatureManager::GetFeatureTreeRootItem2. A macro to select the first component in an assembly would look something like this:
Dim swApp As SldWorks.SldWorks
Dim mDoc As ModelDoc2
Dim rootItem As TreeControlItem
Dim nextItem As TreeControlItem
Dim comp As Component2
Dim featType As swTreeControlItemType_e
Dim featMgr As FeatureManager
Sub main()
Set swApp = Application.SldWorks
Set mDoc = swApp.ActiveDoc
Set featMgr = mDoc.FeatureManager
Set rootItem = featMgr.GetFeatureTreeRootItem2(swFeatMgrPane_e.swFeatMgrPaneTop)
Set nextItem = rootItem.GetFirstChild
featType = nextItem.ObjectType
Do While (Not nextItem Is Nothing) And (featType <> swTreeControlItemType_e.swFeatureManagerItem_Component)
Set nextItem = nextItem.GetNext
If Not nextItem Is Nothing Then
featType = nextItem.ObjectType
End If
Loop
If nextItem Is Nothing Then
Debug.Print ("No components in model")
Else
Set comp = nextItem.Object
comp.Select4 False, Nothing, False
End If
End Sub
Thank you for your replies. For some unrelated reason, my code was selecting feature items ‘at random’ and i got it sorted out. I did find a faster way of finding the first folder or assy/part than traversing the whole tree. Sharing my code here FYI:
'This code will find the first Folder or Assy/Part in the feature tree then select it
swModel.ClearSelection2 True
Set swFeature = swModel.FirstFeature
While Not swFeature Is Nothing And swFeature.GetTypeName2 <> "Reference" And swFeature.GetTypeName2 <> "FtrFolder"
Set swFeature = swFeature.GetNextFeature
If swFeature Is Nothing Then
Debug.Print "There are no Folders or Components."
Exit Sub
Else
Debug.Print swFeature.Name & Chr(9) & swFeature.GetTypeName2
End If
Wend
If swFeature.GetTypeName2 = "Reference" Then
BoolStatus = swModelExt.SelectByID2(swFeature.Name, "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
ElseIf swFeature.GetTypeName2 = "FtrFolder" Then
BoolStatus = swModelExt.SelectByID2(swFeature.Name, "FTRFOLDER", 0, 0, 0, True, 0, Nothing, 0)
End If
EDIT: My code chokes if there is no folder or component…
Thanks, JSculley. After reading your post again, it looks like the code I just does exactly that same thing that you suggested, just using different methods. Thank you for sharing your code.
FYI, I fixed my code so it doesn’t error out if there is an empty feature tree:
swModel.ClearSelection2 True
Set swFeature = swModel.FirstFeature
Do
Debug.Print swFeature.Name & Chr(9) & swFeature.GetTypeName2
If swFeature.GetTypeName2 = "Reference" Or swFeature.GetTypeName2 = "FtrFolder" Then
Exit Do
Else
Set swFeature = swFeature.GetNextFeature
End If
Loop Until swFeature Is Nothing
If swFeature Is Nothing Then
MsgBox "There are no Folders or Components. Exiting"
Exit Sub
End If
If swFeature.GetTypeName2 = "Reference" Then
BoolStatus = swModelExt.SelectByID2(swFeature.Name, "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
ElseIf swFeature.GetTypeName2 = "FtrFolder" Then
BoolStatus = swModelExt.SelectByID2(swFeature.Name, "FTRFOLDER", 0, 0, 0, True, 0, Nothing, 0)
End If