[资料] 创建和编辑AutoCAD实体(二) 创建对象(3)
7、Create Hatches创建图案填充
Closed boundaries can be filled with a pattern.可以使用图案来填充闭合边界。When creating hatch, you do not initially specify the area to be filled. First you must create the Hatch object. Once this is done, you can specify the outer loop, which is the outermost boundary for the hatch. You can then continue to specify any inner loops that may exist in the hatch.创建图案填充时,不必一上来就指定要填充的区域,应首先创建Hatch对象,然后可以指定外环,也就是要填充的区域的最外边界,然后可以继续指定可能存在的所有填充内环。For more information about working with hatches, see “Overview of Hatch Patterns and Fills” in the AutoC AD User's Guide.关于使用图案填充的更多内容,见AutoCAD用户指南的“填充图案与填充概览”。Topics in this section本小节内容
· Create a Hatch Object 创建Hatch对象· Associate a Hatch 关联图案填充· Assign the Hatch Pattern Type and Name 指定填充图案的类型和名称· Define the Hatch Boundaries 定义填充边界
7.1、Create a Hatch Object创建Hatch对象When creating a Hatch object, you specify the hatch pattern type, the hatch pattern name, and the associativity. Once a Hatch object has been created, you will not be able to change the hatch associativity.创建Hatch对象时,需指定填充图案类型、填充图案名称及关联属性。一旦创建好Hatch对象,就不能改变其关联属性。To create a Hatch object, you create a new instance of the object and then use the AppendEntity method to add it to a BlockTableRecord object.所谓创建Hatch对象,就是创建Hatch对象的一个新的实例,然后用AppendEntity方法将该实例添加到BlockTableRecord对象中。7.2、Associate a Hatch关联图案填充You can create associative or nonassociative hatches. Associative hatches are linked to their boundaries and updated when the boundaries are modified. Nonassociative hatches are independent of their boundaries.可以创建关联图案填充或非关联图案填充。关联图案填充就是图案填充对象与其边界是关联的,当边界改变时图案填充随之更新。而非关联图案填充是不依赖其边界的。To make a hatch associative, set the Associative property of the hatch object created to TRUE. To make a hatch nonassociative, set the Associative property to FALSE.要想创建关联图案填充,需将所创建图案填充对象的Associative属性设置为TRUE。创建非关联图案填充,就将Associative属性设置为FALSE。Associativity for hatch must be set before appending the hatch loop. If a hatch object is nonassociative, you can make it associative again by setting the Associative property to TRUE and reappending the hatch loop.图案填充的关联属性必须在将其添加到填充环之前设置。如果图案填充对象是非关联的,可以通过设置Associative属性值为TRUE将其重新设为关联的,然后再重新将其添加到填充环内。7.3、Assign the Hatch Pattern Type and Name指定填充图案的类型和名称AutoCAD supplies a solid-fill and more than fifty industry-standard hatch patterns. Hatch patterns highlight a particular feature or area of a drawing. For example, patterns can help differentiate the components of a 3D object or represent the materials that make up an object.AutoCAD提供了一个实体填充和多至50种工业标准的填充图案。填充图案用来强调图形中的特殊形状或区域。比如,图案可以帮助区分三维对象的部件或表示构成对象的材质等。You can use a pattern supplied with AutoCAD or one from an external pattern library. For a table of the hatch patterns supplied with AutoCAD, see the AutoCAD Command Reference Guide.我们可以使用AutoCAD提供的填充图案,或使用外部填充库提供的填充图案。AutoCAD提供的填充图案的列表,见AutoCAD命令参考指南。To specify a unique pattern, you must specify both a pattern type and name for the Hatch object. The pattern type specifies where to look up the pattern name. When entering the pattern type, use one of the following constants:要指定某一特定的填充图案,必须同时指定Hatch对象的填充图案类型和名称。填充图案类型指明到哪里去查找填充图案的名称。输入填充图案类型时,使用下列常量之一:HatchPatternType.PreDefinedSelects the pattern name from those defined in the acad.pat or files.从acad.pat文件中定义的图案名称中选择;HatchPatternType.UserDefinedDefines a pattern of lines using the current linetype.使用当前线型定义一个直线图案;HatchPatternType.CustomDefinedSelects the pattern name from a PAT other than the acad.pat or files.从acad.pat以外的PAT文件中选择图案名称When entering the pattern name, use a name that is valid for the file specified by the pattern type.输入图案名称时,使用的名称必须是图案类型指定的PAT文件里的有效名称。7.4、Define the Hatch Boundaries定义填充边界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属性可以设置为下列情况:
Hatch style definitions填充样式定义
HatchStyleConditionDescription描述
1
http://hi.csdn.net/attachment/201107/21/0_1311242726sCV1.gif
Normal(HatchStyle.Normal)
Specifies standard style, or normal. This option hatches inward from the outermost area boundary. If AutoCAD encounters an internal boundary, it turns off hatching until it encounters another boundary. This is the default setting for the HatchStyle property.标准样式或常规样式,是HatchStyle属性的默认设置。AutoCAD从最外边往里填充,当遇到一个内边界时关闭填充直到遇到另一个内边界。
2
http://hi.csdn.net/attachment/201107/21/0_13112427361Ut3.gif
Outer(HatchStyle.Outer)
Fills the outermost areas only. This style also hatches inward from the area boundary, but it turns off hatching if it encounters an internal boundary and does not turn it back on again.只填充最外区域。本样式也是从最外边往里填充,当遇到一个内边界时关闭填充并不再打开。
3
http://hi.csdn.net/attachment/201107/21/0_1311242741565a.gif
Ignore(HatchStyle.Ignore)
Ignores internal structure. This option hatches through all internal objects.忽略内部结构。本选项填充全部内部对象。
When you have finished defining the hatch it must be evaluated before it can be displayed. Use the EvaluateHatch method to do this.完成对图案填充的定义后,必须在显示前对其进行求值。使用EvaluateHatch方法来作这件事。Create a Hatch object 创建Hatch对象
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.下面的示例在模型空间创建一个关联图案填充。图案填充创建完后,可以改变填充所关联的圆的大小,填充将改变自己来适应圆的大小。VB.NETImports Autodesk.AutoCAD.RuntimeImports Autodesk.AutoCAD.ApplicationServicesImports Autodesk.AutoCAD.DatabaseServicesImports Autodesk.AutoCAD.Geometry
<CommandMethod("AddHatch")> _Public Sub AddHatch()'' Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database
'' Start a transactionUsing 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.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)
'' 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.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 UsingEnd SubC#using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;
public static void AddHatch(){// Get the current document and databaseDocument acDoc = Application.DocumentManager.MdiActiveDocument;Database acCurDb = acDoc.Database;
// Start a transactionusing (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, OpenMode.ForWrite) as BlockTableRecord;
// Create a circle object for the closed boundary to hatch //创建一个圆作为填充的封闭边界 Circle acCirc = new Circle(); 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);
// Adds the circle to an object id array //将圆的ObjectId添加到对象id数组 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 // 设置填充对象的属性,在调用AppendLoop之前设置填充对象的关联属性 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();}}VBA/ActiveX Code ReferenceSub AddHatch() Dim hatchObj As AcadHatch Dim patternName As String Dim PatternType As Long Dim bAssociativity As Boolean
' Define the hatch patternName = "ANSI31" PatternType = 0 bAssociativity = True
' Create the associative Hatch object Set hatchObj = ThisDrawing.ModelSpace.AddHatch _ (PatternType, patternName, bAssociativity)
' Create the outer boundary for the hatch. (a circle) Dim outerLoop(0 To 0) As AcadEntity Dim center(0 To 2) As Double Dim radius As Double center(0) = 3: center(1) = 3: center(2) = 0 radius = 1 Set outerLoop(0) = ThisDrawing.ModelSpace. _ AddCircle(center, radius)
' Append the outerboundary to the hatch ' object, and display the hatch hatchObj.AppendOuterLoop (outerLoop) hatchObj.Evaluate ThisDrawing.Regen TrueEnd Sub
页:
[1]