CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

AutoCAD 2024 开发者帮助

编辑填充图案 (.NET)

2024-5-18 19:13| 发布者: admin| 查看: 87| 评论: 0|原作者: admin|来自: AutoCAD

编辑填充图案 (.NET)

您可以更改现有填充图案的角度或间距,也可以将其替换为实体填充、渐变填充或 AutoCAD 提供的预定义图案之一。功能区上的“边界填充线”(Boundary Hatch) 对话框或“阵列库”(Pattern gallery) 中的“阵列”(Pattern) 选项将显示这些阵列的列表。为了减小文件大小,在图形中将填充定义为单个图形对象。

使用以下属性和方法来编辑填充图案:

梯度角度

指定填充的渐变角度。

GradientName(渐变名称)

返回填充的渐变名称。

梯度移位

指定填充的梯度偏移。

GradientType

返回填充线的渐变类型。

图案角度

指定填充图案的角度(以弧度为单位)。

模式双倍

指定用户定义的填充线是否为双剖面线。

模式名称

返回填充的填充图案名称。(使用该方法设置填充图案名称和填充类型。SetHatchPattern

图案鳞片

指定填充图案比例。

模式空间

指定用户定义的填充图案间距。

PatternType

返回填充的填充图案类型。(使用该方法设置填充图案名称和填充类型。SetHatchPattern

设置渐变

设置填充的渐变类型和名称。

SetHatchPattern

设置填充的图案类型和名称。

更改图案填充的图案间距

本示例创建一个填充。然后,它将填充的当前图案间距增加 2。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("EditHatchPatternScale")> _
Public Sub EditHatchPatternScale()
    '' 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 boundary of the hatch
        Using acCirc As Circle = New Circle()
            acCirc.Center = New Point3d(5, 3, 0)
            acCirc.Radius = 3

            acBlkTblRec.AppendEntity(acCirc)
            acTrans.AddNewlyCreatedDBObject(acCirc, True)

            '' Adds the arc and line to an object id collection
            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)

                '' Evaluate the hatch
                acHatch.EvaluateHatch(True)

                '' Increase the pattern scale by 2 and re-evaluate the hatch
                acHatch.PatternScale = acHatch.PatternScale + 2
                acHatch.SetHatchPattern(acHatch.PatternType, acHatch.PatternName)
                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("EditHatchPatternScale")]
public static void EditHatchPatternScale()
{
    // 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 boundary of the hatch
        using (Circle acCirc = new Circle())
        {
            acCirc.Center = new Point3d(5, 3, 0);
            acCirc.Radius = 3;

            acBlkTblRec.AppendEntity(acCirc);
            acTrans.AddNewlyCreatedDBObject(acCirc, true);

            // Adds the arc and line to an object id collection
            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);

                // Evaluate the hatch
                acHatch.EvaluateHatch(true);

                // Increase the pattern scale by 2 and re-evaluate the hatch
                acHatch.PatternScale = acHatch.PatternScale + 2;
                acHatch.SetHatchPattern(acHatch.PatternType, acHatch.PatternName);
                acHatch.EvaluateHatch(true);
            }
        }

        // Save the new object to the database
        acTrans.Commit();
    }
}

VBA/ActiveX 代码参考

Sub EditHatchPatternScale()
    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 loop for the hatch.
    Dim outerLoop(0 To 0) As AcadEntity
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 5
    center(1) = 3
    center(2) = 0
    radius = 3
    Set outerLoop(0) = ThisDrawing.ModelSpace. _
                           AddCircle(center, radius)
    hatchObj.AppendOuterLoop (outerLoop)
    hatchObj.Evaluate
 
    ' Change the scale of the hatch pattern by
    ' adding 2 to the current scale
    hatchObj.patternScale = hatchObj.patternScale + 2
    hatchObj.Evaluate
    ThisDrawing.Regen True
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-12-15 22:26

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部