c# Cad批量插入图框
时间: 2023-12-07 22:39:25 浏览: 147
autocad中c#按坐标批量导入图片
5星 · 资源好评率100%
以下是使用C# Cad批量插入图框的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
namespace CadInsert
{
public class CadInsert
{
[CommandMethod("InsertCad")]
public void InsertCad()
{
//设置CAD文件路径和图框坐标
string cadFilePath = @"E:\data\cad\demo.DWG";
Dictionary<string, Point3d> frameCoordinates = new Dictionary<string, Point3d>();
frameCoordinates.Add("Frame1", new Point3d(0, 0, 0));
frameCoordinates.Add("Frame2", new Point3d(100, 100, 0));
//加载CAD插件
try
{
if (!ReferenceEquals(Application.GetSystemVariable("DWGNAME"), null))
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
//打开CAD文件
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//插入图框
foreach (KeyValuePair<string, Point3d> kvp in frameCoordinates)
{
BlockReference br = new BlockReference(kvp.Value, bt[kvp.Key]);
btr.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
}
tr.Commit();
}
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
```
阅读全文