CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

定义图案填充边界 (.NET)

2023-1-1 14:38| 发布者: admin| 查看: 430| 评论: 0|来自: AutoCAD

创建图案填充对象后,可以添加图案填充边界。边界可以是直线、圆弧、圆、二维折线、椭圆、样条曲线和面的任意组合。

添加的第一个边界必须是外部边界,它定义了要由填充填充的最外层边界。若要添加外部边界,请将方法与要追加的循环类型一起使用常量。AppendLoopHatchLoopTypes.Outermost

定义外部边界后,可以继续添加其他边界。使用带有常量的方法添加内部边界。AppendLoopHatchLoopTypes.Default

内部边界定义图案填充内的岛屿。Hatch 对象如何处理这些岛取决于属性的设置。该属性可以设置为以下条件之一:HatchStyleHatchStyle

图案填充样式定义

舱口样式

条件

描述

正常

(影线样式.正常)

指定标准样式或普通样式。此选项从最外层区域边界向内剖面线。如果 AutoCAD 遇到内部边界,它将关闭剖面线,直到遇到另一个边界。这是属性的默认设置。HatchStyle

(HatchStyle.Outer)

仅填充最外层区域。此样式也会从区域边界向内剖面线,但如果遇到内部边界,则会关闭剖面线,并且不会再次将其重新打开。

忽视

(HatchStyle.Ignore)

忽略内部结构。此选项填充所有内部对象。

定义完图案填充后,必须先对其进行评估,然后才能显示图案填充。使用方法执行此操作。EvaluateHatch

创建图案填充对象

下面的示例在模型空间中创建一个关联的图案填充。创建图案填充后,可以更改与图案填充关联的圆的大小。图案填充将更改以匹配圆圈的大小。

VB.NET

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
        Using 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
            Using 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)
            End Using
        End Using

        '' Save the new object to the database
        acTrans.Commit()
    End Using
End Sub

C#

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
        using (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
            ObjectIdCollection acObjIdColl = new ObjectIdCollection();
            acObjIdColl.Add(acCirc.ObjectId);

            // Create the hatch object and append it to the block table record
            using (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.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 代码参考

Sub 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 True
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 12:24

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部