2

I have a large program that pulls in various group layers and feature classes. What I am trying to do is check each feature class within a specific group layer that is empty. The count part is easy and just leaves the feat class unsourced. I really want to remove it when empty. This is a brief code example:

public static void FoiUpdSym()
    {
        IMxDocument MXDoc = (IMxDocument)PublicVars.g_App.Document;
        IMap map = MXDoc.FocusMap;
        IFeatureWorkspace SDEFeatWS = null;
        IFeatureWorkspace SDEMasterWS = null;
        IFeatureWorkspace FoiUpdateWS = null;
        IGxLayer GxLayer;
        IGxFile GxFile;
        ILayer layer;
        UID uid;
        IEnumLayer enumLayer;
        IFeatureLayer featLayer = null;
        IFeatureClass featClass = null;

        // load group layer file
        GxLayer = new GxLayer();
        GxFile = (IGxFile)GxLayer;
        GxFile.Path = "\\\\gisTools\\gis\\symbology\\foiupdate\\foi-update_2016.lyr"; // strSymPath
        GxLayer = (IGxLayer)GxFile;
        layer = GxLayer.Layer;
        map.AddLayer(layer);

        uid = new UIDClass();
        uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}"; // IFeatureLayer
        enumLayer = map.get_Layers(uid, true);
        layer = enumLayer.Next();

        while (layer != null)
        {
            featLayer = (IFeatureLayer)layer;
            if (Regex.IsMatch(layer.Name, "cityp_adds"))
                {
                    try
                    {
                        featClass = FoiUpdateWS.OpenFeatureClass("cityp_adds");
                        IQueryFilter queryFilter = new QueryFilterClass();
                        if (featClass.FeatureCount(queryFilter) > 0)
                        {
                            featLayer.FeatureClass = featClass;
                            featLayer.Selectable = false;
                            featLayer.Visible = false;
                        }
                        else
                        {
                           map.DeleteLayer(featLayer);
                        }
                    }
                    catch { }
                }

The else statement is what isn't working.

1 Answer 1

3

If the featurelayer is selected in the TOC then you can use the DeleteLayer method of IMap. Otherwise you need to first disconnect it from the data source by casting the layer to IDataLayer2 and then calling the disconnect method. This is mentioned at the bottom of the help file: IMap.DeleteLayer Method

Also it would be a good idea to catch your exception in the code and read the message rather than use an empty catch block (it just suppressed the error). Although in this case you probably would have gotten a generic automation error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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