roei_z

featureClass is null

Discussion created by roei_z on Jul 22, 2012
Hello to all,
I am new to GIS and to the forum.
I am using vs2010 and arcgis 10. The idea is to read an mxd file, without opening it in arcgis. See code attached.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace MGEnterprise.Tocon
{
    public static class MxdToMapDocument
    {
        public static int layerNumber = -1;
        public static int layerId = -1;

        public static void AccessMapLayers(string path)
        {
            IMapDocument pMapDocument = new MapDocument();

            // Enumerate the maps in the MapDocument and print their names to the console
            if (pMapDocument.get_IsMapDocument(path))
            {
                pMapDocument.Open(path, null);

                IMap pMap;

                for (int i = 0; i <= pMapDocument.MapCount - 1; i++)
                {
                    pMap = pMapDocument.get_Map(i);

                   
                    IEnumLayer pEnumLayer = pMap.Layers;
                 
                    pEnumLayer.Reset();

                    ILayer pLayer = pEnumLayer.Next();

                    while (pLayer != null)
                    {
                        AccessLayersData(pLayer);

                        pLayer = pEnumLayer.Next();
                    }
                }
                pMapDocument.Close();
            }
        }

        public static void AccessLayersData(ILayer pLayer)
        {
            try
            {
                    if (pLayer is IGroupLayer)
                    {
                        IGroupLayer pParentGroupLayer = pLayer as IGroupLayer;
                        Console.WriteLine(pParentGroupLayer.Name + " is parent group layer");
                    }
                    if (pLayer is IRasterCatalogLayer || pLayer is IRasterLayer)
                    {
                   
                            if (pLayer is IRasterLayer)
                            {
                                IRasterLayer pRasterLayer = pLayer as IRasterLayer;
                                Console.WriteLine(pRasterLayer.Name + " is raster layer");
                            }

                            if (pLayer is IRasterCatalogLayer)
                            {
                                IRasterCatalogLayer pRasterCatalogLayer = pLayer as IRasterCatalogLayer;
                                Console.WriteLine(pRasterCatalogLayer.Name + " is rasterCatalog layer");
                            }
                    }

                    if (pLayer is IFeatureLayer)
                    {
                        IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
                        bool valisLayer = pFeatureLayer.Valid;
                       
                        ESRI.ArcGIS.Geodatabase.IQueryFilter qFilter = new ESRI.ArcGIS.Geodatabase.QueryFilter();
                        qFilter.WhereClause = "";
                        qFilter.SubFields = "*";

                        ESRI.ArcGIS.Geodatabase.IFeatureCursor myCursor = pFeatureLayer.Search(qFilter, false) as ESRI.ArcGIS.Geodatabase.IFeatureCursor;

                        IFields pFields = myCursor.Fields as IFields;

                        IFeature pFeature = myCursor.NextFeature() as IFeature;

                        for (int i = 0; i <= pFields.FieldCount; i++)
                        {
                            IField pField = pFields.get_Field(i) as IField;
                            Console.WriteLine(pField.AliasName);
                            pFeature = myCursor.NextFeature();
                        }


                      
                    }  
                  
                   
            }
            catch (Exception exc) { ToconExceptionLogger.Log(" AccessLayersData(): " + exc.Message); Console.WriteLine(" AccessLayersData(): " + exc.Message); }
        }

        public static void DisplayDistinctFieldAliasNames(IFeatureClass featureClass)
        {
            if (featureClass != null)
            {
                try
                {
                    // Get the Fields collection from the feature class.
                    IFields fields = featureClass.Fields;
                    IField field = null;

                    // On a zero based index, iterate through the fields in the collection.
                    for (int i = 0; i < fields.FieldCount; i++)
                    {
                        // Get the field at the given index.
                        field = fields.get_Field(i);
                        if (field.Name != field.AliasName)
                        {
                            Console.WriteLine("{0} : {1}", field.Name, field.AliasName);
                        }
                    }
                }
                catch (Exception exc)
                {
                    { ToconExceptionLogger.Log(" DisplayDistinctFieldAliasNames(): " + exc.Message); Console.WriteLine(" DisplayDistinctFieldAliasNames(): " + exc.Message); }
                }
            }
            else { Console.WriteLine("DisplayDistinctFieldAliasNames: featureClass is null"); }
        }

        static void DefineUniqueValueRenderer(IGeoFeatureLayer pGeoFeatureLayer, string fieldName)
        {

            //Create the renderer.
            IUniqueValueRenderer pUniqueValueRenderer = new UniqueValueRenderer();



            //These properties should be set prior to adding values.
            pUniqueValueRenderer.FieldCount = 1;
            pUniqueValueRenderer.set_Field(0, fieldName);


            IDisplayTable pDisplayTable = pGeoFeatureLayer as IDisplayTable;
            IFeatureCursor pFeatureCursor = pDisplayTable.SearchDisplayTable(null, false) as
                IFeatureCursor;
            IFeature pFeature = pFeatureCursor.NextFeature();


            bool ValFound;
            int fieldIndex;

            IFields pFields = pFeatureCursor.Fields;
            fieldIndex = pFields.FindField(fieldName);
            while (pFeature != null)
            {


                string classValue;
                classValue = pFeature.get_Value(fieldIndex) as string;

                //Test to see if this value was added to the renderer. If not, add it.
                ValFound = false;
                for (int i = 0; i <= pUniqueValueRenderer.ValueCount - 1; i++)
                {
                    if (pUniqueValueRenderer.get_Value(i) == classValue)
                    {
                        ValFound = true;
                        break; //Exit the loop if the value was found.
                    }
                }
                //If the value was not found, it's new and will be added.
                if (ValFound == false)
                {

                    pUniqueValueRenderer.set_Label(classValue, classValue);

                }
                pFeature = pFeatureCursor.NextFeature();
            }



            for (int j = 0; j <= pUniqueValueRenderer.ValueCount - 1; j++)
            {
                string xv;
                xv = pUniqueValueRenderer.get_Value(j);

            }

        }
    }
}



The problem is that all the none group layetrs return null on featureClass. Please help!

Outcomes