Setting Xdata for Text Entity problem

Mago
Enthusiast
Enthusiast

Setting Xdata for Text Entity problem

Mago
Enthusiast
Enthusiast

Hi,

I'm inserting a blockref and text to a drawing using a modeless windows form.

So I lock the document first before doing anything

I make the blockref and insert it using entityjig, add the newly created blockref, then rotate it with jig, commit  and then add xdata to it

No problem so far, and then, in the same sub, I add a single line text (DBText), and set xdata for it (calling the same sub below)

The problem is that AutoCAD crashes with an unhandled exception when setting the xdata for the text entity.

 

If I don't set xdata for the Text, everything works fine.

Any ideas ??

 

Public Sub SetXData(mcaja As Entity, appname As String, idc As Long, ncaja As String)
Dim rb As ResultBuffer
Dim adoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim tr As Transaction = adoc.TransactionManager.StartTransaction()

Using tr
Dim obj As DBObject = tr.GetObject(mcaja.ObjectId, OpenMode.ForWrite)
AddRegAppTableRecord(appname, adoc)
rb = New ResultBuffer(New TypedValue(1001, appname), New TypedValue(1000, ncaja), New TypedValue(1071, idc))
obj.XData = rb
rb.Dispose()
tr.Commit()
End Using
End Sub


Public Shared Sub AddRegAppTableRecord(regAppName As String, ByRef doc As Document)
'Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database

Dim tr As Transaction = doc.TransactionManager.StartTransaction()
Using tr
Dim ratab As RegAppTable = CType(tr.GetObject(db.RegAppTableId, OpenMode.ForRead, False), RegAppTable)
If Not ratab.Has(regAppName) Then
ratab.UpgradeOpen()
Dim ratrec As New RegAppTableRecord()
ratrec.Name = regAppName
ratab.Add(ratrec)
tr.AddNewlyCreatedDBObject(ratrec, True)
End If
tr.Commit()
End Using
End Sub

0 Likes
Reply
Accepted solutions (2)
1,043 Views
7 Replies
Replies (7)

_gile
Mentor
Mentor

Hi,

 

Try to change the idc parameter to Integer. The 1071 dxf code is to be used with 32 bits signed integers.

 

if this doesn't solve your issue, you should handle the exception in a Try / Catch block to get more information about this exception.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

FRFR1426
Collaborator
Collaborator
Accepted solution

mcaja is already an entity, why are you opening it a second time?

 

And you don't need to start a transaction in each Sub. In AddRegAppTableRecord, you can simply do:

 

Dim ratab As RegAppTable = CType(db.RegAppTableId.GetObject(OpenMode.ForRead), RegAppTable)
[..]
doc.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(ratrec, True)
Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr

Mago
Enthusiast
Enthusiast

Thank you Gilles, but changing idc to integer did not help.

Try catch won't work on debug mode since autocad shuts down and no error is reported.

on release mode, autocad crashes with a "fatal error unhandled access violations reading 0x0010"

so the try/catch isn't helping there either.

0 Likes

Mago
Enthusiast
Enthusiast

Thanks for the tip about the top transaction.

 

mcaja is a different entity everytime I call the setxdata sub.

The first time mcaja is a blockref, and the second time is the dbtext entity.

With the dbtext entity it crashes.

 

0 Likes

_gile
Mentor
Mentor
Accepted solution

Generally you should not pass objects deriving from DBObject as parameters (use ObjectId instead) except when the method is called from within a transaction (in this case, you should use this top transaction in the called method).

 

If I do not misunderstand, you create some entities (a block reference and a text) in a transaction then you pass these entities to another method. If this transaction have been disposed when the entities are passed to SetXData() they also have been disposed with  the transaction (this should occur some time after the calling to Dispose).

So, try passing ObjectIds as parameter or call the SetXData method from within the top transaction, in this case do not start a new transaction to re-open mcaja. The newly created entity is opened for write so that you can directly set its Xdata property.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Mago
Enthusiast
Enthusiast

Thank you Maxence and Gilles. I was creating the dbtext entity in an additional transaction and within an using statement ( Using Mytext as Dbtext = New Dbtext()... End Using), before passing it to the setXData sub. That was causing the fatal error.
Now I'm using the top transaction and got rid of the Using statement and it's working just fine. Thank you again.

0 Likes

_gile
Mentor
Mentor

Hi,

 

You can keep the using statement (it's a good practice to create new DBObjects in a using statement in case an error occurs before they're added to the transaction), you just need to call the SetXData() method from within this scope.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes