Once the Hatch object is created, the hatch boundaries can be added. Boundaries can be any combination of lines, arcs, circles, 2D polylines, ellipses, splines, and regions.
一旦 Hatch 对象被创建,就可以添加图案填充边界。边界可以是直线、圆弧、圆、二维多段线、椭圆、样条曲线和面域的任意组合。
The first boundary added must be the outer boundary, which defines the outermost limits to be filled by the hatch. To add the outer boundary, use the AppendLoop method with the HatchLoopTypes.Outermost constant for the type of loop to append.
添加的第一个边界必须是外边界,即用于定义图案填充最外面的边界。要添加外部边界,请使用添加环的类型为 HatchLoopTypes.Outermost 常量的 AppendLoop 方法。
Once the outer boundary is defined, you can continue adding additional boundaries. Add inner boundaries using the AppendLoop method with the HatchLoopTypes.Default constant.
一旦外边界被定义,就可以继续添加另外的边界。添加内部边界请使用带 HatchLoopTypes.Default 常量的 AppendLoop 方法。
Inner boundaries define islands within the hatch. How these islands are handled by the Hatch object depends on the setting of the HatchStyle property. The HatchStyle property can be set to one of the following conditions:
内边界定义图案填充内的孤岛。Hatch 对象处理这些孤岛的方式取决于 HatchStyle 属性的设置。HatchStyle 属性可以设置为以下状态:
When you have finished defining the hatch it must be evaluated before it can be displayed. Use the EvaluateHatch method to do this.
定义完图案填充之后,必须先对其进行计算,然后才能显示。请使用 EvaluateHatch 方法完成此任务。
The following example creates an associated hatch in Model space. Once the hatch has been created, you can change the size of the circle that the hatch is associated with. The hatch will change to match the size of the circle.
本例在模型空间中创建关联的图案填充。创建图案填充后,可以修改与图案填充关联的圆的大小。图案填充将自动改变以匹配圆的当前大小。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("AddHatch")> _
Public Sub AddHatch()
'' 获得当前文档和数据库 Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
''启动一个事务 Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' 以只读方式打开块表 Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' 以写方式打开模型空间块表记录 Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 创建一个圆对象作为图案填充的封闭边界 Create a circle object for the closed boundary to hatch
Dim acCirc As Circle = New Circle()
acCirc.SetDatabaseDefaults()
acCirc.Center = New Point3d(3, 3, 0)
acCirc.Radius = 1
'' 添加新的圆对象到块表记录和事务中 Add the new circle object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
'' 添加圆到一个 ObjectID 数组中去 Adds the circle to an object id array
Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()
acObjIdColl.Add(acCirc.ObjectId)
'' 创建图案填充对象并添加到块表记录中 Create the hatch object and append it to the block table record
Dim acHatch As Hatch = New Hatch()
acBlkTblRec.AppendEntity(acHatch)
acTrans.AddNewlyCreatedDBObject(acHatch, True)
'' Set the properties of the hatch object
'' Associative must be set after the hatch object is appended to the
'' block table record and before AppendLoop
acHatch.SetDatabaseDefaults()
acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31")
acHatch.Associative = True
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl)
acHatch.EvaluateHatch(True)
'' 保存新对象到数据库中 Save the new object to the database
acTrans.Commit()
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("AddHatch")]
public static void AddHatch()
{
// 获得当前文档和数据库 Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// 启动一个事务 Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// 以只读方式打开块表 Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// 以写方式打开模型空间块表记录 Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建一个圆对象作为图案填充的封闭边界 Create a circle object for the closed boundary to hatch
Circle acCirc = new Circle();
acCirc.SetDatabaseDefaults();
acCirc.Center = new Point3d(3, 3, 0);
acCirc.Radius = 1;
// 添加新的圆对象到块表记录和事务中 Add the new circle object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
// 添加圆到一个 ObjectID 数组中去 Adds the circle to an object id array
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(acCirc.ObjectId);
// 创建图案填充对象并添加到块表记录中 Create the hatch object and append it to the block table record
Hatch acHatch = new Hatch();
acBlkTblRec.AppendEntity(acHatch);
acTrans.AddNewlyCreatedDBObject(acHatch, true);
// Set the properties of the hatch object
// Associative must be set after the hatch object is appended to the
// block table record and before AppendLoop
acHatch.SetDatabaseDefaults();
acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
acHatch.Associative = true;
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
acHatch.EvaluateHatch(true);
// 保存新对象到数据库中 Save the new object to the database
acTrans.Commit();
}
}