IMapDocument pMapDocment = new MapDocument();
pMapDocment.Open(madPathCopyTo);//打开mxd工程 madPathCopyTo为mxd路径
IMap pMap = pMapDocment.get_Map(0);//获取第一个Layers
IAnnotationLayer pAnnotationLayer;//声明一个注记图层
IFeatureWorkspace pFeatureWorkspace = GetWorkspace(mdbPathCopy) as IFeatureWorkspace;//获取mdb数据库工作空间
pAnnotationLayer = GetAnnoationFeatureLayer(pFeatureWorkspace,"T_point");//打开数据库中注记图层T_point
pMap.AddLayer(pAnnotationLayer as ILayer);//加载到map中
pMapDocment.Save();//保存
/// <summary>
/// 加载注记图层
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
public IAnnotationLayer GetAnnoationFeatureLayer(IFeatureWorkspace pFeatureWorkspace, string name)
{
IAnnotationLayerFactory pAnnotationLayerFactory = new FDOGraphicsLayerFactory() as IAnnotationLayerFactory;
IAnnotationLayer pAnnotationLayer = pAnnotationLayerFactory.OpenAnnotationLayer(pFeatureWorkspace,null,name);
return pAnnotationLayer;
}
这里打开注记图层要传三个参数进去,第一个是工作空间,第二个是数据集(可用null代替),第三个是名字。
/// <summary>
/// 获取Workspace
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public IWorkspace GetWorkspace(string path)
{
IWorkspace pWorkspace;
if (path.Contains(".mdb"))
{
IWorkspaceFactory pWorkspaceFactory = new AccessWorkspaceFactory();
pWorkspace = pWorkspaceFactory.OpenFromFile(path, 0);
}
else
{
IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
pWorkspace = pWorkspaceFactory.OpenFromFile(path, 0);
}
return pWorkspace;
}