C# Cad2016二次开发选择文本信息导出(六)

发布时间:2024年01月15日
//选文本信息导出
[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();
        }
    }
}

?

文章来源:https://blog.csdn.net/hb_ljj/article/details/135612784
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。