Teigha应用——解析CAD文件(DWG格式)Teigha在CAD C#二次开发中的基本应用

发布于:2025-05-28 ⋅ 阅读:(21) ⋅ 点赞:(0)

        Teigha是一款专为开发者设计的工具,其核心技术在于强大的API和丰富的功能集,提供了一系列工具和方法,使开发者能够轻松地读取、解析和操作DWG文件。它支持多种操作系统,能在处理大型DWG文件时保持高效性能,还可用于构建数据转换工具,将DWG文件转换为其他格式,或进行反向转换。

        此外,Teigha能与BIM软件集成,支持DWG文件的导入和导出,提升BIM模型的数据兼容性。众多CAD软件替代品,如Bricscad、ZWCAD和IntelliCAD等,也都依赖Teigha来处理DWG格式和与Acad对象进行交互。


Teigha简介


Teigha是Bentley公司提供的CAD文件格式开发库,允许开发者在不依赖AutoCAD的情况下读写DWG/DXF文件。与AutoCAD .NET API相比,Teigha具有跨平台特性,支持多种开发语言和操作系统。
 
核心命名空间
 
-  Bentley.MicroStation.DgnPlatformNET :基础平台功能
 
-  Bentley.DgnPlatformNET :DGN文件操作
 
-  Bentley.DwgPlatformNET :DWG文件操作
 
-  Bentley.GeometryNET :几何图形处理
 
-  Bentley.MathNET :数学计算功能
 
基本功能示例与代码
 
下面通过几个典型场景演示Teigha在C#中的基本应用,包括文件读写、图形创建、对象遍历和属性操作:

using System;
using System.Collections.Generic;
using Bentley.DwgPlatformNET;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using Bentley.MathNET;
using Bentley.Transactions;

