CAD 审图意见的导出

2023-12-16 14:35:44

看图的时候喜欢在图上直接标注意见,但是如果还要再把意见一行一行的导出到word里面就很麻烦,在网上看了一个审图软件,报价要980,而且那个审图意见做的太复杂了。

我的需求就是把图上标的单行文字和多行文字直接导出来就行,因此,采用C#写了这个程序。

不多说,直接上图,上代码。

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using System.Xml.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.IO;

namespace dcyj
{
    public class Class1
    {

        //审图意见导出,内置了默认路径和默认图层,避免重复劳动
        [CommandMethod("stjjdc")]
        public void BlockInLayerCAD()
        {
            Document arcdoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database arcdb = HostApplicationServices.WorkingDatabase;
            Editor ed = arcdoc.Editor;
            // 创建文本
            StreamWriter txtWtriter = new StreamWriter(Stream.Null);
            if (File.Exists("E:\\24审图意见\\temple_yjdc.txt"))
            {
                File.Delete("E:\\24审图意见\\temple_yjdc.txt");
            }
            String layerforexp = "00000styj";
            ed.WriteMessage("\n 先把文字都调整到00000图层,文本保持目录为:E:\\24审图意见\\temple_yjdc.txt");
            using (Transaction trans = arcdb.TransactionManager.StartTransaction())
            {              
                txtWtriter = File.AppendText("E:\\24审图意见\\temple_yjdc.txt");
                txtWtriter.WriteLine("以下意见详见CAD文件批注。");
                LayerTable lt = (LayerTable)trans.GetObject(arcdb.LayerTableId, OpenMode.ForRead);
                if (!(lt.Has(layerforexp)))
                {
                    ed.WriteMessage("\n 找不图层:00000styj,请先把文字都调整到00000styj图层");
                }
                else
                {
                    ed.WriteMessage("\n 已找到图层:00000styj");
                    BlockTableRecord ltr = (BlockTableRecord)trans.GetObject(arcdb.CurrentSpaceId, OpenMode.ForRead);
                    String stryj = "";
                    String enttype = "";
                    int index_YJ = 1;
                    foreach (ObjectId objectid in ltr)
                    {
                        Entity ent = (Entity)trans.GetObject(objectid, OpenMode.ForRead);
                        enttype= ent.GetType().Name;
                       // ed.WriteMessage("\n enttype是:"+ enttype);
                        if (enttype.Equals("MText")&&ent.Layer.Equals(layerforexp)) {
                            stryj = index_YJ+(ent as MText).Text.ToString();
                            txtWtriter.WriteLine(stryj);
                            ed.WriteMessage("\n 文字内容是:" + stryj);
                            index_YJ++;
                        }
                        else if (enttype.Equals("DBText") && ent.Layer.Equals(layerforexp))
                        {
                            stryj = index_YJ + (ent as DBText).TextString.ToString();
                            txtWtriter.WriteLine(stryj);
                            ed.WriteMessage("\n 文字内容是:" + stryj);
                            index_YJ++;
                        }
                    }
                } 
                trans.Commit();
                txtWtriter.Flush();
                txtWtriter.Close();
            }
        }
    }
}

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