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.

asked Jun 18, 2013 at 12:36
12
  • 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? Commented Jun 18, 2013 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. Commented Jun 18, 2013 at 13:24
  • What does ILayer.Valid return? Commented Jun 18, 2013 at 13:29
  • False, which was worrying me from the start, but couldn't find any resources on how to make it valid. Commented Jun 18, 2013 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. Commented Jun 18, 2013 at 13:36

2 Answers 2

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.

answered Jun 18, 2013 at 13:44
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;
}
answered Jun 18, 2013 at 14:20
1
  • 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? Commented Jun 18, 2013 at 14:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.