How to amend object

How to amend object

Anonymous
Not applicable
372 Views
4 Replies
Message 1 of 5

How to amend object

Anonymous
Not applicable

This is my code (thanks to Alfred NESWADBA)

 

   Friend Sub ObjectModified(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs)
      Using tr As Transaction = CurDb.TransactionManager.StartTransaction()
         Dim btr As BlockTableRecord
         btr = tr.GetObject(CurDb.CurrentSpaceId, OpenMode.ForWrite)
         With e.DBObject
            If .IsWriteEnabled Then Exit Sub
            If .Id.ObjectClass.Name = "AcDbAttribute" Then
               Dim ar As AttributeReference = CType(e.DBObject, AttributeReference)
               Dim blk As BlockReference = tr.GetObject(ar.OwnerId, OpenMode.ForRead)
               blk.SetDatabaseDefaults()
               With a_Job
                  For i As Integer = 4 To 19
                     If a_Title.Tags(i) = ar.Tag Then
                        Select Case i
                           Case 4, 5, 6, 7
                              Dim a As String = .Description(i - 4)
                              ar.UpgradeOpen()
                              ar.TextString = a
                              tr.AddNewlyCreatedDBObject(ar, False)
                        End Select
                     End If
                  Next i
               End With
            End If
         End With
         tr.Commit()
      End Using
   End Sub

 Naturally it doesn;t work. Actually it crashes AutoCad

 

 

0 Likes
373 Views
4 Replies
Replies (4)
Message 2 of 5

Alfred.NESWADBA
Consultant
Consultant

HI,

 

1) the event ObjectModified gets fired when objects are modified, but at least not ready for you, so it's always critical if you try to do any modifications within this eventhandler ... modifications to other than the current obj.

2) why do you do this: "blk.SetDatabaseDefaults" because the block is already inserted and already has it's properties set, what should that be for? And as you don't need to do that, you also need not open the BlockReference for read- or write-acess!

3) we don't know what "a_Job" is, please let's see what type that is

4) and most critical (and sorry ... wrong) is this part:

ar.UpgradeOpen()
ar.TextString = a
tr.AddNewlyCreatedDBObject(ar, False)

Here you open an object to get write-access, you modify it and then you try to add this object again to the database! An object (I guess it's an AttributeReference) that already is placed in the database can't be added to the database once more! So remove the AddNewlyCreated.... statement.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 5

Anonymous
Not applicable

I amended the code thus:-

   Friend Sub ObjectModified(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.DatabaseServices.ObjectEventArgs)
      Using tr As Transaction = CurDb.TransactionManager.StartTransaction()
         With e.DBObject
            If .IsWriteEnabled Then Exit Sub
            If .Id.ObjectClass.Name = "AcDbAttribute" Then
               Dim ar As AttributeReference = CType(e.DBObject, AttributeReference)
               With a_Job
                  ar.UpgradeOpen()
                  For i As Integer = 5 To 9
                     If a_Title.Tags(i) = ar.Tag Then
                        Select Case i
                           Case 5 : ar.TextString = .JobNo
                           Case 6, 7, 8, 9 : ar.TextString = .Description(i - 6)
                        End Select
                     End If
                  Next i
               End With
            End If
         End With
         tr.Commit()
      End Using
   End Sub

 a_Title is a class which contains a list of tag strings, which I look for.

 

However the block is not updated.

 

You didn't comment on this code, which I had and removed because it seemed redundant to me.

         Dim btr As BlockTableRecord
         btr = tr.GetObject(CurDb.CurrentSpaceId, OpenMode.ForWrite)

 and I don't see how to update the block.

 

0 Likes
Message 4 of 5

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

have you checked that one of these two lines are executed (.TextString is modified)?

Case 5 : ar.TextString = .JobNo
Case 6, 7, 8, 9 : ar.TextString = .Description(i - 6)

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 5 of 5

Anonymous
Not applicable

Yes they are.

and JobNo <>"" etc.

0 Likes