Author Topic: Update attribute in sideDatabase (AutoCAD 2019)  (Read 382 times)

0 Members and 2 Guests are viewing this topic.


  • Newt
  • Posts: 49
Update attribute in sideDatabase (AutoCAD 2019)
« on: January 21, 2019, 09:57:47 AM »
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
Code: [Select]
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...

Code: [Select]
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)
                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.TextString = attbValue
                                                'realign attributes after editing their values

                                            End If
                                    End If
                                End If
                        End If
                    'end of code from template insert
                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


  • King Gator
  • Posts: 3942
  • C3D user & customizer
Re: Update attribute in sideDatabase (AutoCAD 2019)
« Reply #1 on: January 21, 2019, 10:25:10 AM »
Change this:
targetDb.ReadDwgFile(FilePath, System.IO.FileShare.ReadWrite, True, "")

to this:
targetDb.ReadDwgFile(FilePath, FileOpenMode.OpenForReadAndWriteNoShare, True, "")