C# 二次开发cad填充代码
时间: 2023-07-02 21:11:41 浏览: 237
C#.NET 二次开发 DWG格式的CAD文件
5星 · 资源好评率100%
以下是C#二次开发CAD填充代码的示例:
```csharp
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using System;
namespace FillPolyline
{
public class Commands
{
[CommandMethod("FillPolyline")]
public void FillPolyline()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// 选择多段线
PromptEntityOptions peo = new PromptEntityOptions("\n选择多段线: ");
peo.SetRejectMessage("\n必须选择一个多段线!");
peo.AddAllowedClass(typeof(Polyline), true);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Polyline pline = tr.GetObject(per.ObjectId, OpenMode.ForWrite) as Polyline;
if (pline == null) return;
// 创建填充对象
Hatch hatch = new Hatch();
hatch.SetDatabaseDefaults();
hatch.Layer = pline.Layer;
hatch.ColorIndex = pline.ColorIndex;
hatch.PatternScale = 1.0;
// 设置填充类型
hatch.PatternType = HatchPatternType.PreDefined;
hatch.PatternName = "SOLID";
// 获取多段线边界
Point3dCollection pts = new Point3dCollection();
for (int i = 0; i < pline.NumberOfVertices; i++)
{
pts.Add(pline.GetPoint3dAt(i));
}
// 添加填充边界
hatch.AppendLoop(HatchLoopTypes.Outermost, pts);
// 将填充对象添加到模型空间
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
// 关联填充对象和多段线
hatch.Associative = true;
hatch.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection() { pline.ObjectId });
hatch.EvaluateHatch(true);
tr.Commit();
}
}
}
}
```
上述代码中,通过 `PromptEntityOptions` 和 `ed.GetEntity` 方法选择了一个多段线,然后创建了一个填充对象并将其添加到模型空间。最后,通过将填充对象与多段线关联,实现了填充功能。
阅读全文