.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AcMapMap.GetLayers().Remove() fails

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
andy.hinz
738 Views, 7 Replies

AcMapMap.GetLayers().Remove() fails

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

7 REPLIES 7
Message 2 of 8
kob4lt
in reply to: andy.hinz

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

 

MgLayersCollection layers = AcMapMap.GetLayers();

Message 3 of 8
andy.hinz
in reply to: kob4lt

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

}

Message 4 of 8
kob4lt
in reply to: andy.hinz

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

 

Message 5 of 8
andy.hinz
in reply to: kob4lt

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

Message 6 of 8
andy.hinz
in reply to: andy.hinz

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.

Message 7 of 8
kob4lt
in reply to: andy.hinz

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

 

Message 8 of 8
andy.hinz
in reply to: kob4lt

Wow, it works perfectly now... thanks!

 

-andy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost