當圖中有大量文字,需要全部顯示到一個列表時并縮放到需要的文字時,可采用插件實現,效果如下:
附部分代碼如下:
private void BtnSelectText_Click(object sender, EventArgs e){var doc = Application.DocumentManager.MdiActiveDocument;var db = doc.Database;var ed = doc.Editor;// 激活CAD窗口(修復問題1)Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Focus();// 創建選擇過濾器(新增批量選擇功能)var filter = new SelectionFilter(new[]{new TypedValue((int)DxfCode.Operator, "<or"), // 開始邏輯或new TypedValue((int)DxfCode.Start, "TEXT"),new TypedValue((int)DxfCode.Start, "MTEXT"),new TypedValue((int)DxfCode.Operator, "or>") // 結束邏輯或});var options = new PromptSelectionOptions{MessageForAdding = "\n選擇要添加的文字: ",AllowDuplicates = false};try{// 執行批量選擇(替換原來的GetEntity)var result = ed.GetSelection(options, filter);if (result.Status != PromptStatus.OK) return;// 獲取選中的ObjectId并去重var selectedIds = new HashSet<ObjectId>(result.Value.GetObjectIds());// 獲取現有表格中的ObjectIdvar existingIds = new HashSet<ObjectId>();dataGridView.Invoke((MethodInvoker)delegate{foreach (DataGridViewRow row in dataGridView.Rows){if (row.Tag is ObjectId id){existingIds.Add(id);}}});// 計算需要添加的新IDvar newIds = selectedIds.Except(existingIds).ToList();if (newIds.Count == 0){ed.WriteMessage("\n沒有新文字需要添加!\n");return;}using (Transaction tr = db.TransactionManager.StartTransaction()){// 遍歷所有選中的對象(新增批量處理)foreach (var objectId in newIds){var text = tr.GetObject(objectId, OpenMode.ForRead) as Entity;string content = "";string color = "";if (text is DBText dbText){content = dbText.TextString;color = dbText.Color.ToString();}else if (text is MText mText){content = mText.Contents; // 修正MText內容獲取方式color = mText.Color.ToString();}// 跨線程更新UI(重要!)dataGridView.Invoke((MethodInvoker)delegate{// 創建新行并存儲ObjectIdvar index = dataGridView.Rows.Add(content, color);dataGridView.Rows[index].Tag = objectId; // 關鍵修改:存儲對象ID});}tr.Commit();}}catch (System.Exception ex){ed.WriteMessage($"\n選擇錯誤: {ex.Message}");}}
插件聯系(可另行定制功能)↓?↓?↓?