Hi,
I've been struggling with the following problem for a couple of weeks and I wondered if one of you fine chaps would be able to point me in the right direction:
In VBA, if I want to find and edit specific attributes contained within the layouts of a drawing I'd simply do this: -
Set layouts = thisdrawing.layouts
for each layout in layouts
Thisdrawing.activelayout = layout
If thisdrawing.activelayout.name <> "Model" then
For each Bobj in thisdrawing.paperspace
If Bobj.objectname = "AcDbBlockreference" Then
varattributes = bobj.getattributes
for i = lbound(varattributes) to ubound(varattributes)
if ucase(varattributes(i).TagString = ucase("date") then
varattributes(i).textstring = "some value"
end if
next i
End if
Next
end if
next
Now, I realise that the .NET way of doing this is almost completely different, and, having paid for the latest .NET book by Jerry Winters, I am beginning to get to grips with the problem, but am still having a hard time getting the .NET equivalent of the above code working.
As I understand it, having searched both the Autodesk .NET newsgroups, and this very forum is that I need to search the Blocktablerecords and determine whether the various blockreferences I find are part of a layout, but having implemented the following code, it doesn't function as I'd have expected: -
<CommandMethod("UTB")> _
Public Sub UpdateTitleBlock()
Dim db As Database = HostApplicationServices.WorkingDatabase
Using trans As Transaction = db.TransactionManager.StartTransaction()
Try
Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
Dim btr As BlockTableRecord = DirectCast(trans.GetObject(bt(BlockTableRecord.PaperSpace), OpenMode.ForRead), BlockTableRecord)
For Each bId As ObjectId In btr
Using ent As Entity = DirectCast(trans.GetObject(bId, OpenMode.ForRead, False), Entity)
If ent.GetRXClass().Name.ToString() = "AcDbBlockReference" Then
Dim br As BlockReference = DirectCast(ent, BlockReference)
Dim blkObj As BlockTableRecord = DirectCast(trans.GetObject(br.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
If blkObj.IsDynamicBlock = False Then
btr = DirectCast(trans.GetObject(br.BlockTableRecord, OpenMode.ForRead), BlockTableRecord)
Else
btr = DirectCast(trans.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead), BlockTableRecord)
End If
MsgBox(btr.Name & " : " & vbTab & blkObj.Name) ' these end up the same?
If (blkObj.HasAttributeDefinitions) AndAlso (btr.Name = "Drawing Border") Then
Dim attcol As Autodesk.AutoCAD.DatabaseServices.AttributeCollection = br.AttributeCollection
For Each attId As ObjectId In attcol
Dim attRef As AttributeReference = DirectCast(trans.GetObject(attId, OpenMode.ForWrite), AttributeReference)
MsgBox(attRef.Tag & " : " & vbTab & attRef.TextString)
Next
End If
End If
End Using
Next
trans.Commit()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.ToString())
MessageBox.Show("Unexpected Error: " + ex.ToString())
End Try
End Using
End Sub
Given what I've learnt reading Jerry's very informative book, this should be able to at least list the attributes contained within the block called drawing border, but it doesn't work. Any suggestions?