i need to extract feature data automatically from feature manager pane to compare it with saved data from excel sheet database. how can i do it?
What are you working with? Parts, assemblies, drawings? What kind of data do you need, the feature names or types? There are multiple examples in the help on feature tree traversal, you can check that out. If we have more info, we can give you some more info to get started.
Hi Peter I will try to be more specific now.
I am working with parts. I am opening a neutral format part(.STEP format) and running recognize feature on it by right clicking imported1->FeatureWorks ->Recognize Features as shown below
on completion i get following features
Now what I all need is the number of features (in example above it is 5, Boss-Extrude1,Cut-Extrude1,Cut-Extrude2,M12 Clearance hole1,M13 clearance hole 1) so that i can store it in excel and do later operations on it.
What I have implemented yet :
Dim swApp As SldWorks.SldWorks
Dim myModel As SldWorks.ModelDoc2
Dim featureMgr As SldWorks.FeatureManager
Dim featArr As Variant
Dim count As Long
Set swApp = Application.SldWorks
Set myModel = swApp.ActiveDoc
Set featureMgr = myModel.FeatureManager
count = featureMgr.GetFeatureCount(True)
Set featArr = featureMgr.GetFeatures(True) 'getting type mismatch error here
For i = 0 To count - 1
Set swFeat = featArr(i)
Debug.Print swFeat.Name ' or fo any operation here
I am getting type mismatch at the highlighted line above featureMgr.GetFeatures(True) which returns array of all top level features.
1. How to resolve type mismatch error(is there any specific array type Solidworks provides)?
2. Even if i traverse through array how can i select only particular features?, because the feature names are dynamic.
3. Or is there any function to find if the substring exist in a string so that i can search for Boss-Extrude,Cut-Extrude(which are same for all parts) in the traversed array and retrieve only those?
PS : I am also aware of Set rootNode = featureMgr.GetFeatureTreeRootItem() method.
I am amateur in macro programming, kindly help me out.
The way to resolve the type mismatch is to remove the set command. Set is only required for objects and variant isn't an object per se. It can contain objects though. To prevent issues further down the line, you should consider starting the file with the line Option Explicit so it will give an error when not all variables are defined. In this case i and swFeat weren't defined. You can also turn the option on in the settings by enabling "Require Variable Declarations".
If you're only interested in the feature count, there is a command for that in the feature manager: GetFeatureCount.
When traversing the tree, you can use swFeat.GetTypeName2 to get the type of the feature. That will give you a language-independent and user-input-independent feature name.
Thanks for the mismatch error. But going further other things are not working as expected. As you can see i have already used GetFeatureCount(true), but its returning some different value like 25, see watch below while i have only 5 features.
Is it possible to get it?. Also the featArray is returning null.
You can use Debug.Print swFeat.Name to see what the features actually are. You'll then see that there are a lot of hidden features like the sensors folder etc. You could create a program that excludes or includes certain features to get a more accurate count.
Maybe there is another way around your problem. Why is the feature count something you want to know? I've never seen a reason for that. Certainly with imported parts the amount of features can vary I guess. So what is the real problem that you are trying to solve?
Retrieving data ...