Message 1 of 7
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I’m desperately hoping there’s a solution to this.
If I modify an objects xdata during an object erased event, then hit undo, the xdata no longer exists.
Below is some code that creates a circle with xdata on document load.
To test it you need to draw a line or something before netloading so that there is something to erase.
Delete the line, hit undo, and the circle's xdata is gone.
The xdata coding has been working fine in many other event CommandWillStart and CommandEnded events.
e.GlobalCommandName = "ERASE" seems to be an issue.
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
<Assembly: ExtensionApplication(GetType(MyProject))>
Public Class MyProject
Implements IExtensionApplication
Private DocMan As DocumentCollection
Dim MyInteger As Integer = 100, CircleObjID As ObjectId
Public Sub Initialize() Implements IExtensionApplication.Initialize
DocMan = Application.DocumentManager
For Each doc As Document In DocMan
AddHandler doc.CommandWillStart, AddressOf callback_CommandWillStart
Next
'Add Circle with xdata
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
Using trans As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Using acCirc As Circle = New Circle()
acCirc.Center = New Point3d(0, 0, 0)
acCirc.Radius = 10000
btr.AppendEntity(acCirc)
trans.AddNewlyCreatedDBObject(acCirc, True)
CircleObjID = acCirc.ObjectId
WriteXdata(CircleObjID, trans)
End Using
trans.Commit()
End Using
End Using
End Sub
Private Sub callback_CommandWillStart(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
If e.GlobalCommandName = "ERASE" Then
Using trans As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartOpenCloseTransaction
ReadXData(CircleObjID, trans)
WriteXdata(CircleObjID, trans)
trans.Commit()
End Using
End If
End Sub
Sub WriteXdata(ByVal ObjID As ObjectId, ByRef trans As Transaction)
Using data As ResultBuffer = New ResultBuffer(New TypedValue(DxfCode.ExtendedDataRegAppName, "MyApp"),
New TypedValue(DxfCode.ExtendedDataInteger16, Convert.ToInt16(MyInteger)))
AddXData(ObjID, data, trans)
End Using
End Sub
Sub ReadXData(ByVal ObjID As ObjectId, ByRef trans As Transaction)
Dim Ent As Entity = trans.GetObject(ObjID, OpenMode.ForRead)
Dim myXdata As Array = Ent.XData.AsArray
MyInteger = myXdata.GetValue(1).value
End Sub
Sub AddXData(ByVal EntityID As ObjectId, ByVal BufferIn As ResultBuffer, ByRef trans As Transaction)
AddXDataApp(BufferIn.AsArray(0).Value, trans)
Dim selEnt As Entity = trans.GetObject(EntityID, OpenMode.ForWrite)
selEnt.XData = BufferIn
End Sub
Sub AddXDataApp(ByVal AppName As String, ByRef trans As Transaction)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim myAppTable As RegAppTable = trans.GetObject(doc.Database.RegAppTableId, OpenMode.ForWrite)
If myAppTable.Has(AppName) = False Then
Dim myAppTableRecord As New RegAppTableRecord
myAppTableRecord.Name = AppName
myAppTable.Add(myAppTableRecord)
trans.AddNewlyCreatedDBObject(myAppTableRecord, True)
End If
End Sub
Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
End Sub
End Class
Solved! Go to Solution.