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.
2 Answers 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.
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– ZorleQ2013年06月18日 14:21:20 +00:00Commented Jun 18, 2013 at 14:21
ILayer.Valid
return?