Here is an Example to let you modify the blocks recursively.
{code}
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Colors
' This is an example of selecting a block on the screen and recursively accessing each block
Public Class Sample
Sub Recursive()
Dim Col As New Collection
GetBlockSelection(Col)
Dim blockschecked As New Collection
RecursiveBlockDefinition(Col, blockschecked)
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.Regen()
End Sub
Sub RecursiveBlockDefinition(ByVal Col As Collection, ByRef BlocksChecked As Collection)
' Use BlocksChecked to check if you already modified the block
Using db As Database = HostApplicationServices.WorkingDatabase()
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Using lock As DocumentLock = dwg.LockDocument
Dim BT As BlockTable = dwg.Database.BlockTableId.GetObject(OpenMode.ForWrite)
If Col.Count > 0 Then
For i = 1 To Col.Count
If Not BlocksChecked.Contains(Col(i).ToString) Then
Dim e As Entity = tr.GetObject(Col(i), OpenMode.ForWrite)
If TypeOf e Is BlockReference Then
' Add to Checked List
If Not BlocksChecked.Contains(e.ObjectId.ToString) Then
BlocksChecked.Add(e.ObjectId, e.ObjectId.ToString)
End If
Dim b As BlockReference = tr.GetObject(Col(i), OpenMode.ForWrite)
Dim BTR As BlockTableRecord = tr.GetObject(BT(b.Name), OpenMode.ForWrite)
If Not (BTR.IsFromExternalReference Or BTR.IsFromOverlayReference) Then
For Each item As ObjectId In BTR
' Change Object's Color to red
Dim e2 As Entity = tr.GetObject(item, OpenMode.ForWrite)
e2.Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
If TypeOf e2 Is BlockReference Then
Dim Lcol As New Collection
Lcol.Add(e2.ObjectId)
RecursiveBlockDefinition(Lcol, BlocksChecked)
End If
Next
End If
End If
End If
Next
tr.Commit()
BT.Dispose()
End If
End Using
End Using
End Using
End Sub
Public Sub GetBlockSelection(ByRef ColObjID As Collection)
Dim activeDocument As Document = Application.DocumentManager.MdiActiveDocument
Dim activeDB As Database = activeDocument.Database
Dim ed As Editor = activeDocument.Editor
Using db As Database = HostApplicationServices.WorkingDatabase()
Using trans As Transaction = db.TransactionManager.StartTransaction()
Dim dwg As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Using lock As DocumentLock = dwg.LockDocument
Dim selectionTypeValue As TypedValue() = New TypedValue(0) {}
selectionTypeValue.SetValue(New TypedValue(CInt(DxfCode.Start), "INSERT"), 0)
Dim selectionFilter As New SelectionFilter(selectionTypeValue)
Dim getSelectionResult As PromptSelectionResult
getSelectionResult = ed.GetSelection(selectionFilter)
' If selection actually contains something
If getSelectionResult.Status = PromptStatus.OK Then
Dim selectionSet As SelectionSet = getSelectionResult.Value
For Each selectedObject As SelectedObject In selectionSet
If selectedObject IsNot Nothing Then
Dim selectedEntity As Entity = TryCast(trans.GetObject(selectedObject.ObjectId, OpenMode.ForWrite), Entity)
If Not ColObjID.Contains(selectedEntity.ObjectId.ToString) Then
ColObjID.Add(selectedEntity.ObjectId, selectedEntity.ObjectId.ToString)
End If
End If
Next
trans.Commit()
End If
End Using
End Using
End Using
Dim curDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
' Clear Command Line
curDoc.SendStringToExecute(Chr(27), False, False, False)
End Sub
End Class
{code}
---------------------------
(defun botsbuildbots() (botsbuildbots))