www.pudn.com > CADToSHP.rar > Form1.cs, change:2013-04-20,size:6693b


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 System.IO; 
using System.Collections; 
 
namespace CAD转SHP命令记录工具 
{ 
    public partial class Form1 : Form 
    { 
        private static string result = ""; 
 
        ArrayList patternList; 
        ArrayList file = new ArrayList(); 
        ArrayList FileNames = new ArrayList(); 
         
        public Form1() 
        { 
            InitializeComponent(); 
            patternList = new ArrayList(); 
            patternList.Add("*.dwg"); 
            patternList.Add("*.dgn"); 
            patternList.Add("*.dxf"); 
 
            file.Clear(); 
            setListView(); 
        } 
 
        //打开CAD路径 
        private void button1_Click(object sender, EventArgs e) 
        { 
            FolderBrowserDialog dialog1 = new FolderBrowserDialog(); 
            dialog1.Description = "请选择CAD/DGN文件路径"; 
             
 
            if (dialog1.ShowDialog()==DialogResult.OK) 
            { 
                string foldpath=dialog1.SelectedPath; 
                textBox1.Text =foldpath; 
            } 
            try 
            { 
                if (textBox1.Text != "") 
                { 
                    DirectoryInfo dir = new DirectoryInfo(textBox1.Text);                     
                    file = FindFile(dir, patternList);  
                } 
                 
                if (file.Count > 0) 
                { 
                    //listBox1.Items.Add(file[0]); 
                    for (int i = 0; i < file.Count; i++) 
                    { 
                        int inttemp = 0; 
                        inttemp = i + 1; 
                        listView1.Items.Add(file[i].ToString(), inttemp.ToString(), 0); 
                        listView1.Items[file[i].ToString()].SubItems.Add(file[i].ToString()); 
                    } 
                } 
                     
            } 
            catch(Exception ee) 
            { 
                MessageBox.Show(ee.Message); 
            } 
                 
 
        } 
 
        public  ArrayList FindFile(DirectoryInfo dir,ArrayList pattern) 
        { 
            try 
            {                
                if (dir.Exists) 
                { 
                    for(int i=0;i<pattern.Count;i++) 
                    {                         
                        string str = pattern[i].ToString(); 
                        if (str.Trim() != string.Empty) 
 
                        { 
                            foreach (FileInfo info in dir.GetFiles(str)) 
                            { 
                                result = info.FullName.ToString(); 
                                FileNames.Add(result); 
                            } 
                        }                         
                    } 
 
                    foreach(DirectoryInfo info in dir.GetDirectories()) 
                    { 
                        FindFile(info,pattern); 
                    } 
                }                
                 
            } 
            catch(Exception e) 
            { 
                MessageBox.Show(e.Message); 
            } 
            
            return FileNames; 
        } 
 
        public void setListView() 
        { 
            listView1.Columns.Add("序号",60,HorizontalAlignment.Center); 
            listView1.Columns.Add("CAD路径",1000,HorizontalAlignment.Left); 
            listView1.View = View.Details; 
        } 
 
        private void button2_Click(object sender, EventArgs e) 
        { 
            FolderBrowserDialog dialog1 = new FolderBrowserDialog(); 
            dialog1.Description = "请选择保存SHP文件路径"; 
 
 
            if (dialog1.ShowDialog() == DialogResult.OK) 
            { 
                string foldpath = dialog1.SelectedPath; 
                textBox2.Text = foldpath; 
            } 
        } 
 
        private void button3_Click(object sender, EventArgs e) 
        { 
            FolderBrowserDialog dialog1 = new FolderBrowserDialog(); 
            dialog1.Description = "请选择保存文本文件路径"; 
 
            if (dialog1.ShowDialog() == DialogResult.OK) 
            { 
                string foldpath = dialog1.SelectedPath; 
                textBox3.Text = foldpath+"\\"+"CAD2SHP.txt"; 
                DirectoryInfo dir = new DirectoryInfo(foldpath); 
                dir.Create(); 
                File.Create(textBox3.Text).Close(); 
                 
            } 
        } 
 
        private void button4_Click(object sender, EventArgs e) 
        { 
            ArrayList pattern = new ArrayList(); 
            pattern.Add("Multipatch"); 
            pattern.Add("Annotation"); 
            pattern.Add("Point"); 
            pattern.Add("Polygon"); 
            pattern.Add("Polyline"); 
 
            //检查写入文件是否存在 
            if (!File.Exists(textBox3.Text)) 
            { 
                File.Create(textBox3.Text).Close(); 
            } 
 
            FileStream aFile = new FileStream(textBox3.Text, FileMode.OpenOrCreate); 
            StreamWriter sw = new StreamWriter(aFile); 
 
 
            try 
            { 
                sw.WriteLine("#coding=GBK"); 
                sw.WriteLine("import codecs"); 
                //sw.WriteLine("import sys"); 
                //sw.WriteLine("reload(sys)"); 
                //sw.WriteLine("sys.setdefaultencoding('gbk')"); 
                sw.WriteLine("import arcpy"); 
                if (file.Count > 0) 
                { 
                    for (int i = 0;i< file.Count; i++) 
                    { 
                        string inputfeature,outputfeature,sql; 
                        for (int j = 0; j < 5; j++) 
                        { 
                            inputfeature = file[i] + "\\" + pattern[j].ToString(); 
                            inputfeature.Replace("\\", "\\\\"); 
                            outputfeature = textBox2.Text; 
                            outputfeature.Replace("\\", "\\\\"); 
                            sql = "arcpy.FeatureClassToShapefile_conversion(" + '"' + inputfeature + '"' + "," + '"' + outputfeature +  '"'+")"; 
                            sw.WriteLine(sql); 
                        } 
                    } 
 
                    sw.Flush(); 
                    sw.Close(); 
                } 
            } 
            catch (Exception ee) 
            { 
                MessageBox.Show("文件写入失败!"); 
            } 
 
             
 
        } 
    } 
}