I don't think this line is right:
if (((Entity)swSubFeat).GetType() == (int)swSelectType_e.swSelCOMPONENTS)
I think you need to use swSubFeat.GetTypeName2 and check that instead.
I can check swSubFeat if its component or not but I cannot cast the swSubFeat into component.
As you can see in the above code, I cast the first feature but I cannot cast the feature inside the child component.
You have to get the component from the Entity. Try entity.GetComponent()
I already tried to use entity.GetComponent() but it doesnt work.
You're running into the bug documented in SPR 853484, which has caused me a lot of problems in some of my projects. I ask that you please email email@example.com and ask that your name be added to the notification list for this bug. That will effectively act as a vote for getting it fixed.
I have documented the bug in great depth here. You can use that information to try to write your own workaround, if you wish. For example, if you open the target sub-assembly in the background, the bug will not appear.
In my Macro Library, paying customers can download a macro I wrote called "Traverse components in order" that demonstrates how to deal with this bug. It took a very long time to write, which is why do I not offer it for free. If you want to purchase it by itself without having to get a full membership to my site, please email me.
The key function in the macro is called GetComponents.
- model as ModelDoc2 - The active ModelDoc2
- targetAssembly as Component2 - The desired sub-assembly. If you pass in Nothing, it will use the top-level assembly.
- topLevelOnly as Boolean - True to get all components or just the top-level ones. Using recursion to get sub-components.
Return value is a variant array of Component2 objects.
As you can see, this function mirrors IAssemblyDoc2::GetComponents except 1) it handles SPR 853484, 2) it returns components in the order they appear in the feature tree.
I wonder if this is regression due to SolidWorks trying new ways to open assemblies quicker.
Thanks. I already sent an email to them.
I hope they will fixed it soon.
Great. Would you mind marking my answer as correct if it answered your question? Thanks.