C#+ArcEngine:載入開啟CAD資料(VS2010窗體+程式碼)
阿新 • • 發佈:2019-02-07
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 整幅載入ToolStripMenuItem_Click(object sender, EventArgs e)
{
#region 一:整幅載入
try
{
this.axMapControl1.ActiveView.Clear();//清空當前檢視
OpenFileDialog xjCADOpenFileDialog = new OpenFileDialog();
xjCADOpenFileDialog.Title = "開啟CAD資料檔案";
xjCADOpenFileDialog.Filter = "CAD資料(*.dwg)|*.dwg";
if (xjCADOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string xjCADFullPath = xjCADOpenFileDialog.FileName;//檔案絕對路徑
int xjIndex = xjCADFullPath.LastIndexOf("\\");
string xjCADFilePath = xjCADFullPath.Substring(0, xjIndex);//檔案目錄
string xjCADFileName = xjCADFullPath.Substring(xjIndex + 1);//檔名稱
//開啟CAD資料集
IWorkspaceFactory xjCADWsF = new CadWorkspaceFactoryClass();//using ESRI.ArcGIS.Geodatabase;//using ESRI.ArcGIS.DataSourcesFile;
IFeatureWorkspace xjCADFWs = (IFeatureWorkspace)xjCADWsF.OpenFromFile(xjCADFilePath, 0);
IFeatureDataset xjCADFeatureDataset = xjCADFWs.OpenFeatureDataset(xjCADFileName);
IFeatureClassContainer xjFeatClassContainer = (IFeatureClassContainer)xjCADFeatureDataset;//管理IFeatureDataset中的每個要素類
//遍歷
for (int i = 0; i < xjFeatClassContainer.ClassCount; i++)
{
IFeatureClass xjFeatureClass = xjFeatClassContainer.get_Class(i);//要素集
if (xjFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)//註記層
{
IFeatureLayer xjCADFeatureLayer = new CadAnnotationLayerClass();//using ESRI.ArcGIS.Carto;
xjCADFeatureLayer.Name = xjFeatureClass.AliasName;
xjCADFeatureLayer.FeatureClass = xjFeatureClass;
this.axMapControl1.Map.AddLayer(xjCADFeatureLayer);
}
else //點、線、面
{
IFeatureLayer xjCADFeatureLayer = new FeatureLayerClass();
xjCADFeatureLayer.Name = xjFeatureClass.AliasName;
xjCADFeatureLayer.FeatureClass = xjFeatureClass;
this.axMapControl1.Map.AddLayer(xjCADFeatureLayer);
}
this.axMapControl1.ActiveView.Refresh();//重新整理
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 整幅載入ToolStripMenuItem_Click(object sender, EventArgs e)
{
#region 一:整幅載入
try
{
this.axMapControl1.ActiveView.Clear();//清空當前檢視
OpenFileDialog xjCADOpenFileDialog = new OpenFileDialog();
xjCADOpenFileDialog.Title = "開啟CAD資料檔案";
xjCADOpenFileDialog.Filter = "CAD資料(*.dwg)|*.dwg";
if (xjCADOpenFileDialog.ShowDialog() == DialogResult.OK)
{
string xjCADFullPath = xjCADOpenFileDialog.FileName;//檔案絕對路徑
int xjIndex = xjCADFullPath.LastIndexOf("\\");
string xjCADFilePath = xjCADFullPath.Substring(0, xjIndex);//檔案目錄
string xjCADFileName = xjCADFullPath.Substring(xjIndex + 1);//檔名稱
//開啟CAD資料集
IWorkspaceFactory xjCADWsF = new CadWorkspaceFactoryClass();//using ESRI.ArcGIS.Geodatabase;//using ESRI.ArcGIS.DataSourcesFile;
IFeatureWorkspace xjCADFWs = (IFeatureWorkspace)xjCADWsF.OpenFromFile(xjCADFilePath, 0);
IFeatureDataset xjCADFeatureDataset = xjCADFWs.OpenFeatureDataset(xjCADFileName);
IFeatureClassContainer xjFeatClassContainer = (IFeatureClassContainer)xjCADFeatureDataset;//管理IFeatureDataset中的每個要素類
//遍歷
for (int i = 0; i < xjFeatClassContainer.ClassCount; i++)
{
IFeatureClass xjFeatureClass = xjFeatClassContainer.get_Class(i);//要素集
if (xjFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)//註記層
{
IFeatureLayer xjCADFeatureLayer = new CadAnnotationLayerClass();//using ESRI.ArcGIS.Carto;
xjCADFeatureLayer.Name = xjFeatureClass.AliasName;
xjCADFeatureLayer.FeatureClass = xjFeatureClass;
this.axMapControl1.Map.AddLayer(xjCADFeatureLayer);
}
else //點、線、面
{
IFeatureLayer xjCADFeatureLayer = new FeatureLayerClass();
xjCADFeatureLayer.Name = xjFeatureClass.AliasName;
xjCADFeatureLayer.FeatureClass = xjFeatureClass;
this.axMapControl1.Map.AddLayer(xjCADFeatureLayer);
}
this.axMapControl1.ActiveView.Refresh();//重新整理
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
}
}
VS2010+ArcEngine10.1具體窗體+程式碼見:點選開啟連結