.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to amend object

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
257 Views, 4 Replies

How to amend object

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

 

 

4 REPLIES 4
Message 2 of 5
Alfred.NESWADBA
in reply to: Anonymous

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 5
Anonymous
in reply to: Alfred.NESWADBA

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.

 

Message 4 of 5
Alfred.NESWADBA
in reply to: Anonymous

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
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 5
Anonymous
in reply to: Alfred.NESWADBA

Yes they are.

and JobNo <>"" etc.

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost