当图中有大量文字,需要全部显示到一个列表时并缩放到需要的文字时,可采用插件实现,效果如下:
附部分代码如下:
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}");}}
插件联系(可另行定制功能)↓ ↓ ↓