In Map 3D, you can use APIs to connect to various data sources such as SHP and Oracle. If you are not familiar with this process, refer to the BuildMap example in Map 3D ObjectARX SDK, however, BuildMap does not demonstrate how to disconnect from these data sources. If you have read the BuildMap implementation code, you will find that connecting to the data source is actually the process of creating the FeatureSource resource. adding layers to the map is the process of creating LayerDefinition. In this case, disconnecting is the process of deleting the corresponding resource.
The following code deletes a layer from the Map and disconnects it (tested in Map 3D 2013 ):
[CommandMethod("RemoveLayer")] public void RemoveLayer() { Document doc = Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database db = doc.Database; AcMapMap map = AcMapMap.GetCurrentMap(); string layerName = "layer1"; // remove the layer var layers = map.GetLayers(); if (!layers.Contains(layerName)) { ed.WriteMessage("\nLayer does not exist: " + layerName); return; } MgLayerBase layer = layers.GetItem(layerName); layers.Remove(layer); // remove the layer resource MgResourceIdentifier identifier = layer.LayerDefinition; MgResourceService resourceService = AcMapServiceFactory.GetService(MgServiceType.ResourceService) as MgResourceService; if (resourceService.ResourceExists(identifier)) resourceService.DeleteResource(identifier); // remove the feature source identifier = new MgResourceIdentifier(layer.FeatureSourceId); if (resourceService.ResourceExists(identifier)) resourceService.DeleteResource(identifier); }
In addition, this method also has other advantages. It is no longer available today and will be detailed later.