去年的某个时候,当我们使用ArcGIS 9.3时,我在Visual Studio 2008中编写了一个C#程序来遍历文件夹中的所有MXD文件,检查所有图层源,并在错误或不存在时替换它们。它完美地工作了。

去年晚些时候,我们升级到了ArcGIS10。现在,我们在每年的那个时候到来,以修复所有有问题的源。因此,我进入了源代码,修复了绑定,并重新引用了所有Arc源。这样做是为了使其与较新的ArcGIS 10对象兼容。该程序可在使用ArcGIS 10制作的任何MXD上正常运行,但是在使用ArcGIS 9.3制作的任何MXD上失败并显示以下错误:

Unable to cast COM object of type 'System.__ComObject' to interface type
'ESRI.ArcGIS.Carto.IFeatureLayer'. This operation failed because the
QueryInterface call on the COM component for the interface with IID
'{40A9E885-5533-11D0-98BE-00805F7CED21}' failed due to the following
error: No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).


在将ILayer投射到IFeatureLayer时发生此错误。例如,这里的第二行:

ILayer layer = ...;
IFeatureLayer featureLayer = (IFeatureLayer)layer;
IFeatureClass featureClass = featureLayer.FeatureClass;


我需要能够访问该要素类。

那么,为什么该程序在较新的MXD而不是旧的MXD上起作用?

谢谢。

编辑:这就是我正在做的事情(略有简化,并且没有函数调用):

string[] featureClassNames = {"Building_Anno", ...};
IEnumLayer pEnumLayer = map.get_Layers(null, true);

for (int i = 0; i < featureClassNames.Length; i++)
{
    pEnumLayer.Reset();

    ILayer layer = null;

    while ((layer = pEnumLayer.Next()) != null)
    { /* stuff */ }

    if (layer == null) continue;

    IFeatureLayer featureLayer = (IFeatureLayer)layer;
    IFeatureClass oldFeatureClass = null;

    try
    {
        oldFeatureClass = featureLayer.FeatureClass;
    }
    catch (NullReferenceException) { }

    featureLayer.FeatureClass = newFeatureClass;

    if (oldFeatureClass == null)
        mapAdmin2.FireChangeFeatureClass(oldFeatureClass, newFeatureClass);
    else if (oldFeatureClass.Equals(newFeatureClass))
        return "Nope: Same source.";
    else
        return "Nope: Source already exists.";

} // end of for loop

最佳答案

听起来您的旧文档包含不是要素图层的图层。例如,如果您的MXD中有栅格,则它会通过ILayer来实现IFeatureLayer,因为RasterLayer CoClass不会实现IFeatureLayer

通常处理此问题的方法是在使用类型之前进行检查,即:

ILayer layer = ...;
IFeatureLayer featureLayer = layer as IFeatureLayer; // use as keyword
if (featureLayer != null)
{
   // You have a feature class layer:
   IFeatureClass fc = featureLayer.FeatureClass;

}

关于c# - 为什么用ArcGIS 9.3对象编写的程序在升级到ArcGIS 10后将不再起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21686487/

10-13 07:29