5 Replies Latest reply on Jun 11, 2018 5:51 PM by Solid Air

    GetFeatures for Part

    Sam Pike

      I am a little confused on something I have been reading, trying to learn about APIs.

      Does anyone have some basic code to show me how to use IFeatureManager::GetFeatures

      with a For/Next Loop for a part?

        • Re: GetFeatures for Part
          Alex Burnett

          In the past, I haven't used the feature manager to get my features but there is more than one way to get to the same place most of the time.

           

          For my typical setup, I'll have my model (IModelDoc2) object and then do something like the following:

           

          Feature curFeature = model.FirstFeature()
          

           

          Then from there, I'll iterate through all of the features to get what I'm looking for with a while loop.

           

          While Not curFeature.isNull
              'put your code here to do what you want
              curFeature = curFeature.GetNextFeature()  'this will get the next feature in the tree before the next while loop iteration
          End While
          

           

          I don't know if that helps but it might get you started a little bit.

            • Re: GetFeatures for Part
              Sam Pike

              This was the way that was shown, but I want to see how to get all the features in the feature manager design tree. From what I am reading the IFeatureManager::GetFeatures will get all the children also, not just the Top level. I believe what you are showing will get just top level.

                • Re: GetFeatures for Part
                  Alex Burnett

                  I wrote my function to work on a part level and it will iterate through the full tree and hide all sketches that are shown. My example above is pretty basic but I added the following code as well which returns items that are one level below features. Place this code inside the first while loop.

                   

                  subFeature = curFeature.GetFirstSubFeature()
                  

                   

                  Then add another while loop to iterate through these similar to the example above.

                   

                  While Not subFeature is Nothing
                       ' do things
                       subFeature = subFeature.GetNextSubFeature()
                  End While
                  

                   

                  Disclaimer: My code is written in c# so I some of my syntax an variable may not be translated 100% correctly. If you want to see the c# code, just let me know.

                  • Re: GetFeatures for Part
                    Josh Brady

                    Sam Pike wrote:

                     

                    This was the way that was shown, but I want to see how to get all the features in the feature manager design tree. From what I am reading the IFeatureManager::GetFeatures will get all the children also, not just the Top level. I believe what you are showing will get just top level.

                     

                    What exactly do you want to do with the features?  Because GetFeatures will just return you an un-ordered random pile of Feature objects.

                    There is an example in the API Help under the GetFeatures method.  It's just right at the end, and it just outputs a list of the feature names to the immediate window.

                     

                    If you care about the order, you can use the FirstFeature option as mentioned by Alex.

                     

                    If you need to go deeper, write it recursively.

                     

                    If you really care about how things appear in the design tree, use the TreeControlItem.  There's an example listed under GetFeatureTreeRootItem2 that shows how to recursively traverse the feature manager design tree.

                • Re: GetFeatures for Part
                  Solid Air

                  I have to agree with Alex Burnett and Josh Brady; GetFeatures is probably not the way you want to go.  However to answer just your question, below is an example.

                   

                  Dim swApp As SldWorks.SldWorks
                  Dim swDoc As SldWorks.ModelDoc2
                  Dim swFeatMgr As SldWorks.FeatureManager
                  Dim TreeFeatures As Variant
                  Dim TreeFeature As Variant
                  Dim swFeat As SldWorks.Feature
                  
                  
                  Sub main()
                  
                  
                      Set swApp = Application.SldWorks
                      Set swDoc = swApp.ActiveDoc
                      Set swFeatMgr = swDoc.FeatureManager
                      
                      TreeFeatures = swFeatMgr.GetFeatures(False)
                      
                      For Each TreeFeature In TreeFeatures
                      
                          Set swFeat = TreeFeature
                          
                          Debug.Print swFeat.Name
                          
                      Next
                      
                      
                  End Sub