Hi everyone,
I'm developing a C# SolidWorks Add-in (.NET Framework 4.8) that performs health checks on assemblies and displays the results in a TaskPane. When the user double-clicks a finding in the results list, I want to navigate to the affected component and select the specific feature (e.g. a sketch) in the FeatureManager tree.
The problem: SelectByID2 returns true and ViewZoomToSelection correctly zooms to the component in the graphics area, but the feature is not highlighted or scrolled to in the FeatureManager tree. The tree stays where it is and nothing appears selected there.
What I've tried
Attempt 1: Direct SelectByID2 with full qualification
private void NavigateToComponent(string componentName, string featureName)
{
var modelDoc = swApp.ActiveDoc as IModelDoc2;
if (modelDoc == null) return;
modelDoc.ClearSelection2(true);
var asmName = Path.GetFileNameWithoutExtension(modelDoc.GetPathName());
// Select the feature directly with full path: "FeatureName@ComponentName@AssemblyName"
var featSelectName = \\\$"{featureName}@{componentName}@{asmName}";
var featureTypes = new[] { "SKETCH", "PROFILEFEATURE", "BODYFEATURE",
"REFERENCECURVE", "SUBATOMFOLDER", "" };
foreach (var featType in featureTypes)
{
bool selected = modelDoc.Extension.SelectByID2(
featSelectName, featType,
0, 0, 0, false, 0, null,
(int)swSelectOption_e.swSelectOptionDefault);
// This returns TRUE for the correct type, but the FeatureManager
// tree does not scroll to or highlight the feature
if (selected) break;
}
modelDoc.ViewZoomToSelection(); // This works - zooms to the right area
}
Result: SelectByID2 returns true, zoom works, but FeatureManager tree does not show the selection.
Attempt 2: Using IFeature.Select2 after SelectByID2
if (selected)
{
var selMgr = modelDoc.SelectionManager as ISelectionMgr;
if (selMgr != null && selMgr.GetSelectedObjectCount2(-1) > 0)
{
var selObj = selMgr.GetSelectedObject6(1, -1);
var selFeature = selObj as IFeature;
if (selFeature != null)
{
modelDoc.ClearSelection2(true);
selFeature.Select2(true, 0); // Should select in tree
}
}
}Result: Same behavior - graphics selection works, FeatureManager tree doesn't respond.
Attempt 3: FeatureManager EnableFeatureTree toggle
// After SelectByID2:
modelDoc.FeatureManager.EnableFeatureTree = false;
modelDoc.FeatureManager.EnableFeatureTree = true;Result: No change.
Attempt 4: EditPart2 to enter component context
// Select component first
var compSelectName = \\\$"{componentName}@{asmName}";
modelDoc.Extension.SelectByID2(compSelectName, "COMPONENT", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);
// Enter edit mode for the component
var asmDoc = modelDoc as IAssemblyDoc;
asmDoc.EditPart2(true, false, 0);
modelDoc.ClearSelection2(true);
// Now select feature without component qualifier
modelDoc.Extension.SelectByID2(featureName, "SKETCH", 0, 0, 0, false, 0, null, (int)swSelectOption_e.swSelectOptionDefault);Result: This actually works for showing the feature in the tree, but it puts the assembly into Edit Part mode which is not ideal for a read-only health check tool. Users then have to manually exit Edit Part mode.
Environment
SolidWorks 2022 SP5
C# .NET Framework 4.8
Add-in running in TaskPane
Testing with assemblies containing 100-3000+ components
What I'm looking for
Is there a way to programmatically select a feature inside a component so that:
The FeatureManager tree scrolls to and highlights the feature
The component node expands to show the feature
WITHOUT entering Edit Part mode
Works reliably in large assemblies (3000+ parts)
I've seen that when you manually click a feature in the tree, SolidWorks clearly handles tree navigation differently than when selecting via API. Is there an API call I'm missing that triggers the tree to sync with the selection?
Things I haven't tried yet but am considering:
IModelDoc2.ShowNamedView2or similar view commandsIFeatureManagermethods for tree controlSending Windows messages to the FeatureManager tree control
ITreeControlItemnavigation (tried earlier but too slow for large assemblies)
Any help or ideas would be greatly appreciated. Thanks
