How to read DWG files in C#

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


1. Introduction to code snippets

To read DWG files in C#, you need to use CAD software or CAD-related APIs. Here I introduce two methods: using AutoCAD COM objects or using DWG TrueView API.

2. Implementation method 1

  1. Working with AutoCAD COM objects

AutoCAD is a widely used CAD software that provides a COM object model that enables access to AutoCAD functions through programming languages ​​such as C#. The following is a simple example that demonstrates how to use AutoCAD COM objects to read entity (Entity) information in DWG files:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

public void ReadEntitiesFromDWG(string dwgFilePath)
{
    
    
    // 启动AutoCAD应用程序
    var app = new Application();

    // 打开指定的DWG文件
    var doc = app.Documents.Open(dwgFilePath, false);

    // 获取当前文档的数据库对象
    var db = doc.Database;

    // 开启事务
    using (var trans = db.TransactionManager.StartTransaction())
    {
    
    
        // 获取所有的实体对象
        var entites = new DBObjectCollection();
        foreach (ObjectId id in db.BlockTableId.GetObjectIds())
        {
    
    
            var btr = trans.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
            if (btr != null)
            {
    
    
                foreach (ObjectId entityId in btr)
                {
    
    
                    var entity = trans.GetObject(entityId, OpenMode.ForRead) as Entity;
                    if (entity != null)
                    {
    
    
                        entites.Add(entity);
                    }
                }
            }
        }

        // 打印实体信息
        foreach (Entity entity in entites)
        {
    
    
            Console.WriteLine($"Entity {
      
      entity.GetType().Name} found");
        }

        // 提交事务并关闭文档
        trans.Commit();
        doc.CloseAndDiscard();
    }

    // 退出AutoCAD应用程序
    app.Quit();
}

It should be noted that to use AutoCAD COM objects to read DWG files, it is necessary to ensure that AutoCAD software has been installed on the computer and that the program is consistent with the version of AutoCAD.

3. Implementation method 2

  1. Using the DWG TrueView API

DWG TrueView is a free DWG file viewer that also provides a .NET API that can be used to read DWG files. The following is a simple example that demonstrates how to use the DWG TrueView API to read entity information in a DWG file:

using System;
using System.IO;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.DwgTrueView.ApplicationServices;
using Autodesk.DwgTrueView.DatabaseServices;

public void ReadEntitiesFromDWG(string dwgFilePath)
{
    
    
    // 创建DWG TrueView应用程序对象
    var app = new DwgApplication();

    // 打开指定的DWG文件
    var db = new Database(false, true);
    db.ReadDwgFile(dwgFilePath, FileShare.ReadWrite, true, "");

    // 开启事务
    using (var trans = db.TransactionManager.StartTransaction())
    {
    
    
        // 获取所有的实体对象
        var entites = new DBObjectCollection();
        foreach (ObjectId id in db.BlockTableId.GetObjectIds())
        {
    
    
            var btr = trans.GetObject(id, OpenMode.ForRead) as BlockTableRecord;
            if (btr != null)
            {
    
    
                foreach (ObjectId entityId in btr)
                {
    
    
                    var entity = trans.GetObject(entityId, OpenMode.ForRead) as Entity;
                    if (entity != null)
                    {
    
    
                        entites.Add(entity);
                    }
                }
            }
        }

        // 打印实体信息
        foreach (Entity entity in entites)
        {
    
    
            Console.WriteLine($"Entity {
      
      entity.GetType().Name} found");
        }

        // 提交事务并关闭数据库
        trans.Commit();
        db.Dispose();
    }

    // 退出DWG TrueView应用程序
    app.Quit();
}

Summarize

It should be noted that, to use DWG TrueView API to read DWG files, DWG TrueView software needs to be installed on the computer, and corresponding references need to be added.

Guess you like

Origin blog.csdn.net/2302_77270563/article/details/129968069