CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

分解块引用 (.NET)

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

使用该方法断开块引用。通过分解块参照,可以修改块,或者添加或删除定义它的对象。Explode

显示分解块参照的结果

本示例创建一个块,并在块的定义中添加一个圆。然后将块作为块参照插入到图形中。然后分解块参照,并显示分解过程产生的对象及其对象类型。

VB.NET

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

<CommandMethod("ExplodingABlock")> _
Public Sub ExplodingABlock()
    ' 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)

        Dim blkRecId As ObjectId = ObjectId.Null

        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

                blkRecId = acBlkTblRec.Id
            End Using
        Else
            blkRecId = acBlkTbl("CircleBlock")
        End If

        ' Insert the block into the current space
        If blkRecId <> ObjectId.Null Then
            Using acBlkRef As New BlockReference(New Point3d(0, 0, 0), blkRecId)

                Dim acCurSpaceBlkTblRec As BlockTableRecord
                acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)

                acCurSpaceBlkTblRec.AppendEntity(acBlkRef)
                acTrans.AddNewlyCreatedDBObject(acBlkRef, True)

                Using dbObjCol As New DBObjectCollection
                    acBlkRef.Explode(dbObjCol)

                    For Each dbObj As DBObject In dbObjCol
                        Dim acEnt As Entity = dbObj

                        acCurSpaceBlkTblRec.AppendEntity(acEnt)
                        acTrans.AddNewlyCreatedDBObject(dbObj, True)

                        acEnt = acTrans.GetObject(dbObj.ObjectId, OpenMode.ForWrite)

                        acEnt.ColorIndex = 1
                        MsgBox("Exploded Object: " & acEnt.GetRXClass().DxfName)
                    Next
                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("ExplodingABlock")]
public void ExplodingABlock()
{
    // 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;

        ObjectId blkRecId = ObjectId.Null;

        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);
                }

                blkRecId = acBlkTblRec.Id;
            }
        }
        else
        {
            blkRecId = acBlkTbl["CircleBlock"];
        }

        // Insert the block into the current space
        if (blkRecId != ObjectId.Null)
        {
            using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId))
            {
                BlockTableRecord acCurSpaceBlkTblRec;
                acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                acCurSpaceBlkTblRec.AppendEntity(acBlkRef);
                acTrans.AddNewlyCreatedDBObject(acBlkRef, true);

                using (DBObjectCollection dbObjCol = new DBObjectCollection())
                {
                    acBlkRef.Explode(dbObjCol);

                    foreach (DBObject dbObj in dbObjCol)
                    {
                        Entity acEnt = dbObj as Entity;

                        acCurSpaceBlkTblRec.AppendEntity(acEnt);
                        acTrans.AddNewlyCreatedDBObject(dbObj, true);

                        acEnt = acTrans.GetObject(dbObj.ObjectId, OpenMode.ForWrite) as Entity;

                        acEnt.ColorIndex = 1;
                        Application.ShowAlertDialog("Exploded Object: " + acEnt.GetRXClass().DxfName);
                    }
                }
            }
        }

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

        // Dispose of the transaction
    }
}

VBA/ActiveX Code Reference

Sub ExplodingABlock()
    ' 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 circleObj As AcadCircle
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 0
    center(1) = 0
    center(2) = 0
    rad = 2
    Set circleObj = blockObj.AddCircle(center, rad)
 
    ' Insert the block
    Dim blockRefObj As AcadBlockReference
    insertionPnt(0) = 0
    insertionPnt(1) = 0
    insertionPnt(2) = 0
    Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "CircleBlock", 1, 1, 1, 0)

    ' Explode the block reference
    Dim explodedObjects As Variant
    explodedObjects = blockRefObj.Explode
 
    ' Loop through the exploded objects
    Dim I As Integer
    For I = 0 To UBound(explodedObjects)
        explodedObjects(I).Color = acRed
        explodedObjects(I).Update
        MsgBox "Exploded Object " & I & ": " & explodedObjects(I).ObjectName
    Next
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 15:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部