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
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
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
Wouldn't it for reason of that the AttributeReference
is not open for write?
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:
HTH, - alfred -
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.
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 -
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
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 -