| 定义块 (.NET) 
 若要创建新块,请创建一个新对象,并使用该方法将其追加到该对象。创建对象后,使用该属性为块和使用该方法将块插入图形时要显示的对象指定名称。BlockTableRecordAddBlockTableBlockTableRecordNameAppendEntity 然后,将任何几何对象或其他块添加到新创建的对象中。使用该方法将对象添加到对象中。然后,可以通过创建新对象并将其追加到模型空间或与 Layout 对象关联的对象来将块的实例插入到图形中。插入的块是称为块引用的对象。BlockTableRecordBlockTableRecordAppendEntityBlockReferenceBlockTableRecord 还可以通过使用将对象写出到单独的图形文件的方法创建块。然后,图形文件可以用作其他图形的块定义。AutoCAD 将插入到另一个图形中的任何图形视为块。WBlock 有关定义模块的更多信息,请参阅产品帮助系统中的“关于定义模块”。 定义块此示例定义一个块,并向块定义添加一个圆圈。然后,它将块作为块参照插入到图形中。 VB.NETImports 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 SubC#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父主题: | 
 |Archiver|CAD开发者社区
( 苏ICP备2022047690号-1   苏公网安备32011402011833)
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1   苏公网安备32011402011833)
GMT+8, 2025-10-31 14:22
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.