namespace TeighaCADExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化Teigha平台
            InitializeTeigha();

            try
            {
                // 1. 创建新DWG文件并添加图形
                CreateNewDWGFile();
                
                // 2. 读取DWG文件并遍历对象
                ReadAndProcessDWGFile();
                
                // 3. 修改现有图形对象属性
                ModifyEntityProperties();
                
                // 4. 执行图形集合运算
                PerformGeometryOperations();
                
                Console.WriteLine("Teigha示例执行完成!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误: {ex.Message}");
            }
            finally
            {
                // 清理资源
                CleanUpTeigha();
            }
            
            Console.ReadKey();
        }

        // 初始化Teigha平台
        static void InitializeTeigha()
        {
            // 加载Teigha许可证
            License BentleyLicense = new License();
            BentleyLicense.SetProductLevel(License.ProductLevels.Professional);
            Console.WriteLine("Teigha平台初始化成功");
        }

        // 清理Teigha资源
        static void CleanUpTeigha()
        {
            // 释放许可证等资源
            Console.WriteLine("Teigha资源已清理");
        }

        // 示例1:创建新DWG文件并添加图形
        static void CreateNewDWGFile()
        {
            string filePath = @"C:\Temp\TeighaExample.dwg";
            
            // 创建DWG文档
            using (DwgDocument doc = new DwgDocument())
            {
                // 开始事务
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    // 获取模型空间
                    BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
                    
                    // 1. 添加直线
                    LineString line = new LineString();
                    line.StartPoint = new Point3d(0, 0, 0);
                    line.EndPoint = new Point3d(10, 0, 0);
                    
                    // 创建DwgLine实体
                    DwgLine dwgLine = new DwgLine();
                    dwgLine.Geometry = line;
                    
                    // 添加到模型空间
                    modelSpace.AppendEntity(dwgLine);
                    tr.AddNewlyCreatedDBObject(dwgLine, true);
                    Console.WriteLine("已添加直线");
                    
                    // 2. 添加圆
                    Circle circle = new Circle();
                    circle.Center = new Point3d(5, 5, 0);
                    circle.Radius = 3.0;
                    
                    DwgCircle dwgCircle = new DwgCircle();
                    dwgCircle.Geometry = circle;
                    
                    modelSpace.AppendEntity(dwgCircle);
                    tr.AddNewlyCreatedDBObject(dwgCircle, true);
                    Console.WriteLine("已添加圆");
                    
                    // 3. 添加文本
                    TextElement text = new TextElement();
                    text.Text = "Teigha示例文本";
                    text.Position = new Point3d(5, 0, 0);
                    text.Height = 1.0;
                    
                    DwgText dwgText = new DwgText();
                    dwgText.Geometry = text;
                    
                    modelSpace.AppendEntity(dwgText);
                    tr.AddNewlyCreatedDBObject(dwgText, true);
                    Console.WriteLine("已添加文本");
                    
                    // 提交事务并保存文件
                    tr.Commit();
                    doc.SaveAs(filePath);
                    Console.WriteLine($"文件已保存至: {filePath}");
                }
            }
        }

        // 示例2:读取DWG文件并遍历对象
        static void ReadAndProcessDWGFile()
        {
            string filePath = @"C:\Temp\TeighaExample.dwg";
            
            if (!System.IO.File.Exists(filePath))
            {
                Console.WriteLine("示例文件不存在,请先执行创建文件操作");
                return;
            }
            
            using (DwgDocument doc = new DwgDocument())
            {
                // 打开DWG文件
                doc.Open(filePath);
                Console.WriteLine("已打开DWG文件");
                
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    // 获取模型空间
                    BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
                    
                    Console.WriteLine("开始遍历模型空间对象:");
                    int entityCount = 0;
                    
                    // 遍历模型空间所有实体
                    foreach (DBObject obj in modelSpace)
                    {
                        if (obj is Entity entity)
                        {
                            entityCount++;
                            
                            // 根据实体类型执行不同操作
                            if (entity is DwgLine line)
                            {
                                LineString lineGeom = line.Geometry as LineString;
                                Console.WriteLine($"发现直线: 起点({lineGeom.StartPoint.X},{lineGeom.StartPoint.Y}), 终点({lineGeom.EndPoint.X},{lineGeom.EndPoint.Y})");
                            }
                            else if (entity is DwgCircle circle)
                            {
                                Circle circleGeom = circle.Geometry as Circle;
                                Console.WriteLine($"发现圆: 圆心({circleGeom.Center.X},{circleGeom.Center.Y}), 半径{circleGeom.Radius}");
                            }
                            else if (entity is DwgText text)
                            {
                                TextElement textGeom = text.Geometry as TextElement;
                                Console.WriteLine($"发现文本: \"{textGeom.Text}\", 位置({textGeom.Position.X},{textGeom.Position.Y})");
                            }
                        }
                    }
                    
                    Console.WriteLine($"共找到{entityCount}个图形实体");
                    tr.Commit();
                }
            }
        }

        // 示例3:修改现有图形对象属性
        static void ModifyEntityProperties()
        {
            string filePath = @"C:\Temp\TeighaExample.dwg";
            
            if (!System.IO.File.Exists(filePath))
            {
                Console.WriteLine("示例文件不存在,请先执行创建文件操作");
                return;
            }
            
            using (DwgDocument doc = new DwgDocument())
            {
                doc.Open(filePath);
                
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
                    
                    Console.WriteLine("开始修改对象属性:");
                    int modifiedCount = 0;
                    
                    foreach (DBObject obj in modelSpace)
                    {
                        if (obj is Entity entity)
                        {
                            // 1. 修改颜色:将所有对象设为红色
                            entity.Color = Color.FromColorIndex(ColorMethod.ByAci, 1); // 红色
                            
                            // 2. 特别处理圆:增大半径
                            if (entity is DwgCircle circle)
                            {
                                Circle circleGeom = circle.Geometry as Circle;
                                circleGeom.Radius *= 1.5;
                                circle.Geometry = circleGeom;
                                Console.WriteLine($"圆半径已修改为{circleGeom.Radius}");
                                modifiedCount++;
                            }
                            // 3. 特别处理文本:修改内容
                            else if (entity is DwgText text)
                            {
                                TextElement textGeom = text.Geometry as TextElement;
                                textGeom.Text = "修改后的Teigha示例文本";
                                text.Geometry = textGeom;
                                Console.WriteLine("文本内容已修改");
                                modifiedCount++;
                            }
                        }
                    }
                    
                    if (modifiedCount > 0)
                    {
                        Console.WriteLine($"成功修改{modifiedCount}个对象属性");
                        tr.Commit();
                        doc.Save();
                    }
                    else
                    {
                        Console.WriteLine("未找到可修改的对象");
                    }
                }
            }
        }

        // 示例4:执行图形集合运算
        static void PerformGeometryOperations()
        {
            // 创建新文件用于演示几何运算
            string filePath = @"C:\Temp\TeighaGeometry.dwg";
            
            using (DwgDocument doc = new DwgDocument())
            {
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
                    
                    // 1. 创建两个相交的圆用于求交运算
                    Circle circle1 = new Circle();
                    circle1.Center = new Point3d(5, 5, 0);
                    circle1.Radius = 3.0;
                    
                    DwgCircle circleEnt1 = new DwgCircle();
                    circleEnt1.Geometry = circle1;
                    modelSpace.AppendEntity(circleEnt1);
                    tr.AddNewlyCreatedDBObject(circleEnt1, true);
                    
                    Circle circle2 = new Circle();
                    circle2.Center = new Point3d(8, 5, 0);
                    circle2.Radius = 3.0;
                    
                    DwgCircle circleEnt2 = new DwgCircle();
                    circleEnt2.Geometry = circle2;
                    modelSpace.AppendEntity(circleEnt2);
                    tr.AddNewlyCreatedDBObject(circleEnt2, true);
                    
                    // 2. 执行几何求交运算
                    GeometryIntersector intersector = new GeometryIntersector();
                    GeometryBase result = intersector.GetIntersection(circle1, circle2);
                    
                    if (result is Point3dCollection intersectionPoints)
                    {
                        Console.WriteLine($"两圆相交,找到{intersectionPoints.Count}个交点");
                        
                        // 将交点绘制成点
                        foreach (Point3d pt in intersectionPoints)
                        {
                            PointElement point = new PointElement();
                            point.Position = pt;
                            
                            DwgPoint pointEnt = new DwgPoint();
                            pointEnt.Geometry = point;
                            
                            modelSpace.AppendEntity(pointEnt);
                            tr.AddNewlyCreatedDBObject(pointEnt, true);
                        }
                    }
                    else
                    {
                        Console.WriteLine("两圆未相交");
                    }
                    
                    // 3. 创建矩形和圆形用于求差运算
                    Polyline polyline = new Polyline();
                    polyline.AddVertexAt(0, new Point3d(2, 2, 0), 0, 0, 0);
                    polyline.AddVertexAt(1, new Point3d(2, 8, 0), 0, 0, 0);
                    polyline.AddVertexAt(2, new Point3d(8, 8, 0), 0, 0, 0);
                    polyline.AddVertexAt(3, new Point3d(8, 2, 0), 0, 0, 0);
                    polyline.Closed = true;
                    
                    DwgPolyline rectEnt = new DwgPolyline();
                    rectEnt.Geometry = polyline;
                    modelSpace.AppendEntity(rectEnt);
                    tr.AddNewlyCreatedDBObject(rectEnt, true);
                    
                    // 执行矩形减去圆形的差集运算
                    GeometryDifferencer differencer = new GeometryDifferencer();
                    GeometryBase diffResult = differencer.GetDifference(polyline, circle1);
                    
                    if (diffResult is Polyline resultPolyline)
                    {
                        Console.WriteLine("成功执行差集运算");
                        
                        // 绘制差集结果
                        DwgPolyline diffEnt = new DwgPolyline();
                        diffEnt.Geometry = resultPolyline;
                        diffEnt.Color = Color.FromColorIndex(ColorMethod.ByAci, 2); // 绿色
                        
                        modelSpace.AppendEntity(diffEnt);
                        tr.AddNewlyCreatedDBObject(diffEnt, true);
                    }
                    
                    tr.Commit();
                    doc.SaveAs(filePath);
                    Console.WriteLine($"几何运算结果已保存至: {filePath}");
                }
            }
        }
    }
}


                    
 

