cad中大量輸入一定格式的遞增編號時,可用插件實現,效果如下:
①本插件可指定數字位數、起始號碼、加前綴、后綴、文字顏色等(字體樣式和文字所在圖層為cad當前圖層和當前字體樣式)。
②插件采用Jig方式,即插入文字過程中動態顯示文字的大小占位情況,免去插入文字后調整位置等繁瑣步驟。
③插件自動記錄用戶上次輸入前綴、后綴等信息,下次使用自動顯示上次信息,免去用戶重復輸入。
?使用方式如下(針對AutoCAD2012-2024版本):
附部分代碼如下:
public void 自動遞增編號簡單版(){Document doc = Application.DocumentManager.MdiActiveDocument;Database db = doc.Database;Editor ed = doc.Editor;************************************** 省去部分代碼 ***************** *********************************************while (true){// 獲取插入點PromptPointOptions ppo = new PromptPointOptions("\n請指定文字插入位置(ESC退出): ");PromptPointResult ppr = ed.GetPoint(ppo);if (ppr.Status != PromptStatus.OK){// 用戶按ESC退出ed.WriteMessage("\n已退出文字插入。");break;}// 轉換坐標系(UCS到WCS)Point3d insertionPoint = ppr.Value.TransformBy(ed.CurrentUserCoordinateSystem);// 開始事務處理using (Transaction tr = db.TransactionManager.StartTransaction()){try{// 獲取塊表BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;// 獲取模型空間BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;// 根據最小位數格式化編號string formattedNumber = counter.ToString().PadLeft(minDigits, '0');// 創建文字對象************************************** 省去部分代碼 ***************** *********************************************// 添加文字到模型空間btr.AppendEntity(text);tr.AddNewlyCreatedDBObject(text, true);tr.Commit();}catch (System.Exception ex){ed.WriteMessage($"\n錯誤:{ex.Message}");tr.Abort();}}counter++; // 遞增計數器ed.WriteMessage($"\n請指定插入點:");}}
?
?