//选文本信息导出
[CommandMethod("getdata")]
public void getdata()
{// 获取当前文档和数据库Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;Database db = doc.Database;Editor ed = doc.Editor;// 获取当前图形数据库的路径string path = db.Filename;// 提示用户选择文本PromptSelectionResult selectionResult = doc.Editor.GetSelection(new SelectionFilter(new TypedValue[] { new TypedValue((int)DxfCode.Start, "TEXT") }));if (selectionResult.Status == PromptStatus.OK){using (Transaction trans = db.TransactionManager.StartTransaction()){SelectionSet selection = selectionResult.Value;StreamWriter mydata = new StreamWriter(path + "文字信息.csv", append: true, Encoding.Default);string value = "图层,名称,X,Y,字高,颜色\t,";mydata.WriteLine(value);// 遍历选择集中的文本对象foreach (ObjectId id in selection.GetObjectIds()){DBText text = trans.GetObject(id, OpenMode.ForRead) as DBText;if (text != null){// 获取图层名称string layerName = text.Layer;//文本信息string textContent = text.TextString;// 获取文本坐标double xPos = text.Position.X;double yPos = text.Position.Y;// 获取文本颜色int colorIndex = text.Color.ColorIndex;// 获取文本字高double textHeight = text.Height;//拼接CSV字符串value = layerName + "," + textContent + "," + xPos + "," + yPos + "," + textHeight + "," + colorIndex + "\t,";// 打印输入输出ed.WriteMessage("\n" + value);mydata.WriteLine(value);}}mydata.Close();// 打印输入输出ed.WriteMessage("\n导出完成");trans.Commit();}}
}