MapControl加载CAD数据的示例代码
cadFile代表AutoCad文件的路径
private void btnLoadCAD_Click(object sender, EventArgs e)
{
string cadFile = @"F:AEStudyzxgTest.dwg";
string workspacePath = System.IO.Path.GetDirectoryName(cadFile);
string fileName = System.IO.Path.GetFileName(cadFile);
//Add passed file to MapControl
ICadDrawingDataset cadDrawingDataset = GetCadDataset(workspacePath, fileName);
if (cadDrawingDataset == null) return;
ICadLayer cadLayer = new#p#分页标题#e# CadLayerClass();
cadLayer.CadDrawingDataset = cadDrawingDataset;
cadLayer.Name = fileName;
mapMain.AddLayer(cadLayer, 0);
}
private ICadDrawingDataset GetCadDataset(string cadWorkspacePath, string cadFileName)
{
//Create a WorkspaceName object
IWorkspaceName workspaceName = new WorkspaceNameClass();
workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.CadWorkspaceFactory";
workspaceName.PathName = cadWorkspacePath;
//Create a CadDrawingName object
IDatasetName cadDatasetName #p#分页标题#e#= new CadDrawingNameClass();
cadDatasetName.Name = cadFileName;
cadDatasetName.WorkspaceName = workspaceName;
//Open the CAD drawing
IName name = (IName)cadDatasetName;
return (ICadDrawingDataset)name.Open();
}
{
string cadFile = @"F:AEStudyzxgTest.dwg";
string workspacePath = System.IO.Path.GetDirectoryName(cadFile);
string fileName = System.IO.Path.GetFileName(cadFile);
//Add passed file to MapControl
ICadDrawingDataset cadDrawingDataset = GetCadDataset(workspacePath, fileName);
if (cadDrawingDataset == null) return;
ICadLayer cadLayer = new#p#分页标题#e# CadLayerClass();
cadLayer.CadDrawingDataset = cadDrawingDataset;
cadLayer.Name = fileName;
mapMain.AddLayer(cadLayer, 0);
}
private ICadDrawingDataset GetCadDataset(string cadWorkspacePath, string cadFileName)
{
//Create a WorkspaceName object
IWorkspaceName workspaceName = new WorkspaceNameClass();
workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.CadWorkspaceFactory";
workspaceName.PathName = cadWorkspacePath;
//Create a CadDrawingName object
IDatasetName cadDatasetName #p#分页标题#e#= new CadDrawingNameClass();
cadDatasetName.Name = cadFileName;
cadDatasetName.WorkspaceName = workspaceName;
//Open the CAD drawing
IName name = (IName)cadDatasetName;
return (ICadDrawingDataset)name.Open();
}
比较困惑的是AutoCAD和地图文件的坐标怎么匹配。