在Map 3D中可以通过API来连接到SHP,Oracle等各种数据源,如果你对这个过程不熟悉,可以参考Map 3D ObjectARX SDK中的BuildMap示例,但BuildMap中没有演示如何断开到这些数据源的连接。如果你看过BuildMap的实现代码,就会发现连接到数据源其实就是创建FeatureSource资源的过程,添加图层到地图上即创建LayerDefinition的过程。那么于此对应,断开连接也就是删除相应资源的过程。
下面是实现从地图中删除图层并断开连接的代码(在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); }
另外,这个方法也还有其他的妙用,今天没时间了,以后再详细写。