This code does not work anymore on AutoCAD 2019. I'm targeting the Framework 4.7. The managed .NET Reference guide specified that the function Database.ReadDwgFile(string, FileShare, [MarshalAs(UnmanagedType.U1)] bool, string) Method
is obsolete & will be remove in future release. When debugging, It does not iterate through the object ID's in the BlockTableRecord? Could the function have been replace? I'm looking into the Graphic system changes... But any input would be nice...
Thanks.
Public Shared Sub UpdateAttributesInSideDatabase(ByVal FilePath As String, blockName As String, attbName As String, attbValue As String)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim currentDb As Database = doc.Database
Dim ed As Editor = doc.Editor
Using targetDb As New Database(False, True)
Try
targetDb.ReadDwgFile(FilePath, System.IO.FileShare.ReadWrite, True, "")
Using tr As Transaction = targetDb.TransactionManager.StartTransaction()
Dim layoutDict As DBDictionary = tr.GetObject(targetDb.LayoutDictionaryId, OpenMode.ForRead)
For Each dicEnt As DBDictionaryEntry In layoutDict
Dim lay As Layout = DirectCast(dicEnt.Value.GetObject(OpenMode.ForRead), Layout)
''do your stuffs in every layout
If lay.LayoutName <> "Model" Then
Dim btr As BlockTableRecord = tr.GetObject(lay.BlockTableRecordId, OpenMode.ForRead)
For Each id As ObjectId In btr
If id.ObjectClass.DxfName = "INSERT" Then
Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForRead), BlockReference)
' ... to see whether it's a block with
' the name we're after
If br.Name.ToUpper() = blockName.ToUpper() Then
For Each arId As ObjectId In br.AttributeCollection
Dim ar As AttributeReference = DirectCast(tr.GetObject(arId, OpenMode.ForRead), AttributeReference)
' ... to see whether it has
' the tag we're after
If ar.Tag.ToUpper() = attbName.ToUpper() Then
' If so, update the value
' and increment the counter
ar.UpgradeOpen()
ar.TextString = attbValue
'realign attributes after editing their values
'ar.AdjustAlignment(targetDb)
'ar.DowngradeOpen()
End If
Next
End If
End If
Next
End If
Next
'end of code from template insert
tr.Commit()
End Using
targetDb.SaveAs(FilePath, DwgVersion.Current)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
ed.WriteMessage(vbLf & "Error while running > " + ex.Message)
End Try
End Using
End Sub