.Net/C#读取CAD软件dwg、dxf数据表实体

.Net/C#读取CAD软件dwg、dxf数据表实体

使用ACadSharp库读取CAD软件dwg数据表实体

文末附ACadSharp.dll库文件及源码

CadDocReader

using ACadSharp;
using ACadSharp.Entities;
using ACadSharp.IO;
using CSMath;
using System.Text.RegularExpressions;

namespace NCad.Toolkits
{
    internal class CadDocReader
    {
        public CadDocReader(string filePath)
        {
            this.FilePath = filePath;
        }

        public string FilePath { get; private set; }

        public void ReadTable()
        {
            string filePath = this.FilePath;
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(filePath);
            }
            CadDocument cadDocument = LoadCadDocument(filePath);
            foreach (var blockRecord in cadDocument.BlockRecords)
            {
                // 过滤非表实体
                if (!blockRecord.Name.StartsWith("*T"))
                {
                    continue;
                }
                if (blockRecord.Entities.Count < 1)
                {
                    continue;
                }
                for (int i = 0; i < blockRecord.Entities.Count; ++i)
                {
                    Entity entity = blockRecord.Entities[i];
                    if (entity.ObjectName.EndsWith("TEXT"))
                    {
                        MText mText = entity as MText;
                        // 实体文本内容
                        string entityText;
                        // 锚点
                        XYZ insertPoint;
                        if (mText == null)
                        {
                            TextEntity textEntity = entity as TextEntity;
                            entityText = textEntity.Value;
                            insertPoint = textEntity.InsertPoint;
                        }
                        else
                        {
                            entityText = mText.Value;
                            insertPoint = mText.InsertPoint;
                        }
                        // 移除文本字体样式字符
                        if (!string.IsNullOrEmpty(entityText))
                        {
                            string pattern = @"\\f.*?;";
                            entityText = Regex.Replace(entityText, pattern, string.Empty).TrimStart('{').TrimEnd('}');
                        }
                        Console.Write(entityText);
                        Console.Write("\t");
                    }
                }
                Console.WriteLine();
                Console.WriteLine();
            }
        }

        /// 
        /// 加载CAD文档
        /// 
        /// 文件路径
        /// CAD文档
        /// 文件类型不支持异常
        private CadDocument LoadCadDocument(string filePath)
        {
            string extension = Path.GetExtension(filePath);
            CadDocument cadDocument = null;
            if (extension.Equals(".dwg", StringComparison.OrdinalIgnoreCase))
            {
                DwgReader dwgReader = null;
                try
                {
                    dwgReader = new DwgReader(filePath);
                    cadDocument = dwgReader.Read();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    dwgReader?.Dispose();
                }
            }
            else if (extension.Equals(".dxf", StringComparison.OrdinalIgnoreCase))
            {
                DxfReader dxfReader = null;
                try
                {
                    dxfReader = new DxfReader(filePath);
                    cadDocument = dxfReader.Read();
                }
                catch
                {
                    throw;
                }
                finally
                {
                    dxfReader?.Dispose();
                }
            }
            else
            {
                throw new NotSupportedException($"文件:{filePath},不支持类型:{extension}");
            }
            return cadDocument;
        }
    }
}

调用代码

string dwgFile = "D:\\admin\\Desktop\\测试图-测绘&dfx&表格\\8东8支8北\\01-夏茅站结构总平面图.dwg";
CadDocReader cadDocReader = new CadDocReader(dwgFile);
cadDocReader.ReadTable();

测试输出

用地信息列表    类型    权属单位        地块面积(㎡)  拆迁/临迁房屋面积(㎡) 征地    AAAAAAA 5674.3934       0.0000  征地    BBBBBBB 5297.4953       0.0000  借地    CCCCCCCC        92314.0540      0.0000  拆迁    DDDDDDD 2497.0416      23213213.6568    临迁    EEEEEEE 1456.0170       343423434.5650  临迁    FFFFFFF 494.0636        453535.7878     临迁   GGGGGGG  494.0468        567676.6760     临迁    HHHHHHH 558.6505        645546.8780     临迁    IIIIIII 682.7144       3432424.8787     临迁    JJJJJJJ 682.7144        5676767.4342    临迁    KKKKKKK 682.7144        2324324.67868

.Net/C#读取CAD软件dwg、dxf数据表实体_第1张图片

ACadSharp.dll库文件及源码

https://shaoshao.lanzouw.com/b0ny05i9a
密码:4ih4

你可能感兴趣的:(.NET,.net,c#)