以下代码展示:
往模型空间中添加填充对象。
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Colors;
[CommandMethod("NewHatch")]
public static void NewHatch()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//-------------------------------
// 获取模型空间
//-------------------------------
BlockTable blockTbl = tr.GetObject(
db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord modelSpace = tr.GetObject(
blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
//-------------------------------
// 创建圆作为填充的封闭边界
//-------------------------------
Circle circle = new Circle();
circle.Center = new Point3d(100, 100, 0);
circle.Radius = 50;
circle.Normal = new Vector3d(0, 0, 1);
modelSpace.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
//-------------------------------
// 创建填充对象
//-------------------------------
Hatch hatch = new Hatch();
modelSpace.AppendEntity(hatch);
tr.AddNewlyCreatedDBObject(hatch, true);
hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
hatch.Associative = true;
ObjectIdCollection ids = new ObjectIdCollection();
ids.Add(circle.ObjectId);
hatch.AppendLoop(HatchLoopTypes.Outermost, ids);
hatch.EvaluateHatch(true);
//
tr.Commit();
}
}