1. 基础文件操作
 
- 文件创建:通过 DwgDocument 类创建新DWG文件,使用 Transaction 管理数据库事务
 
- 文件读取:通过 doc.Open() 方法读取现有DWG文件,遍历 ActiveModelSpace 中的实体
 
2. 图形对象操作
 
- 直线创建:通过 LineString 定义线段起点和终点,封装为 DwgLine 实体
 
- 圆形创建:使用 Circle 几何对象定义圆心和半径,转换为 DwgCircle 
 
- 文本创建:通过 TextElement 设置文本内容、位置和高度,生成 DwgText 实体
 
3. 对象属性修改
 
- 颜色修改:通过 entity.Color 属性设置对象颜色(示例中使用红色Aci 1)
 
- 几何修改:直接操作几何对象(如圆的半径、文本内容)并更新实体
 
4. 几何运算应用
 
- 求交运算:使用 GeometryIntersector 计算两圆交点,结果为 Point3dCollection 
 
- 差集运算:通过 GeometryDifferencer 实现矩形与圆形的差集计算,返回新的多边形
 
Teigha与AutoCAD API的主要差异
 
5. 架构设计:
 
- Teigha采用更通用的对象模型,不依赖AutoCAD环境
 
- AutoCAD API紧密绑定AutoCAD进程,需在AutoCAD中运行
 
