I have a .NET plugin that loads an ArcSDE geodatabase layer programmatically using AcMapMap.LoadLayer(layerFilepath). I have been trying to programmatically remove the layer from the Task Pane using AcMapMap.GetLayers().Remove(layer), but this function is returning 'false' and is not removing the layer.
Does anybody know why this might not be working?
Thanks,
-andy
Solved! Go to Solution.
Solved by andy.hinz. Go to Solution.
Solved by andy.hinz. Go to Solution.
Did you try to list layers in MgLayersCollection to see if the layer is there?
MgLayersCollection layers = AcMapMap.GetLayers();
Yes, my code actually looks more like this:
MgLayerCollection layers = CurrentMap.GetLayers();
AcMapLayer layer = null;
foreach (MgLayerBase layerBase in layers)
{
if (layerBase.LayerType == MgLayerType.Dynamic && layerBase.FeatureClassName.Contains("GEODATABASE_DBO:PARCELS") && layerBase is AcMapLayer)
{
layer = layerBase as AcMapLayer;
}
}
if(layer != null)
{
bool result = CurrentMap.GetLayers().Remove(layer);
}
Why do you convert MgLayerBase in AcMapLayer
Did you try with MgLayerBase?
I have similar code like this and it works fine
AcMapMap map = AcMapMap.GetCurrentMap(); MgResourceService rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService) as MgResourceService; MgResourceIdentifier ri3 = new MgResourceIdentifier("Library://Raster_1.FeatureSource"); MgLayerCollection layers = map.GetLayers(); foreach (MgLayerBase layer in layers) { if (layer.Name.Contains("samobor")) { layers.Remove(layer); break; } } rs.DeleteResource(ri3);
Thanks for your reply. I tried the code you posted and it is still failing to remove the layer. Can you think of any hooks I might have into the layer that need to be released? I'm already unsubscribing from document events (which I assume are unrelated anyways). The only other code that is executing with respect to the layer is GetCurrentMap().ImportLayer(layerFilePath); then layer.ForceRefresh().
I've figured out the problem. The call to remove the layer failed when the function was called from a click event on a custom Palette I'd created. When I call the exact same function from the command line in AutoCAD, the removal works fine.
Aaaaa, why didn't you say that earlier. If you are calling fuction for making changis in database or document from a pallet or a form, you have to lock the document first.
Something like this
using (DocumentLock docLock = doc.LockDocument()) { // code goes here.... }