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.
ILayer.Valid
return? – Kirk Kuykendall Jun 18 '13 at 13:29