6. 命名空间差异:
 
- Teigha使用 Bentley 命名空间(如 Bentley.DwgPlatformNET )
 
- AutoCAD API使用 Autodesk.AutoCAD 命名空间
 
7. 几何处理:
 
- Teigha提供独立的 GeometryNET 命名空间,几何运算功能更丰富
 
- AutoCAD API的几何处理与实体对象耦合更紧密
 
8. 事务管理:
 
- 两者都使用事务机制管理数据库操作,但Teigha的 Transaction 接口更简洁
 
开发环境配置
 
3. 引用程序集:
 
- 需要在项目中引用以下Teigha程序集:
 
- Bentley.DgnPlatformNET.dll
 
- Bentley.DwgPlatformNET.dll
 
- Bentley.GeometryNET.dll
 
- Bentley.MathNET.dll
 
- Bentley.Transactions.dll
 
4. 许可证管理:
 
- 使用Teigha前需初始化许可证(如示例中的 License 类)
 
- 商业应用需购买Bentley官方许可证
 
应用场景扩展
 
5. 批量文件处理:利用Teigha不依赖AutoCAD的特性,开发批量转换、检查DWG文件的工具
 
6. 跨平台应用:基于Teigha的跨平台能力,开发Windows、Linux或macOS环境下的CAD文件处理程序
 
7. 轻量化显示:提取DWG文件中的几何数据,用于轻量化图形显示或Web端可视化
 
8. 数据提取与分析:从DWG文件中提取特定类型的对象(如管道、结构构件)进行数据分析
 
上述示例代码可直接编译运行(需正确配置Teigha环境),实际项目中可根据需求进一步扩展功能,如添加图层管理、块参照处理、尺寸标注操作等高级功能。

以下是关于 **Teigha (Open Design Alliance SDK)** 在CAD二次开发中的基本应用示例,包含常见功能的详细代码和注释。Teigha 是用于处理DWG/DXF文件的跨平台开发库,支持无需AutoCAD环境即可操作CAD文件。

一、Teigha开发环境准备**
1. 安装Teigha SDK(ODA SDK)
2. 在C#项目中添加以下引用:
   - `Teigha.Core.dll`
   - `Teigha.Runtime.dll`
   - `Teigha.DatabaseServices.dll`
   - `Teigha.Geometry.dll`

二、基本功能示例**#### **1. 创建新DWG文件并添加直线**
 

using Teigha.Core;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using System;

public class TeighaBasicDemo
{
    public static void CreateDwgWithLine()
    {
        // 初始化Teigha环境
        Services.Start(); 

        try
        {
            // 创建内存中的新Database(相当于DWG文件)
            using (Database db = new Database(true, true))
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // 打开块表(BlockTable)
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;

                // 打开模型空间块表记录
                BlockTableRecord modelSpace = tr.GetObject(
                    bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                // 创建一条直线(从点(0,0,0)到点(100,100,0))
                Line line = new Line(
                    new Point3d(0, 0, 0),
                    new Point3d(100, 100, 0)
                );

                // 将直线添加到模型空间
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);

                // 保存为DWG文件
                db.SaveAs("C:\\Temp\\Output.dwg", DwgVersion.Current);

                tr.Commit();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            // 释放Teigha资源
            Services.Release();
        }
    }
}

2. 读取DWG文件并遍历所有实体**
 

using Teigha.Core;
using Teigha.DatabaseServices;

