2

I'm no expert with GIS development but I have created a tool that is used to interface with selected data in the Attribute Table that is currently open. Everything has been working fine since I wrote the tool but now all of a sudden my tool cannot find the CurrentLayer. My general question is "Is there a configuration setting that would cause this behaviour?"

Using ArcMap 10 on Windows 7. SQL backend, VS 2010 with C#.

In the code snippet below _map has zero Layers when I know they are there!

    private IMap _map;
    public MyEditorExtension()
    {
        _extensionStateService = Services.ExtensionStateService;
        _map = ArcMap.Document.FocusMap;
        _attributeWindow = (AddIn.FromID<AttributesWindow.AddinImpl>(ThisAddIn.IDs.AttributesWindow));
    }

The code below should iterate all Layers in the map and find the correct layer based on the featureClassName string passed in, but because the map has no Layers (code above) it can't return the Layer that I need to edit. I'm 99.99% sure that this is a configuration problem because it worked and then it stopped. I can't even get my production Compiled AddIn to work either, but my colleague has no problem, however I'm the only one developing the Tool.

    public static IFeatureLayer GetFeatureLayerByFeatureClassName(IMap map, string featureClassName)
    {
        IFeatureLayer result = null;
        for (var i = 0; i < map.LayerCount; i++)
        {
            var layer = map.Layer[i];
            var featureLayer = layer as IFeatureLayer;
            if (featureLayer == null) continue;

            var layerFc = featureLayer.FeatureClass;
            var layerDs = (IDataset)layerFc;

            if (layerDs.Name.Trim() == featureClassName.Trim())
            {
                //print the layer name in TOC..
                result = featureLayer;
                break;
            }
        }
        return result;
    }
  • 1
    Please edit your question to include a snippet of the code that isn't working, and exact error message you are receiving. At the moment your question is quite vague and would benefit from more detail – Midavalo Aug 8 '16 at 2:38
  • I have update the question with code as requested. Thanks. – Dave Stuart Aug 8 '16 at 2:48
  • 3
    Is MyEditorExtension() a constructor? I don't recall ever putting any code in the constructor - usually code is in an event handler. see resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/… – Kirk Kuykendall Aug 8 '16 at 3:20
  • 2
    What's the current layer? That's more of a QGIS API thing.. is it the currently highlighted layer, first layer, the one with a selection... have a look at gis.stackexchange.com/questions/77881/… for some more info. I do know that between 9 & 10 the attribute table changed significantly so older reference code may no longer work as intended. IMap can have 0 layers if it's not linked to the current document or you've got an empty data frame highlighted. – Michael Stimson Aug 8 '16 at 3:52
  • I added more to the question so hopefully it's starting to make more sense. The Current Layer in the Attribute Table that is open and being edited. Yes, the _map is in the constructor and the OnEditing Event calls GetFeatureLayerByFeatureClassName to get the Current Layer. Since the _map object has zero layers it will not find the Current Layer. – Dave Stuart Aug 9 '16 at 2:30
2

If I understand you correctly, you see the Layers in your MXD but the map.LayerCount is 0? Do you have a second data frame, see the attached screenshot.

TOC

The variable ArcMap.Document.FocusMap returns the currently active map (in my case the Layer2, the bold one). In my case this one has no layers. Could this be the reason in your case, too? In this case you should first iterate about all maps of ArcMap.Document.Maps.

|improve this answer|||||
  • Thanks for the information and now I can understand why my Map has no Layers. When I open ArcMap I don't open an mxd. I am prompted to load one so my Map is actually empty when the constructor loads! It's only after I load the map that I have Layers. So as proposed by " Kirk Kuykendall" I have copied the initialize code to the On Start Editing event and all works. I totally understand this now and it was just by chance that no one noticed this before. – Dave Stuart Aug 9 '16 at 16:35
  • 3
    That sounds reasonable. Another possibility would be to initialize that stuff in the IDocumentEvents.OpenDocument event which (depending on the frequency of starting and stopping the edit session) could be the better place. – Propper Aug 10 '16 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.