批量输出dwg文件中的文本
生活随笔
收集整理的這篇文章主要介紹了
批量输出dwg文件中的文本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
公司來了一批圖紙,里面有一部分內容需要復制到excel中,幾百張來圖每一張都
手工復制,煩死了。編寫一個CAD插件,自動導出文本,簡單記錄在下面。
想法是:
1.輸入命令,選擇所有dwg文件
2.挨個處理dwg文件,生成同名的txt文件保存文本
基本思路是用Database.ReadDwgFile 讀取dwg文件,因為這樣可以不用顯示文檔,可以提高速度;
[CommandMethod("GetText")]public void GetTextCST(){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = "dwg files (*.dwg)|*.dwg|All files (*.*)|*.*";ofd.FilterIndex = 1;ofd.RestoreDirectory = true;ofd.Multiselect = true;if(ofd.ShowDialog()== DialogResult.OK)foreach (string fn in ofd.FileNames)try{DoDwg2Csv(fn);}catch{File.AppendAllText("D:\\dwg2csv.txt", DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + fn + "\n");}}private void DoDwg2Csv(string fn){string csvname = fn + ".csv";using (Database db = new Database(false, true)){db.ReadDwgFile(fn, FileOpenMode.OpenForReadAndAllShare, false, "");db.CloseInput(true);using (Transaction tr = db.TransactionManager.StartTransaction()){// 模型空間BlockTable blkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;BlockTableRecord modelSpace = tr.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead)as BlockTableRecord;// 遍歷模型空間,提取文字 List<DBText> txts = new List<DBText>();foreach (ObjectId oid in modelSpace){DBObject dbobj = tr.GetObject(oid, OpenMode.ForRead);if (dbobj is Entity){Entity entity = dbobj as Entity;string enttype = entity.GetRXClass().Name;if (enttype == "AcDbText"){DBText acText = entity as DBText;if (acText.Position.X < 587 || acText.Position.Y < 115)//指定范圍continue; txts.Add( acText );}}//if (dbobj is Entity)}//foreach (ObjectId oid in modelSpace)txts.Sort((t1, t2) => t1.Position.X >= t2.Position.X ? 1 : -1);for (int i = 0; i < txts.Count;i++)File.AppendAllText(csvname, txts[i].TextString+"\r\n", Encoding.Default);}//using}//using }
?
轉載于:https://www.cnblogs.com/sinceret/p/10108866.html
總結
以上是生活随笔為你收集整理的批量输出dwg文件中的文本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mac mini 键盘调节显示器亮度
- 下一篇: 经典坦克大战的python实现