Get Editing Status of Features Example (C#)
This example shows how to get the editing status of one or more features.
//---------------------------------------------------------------------------
// Preconditions:
// 1. Open:
// <SolidWorks_install_dir>\samples\tutorial\introtosw\pressure_plate.sldprt
// 2. Insert a breakpoint in your macro at this line:
// retVal = swModelDocExt.SelectByID2("Sketch2",
"SKETCH", 0, 0, 0, False, 0,
//
Nothing, 0)
// 3. Run the macro (F5) and then step into the code using
// the
debugger (F8) after execution stops at the breakpoint.
// 4. Examine the results displayed in the Immediate window and
// FeatureManager
design tree while stepping through the
// remaining
code.
// 5. Click the Run button just prior
to executing this line:
// End Sub
//
// Postconditions: None
//
// NOTE: Because this 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 GetEditStatusFeature_CSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
FeatureManager
swFeatMgr = default(FeatureManager);
SelectionMgr
swSelMgr = default(SelectionMgr);
ModelDocExtension
swModelDocExt = default(ModelDocExtension);
Object[]
varFeat;
long
editStatus = 0;
bool
retVal = false;
long
i = 0;
string
featName = null;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swFeatMgr
= swModel.FeatureManager;
swSelMgr
= (SelectionMgr)swModel.SelectionManager;
swModelDocExt
= swModel.Extension;
//
Traverse through the FeatureManager design tree
//
to get the editing status of all features
//
Change the editing status of a sketch and feature
//
during feature traversal
varFeat
= (Object[])swFeatMgr.GetFeatures(true);
editStatus
= (long)swFeatureEditStatus_e.swFeature_NonEditable;
for
(i = varFeat.GetLowerBound(0); i <= varFeat.GetUpperBound(0); i++)
{
Feature
swFeat = default(Feature);
swFeat
= (Feature)varFeat[i];
featName
= swFeat.Name;
switch
((featName))
{
case
"Sketch2":
//
Select and edit a sketch
retVal
= swModelDocExt.SelectByID2("Sketch2", "SKETCH", 0,
0, 0, false, 0, null, 0);
swModel.EditSketch();
break;
case
"Extrude3":
//
Close the open sketch
swModel.InsertSketch2(true);
break;
case
"Cut-Extrude2":
//
Select and edit a feature
retVal
= swModelDocExt.SelectByID2("Cut-Extrude2", "BODYFEATURE",
0, 0, 0, false, 0, null, 0);
swModel.FeatEdit();
break;
}
//
Get the editing status of the current feature
editStatus
= swFeat.GetEditStatus();
switch
((editStatus))
{
case
0:
Debug.Print(swFeat.Name
+ " can be edited.");
break;
case
1:
Debug.Print(swFeat.Name
+ " cannot currently be edited.");
break;
case
2:
Debug.Print(swFeat.Name
+ " is already being edited.");
break;
}
swFeat
= null;
}
//
End feature editing
swModel.InsertSketch2(true);
}
public
SldWorks swApp;
}
}