AcMapMap.GetLayers().Remove() fails

AcMapMap.GetLayers().Remove() fails

Anonymous
Not applicable
1,420 Views
7 Replies
Message 1 of 8

AcMapMap.GetLayers().Remove() fails

Anonymous
Not applicable

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

0 Likes
Accepted solutions (2)
1,421 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Did you try to list layers in MgLayersCollection to see if the layer is there?

 

MgLayersCollection layers = AcMapMap.GetLayers();

0 Likes
Message 3 of 8

Anonymous
Not applicable

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);

}

0 Likes
Message 4 of 8

Anonymous
Not applicable

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);

 

0 Likes
Message 5 of 8

Anonymous
Not applicable

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().

0 Likes
Message 6 of 8

Anonymous
Not applicable
Accepted solution

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.

0 Likes
Message 7 of 8

Anonymous
Not applicable

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....
}

 

0 Likes
Message 8 of 8

Anonymous
Not applicable
Accepted solution

Wow, it works perfectly now... thanks!

 

-andy

0 Likes