If you create macros or add-ins for SOLIDWORKS, you will have to deal with features and the feature tree.
But traversing the tree can be a pain. So I’m writing down everything I know.
This post is part of a series on the SOLIDWORKS API. You can find links to the other parts at the end of the post.
Features are the basic building blocks within SOLIDWORKS models. They are organized in the feature tree (the official name is FeatureManager Design Tree) on the left of the SOLIDWORKS window. Each feature
Features are represented by IFeature in the API. See the interface and members docs. The most important properties are the type name, the name (that can be changed by users), the definition and the relation with its parent(s) and child / children.
Every model has its unique feature types:
You may often have to traverse these features from top to bottom to find the feature(s) you are looking for. You do this by getting the first feature from the model, then the next feature from the previous feature. In C#:
1 2 3 4 5 6 7 | var swModel = swApp.ActiveDoc; var swFeat = swModel.IFirstFeature(); while (swFeat != null) { var name = swFeat.Name; swFeat = swFeat.IGetNextFeature } |
There are two other ways to get all features:
This took me a while to figure out. Apparently, when two otherwise identical methods are available, the method without I returns a generic object. You then have to cast that object to a Feature. The method with I returns the specific object, so you don’t have to cast it.
So in the case of features:
Confusingly, there are not always two copies of the same method available. Often there is only one version (always without the I) and sometimes it returns a generic object and sometimes it returns the specific type.
According to Joyce at the documentation team, SOLIDWORKS is phasing out the methods that start with I. I just hope they will be phasing out the methods that return plain objects as well.
SOLIDWORKS stores feature data in two groups of objects:
I have not found the rationale behind these two different object types (and why some have one, the other or both), so you better read the documentation.
Features can have subfeatures: a extrude feature always has an underlying sketch. The same sketch can be used to create multiple extrudes, so the subfeature may not be unique.
The most reliable way of working with subfeatures is to start at the top of the tree and work your way down. You could call GetFirstSubFeature on any random feature, but I have seen it return garbage.
This example is simple enough:
Traverse features and subfeatures example
From what I understand, each model has two lists: a list of features and a list of subfeatures. They use pointers to point from one feature to the next, and to its first subfeature. So if you traverse the tree, you follow these pointers from beginning to end. But if you would access this raw list from top to bottom, the order makes no sense.
If you want to add features to a model, you need the FeatureManager object. It belongs to a model, so you get it as a property first.
The methods to create features are not named consistently, some start with Create, some start with Feature (which I find a pretty useless prefix) and most have an Insert (or IInsert) prefix.
Getting creating new features with the API can be a very frustrating task. Take, for example, the FeatureCut method. It takes 27 parameters! And if you pass the wrong type of parameter once, SOLIDWORKS just returns null. No error message, no help, nothing. You’re on your own.
Luckily, you can create some features from a FeatureData object. We’ll discuss that next.
Every feature needs a bunch of properties and relations to edges or faces to store how that feature is made. SOLIDWORKS does this with Feature data objects.
There is no generic feature data type, but there are 151 interfaces (as of SW2022) that end in FeatureData. To know which interface you need:
If you are lucky, you don’t need to call a method with 20+ parameters to create a new feature. You can now create a feature data object first, set its properties, then create a feature from that object.
To create a new instance of a specific feature data object, call FeatureManager.CreateDefinition, pass along the type and cast it to the correct object type:
1 | var featureData = (TabAndSlotFeatureData) swModel.FeatureManager.CreateDefinition((int) swFeatureNameID_e.swFmTabAndSlot); |
In the last few years, SOLIDWORKS has added more and more support for this feature. In 2017, it supported only two object types, which grew to eight in 2019 and as of SW2022, it supports 31 feature types. That’s progress.
The list of feature types includes patterns, sheet metal features and a bunch of sweep features.
To edit the properties of an existing feature:
If the feature you want to change has selection boxes for geometry, you need to follow these steps:
Call ReleaseSelectionAccess after you called AccessSelections, but didn’t change anything.
Every time you make a change via the API, SOLIDWORKS checks if that change is allowed. It may also try to do smart stuff, like adding sketch relations.
But if you are going to do hundreds of method calls, you may only need to run those checks once, not a hundred times.
Luckily, you can disable a dozen different SOLIDWORKS features via the API. You can read about seven methods in my blog post How to improve SOLIDWORKS macro speed 10x and about a few extra methods (that I found later) in my ebook Secrets to SOLIDWORKS Performance.
Subscribe to our newsletter and get our TimeSavers add-in for free.