2

I've spent all day yesterday trying to trace all errors but I'm just completely stuck now. Google isn't helpful either.

I'm trying to create a small C# tool which opens a MXD file in background and reads data from one of the datasets (layers). I can happily open the file, I can list all the layers inside with no problems. But no matter how hard I try, my FeatureClass or DataTable (tried both techniques) are always null.

I'm simply trying to get a reference to an ITable or IFeatureClass object to perform a QueryFilter on it.

EDIT: Solution

I really don't know why my approach using IMapDocument didn't work. Documentation describes it as the best and most efficient way of accessing your data in background. The final solution involved creating an instance of ArcMap, creating a workspace and opening a feature class within it.

    ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
    m_application = new ESRI.ArcGIS.ArcMapUI.MxDocumentClass().Parent;

    IWorkspace2 workspace = FileGdbWorkspaceFromPath(@"F:\Projects\!Water\Sewers.gdb");

    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;                  
    IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Sewers_v5");

And

public IWorkspace2 FileGdbWorkspaceFromPath(String path)
{        
    IObjectFactory objFactory = m_application as IObjectFactory;
    Type shpWkspFactType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");      
    string typeClsID = shpWkspFactType.GUID.ToString("B");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)objFactory.Create(typeClsID);
    return workspaceFactory.OpenFromFile(path, 0) as IWorkspace2;
}

It's probably not the prettiest solution (made up from 5 different source codes) but it works.

I'm really disappointed by the ArcGIS documentation. So detailed and rich, and yet so useless.

  • Can you elaborate more on your end purpose for looking at the table data, e.g are you looking to make selections, search for values, or get a collection of certain values for a certain field? – artwork21 Jun 18 '13 at 13:21
  • I'm looking for the actual values. I've got a nice QueryFilter set up to iterate through the results, that's not a problem. It's just getting that ITable or IFeatureClass object on which I can apply my query. – ZorleQ Jun 18 '13 at 13:24
  • What does ILayer.Valid return? – Kirk Kuykendall Jun 18 '13 at 13:29
  • False, which was worrying me from the start, but couldn't find any resources on how to make it valid. – ZorleQ Jun 18 '13 at 13:31
  • Do you have arcmap? If so, open and fix the mxd. It might contain paths to datasets that are no longer valid. Often checking "use relative paths" can simplify life. – Kirk Kuykendall Jun 18 '13 at 13:36
2

Instead of using ILayer I've used IFeatureLayer within a cursor (ICursor) and IDataStatistics enumerating by System.Collections.IEnumerator to get values for a certain column.

1

Working solution

Just to tag this question as answered, I'm reposting my working solution:

    ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
    m_application = new ESRI.ArcGIS.ArcMapUI.MxDocumentClass().Parent;

    IWorkspace2 workspace = FileGdbWorkspaceFromPath(@"F:\Projects\!Water\Sewers.gdb");

    IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;                  
    IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Sewers_v5");

And

public IWorkspace2 FileGdbWorkspaceFromPath(String path)
{        
    IObjectFactory objFactory = m_application as IObjectFactory;
    Type shpWkspFactType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");      
    string typeClsID = shpWkspFactType.GUID.ToString("B");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)objFactory.Create(typeClsID);
    return workspaceFactory.OpenFromFile(path, 0) as IWorkspace2;
}
  • Looks like I can't accept my own answer for 2 days!. Is there a better way of tagging this question as answered so it doesn't clutter the board? – ZorleQ Jun 18 '13 at 14:21

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.