Referencing Map Layers
One of the bugs in my app which gave me the most trouble was related the order in which layers are loaded into the application. The problem was caused by the following code:
this.axMapControl1.AddLayerFromFile(dataPath + "counties.lyr");
pCntyLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer;
pCntyFC = pCntyLayer.FeatureClass;
this.axMapControl1.AddLayerFromFile(dataPath + "recareas.lyr");
pRecLayer = this.axMapControl1.get_Layer(1) as IFeatureLayer;
pRecFC = pRecLayer.FeatureClass;
this.axMapControl1.AddLayerFromFile(dataPath + "interstates.lyr");
pInterLayer = this.axMapControl1.get_Layer(2) as IFeatureLayer;
pInterFC = pInterLayer.FeatureClass;
this.axMapControl1.AddLayerFromFile(dataPath + "cities.lyr");
pCityLayer = this.axMapControl1.get_Layer(3) as IFeatureLayer;
pCityFC = pCityLayer.FeatureClass;
Notice the index values in the get_Layer function. I initially expected the layer indexes to be the same as the order in which they were added to the map. This turned out to be wrong. Actually each layer was added to the beginning of the array, meaning that for the code above, each of the indexes should be 0.
This bug through me on a wild goose chance because it turned out I got the first layer index correct, so I didn't get a error message until I was further into writing application logic. In the code above, each of the layers will be the county layer. By coincidence this was the first layer I was doing operations one which cause additional confusion when operations on the cities layer didn't work.
Creating the Geometry Bag
When creating a geometry bag of features from Query Filter, I first attempted writting my own functions but quick found that the professors solution was more elegant. I translated the VBA code into the following C# function:
private IGeometryBag getGeomBag(IQueryFilter pQueryFilter, IFeatureClass pFClass)
IEnumGeometry pEnumGeom = new EnumFeatureGeometryClass();
IEnumGeometryBind pEnumGeomBind = pEnumGeom as IEnumGeometryBind;
IGeometryFactory pGeomFactory = new GeometryEnvironmentClass();
return pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom) as IGeometryBag;
This function applies the filter and return a nice geometry for additional site selection steps.
While I got my stand alone application working, I was unable to integrate it into excel and powerpoint. I am currently experimenting with VSTO (Visual Studio Tools for Office) to see if I find a solution.