以下函数可解决该问题,该函数使用了大家比较少认识的CopyObjects方法来直接将图块中的对象复制到当前的空间中并按照插入点的位置移动到图块插入的位置。
- Sub BlkExp()
- Dim ent As AcadEntity
- Dim pnt As Variant
- On Error Resume Next
- Do
- ThisDrawing.Utility.GetEntity ent, pnt, "选择要分解的图块参照对象:"
- If Err <> 0 Then
- Err.Clear
- Else
- If ent.ObjectName = "AcDbBlockReference" Then Exit Do
- End If
- Loop
- MsgBox "选定的图块被分解后共有" & UBound(BlockRefExplode(ent)) & "个图元。", , "明经通道VBA示例"
- End Sub
- ' 该函数用于代替ActiveX方法中图块的Explode方法, _
- 因为原先的Explode方法带有BUG, _
- 分解带MText时连MText都被分解成Text。
-
- Function BlockRefExplode(BlockRef As AcadBlockReference) As Variant
- Dim Space As AcadBlock
- Dim BlockName As String
- Dim InsertPoint As Variant
- Dim OriginPoint(2) As Double
- Dim Block As AcadBlock
- BlockName = BlockRef.Name
- InsertPoint = BlockRef.InsertionPoint
- Set Space = ThisDrawing.ObjectIdToObject(BlockRef.OwnerID)
- Set Block = ThisDrawing.Blocks(BlockName)
- Dim BlkEnt() As AcadEntity
- ReDim BlkEnt(Block.Count - 1)
- Dim i As Long
- For i = 0 To Block.Count - 1
- Set BlkEnt(i) = Block(i)
- Next
- Dim SpaceCount As Long
- SpaceCount = Space.Count
- ThisDrawing.CopyObjects BlkEnt, Space
-
- Dim TotalCount As Long
- TotalCount = Space.Count - SpaceCount
-
- Dim BlkRefEnt() As AcadEntity
- ReDim BlkRefEnt(TotalCount)
- For i = 0 To TotalCount - 1
- Space(i + SpaceCount).Move OriginPoint, InsertPoint
- Set BlkRefEnt(i) = Space(i + SpaceCount)
- Next
- BlockRef.Delete
- BlockRefExplode = BlkRefEnt
- End Function
|