public class TeighaReadDemo
{
    public static void ReadDwgEntities()
    {
        Services.Start();

        try
        {
            using (Database db = new Database(false, true))
            {
                // 读取现有DWG文件
                db.ReadDwgFile("C:\\Temp\\Sample.dwg", FileOpenMode.OpenForReadAndAllShare, false, null);

                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(
                        bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

                    // 遍历模型空间中的实体
                    foreach (ObjectId entityId in modelSpace)
                    {
                        Entity entity = tr.GetObject(entityId, OpenMode.ForRead) as Entity;
                        if (entity != null)
                        {
                            // 输出实体类型和句柄
                            Console.WriteLine($"Entity Type: {entity.GetType().Name}, Handle: {entity.Handle}");
                        }
                    }

                    tr.Commit();
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            Services.Release();
        }
    }
}

3. 创建图层并设置颜色**
 

using Teigha.Core;
using Teigha.DatabaseServices;

public class LayerDemo
{
    public static void CreateLayer()
    {
        Services.Start();

        try
        {
            using (Database db = new Database(true, true))
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // 获取层表(LayerTable)
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;

                // 创建新图层
                LayerTableRecord layer = new LayerTableRecord
                {
                    Name = "MyLayer",
                    Color = Color.FromColorIndex(ColorMethod.ByAci, 1) // 红色
                };

                // 将图层添加到层表
                lt.Add(layer);
                tr.AddNewlyCreatedDBObject(layer, true);

                tr.Commit();
                db.SaveAs("C:\\Temp\\LayerDemo.dwg", DwgVersion.Current);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            Services.Release();
        }
    }
}


4. 添加块定义与块引用
 

using Teigha.Core;
using Teigha.DatabaseServices;

public class BlockDemo
{
    public static void CreateBlockWithReference()
    {
        Services.Start();

        try
        {
            using (Database db = new Database(true, true))
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // 创建块定义
                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
                BlockTableRecord blockDef = new BlockTableRecord
                {
                    Name = "MyBlock"
                };

                // 在块定义中添加一个圆
                Circle circle = new Circle(
                    new Point3d(0, 0, 0),
                    Vector3d.ZAxis,
                    50.0
                );
                blockDef.AppendEntity(circle);
                tr.AddNewlyCreatedDBObject(circle, true);

                // 将块定义添加到块表
                bt.Add(blockDef);
                tr.AddNewlyCreatedDBObject(blockDef, true);

                // 在模型空间中插入块引用
                BlockTableRecord modelSpace = tr.GetObject(
                    bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                BlockReference blockRef = new BlockReference(
                    new Point3d(200, 200, 0),
                    blockDef.ObjectId
                );
                modelSpace.AppendEntity(blockRef);
                tr.AddNewlyCreatedDBObject(blockRef, true);

                tr.Commit();
                db.SaveAs("C:\\Temp\\BlockDemo.dwg", DwgVersion.Current);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            Services.Release();
        }
    }
}

三、关键技术点说明**
1. **环境初始化与释放**  
   - `Services.Start()`:初始化Teigha运行时环境
   - `Services.Release()`:释放资源(必须调用,否则内存泄漏)

2. **数据库操作**  
   - `new Database(true, true)`:创建空数据库(第一个参数为`true`表示新建)
   - `db.ReadDwgFile()`:读取现有DWG文件
   - `db.SaveAs()`:保存DWG文件

3. **事务管理**  
   - 所有数据库操作必须包裹在事务中
   - 使用`TransactionManager.StartTransaction()`开启事务
   - 必须调用`Commit()`或`Abort()`结束事务

4. **实体操作流程**  
   ```plaintext
   创建实体 → 打开目标块表记录 → AppendEntity → AddNewlyCreatedDBObject
   ```

四、Teigha与AutoCAD API的区别


| 功能                 | Teigha (ODA)                           | AutoCAD .NET API          |
|---------------------|----------------------------                |---------------------------|
| 环境依赖            | 无需安装AutoCAD                 | 必须安装AutoCAD           |
| 跨平台支持         | 支持Windows/Linux/macOS  | 仅Windows                 |
| 文件格式支持     | 支持DWG/DXF/DWF             | 主要支持DWG               |
| 类名前缀            | 无命名空间前缀(如`Database`)| 带`Autodesk.AutoCAD`前缀 |
| 商业授权            | 需要ODA商业许可                    | 需AutoCAD授权             |

五、常见问题处理


1. **句柄(Handle)冲突**  
   使用`db.Undo()`回滚操作时需注意对象状态管理。

2. **内存泄漏**  
   确保所有`DBObject`在使用后调用`Dispose()`或包裹在`using`语句中。

3. **坐标系差异**  
   Teigha使用右手坐标系,需注意与AutoCAD坐标系的一致性。

---

通过上述示例,可以快速掌握Teigha在CAD文件操作中的核心功能。建议参考ODA官方文档([Open Design Alliance](https://www.opendesign.com/))获取更详细的API说明。


网站公告

今日签到

点亮在社区的每一天
去签到