www.pudn.com > dwg2shp.rar > dwg2shp.cs, change:2010-05-19,size:6154b


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using ESRI.ArcGIS.Controls; 
using ESRI.ArcGIS.Geodatabase; 
using ESRI.ArcGIS.DataSourcesFile; 
using System.IO; 
using ESRI.ArcGIS.Carto; 
using ESRI.ArcGIS.esriSystem; 
 
namespace dwg2shp 
{ 
    public partial class DwgToShpForm : Form 
    { 
        private string strFile = "";  //dwg文件名称 
        private string filePath = ""; //存储dwg文件路径 
        private IWorkspace outWorkspace = null;  //输出工作空间 
        private string outShpPath = "";   //shp文件输出目录 
        private IMapControl2 pMapControl = null; 
 
        public DwgToShpForm() 
        { 
            InitializeComponent(); 
        } 
 
        public DwgToShpForm(IMapControl2 mapControl) 
        { 
            InitializeComponent(); 
            pMapControl = mapControl; 
        } 
 
        private void btnOpen_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog openDwg = new OpenFileDialog(); 
            openDwg.Filter = "CAD file(*.DWG)|*.DWG|CAD DGN file(*.DGN)|*.DGN|CAD DXF file(*.DXF)|*.DXF"; 
            openDwg.Title = "DWG格式数据的输入"; 
            string dwgfileName = ""; 
            if (openDwg.ShowDialog() == DialogResult.OK) 
            { 
                dwgfileName = openDwg.FileName; 
                this.textBoxDwg.Text = dwgfileName; 
            } 
            strFile = dwgfileName.Substring(dwgfileName.LastIndexOf(@"\") + 1); 
            //MessageBox.Show("strFile="+strFile); 
            filePath = dwgfileName.Substring(0, dwgfileName.Length - strFile.Length - 1); 
            //MessageBox.Show("filePath=" + filePath); 
        } 
 
        private void btnCancel_Click(object sender, EventArgs e) 
        { 
            this.Dispose(); 
        } 
 
        private void btnOK_Click(object sender, EventArgs e) 
        { 
            //shp文件空间的数据集以及空间名 
            IDataset shpDataset = (IDataset)outWorkspace; 
            IWorkspaceName pShpWorkspaceName = (IWorkspaceName)shpDataset.FullName; 
            //CAD文件的空间数据集的遍历 
            IWorkspaceFactory pCADWorkSpaceFactory = new CadWorkspaceFactoryClass(); 
            IFeatureWorkspace pCADFeatureWorkSpace = (IFeatureWorkspace)pCADWorkSpaceFactory.OpenFromFile(filePath, 0); 
            IFeatureDataset pCADFeatureDataset = pCADFeatureWorkSpace.OpenFeatureDataset(strFile); 
            IFeatureClassContainer pFeatureClassContainer = pCADFeatureDataset as IFeatureClassContainer; 
            IEnumFeatureClass pEnumFeatureClass = pFeatureClassContainer.Classes; 
            IFeatureClass pFeatureClass = pEnumFeatureClass.Next(); 
            while (pFeatureClass != null) 
            { 
                IDataset cadDataset = (IDataset)pFeatureClass; 
                IFeatureClassName cadFeatureClassName = (IFeatureClassName)cadDataset.FullName; 
                IFeatureClassName shpFeatureClassName = new FeatureClassNameClass(); 
                IDatasetName shpDatasetName = (IDatasetName)shpFeatureClassName; 
                string sname = pFeatureClass.FeatureDataset.Name; 
                shpDatasetName.Name = sname.Replace(".dxf", "_") + pFeatureClass.AliasName; 
                shpDatasetName.WorkspaceName = pShpWorkspaceName; 
                IFieldChecker pFieldChecker = new FieldCheckerClass(); 
                IFields inFields = pFeatureClass.Fields; 
                IFields outFields = null; 
                IEnumFieldError pEnumFieldError = null; 
                pFieldChecker.InputWorkspace = (IWorkspace)pCADFeatureWorkSpace; 
                pFieldChecker.ValidateWorkspace = outWorkspace; 
                pFieldChecker.Validate(inFields, out pEnumFieldError, out outFields); 
 
                IFeatureDataConverter pFeatureDataConverter = new FeatureDataConverterClass(); 
                pFeatureDataConverter.ConvertFeatureClass(cadFeatureClassName, null, null, shpFeatureClassName, null, outFields, "", 100, 0); 
 
                pFeatureClass = pEnumFeatureClass.Next(); 
            } 
            DialogResult result = MessageBox.Show("添加到当前地图?", "添加到当前地图?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); 
            if (result == DialogResult.OK) 
            { 
                string[] filepathname = Directory.GetFiles(outShpPath, "*.shp"); 
                IFeatureWorkspace tempFeatureWorkspace = outWorkspace as IFeatureWorkspace; 
                for (int i = 0; i < filepathname.Length; i++) 
                { 
                    string filename = System.IO.Path.GetFileName(filepathname.ToString()); 
                    //MessageBox.Show(filename); 
                    IFeatureClass m_pFeatureClass = tempFeatureWorkspace.OpenFeatureClass(filename); 
                    IDataset m_pDataset = m_pFeatureClass as IDataset; 
                    IFeatureLayer m_pFeatureLayer = null; 
                    m_pFeatureLayer = new FeatureLayerClass(); 
 
 
                    m_pFeatureLayer.FeatureClass = m_pFeatureClass; 
                    m_pFeatureLayer.Name = m_pDataset.Name; 
                    //pFeatureLayer.Name = pFeatureClass.AliasName;  //这句代码也与上句一个效果 
                    ILayer pLayer = m_pFeatureLayer as ILayer; 
                    this.pMapControl.Map.AddLayer(pLayer); 
                } 
            } 
            MessageBox.Show("转换成功!"); 
        } 
 
        private void btnOutput_Click(object sender, EventArgs e) 
        { 
            outShpPath = ""; 
            FolderBrowserDialog outputDialog = new FolderBrowserDialog(); 
            if (outputDialog.ShowDialog() == DialogResult.OK) 
            { 
                outShpPath = outputDialog.SelectedPath; 
                this.textBoxOutput.Text = outShpPath; 
            } 
            IWorkspaceFactory pWorkSpaceFactory = new ShapefileWorkspaceFactoryClass(); 
            outWorkspace = pWorkSpaceFactory.OpenFromFile(outShpPath, 0); 
        } 
    } 
}