By Daniel Du
This code snippet demonstrates how to select some feature to delete in Map 3D, it includes how to handle the AutoCAD SelectionSet and MgSelectionBase, please refer the to code itself for more information:
[CommandMethod("FeatureDelete", CommandFlags.UsePickSet
| CommandFlags.Modal | CommandFlags.Redraw)]
public void FeatureDelete()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices
.Application.DocumentManager.MdiActiveDocument.Editor;
MgFeatureService _featureService = AcMapServiceFactory
.GetService(MgServiceType.FeatureService) as MgFeatureService;
PromptSelectionResult res = ed.SelectImplied();
if (PromptStatus.OK == res.Status)
{
// Convert the SelectionSet to MgSelectionBase
// using AcMapFeatureEntityService.
Debug.Assert(res.Value != null);
ObjectId entityID = res.Value[0].ObjectId;
int entitytype = AcMapFeatureEntityService.GetEntityType(entityID);
if (entitytype == EntityType.BulkEntity)
{
MgSelectionBase curSelection = AcMapFeatureEntityService
.GetSelection(res.Value);
foreach (MgLayerBase layer in curSelection.GetLayers())
{
string filter = curSelection
.GenerateFilter(layer, layer.FeatureClassName);
if (filter != "")
{
MgFeatureCommandCollection featCommands =
new MgFeatureCommandCollection();
string strclassName = layer.FeatureClassName;
MgDeleteFeatures delFeat =
new MgDeleteFeatures(strclassName, filter);
featCommands.Add(delFeat);
try
{
AcMapLayer aclayer = layer as AcMapLayer;
MgPropertyCollection pProps = aclayer
.UpdateFeatures(featCommands);
// save and update the layer, commite the changes
MgFeatureQueryOptions opt = new MgFeatureQueryOptions();
// build the filter string, this is required for
// data source such as Sql Server/Oracle...
String commitFilter = "";
if (aclayer.IsCached()
&& EditMode.EditSet == aclayer.EditMode)
{
commitFilter = ClassSystemProperties.FeatureStatus
+ " = "
+ ClassSystemProperties.FeatureStatusDeleted;
}
opt.SetFilter(commitFilter);
//This is a must
aclayer.SaveFeatureChanges(opt);
aclayer.ForceRefresh();
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
}
//remove the highlight
AcMapFeatureEntityService.UnhighlightFeatures(curSelection);
}
}
}
Hope this helps!