Can anyone tell my why features in the SW feature treehavecircular references?
For example, calling GetFirstSubFeature on an extrude featurewillreturn it'schild sketch as expected, but callingGetFirstSubFeatureon the sketch will return theparentextrude feature!? This is a circular reference andsuch occurrencesare generally considered very bad (AKA bugs) insoftwaredevelopment.
As a consequence of this behavior, when you attempt toenumerate(loop through) each node in the feature tree using'recursion' the macro enters an infinite loop. I have includeda macrobelow to demonstrate this. Don't worry, the macro does allowyou toexit the infinite loop - just press cancel in the dialog boxitshows for each node in the feature tree.
Using recursion to read nodes in a tree data structure is theonlyway to enumerate all the nodes without having a predefinedlimit tothe number of child levels in the tree. So, can anyone shedanylight on this seemingly insane design decision, or is it a bugwithSW?
Tim
Sub main() Dim swApp As SldWorks.SldWorks Set swApp=Application.SldWorks Dim swDoc As SldWorks.ModelDoc2 Set swDoc=swApp.ActiveDoc If Not swDoc Is Nothing ThenRecursiveReadswDoc.FirstFeature End IfEnd SubSubRecursiveRead(swFeature AsSldWorks.Feature) Do While Not swFeatureIs Nothing IfMsgBox(swFeature.Name, vbOKCancel) = vbCancel ThenExit Sub End IfRecursiveRead swFeature.GetFirstSubFeature SetswFeature =swFeature.GetNextFeature LoopEnd SubSolidworksApi macros
For example, calling GetFirstSubFeature on an extrude featurewillreturn it'schild sketch as expected, but callingGetFirstSubFeatureon the sketch will return theparentextrude feature!? This is a circular reference andsuch occurrencesare generally considered very bad (AKA bugs) insoftwaredevelopment.
As a consequence of this behavior, when you attempt toenumerate(loop through) each node in the feature tree using'recursion' the macro enters an infinite loop. I have includeda macrobelow to demonstrate this. Don't worry, the macro does allowyou toexit the infinite loop - just press cancel in the dialog boxitshows for each node in the feature tree.
Using recursion to read nodes in a tree data structure is theonlyway to enumerate all the nodes without having a predefinedlimit tothe number of child levels in the tree. So, can anyone shedanylight on this seemingly insane design decision, or is it a bugwithSW?
Tim
Sub main() Dim swApp As SldWorks.SldWorks Set swApp=Application.SldWorks Dim swDoc As SldWorks.ModelDoc2 Set swDoc=swApp.ActiveDoc If Not swDoc Is Nothing ThenRecursiveReadswDoc.FirstFeature End IfEnd SubSubRecursiveRead(swFeature AsSldWorks.Feature) Do While Not swFeatureIs Nothing IfMsgBox(swFeature.Name, vbOKCancel) = vbCancel ThenExit Sub End IfRecursiveRead swFeature.GetFirstSubFeature SetswFeature =swFeature.GetNextFeature LoopEnd SubSolidworksApi macros

Please, in the future, paste code into your posts so that it can beeasily cut and pasted into the VBA editor. It took me longer than Iwanted to sort through your code just so I could run it.
Anyway, to answer your question, I doubt whether SolidWorksconsiders it a circular reference (I would write API help for aclarification). SolidWorks treats sketches as both a top levelfeature and a subfeature (if used to make another top levelfeature) except in the case of HoleWizard holes; those sketches areconsidered subfeatures only. When you run your code, you should seeit finds the sketch first then the parent extrude followed by theendless loop of sketch, parent feature, sketch, parent feature,etc. If you look more closely, you will also notice your code doesnot find all the sub features under a HoleWizard hole or even theLights and Camera folder for that matter. What you need to do ismodel your code after theTraverse Subfeatures Example (VB) example in API help. Thatway all the sub features of a top level feature will be returned.
SA