Get Feature Type and Name Example (C#)
This example shows how to get the feature type and name of the interactively
selected feature for use with IModelDocExtension::SelectByID2.
//----------------------------
// Preconditions:
// 1. Open:
// <SolidWorks_install_dir>\samples\tutorial\cosmosfloxpress\ball valve\ball_valve.sldasm
// 2. Expand any component in the FeatureManager design
tree
// and
select one of its features.
// 3. Run the macro.
//
// Postconditions: The selected feature's type and name
are
// printed to the Immediate window. Examine the Immediate
window to
// verify the results.
//
// NOTE: Because this assembly document is used
// by a SolidWorks online tutorial, do not
// save any changes when closing the document.
//------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
namespace GetNameForSelectionFeatureCSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
ModelDocExtension
swModelDocExt = default(ModelDocExtension);
SelectionMgr
swSelMgr = default(SelectionMgr);
Feature
swFeat = default(Feature);
string
featName = null;
string
featType = null;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swSelMgr
= (SelectionMgr)swModel.SelectionManager;
swModelDocExt
= (ModelDocExtension)swModel.Extension;
//
Get the selected feature
swFeat
= (Feature)swSelMgr.GetSelectedObject6(1,
-1);
swModel.ClearSelection2(true);
//
Get the feature's type and name
featName
= swFeat.GetNameForSelection(out
featType);
swModelDocExt.SelectByID2(featName, featType, 0, 0,
0, true, 0, null, 0);
//
Print the feature's type and name
//
to the Immediate window
Debug.Print("Feature
type: " + featType);
Debug.Print("Feature
name: " + featName);
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}