Public Class BlockModel Inherits ACADObjectModel Implements IDisposable Public Event BlockModified() Public Sub New(ObjID As ObjectId) MyBase.New(ObjID) AddHandlers() End Sub Private Sub RemoveHandlers() Try Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(ObjID.Database) Using tran As Transaction = doc.TransactionManager.StartTransaction() Dim item As BlockReference = ObjID.GetObject(OpenMode.ForRead) RemoveHandler item.Modified, AddressOf OnBlockModified End Using Catch ex As Exception 'TODO: Handle End Try End Sub Private Sub AddHandlers() Try Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.GetDocument(ObjID.Database) Using tran As Transaction = doc.TransactionManager.StartTransaction() Dim item As BlockReference = ObjID.GetObject(OpenMode.ForRead) AddHandler item.Modified, AddressOf OnBlockModified End Using Catch ex As Exception 'TODO: Handle End Try End Sub Protected Sub OnBlockModified(ByVal Sender As Object, e As EventArgs) RaiseEvent BlockModified() End Sub Public Overrides Function GetState() As ACADObjectState Return BlockStateFactory.BuildBlockState(ObjID) End Function Public Overrides Sub SetState(State As ACADObjectState) If Not State.GetType() Is GetType(BlockState) Then Throw New ArgumentException("State Type is Invalid.") Dim blkState As BlockState = State RemoveHandlers() DynamicBlockHelper.UpdateAttributeValues(ObjID, blkState.Attributes) For Each bprop As BlockProperty In blkState.Properties Select Case bprop.PropertyType Case 1 'numeric (double) Dim n As Double If Not Double.TryParse(bprop.SelectedValue, n) Then Throw New InvalidOperationException("BlockDefID " + bprop.BlockDefID + " PropertyDefID " + bprop.PropertyDefID + " Value " + bprop.SelectedValue + " Should be Numeric but isn't.") DynamicBlockHelper.SetParameter(ObjID, bprop.Name, n) Case 3 'boolean Dim b As Short If Not Short.TryParse(bprop.SelectedValue, b) AndAlso (b = 0 OrElse b = 1) Then Throw New InvalidOperationException("BlockDefID " + bprop.BlockDefID + " PropertyDefID " + bprop.PropertyDefID + " Value " + bprop.SelectedValue + " Should be Boolean but isn't.") DynamicBlockHelper.SetParameter(ObjID, bprop.Name, b) Case Else 'assume string DynamicBlockHelper.SetParameter(ObjID, bprop.Name, bprop.SelectedValue) End Select Next AddHandlers() End Sub #Region "IDisposable Support" Private disposedValue As Boolean ' To detect redundant calls ' IDisposable Protected Overridable Sub Dispose(disposing As Boolean) If Not Me.disposedValue Then If disposing Then Try RemoveHandlers() Catch ex As Exception End Try _ObjID = Nothing End If ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. ' TODO: set large fields to null. End If Me.disposedValue = True End Sub ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources. 'Protected Overrides Sub Finalize() ' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. ' Dispose(False) ' MyBase.Finalize() 'End Sub ' This code added by Visual Basic to correctly implement the disposable pattern. Public Sub Dispose() Implements IDisposable.Dispose ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above. Dispose(True) GC.SuppressFinalize(Me) End Sub #End Region End Class Public Shared Function SetParameter(ByVal BlockID As ObjectId, ByVal ParameterName As String, ByVal Value As String) As Boolean Using dl As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument(DocumentLockMode.ProtectedAutoWrite, Nothing, Nothing, True) Using myTrans As Transaction = BlockID.Database.TransactionManager.StartTransaction Dim myBRef As BlockReference = BlockID.GetObject(OpenMode.ForWrite) For Each myDynamProp As DynamicBlockReferenceProperty In _ myBRef.DynamicBlockReferencePropertyCollection If myDynamProp.PropertyName.Equals( _ ParameterName, StringComparison.OrdinalIgnoreCase) = True Then myDynamProp.Value = Value myTrans.Commit() Return True End If Next Return False End Using End Using End Function