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

Add XData to appended block

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
SRSDS
996 Views, 7 Replies

Add XData to appended block

Hi again,

 

I can't work this out and can't seem to find the solution.

When capturing the ObjectAppended event of a block containing a "Drawing_No" attribute tag, then trying to add xdata to the object, I get a "Object reference not set to an instance of an object" error. I've tried writing the XData after commiting the transaction but it didn't help. Can someone explain what the problem is with the ObjectID I'm using?

 

                If TypeOf e.DBObject Is BlockReference Then
                    Dim bref As BlockReference = DirectCast(e.DBObject, BlockReference)
                    If bref.AttributeCollection.Count <> 0 Then
                        For Each attId As ObjectId In bref.AttributeCollection
                            Dim attRef As AttributeReference = trans.GetObject(attId, OpenMode.ForRead)
                            Select Case attRef.Tag
                                Case "DRAWING_NO"
                                    WriteTitleBlock(e.DBObject.ObjectId) '<<Subroutine used to add XData
                                    Exit For
                            End Select
                        Next
                    End If
                End If
7 REPLIES 7
Message 2 of 8
matus.brlit
in reply to: SRSDS

where exactly do you get that exception thrown?

because i think that the block doesn't have any attribute definitions, before it is placed in the drawing

Message 3 of 8
Hallex
in reply to: SRSDS

Wouldn't it for reason of that the AttributeReference

is not open for write?

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 8
Alfred.NESWADBA
in reply to: SRSDS

Hi,

 

>> the ObjectAppended event

The AttributeReferences are appended to database after the BlockReference. So matus.brlit is very right in mentioning that within this event the AttRefs don't exist.

So there are two chances:

  • Change the Eventhandling to watching for an AttRef called "DRAWING_NO" (instead of watching for the BlockReference)
  • The other way (prefered) would be: if the ObjectAppended-Handler found a BlockReference or AttributeReference to be appended that is of interest ==> create an EventHandler that watches e.g. to "CommandEnded" + save the BlockReference-ObjectID. If then (after inserting the BlockReference) the command ends ==> your second part has to start now with modifying the XData as you like.

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 8
SRSDS
in reply to: Alfred.NESWADBA

Alfred,

 

That seems to have been the problem. I was thinking that the object hadn't been appended at that stage but didn't want to t explain my theories to confuse the matter. And that may still not be quite right anyway.

 

Still not writing though and now another problem.

 

Using this in the ObjectAppended event

For Each attId As ObjectId In bref.AttributeCollection
	Dim attRef As AttributeReference = attId.GetObject(OpenMode.ForWrite)

  I get a NullReferenceExeception error

 

So I changed it to opening and closing a transaction using

Dim attRef As AttributeReference = trans.GetObject(attId, OpenMode.ForWrite)

The ObjectAppended gets fired multiple times and opening and closing transactions using this method seems to cause a problem.

It results in an Unhandled Exception trying when opening a transaction in the ObjectAppended event even though I'm sure they have all been properly closed.

 

Such a simple thing and I can't get it working. Hope someone can help.

Message 6 of 8
Alfred.NESWADBA
in reply to: SRSDS

Hi,

 

>> I get an unhandled exception trying to open a transaction in the ObjectAppended event

And exactly this was the reason why I showed you my suggestion to create an Eventhandler for CommandEnded (or an Event that gets fired when AutoCAD reaches the Quiescent-state). So the principe workflow is like this

Dim myObjAppColl as ObjectIDCollection


EventHandler ObjectAppended:
   if is type AttributeReference then
     if AttRef has Tag "DRAWING-NR"
        'this is the point where you know an object is appended
        'that you need to do something with, so save it
        if not myObjAppColl.Contains(ObjID) then myObjAppColl.Add(ObjID)
        'then check if CommandEnded-Event is initialized
        if not ComEndHandlerInitialized then
           AddHandler for CommandEnded
           ComEndHandlerInitialized = true
        endif
     endif
   endif



EventHandler CommandEnded:
   RemoveHander for CommandEnded
   ComEndHandlerInitialized = false
   create transaction
   For each ObjID in myObjAppColl
     get AttRef from ObjID
     'do whatever you have to do now
   Next
   myObjAppColl.Clear

There maybe (and will be) other ways also, this would be how I would get it running.

 

HTH, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 7 of 8
SRSDS
in reply to: Alfred.NESWADBA

Sorry to drag this out..

Such a simple thing and amazingly difficult.

 

1.

I need the objectID of the inserted titleblock to add the xData to. Will capturing the ObjectID of the attRef give me the ObjectID of the block?

 

2.

 I get an "Unhandled execption. Object reference not set to an instance of the object" trying to use this:

 

  EventHandler ObjectAppended:
If TypeOf e.DBObject Is AttributeReference Then Dim attRef As AttributeReference = e.DBObject.ObjectId.GetObject(OpenMode.ForWrite) If attRef.Tag = "DRAWING_NO" Then TitleBlockObjectID = e.DBObject.ObjectId End If End If
Message 8 of 8
Alfred.NESWADBA
in reply to: SRSDS

Hi,

 

ad 1) check AttRef.OwnerID 😉

 

ad 2) after checking if this: e.DBObject is an AttRef ==> you alredy have the AttRef, you don't need to open it once more (and especially not for WriteAccess as this object is under construction), so do that:

If TypeOf e.DBObject Is AttributeReference Then 
   Dim attRef As AttributeReference = CType(e.DBObject,AttributeReference) 
   If attRef.Tag = "DRAWING_NO" Then 
      TitleBlockObjectID = e.DBObject.OwnerId 
   End If 
End If

 

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

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