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

//选文本信息导出
[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();
        }
    }
}

C# Cad2016二次开发选择文本信息导出(六)_第1张图片

C# Cad2016二次开发选择文本信息导出(六)_第2张图片

 

你可能感兴趣的:(CAD二次开发,c#,c语言,数据库)