详情

南飓风 Lv.8

关注
        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#中的根本应用,包括文件读写、图形创建、对象遍历和属性操纵:
  1. using System;
  2. using System.Collections.Generic;
  3. using Bentley.DwgPlatformNET;
  4. using Bentley.DgnPlatformNET;
  5. using Bentley.GeometryNET;
  6. using Bentley.MathNET;
  7. using Bentley.Transactions;
  8. namespace TeighaCADExample
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             // 初始化Teigha平台
  15.             InitializeTeigha();
  16.             try
  17.             {
  18.                 // 1. 创建新DWG文件并添加图形
  19.                 CreateNewDWGFile();
  20.                
  21.                 // 2. 读取DWG文件并遍历对象
  22.                 ReadAndProcessDWGFile();
  23.                
  24.                 // 3. 修改现有图形对象属性
  25.                 ModifyEntityProperties();
  26.                
  27.                 // 4. 执行图形集合运算
  28.                 PerformGeometryOperations();
  29.                
  30.                 Console.WriteLine("Teigha示例执行完成!");
  31.             }
  32.             catch (Exception ex)
  33.             {
  34.                 Console.WriteLine($"错误: {ex.Message}");
  35.             }
  36.             finally
  37.             {
  38.                 // 清理资源
  39.                 CleanUpTeigha();
  40.             }
  41.             
  42.             Console.ReadKey();
  43.         }
  44.         // 初始化Teigha平台
  45.         static void InitializeTeigha()
  46.         {
  47.             // 加载Teigha许可证
  48.             License BentleyLicense = new License();
  49.             BentleyLicense.SetProductLevel(License.ProductLevels.Professional);
  50.             Console.WriteLine("Teigha平台初始化成功");
  51.         }
  52.         // 清理Teigha资源
  53.         static void CleanUpTeigha()
  54.         {
  55.             // 释放许可证等资源
  56.             Console.WriteLine("Teigha资源已清理");
  57.         }
  58.         // 示例1:创建新DWG文件并添加图形
  59.         static void CreateNewDWGFile()
  60.         {
  61.             string filePath = @"C:\Temp\TeighaExample.dwg";
  62.             
  63.             // 创建DWG文档
  64.             using (DwgDocument doc = new DwgDocument())
  65.             {
  66.                 // 开始事务
  67.                 using (Transaction tr = doc.TransactionManager.StartTransaction())
  68.                 {
  69.                     // 获取模型空间
  70.                     BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
  71.                     
  72.                     // 1. 添加直线
  73.                     LineString line = new LineString();
  74.                     line.StartPoint = new Point3d(0, 0, 0);
  75.                     line.EndPoint = new Point3d(10, 0, 0);
  76.                     
  77.                     // 创建DwgLine实体
  78.                     DwgLine dwgLine = new DwgLine();
  79.                     dwgLine.Geometry = line;
  80.                     
  81.                     // 添加到模型空间
  82.                     modelSpace.AppendEntity(dwgLine);
  83.                     tr.AddNewlyCreatedDBObject(dwgLine, true);
  84.                     Console.WriteLine("已添加直线");
  85.                     
  86.                     // 2. 添加圆
  87.                     Circle circle = new Circle();
  88.                     circle.Center = new Point3d(5, 5, 0);
  89.                     circle.Radius = 3.0;
  90.                     
  91.                     DwgCircle dwgCircle = new DwgCircle();
  92.                     dwgCircle.Geometry = circle;
  93.                     
  94.                     modelSpace.AppendEntity(dwgCircle);
  95.                     tr.AddNewlyCreatedDBObject(dwgCircle, true);
  96.                     Console.WriteLine("已添加圆");
  97.                     
  98.                     // 3. 添加文本
  99.                     TextElement text = new TextElement();
  100.                     text.Text = "Teigha示例文本";
  101.                     text.Position = new Point3d(5, 0, 0);
  102.                     text.Height = 1.0;
  103.                     
  104.                     DwgText dwgText = new DwgText();
  105.                     dwgText.Geometry = text;
  106.                     
  107.                     modelSpace.AppendEntity(dwgText);
  108.                     tr.AddNewlyCreatedDBObject(dwgText, true);
  109.                     Console.WriteLine("已添加文本");
  110.                     
  111.                     // 提交事务并保存文件
  112.                     tr.Commit();
  113.                     doc.SaveAs(filePath);
  114.                     Console.WriteLine($"文件已保存至: {filePath}");
  115.                 }
  116.             }
  117.         }
  118.         // 示例2:读取DWG文件并遍历对象
  119.         static void ReadAndProcessDWGFile()
  120.         {
  121.             string filePath = @"C:\Temp\TeighaExample.dwg";
  122.             
  123.             if (!System.IO.File.Exists(filePath))
  124.             {
  125.                 Console.WriteLine("示例文件不存在,请先执行创建文件操作");
  126.                 return;
  127.             }
  128.             
  129.             using (DwgDocument doc = new DwgDocument())
  130.             {
  131.                 // 打开DWG文件
  132.                 doc.Open(filePath);
  133.                 Console.WriteLine("已打开DWG文件");
  134.                
  135.                 using (Transaction tr = doc.TransactionManager.StartTransaction())
  136.                 {
  137.                     // 获取模型空间
  138.                     BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
  139.                     
  140.                     Console.WriteLine("开始遍历模型空间对象:");
  141.                     int entityCount = 0;
  142.                     
  143.                     // 遍历模型空间所有实体
  144.                     foreach (DBObject obj in modelSpace)
  145.                     {
  146.                         if (obj is Entity entity)
  147.                         {
  148.                             entityCount++;
  149.                            
  150.                             // 根据实体类型执行不同操作
  151.                             if (entity is DwgLine line)
  152.                             {
  153.                                 LineString lineGeom = line.Geometry as LineString;
  154.                                 Console.WriteLine($"发现直线: 起点({lineGeom.StartPoint.X},{lineGeom.StartPoint.Y}), 终点({lineGeom.EndPoint.X},{lineGeom.EndPoint.Y})");
  155.                             }
  156.                             else if (entity is DwgCircle circle)
  157.                             {
  158.                                 Circle circleGeom = circle.Geometry as Circle;
  159.                                 Console.WriteLine($"发现圆: 圆心({circleGeom.Center.X},{circleGeom.Center.Y}), 半径{circleGeom.Radius}");
  160.                             }
  161.                             else if (entity is DwgText text)
  162.                             {
  163.                                 TextElement textGeom = text.Geometry as TextElement;
  164.                                 Console.WriteLine($"发现文本: "{textGeom.Text}", 位置({textGeom.Position.X},{textGeom.Position.Y})");
  165.                             }
  166.                         }
  167.                     }
  168.                     
  169.                     Console.WriteLine($"共找到{entityCount}个图形实体");
  170.                     tr.Commit();
  171.                 }
  172.             }
  173.         }
  174.         // 示例3:修改现有图形对象属性
  175.         static void ModifyEntityProperties()
  176.         {
  177.             string filePath = @"C:\Temp\TeighaExample.dwg";
  178.             
  179.             if (!System.IO.File.Exists(filePath))
  180.             {
  181.                 Console.WriteLine("示例文件不存在,请先执行创建文件操作");
  182.                 return;
  183.             }
  184.             
  185.             using (DwgDocument doc = new DwgDocument())
  186.             {
  187.                 doc.Open(filePath);
  188.                
  189.                 using (Transaction tr = doc.TransactionManager.StartTransaction())
  190.                 {
  191.                     BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
  192.                     
  193.                     Console.WriteLine("开始修改对象属性:");
  194.                     int modifiedCount = 0;
  195.                     
  196.                     foreach (DBObject obj in modelSpace)
  197.                     {
  198.                         if (obj is Entity entity)
  199.                         {
  200.                             // 1. 修改颜色:将所有对象设为红色
  201.                             entity.Color = Color.FromColorIndex(ColorMethod.ByAci, 1); // 红色
  202.                            
  203.                             // 2. 特别处理圆:增大半径
  204.                             if (entity is DwgCircle circle)
  205.                             {
  206.                                 Circle circleGeom = circle.Geometry as Circle;
  207.                                 circleGeom.Radius *= 1.5;
  208.                                 circle.Geometry = circleGeom;
  209.                                 Console.WriteLine($"圆半径已修改为{circleGeom.Radius}");
  210.                                 modifiedCount++;
  211.                             }
  212.                             // 3. 特别处理文本:修改内容
  213.                             else if (entity is DwgText text)
  214.                             {
  215.                                 TextElement textGeom = text.Geometry as TextElement;
  216.                                 textGeom.Text = "修改后的Teigha示例文本";
  217.                                 text.Geometry = textGeom;
  218.                                 Console.WriteLine("文本内容已修改");
  219.                                 modifiedCount++;
  220.                             }
  221.                         }
  222.                     }
  223.                     
  224.                     if (modifiedCount > 0)
  225.                     {
  226.                         Console.WriteLine($"成功修改{modifiedCount}个对象属性");
  227.                         tr.Commit();
  228.                         doc.Save();
  229.                     }
  230.                     else
  231.                     {
  232.                         Console.WriteLine("未找到可修改的对象");
  233.                     }
  234.                 }
  235.             }
  236.         }
  237.         // 示例4:执行图形集合运算
  238.         static void PerformGeometryOperations()
  239.         {
  240.             // 创建新文件用于演示几何运算
  241.             string filePath = @"C:\Temp\TeighaGeometry.dwg";
  242.             
  243.             using (DwgDocument doc = new DwgDocument())
  244.             {
  245.                 using (Transaction tr = doc.TransactionManager.StartTransaction())
  246.                 {
  247.                     BlockTableRecord modelSpace = doc.Database.ActiveModelSpace;
  248.                     
  249.                     // 1. 创建两个相交的圆用于求交运算
  250.                     Circle circle1 = new Circle();
  251.                     circle1.Center = new Point3d(5, 5, 0);
  252.                     circle1.Radius = 3.0;
  253.                     
  254.                     DwgCircle circleEnt1 = new DwgCircle();
  255.                     circleEnt1.Geometry = circle1;
  256.                     modelSpace.AppendEntity(circleEnt1);
  257.                     tr.AddNewlyCreatedDBObject(circleEnt1, true);
  258.                     
  259.                     Circle circle2 = new Circle();
  260.                     circle2.Center = new Point3d(8, 5, 0);
  261.                     circle2.Radius = 3.0;
  262.                     
  263.                     DwgCircle circleEnt2 = new DwgCircle();
  264.                     circleEnt2.Geometry = circle2;
  265.                     modelSpace.AppendEntity(circleEnt2);
  266.                     tr.AddNewlyCreatedDBObject(circleEnt2, true);
  267.                     
  268.                     // 2. 执行几何求交运算
  269.                     GeometryIntersector intersector = new GeometryIntersector();
  270.                     GeometryBase result = intersector.GetIntersection(circle1, circle2);
  271.                     
  272.                     if (result is Point3dCollection intersectionPoints)
  273.                     {
  274.                         Console.WriteLine($"两圆相交,找到{intersectionPoints.Count}个交点");
  275.                         
  276.                         // 将交点绘制成点
  277.                         foreach (Point3d pt in intersectionPoints)
  278.                         {
  279.                             PointElement point = new PointElement();
  280.                             point.Position = pt;
  281.                            
  282.                             DwgPoint pointEnt = new DwgPoint();
  283.                             pointEnt.Geometry = point;
  284.                            
  285.                             modelSpace.AppendEntity(pointEnt);
  286.                             tr.AddNewlyCreatedDBObject(pointEnt, true);
  287.                         }
  288.                     }
  289.                     else
  290.                     {
  291.                         Console.WriteLine("两圆未相交");
  292.                     }
  293.                     
  294.                     // 3. 创建矩形和圆形用于求差运算
  295.                     Polyline polyline = new Polyline();
  296.                     polyline.AddVertexAt(0, new Point3d(2, 2, 0), 0, 0, 0);
  297.                     polyline.AddVertexAt(1, new Point3d(2, 8, 0), 0, 0, 0);
  298.                     polyline.AddVertexAt(2, new Point3d(8, 8, 0), 0, 0, 0);
  299.                     polyline.AddVertexAt(3, new Point3d(8, 2, 0), 0, 0, 0);
  300.                     polyline.Closed = true;
  301.                     
  302.                     DwgPolyline rectEnt = new DwgPolyline();
  303.                     rectEnt.Geometry = polyline;
  304.                     modelSpace.AppendEntity(rectEnt);
  305.                     tr.AddNewlyCreatedDBObject(rectEnt, true);
  306.                     
  307.                     // 执行矩形减去圆形的差集运算
  308.                     GeometryDifferencer differencer = new GeometryDifferencer();
  309.                     GeometryBase diffResult = differencer.GetDifference(polyline, circle1);
  310.                     
  311.                     if (diffResult is Polyline resultPolyline)
  312.                     {
  313.                         Console.WriteLine("成功执行差集运算");
  314.                         
  315.                         // 绘制差集结果
  316.                         DwgPolyline diffEnt = new DwgPolyline();
  317.                         diffEnt.Geometry = resultPolyline;
  318.                         diffEnt.Color = Color.FromColorIndex(ColorMethod.ByAci, 2); // 绿色
  319.                         
  320.                         modelSpace.AppendEntity(diffEnt);
  321.                         tr.AddNewlyCreatedDBObject(diffEnt, true);
  322.                     }
  323.                     
  324.                     tr.Commit();
  325.                     doc.SaveAs(filePath);
  326.                     Console.WriteLine($"几何运算结果已保存至: {filePath}");
  327.                 }
  328.             }
  329.         }
  330.     }
  331. }
                    
 
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文件并添加直线**
 
  1. using Teigha.Core;
  2. using Teigha.DatabaseServices;
  3. using Teigha.Geometry;
  4. using System;
  5. public class TeighaBasicDemo
  6. {
  7.     public static void CreateDwgWithLine()
  8.     {
  9.         // 初始化Teigha环境
  10.         Services.Start(); 
  11.         try
  12.         {
  13.             // 创建内存中的新Database(相当于DWG文件)
  14.             using (Database db = new Database(true, true))
  15.             using (Transaction tr = db.TransactionManager.StartTransaction())
  16.             {
  17.                 // 打开块表(BlockTable)
  18.                 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  19.                 // 打开模型空间块表记录
  20.                 BlockTableRecord modelSpace = tr.GetObject(
  21.                     bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  22.                 // 创建一条直线(从点(0,0,0)到点(100,100,0))
  23.                 Line line = new Line(
  24.                     new Point3d(0, 0, 0),
  25.                     new Point3d(100, 100, 0)
  26.                 );
  27.                 // 将直线添加到模型空间
  28.                 modelSpace.AppendEntity(line);
  29.                 tr.AddNewlyCreatedDBObject(line, true);
  30.                 // 保存为DWG文件
  31.                 db.SaveAs("C:\\Temp\\Output.dwg", DwgVersion.Current);
  32.                 tr.Commit();
  33.             }
  34.         }
  35.         catch (Exception ex)
  36.         {
  37.             Console.WriteLine("Error: " + ex.Message);
  38.         }
  39.         finally
  40.         {
  41.             // 释放Teigha资源
  42.             Services.Release();
  43.         }
  44.     }
  45. }
2. 读取DWG文件并遍历所有实体**
 
  1. using Teigha.Core;
  2. using Teigha.DatabaseServices;
  3. public class TeighaReadDemo
  4. {
  5.     public static void ReadDwgEntities()
  6.     {
  7.         Services.Start();
  8.         try
  9.         {
  10.             using (Database db = new Database(false, true))
  11.             {
  12.                 // 读取现有DWG文件
  13.                 db.ReadDwgFile("C:\\Temp\\Sample.dwg", FileOpenMode.OpenForReadAndAllShare, false, null);
  14.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  15.                 {
  16.                     BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  17.                     BlockTableRecord modelSpace = tr.GetObject(
  18.                         bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
  19.                     // 遍历模型空间中的实体
  20.                     foreach (ObjectId entityId in modelSpace)
  21.                     {
  22.                         Entity entity = tr.GetObject(entityId, OpenMode.ForRead) as Entity;
  23.                         if (entity != null)
  24.                         {
  25.                             // 输出实体类型和句柄
  26.                             Console.WriteLine($"Entity Type: {entity.GetType().Name}, Handle: {entity.Handle}");
  27.                         }
  28.                     }
  29.                     tr.Commit();
  30.                 }
  31.             }
  32.         }
  33.         catch (Exception ex)
  34.         {
  35.             Console.WriteLine("Error: " + ex.Message);
  36.         }
  37.         finally
  38.         {
  39.             Services.Release();
  40.         }
  41.     }
  42. }
3. 创建图层并设置颜色**
 
  1. using Teigha.Core;
  2. using Teigha.DatabaseServices;
  3. public class LayerDemo
  4. {
  5.     public static void CreateLayer()
  6.     {
  7.         Services.Start();
  8.         try
  9.         {
  10.             using (Database db = new Database(true, true))
  11.             using (Transaction tr = db.TransactionManager.StartTransaction())
  12.             {
  13.                 // 获取层表(LayerTable)
  14.                 LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForWrite) as LayerTable;
  15.                 // 创建新图层
  16.                 LayerTableRecord layer = new LayerTableRecord
  17.                 {
  18.                     Name = "MyLayer",
  19.                     Color = Color.FromColorIndex(ColorMethod.ByAci, 1) // 红色
  20.                 };
  21.                 // 将图层添加到层表
  22.                 lt.Add(layer);
  23.                 tr.AddNewlyCreatedDBObject(layer, true);
  24.                 tr.Commit();
  25.                 db.SaveAs("C:\\Temp\\LayerDemo.dwg", DwgVersion.Current);
  26.             }
  27.         }
  28.         catch (Exception ex)
  29.         {
  30.             Console.WriteLine("Error: " + ex.Message);
  31.         }
  32.         finally
  33.         {
  34.             Services.Release();
  35.         }
  36.     }
  37. }
4. 添加块界说与块引用
 
  1. using Teigha.Core;
  2. using Teigha.DatabaseServices;
  3. public class BlockDemo
  4. {
  5.     public static void CreateBlockWithReference()
  6.     {
  7.         Services.Start();
  8.         try
  9.         {
  10.             using (Database db = new Database(true, true))
  11.             using (Transaction tr = db.TransactionManager.StartTransaction())
  12.             {
  13.                 // 创建块定义
  14.                 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  15.                 BlockTableRecord blockDef = new BlockTableRecord
  16.                 {
  17.                     Name = "MyBlock"
  18.                 };
  19.                 // 在块定义中添加一个圆
  20.                 Circle circle = new Circle(
  21.                     new Point3d(0, 0, 0),
  22.                     Vector3d.ZAxis,
  23.                     50.0
  24.                 );
  25.                 blockDef.AppendEntity(circle);
  26.                 tr.AddNewlyCreatedDBObject(circle, true);
  27.                 // 将块定义添加到块表
  28.                 bt.Add(blockDef);
  29.                 tr.AddNewlyCreatedDBObject(blockDef, true);
  30.                 // 在模型空间中插入块引用
  31.                 BlockTableRecord modelSpace = tr.GetObject(
  32.                     bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  33.                 BlockReference blockRef = new BlockReference(
  34.                     new Point3d(200, 200, 0),
  35.                     blockDef.ObjectId
  36.                 );
  37.                 modelSpace.AppendEntity(blockRef);
  38.                 tr.AddNewlyCreatedDBObject(blockRef, true);
  39.                 tr.Commit();
  40.                 db.SaveAs("C:\\Temp\\BlockDemo.dwg", DwgVersion.Current);
  41.             }
  42.         }
  43.         catch (Exception ex)
  44.         {
  45.             Console.WriteLine("Error: " + ex.Message);
  46.         }
  47.         finally
  48.         {
  49.             Services.Release();
  50.         }
  51.     }
  52. }
三、关键技术点说明**
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说明。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
57阅读
0回复

暂无评论,点我抢沙发吧