AnsweredAssumed Answered

How to access component objects from drawing?

Question asked by Stefan Hoffmann on Mar 25, 2020
Latest reply on Mar 30, 2020 by Stefan Hoffmann

Hello everybody,


Firstly, let me thank you for all the awsome help I have gotten here so far as a passive reader.

However, I am now stuck with a problem I cant't for the love of it find an answer to:


Is there a way to access the objects of the components of an assembly from the drawing by using the way below?

I have the following code which gets all the objects and prints their names just fine when run from an assembly:


Sub main()

   Dim swApp as Object

   Dim swModel as Object

   Dim swFeatMgr As SldWorks.FeatureManager

   Dim swRootFeatNode As SldWorks.TreeControlItem
   Set swApp = Application.SldWorks
   Set SwModel = swApp.ActiveDoc
   Set swFeatMgr = SwModel.FeatureManager
   Set swRootFeatNode = swFeatMgr.GetFeatureTreeRootItem2_

   If Not swRootFeatNode Is Nothing Then
            TraverseFeatureNode swRootFeatNode

   End If

End Sub



Sub TraverseFeatureNode(featNode As SldWorks.TreeControlItem)

   Dim swChildFeatNode As SldWorks.TreeControlItem
   Dim featObject As Object

   Set swChildFeatNode = featNode.GetFirstChild()  
   Set featObject = featNode.Object


   If Not featObject Is Nothing Then Debug.Print featObject.Name
   While Not swChildFeatNode Is Nothing
       TraverseFeatureNode swChildFeatNode
       Set swChildFeatNode = swChildFeatNode.GetNext
End Sub


If however the code is run from a drawing, it won't get the components. It will get everything else, sheets, drawings and even the features contained in the components, but not the components themselves (featObject = Nothing).

What am I missing here?

In the end, it's not about the names, I just broke the code down to the relevant bits, the objects are what I'm looking for.


I know I could maybe make it work with .GetChildren and running through the collection, the solution above will however keep the structure of the feature tree, which would help me a lot.


I would be really happy if someone could help me here.


Best regards