Public Class acXrec(Of T)
Private oid As ObjectId
Public oidIsConnectedToAcad As Boolean
Private _name As String = "unknown"
Private _isundoing As Boolean
Public Event isUndoingEvent()
Public ReadOnly Property nameXrecord As String
Get
Return _name
End Get
End Property
Public Property isUndoing As Boolean
Get
Return _isundoing
End Get
Set(ByVal value As Boolean)
_isundoing = value
If value = True Then RaiseEvent isUndoingEvent()
End Set
End Property
Private Sub reg(ByVal trans As Transaction, ByVal on_off As Boolean)
'If oid.IsValid Then
Dim obj As DBObject = trans.GetObject(oid, OpenMode.ForRead, True)
Select Case on_off
Case True
If oidIsConnectedToAcad = False Then
AddHandler obj.ModifyUndone, AddressOf handler_objUndone
oidIsConnectedToAcad = True
End If
Case False
If oidIsConnectedToAcad = True Then
RemoveHandler obj.ModifyUndone, AddressOf handler_objUndone
oidIsConnectedToAcad = False
End If
End Select
End Sub
Public Sub disconnect_Xrecord(ByVal trans As Transaction)
If oidIsConnectedToAcad Then
register_oid_events(trans, False)
End If
End Sub
Private Sub handler_objUndone(ByVal o As Object, ByVal e As EventArgs)
''''''''''''''''''''''''''''''''''''''''''''''''''''
'uknown 3 event firering after REDO, otherwise 1
''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim dbo As DBObject = TryCast(o, DBObject)
If isUndoing = False Then isUndoing = True 'dbo.IsUndoing
End Sub
Public Sub connect_Xrecord(ByVal owners_oid As ObjectId, ByVal trans As Transaction, ByVal forceNewIfNotExists As Boolean, ByVal newname As String)
If oidIsConnectedToAcad = False Then
_name = newname
Dim ent As Entity = trans.GetObject(owners_oid, OpenMode.ForRead)
Dim novioid As ObjectId = checkIfExists(ent, trans)
If novioid.IsValid Then
oid = novioid
reg(trans, True)
Else
If forceNewIfNotExists Then
oid = createXrecForEntity(ent, trans)
register_oid_events(trans, True)
End If
End If
End If
End Sub
Private Function checkIfExists(ByVal ent As Entity, ByRef trans As Transaction) As ObjectId
Dim retval As New ObjectId
If ent.ExtensionDictionary.IsValid Then
Dim extensionDict As DBDictionary = trans.GetObject(ent.ExtensionDictionary(), OpenMode.ForRead)
If extensionDict.Contains(_name) Then
Dim xRec As Xrecord = trans.GetObject(extensionDict.GetAt(_name), OpenMode.ForRead)
retval = xRec.ObjectId
End If
End If
Return retval
End Function
Private Function createXrecForEntity(ByVal ent As Entity, ByRef trans As Transaction) As ObjectId
ent.Database.DisableUndoRecording(True)
Dim retval As New ObjectId
If ent.ExtensionDictionary.IsNull Then
ent.UpgradeOpen()
ent.CreateExtensionDictionary()
End If
Dim extensionDict As DBDictionary = trans.GetObject(ent.ExtensionDictionary(), OpenMode.ForWrite)
Using newxrec As New Xrecord
extensionDict.SetAt(_name, newxrec)
trans.AddNewlyCreatedDBObject(newxrec, True)
retval = newxrec.ObjectId
End Using
ent.Database.DisableUndoRecording(False)
Return retval
End Function
#Region "XrecSaveRead of T"
Public Sub SaveDataToXrec(ByVal dataobject As T, ByVal trans As Transaction)
SaveStreamToXrec(DataToStream(dataobject), trans)
End Sub
Public Function GetDataFromXrec(ByVal trans As Transaction) As T
Dim retval As T
If oidIsConnectedToAcad Then
Dim xs As New XmlSerializer(GetType(T))
Dim ms As MemoryStream = ReadStreamFromXrec(trans)
If ms.Length > 0 Then retval = xs.Deserialize(ms)
End If
Return retval
End Function
Private Function DataToStream(ByVal objectToSerializate As T) As MemoryStream
Dim ms As New MemoryStream
Dim xs As New XmlSerializer(GetType(T))
xs.Serialize(ms, objectToSerializate)
ms.Position = 0
Return ms
End Function
#Region "Xrec stream"
Public Sub SaveStreamToXrec(ByVal memstream As MemoryStream, ByVal trans As Transaction)
If oidIsConnectedToAcad = True Then
Dim ent As Xrecord = trans.GetObject(oid, OpenMode.ForWrite)
ent.Data = tools.StreamToResBuf(memstream)
End If
End Sub
Public Function ReadStreamFromXrec(ByVal trans As Transaction) As MemoryStream
Dim ms As New MemoryStream
If oidIsConnectedToAcad = True Then
Dim ent As Xrecord = trans.GetObject(oid, OpenMode.ForRead)
Dim xbuff As ResultBuffer = Nothing
xbuff = ent.Data
If xbuff IsNot Nothing Then ms = tools.ResBufToStream(xbuff)
Me.isUndoing = False
End If
Return ms
End Function
End Class