分解块参照

AutoCAD ActiveX/VBA

 
分解块参照
 
 
 

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

显示分解的块参照的结果

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

Sub Ch10_ExplodingABlock()
    ' 定义块
    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")
    ' 向块中添加圆
    Dim circleObj As AcadCircle
    Dim center(0 To 2) As Double
    Dim radius As Double
    center(0) = 0
    center(1) = 0
    center(2) = 0
    radius = 1
    Set circleObj = blockObj.AddCircle(center, radius)
    ' 插入块
    Dim blockRefObj As AcadBlockReference
    insertionPnt(0) = 2
    insertionPnt(1) = 2
    insertionPnt(2) = 0
    Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock _
               (insertionPnt, "CircleBlock", 1#, 1#, 1#, 0)
    ZoomAll
    MsgBox "The circle belongs to " & blockRefObj.ObjectName
    ' 分解块参照
    Dim explodedObjects As Variant
    explodedObjects = blockRefObj.Explode
    ' 遍历分解的对象
    Dim I As Integer
    For I = 0 To UBound(explodedObjects)
        explodedObjects(I).Color = acRed
        explodedObjects(I).Update
        MsgBox "Exploded Object " & I & ": " _
                            & explodedObjects(I).ObjectName
        explodedObjects(I).Color = acByLayer
        explodedObjects(I).Update
    Next
End Sub