日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

批量输出dwg文件中的文本

發布時間:2023/12/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 批量输出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文件中的文本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。