Creating an AutoCAD Selection set from the Map 3D Platform Selection set (MgSelectionBase)

By Partha Sarkar

I am trying
to use the _MAPExtractFeatureGeometry from SendStringToExecute() function and I
want to pass in a selection set containing a Feature.

I have used
Geospatial Platform API and MgSelectionBase to create a selection set of
features which works fine as I could highlight the selected feature. But
subsequently when I use 
_MAPExtractFeatureGeometry from SendStringToExecute(), I get a response
'there is no selection set'. I am trying to avoid the user intervention to
select a feature on screen. How do I create an AutoCAD selection set from the Map
3D Platform Selection set (MgSelectionBase) ?

 

Here is a C# .NET code snippet demonstrating the same –

// Select using API
 
AcMapMap currentMap = AcMapMap.GetCurrentMap();
MgLayerCollection layers = currentMap.GetLayers();
MgLayerBase layer = layers.GetItem("Parcels");
MgFeatureService fs = AcMapServiceFactory.GetService(MgServiceType.FeatureService) as MgFeatureService;
 
string fsId = layer.GetFeatureSourceId();
string className = layer.GetFeatureClassName();
MgFeatureQueryOptions query = new MgFeatureQueryOptions();
// Parcels.shp file is used
query.SetFilter("FeatId = 166");
MgResourceIdentifier resId = new MgResourceIdentifier(fsId);
MgFeatureReader featureReader = fs.SelectFeatures(resId, className, query);
 
MgSelectionBase selectionSet = new MgSelectionBase(currentMap);
 
//Add all features in the feature reader to the newly constructed selection set
selectionSet.AddFeatures(layer, featureReader, 0);
 
AcMapFeatureEntityService.HighlightFeatures(selectionSet);
 
// create an AutoCAD selection
SelectionSet acadSelSet = AcMapFeatureEntityService.AddFeaturesToSelectionSet(null, selectionSet);
 
// force this selection to be the implied selection

Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
.Editor.SetImpliedSelection(acadSelSet);  
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
.SendStringToExecute("_MAPExtractFeatureGeometry ", true, false, true);
 
featureReader.Close();

Hope this is useful to you!


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading