CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

定义块 (.NET)

2023-1-1 10:32| 发布者: admin| 查看: 578| 评论: 0|来自: AutoCAD

要创建新块,请创建一个新对象并使用该方法将其附加到对象中。创建对象后,使用特性为块指定名称,以及使用该方法将块插入图形时要显示的对象。BlockTableRecordAddBlockTableBlockTableRecordNameAppendEntity

然后,将任何几何对象或其他块添加到新创建的对象中。对象使用方法添加到对象中。然后,可以通过创建一个新对象并将其追加到模型空间或与布局对象关联的对象,将块的实例插入到图形中。插入的块是称为块参照的对象。BlockTableRecordBlockTableRecordAppendEntityBlockReferenceBlockTableRecord

还可以通过使用该方法将对象写出到单独的图形文件来创建块。然后,可以将图形文件用作其他图形的块定义。AutoCAD 将插入到另一个图形中的任何图形视为块。WBlock

有关定义块的更多信息,请参阅产品帮助系统中的“关于定义块”。

定义块

此示例定义一个块,并在块定义中添加一个圆。然后,它将块作为块参照插入到图形中。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

<CommandMethod("CreatingABlock")> _
Public Sub CreatingABlock()
    ' Get the current database and start a transaction
    Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        ' Open the Block table for read
        Dim acBlkTbl As BlockTable
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

        If Not acBlkTbl.Has("CircleBlock") Then
            Using acBlkTblRec As New BlockTableRecord
                acBlkTblRec.Name = "CircleBlock"

                ' Set the insertion point for the block
                acBlkTblRec.Origin = New Point3d(0, 0, 0)

                ' Add a circle to the block
                Using acCirc As New Circle
                    acCirc.Center = New Point3d(0, 0, 0)
                    acCirc.Radius = 2

                    acBlkTblRec.AppendEntity(acCirc)

                    acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite)
                    acBlkTbl.Add(acBlkTblRec)
                    acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True)
                End Using
            End Using
        End If

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

        ' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("CreatingABlock")]
public void CreatingABlock()
{
    // Get the current database and start a transaction
    Database acCurDb;
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Block table for read
        BlockTable acBlkTbl;
        acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

        if (!acBlkTbl.Has("CircleBlock"))
        {
            using (BlockTableRecord acBlkTblRec = new BlockTableRecord())
            {
                acBlkTblRec.Name = "CircleBlock";

                // Set the insertion point for the block
                acBlkTblRec.Origin = new Point3d(0, 0, 0);

                // Add a circle to the block
                using (Circle acCirc = new Circle())
                {
                    acCirc.Center = new Point3d(0, 0, 0);
                    acCirc.Radius = 2;

                    acBlkTblRec.AppendEntity(acCirc);

                    acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite);
                    acBlkTbl.Add(acBlkTblRec);
                    acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true);
                }
            }
        }

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

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub CreatingABlock()
    ' Define the block
    Dim blockObj As AcadBlock
    Dim insertionPnt(0 To 2) As Double
    insertionPnt(0) = 0
    insertionPnt(1) = 0
    insertionPnt(2) = 0
    Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "CircleBlock")

    ' Add a circle to the block
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 0
    center(1) = 0
    center(2) = 0
    rad = 2
    blockObj.AddCircle(center, rad)
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-27 09:05

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部