AE+C#批量修改MXD中图层数据源
在实际的工作中经常遇到MXD找不到数据源或者没有需要批量转换数据源,本文样例代码为AE+C#如何批量修改MXD的数据源。
代码如下
stringpath = @"D:\temp";
stringsdeName = @"localhost.sde";
stringsdePath = path + "\\" + sdeName;
if(File.Exists(sdePath))
{
File.Delete(sdePath);
}
IWorkspaceFactorypWorkspaceFactory = new SdeWorkspaceFactoryClass();
IWorkspaceNamepWorkspaceName = pWorkspaceFactory.Create(path, sdeName, ConnectSDE(true), 0);
ILayerpLayer = null;
IFeatureLayerpFeatureLayer = null;
IDataLayer2pDataLayer = null;
IDatasetNamepDatasetName = null;
for(int i = 0; i < mapCtrl.LayerCount; i++)
{
//获取图层
pLayer = null;
pLayer =mapCtrl.get_Layer(i);
pFeatureLayer = (IFeatureLayer)pLayer;
stringlayername = pLayer.Name;
if(pLayer is IDataLayer)
{
pDataLayer = (IDataLayer2)pFeatureLayer;
pDatasetName = (IDatasetName)pDataLayer.DataSourceName;
try
{
if (pFeatureLayer.FeatureClass != null)
{
if (!(pFeatureLayer isIDimensionLayer))
{
pDataLayer.Disconnect();
}
}
pDatasetName.WorkspaceName = pWorkspaceName;
pDataLayer.Connect((IName)pDatasetName);
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
mapCtrl.Refresh();
主要用的接口是IDataLayer2。其中对于IDimensionLayer类型的数据,pDataLayer.Disconnect();
没有实现,这里已经进行了屏蔽。