Showing results for 
Show  only  | Search instead for 
Did you mean: 
Message 1 of 3
4017 Views, 2 Replies


HI I am new to VB.NET.


I am trying to use the code provided on:



The only thing is I have translated it to VB.NET instead.  (this could be the problem I am sure)


I am opening the Database of all the drawings (from the file) stepping thru each object and then looking for the attribute to change.


If I have a drawing with 1 object (a block with the attributes I want to edit) in it the code works fine.  But if I add a line to the file it fails.


It fails just after the first For each entid....


Dim Ent As Entity = TR.GetObject(entId, OpenMode.ForRead)




    Private Function UpdateAttributesInBlock(Byref DB As Database, ByVal btrID As ObjectId) As Integer

        Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim Ed As Editor = Doc.Editor

        Dim ChangeIndex As Integer, Intchanged As Integer
        Dim TR As Transaction = DB.TransactionManager.StartTransaction

        Using TR

            Dim btr As BlockTableRecord = TR.GetObject(btrID, OpenMode.ForRead)

            For Each entId As ObjectId In btr

                Dim Ent As Entity = TR.GetObject(entId, OpenMode.ForRead)

                If Ent <> Nothing Then 'And Ent.GetType.ToString = "Autodesk.AutoCAD.DatabaseServices.BlockReference" Then

                    Dim br As BlockReference = Ent

                    If br <> Nothing Then

                        Dim bd As BlockTableRecord = TR.GetObject(br.BlockTableRecord, OpenMode.ForRead)

                        'does the mSheet array contain current Block Name
                        If mSheets.Contains(bd.Name.ToUpper) = True Then

                            'step thru all sheet
                            For Each Sheet As String In mSheets

                                'if next sheet contains block name
                                If Sheet.ToUpper = bd.Name.ToUpper Then

                                    'step thru all attributes
                                    For Each arid As ObjectId In br.AttributeCollection

                                        Dim obj As DBObject = TR.GetObject(arid, OpenMode.ForRead)
                                        Dim ar As AttributeReference = obj

                                        'if current sheet change, and attribute matches then change text value
                                        If ar.Tag.ToUpper = mAttributes.GetValue(ChangeIndex).ToString.ToUpper Then

                                            ar.TextString = mChangeValues.GetValue(ChangeIndex).ToString

                                            'added to fix alignment issue
                                            Dim WDB As Database = HostApplicationServices.WorkingDatabase
                                            HostApplicationServices.WorkingDatabase = DB
                                            HostApplicationServices.WorkingDatabase = WDB
                                            Intchanged = 1

                                        End If


                                End If

                                ChangeIndex = ChangeIndex + 1

                            ChangeIndex = 0

                        End If

                    End If

                End If


        End Using

        Ed.WriteMessage(Intchanged.ToString + " ITEMS CHANGED!" + vbCrLf)

        Return Intchanged

    End Function


Some help would be greatly appreciated.
Merry Christmas.


Message 2 of 3
in reply to: Anonymous

Try changing the line that fails to


Dim Ent As Entity = TR.GetObject(entId, OpenMode.ForRead, True)


This will return the entity instead of throwing the eWasErased error. Although you have to be aware that the entity returned has been erased from the drawing. AutoCAD does not clean up erased items until you save the drawing.

Message 3 of 3
in reply to: Anonymous

Fantastic.... Smiley Happy


Thanks So much.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators

Autodesk Design & Make Report