As @norman.yuan points out, using Transients would be ideal for something like this. I use transients all the time so I came up with the following classes.
Transient Base
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.GraphicsInterface
Namespace Transients
Public MustInherit Class TransientBase
Implements IDisposable
Public Property Entities As New List(Of Entity)
Public Overridable Sub SetColor(index As Integer)
For Each ent As Entity In Entities
ent.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, index)
'ent.ColorIndex = index
Next
UpdateTransient()
End Sub
Protected Friend Sub AddTransients()
Dim ctm As TransientManager = TransientManager.CurrentTransientManager
For Each drawable As Drawable In Entities
ctm.AddTransient(drawable, TransientDrawingMode.Main, 128, New IntegerCollection)
Next
End Sub
Protected Friend Sub RemoveTransients()
Dim ctm As TransientManager = TransientManager.CurrentTransientManager
For Each drawable As Drawable In Entities
ctm.EraseTransient(drawable, New IntegerCollection)
Next
For Each ent As Entity In Entities
ent.Dispose()
Next
Entities.Clear()
End Sub
Protected Sub UpdateTransient()
Dim ctm As TransientManager = TransientManager.CurrentTransientManager
For Each drawable As Drawable In Entities
ctm.UpdateTransient(drawable, New IntegerCollection)
Next
End Sub
#Region "IDisposable Support"
Friend disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
RemoveTransients()
For Each drawable As Drawable In Entities
drawable.Dispose()
Next
Entities.Clear()
End If
disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(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(disposing As Boolean) above.
Dispose(True)
' TODO: uncomment the following line if Finalize() is overridden above.
' GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
End Namespace
Transient that clears upon Regen
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices
Namespace Transients
Public Class RegenClearTransient
Inherits TransientBase
Private _doc As Document
Public Sub New(entity As Entity, doc As Document)
Me.Entities.Add(entity)
AddTransients()
_doc = doc
AddHandler _doc.CommandWillStart, cmdStartHandler
End Sub
Private ReadOnly cmdStartHandler As CommandEventHandler = AddressOf OnCmdStart
Private Sub OnCmdStart(sender As Object, e As CommandEventArgs)
If e.GlobalCommandName.Contains("REGEN") Then
Me.RemoveTransients()
RemoveHandler _doc.CommandWillStart, cmdStartHandler
End If
End Sub
End Class
End Namespace
And then an example on how to use it
Private rcTrans As Transients.RegenClearTransient 'Must have a backer variable for the transient to stay in view.
<CommandMethod("TempPolyTransientExample")>
Public Sub CmdTempPolyTrans()
Dim aDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = aDoc.Database
Dim ed As Editor = aDoc.Editor
Dim opt As New PromptEntityOptions(vbCrLf + "Select Polyline: ")
opt.SetRejectMessage(vbCrLf + "Selected entity must be a Polyline. Try Again.")
opt.AddAllowedClass(GetType(Polyline), False)
Dim res As PromptEntityResult = ed.GetEntity(opt)
If res.Status <> PromptStatus.OK Then
Exit Sub
End If
Using tr As Transaction = db.TransactionManager.StartTransaction
Dim poly As Polyline = tr.GetObject(res.ObjectId, OpenMode.ForRead)
Dim cPoly As Entity = poly.Clone
cPoly.ColorIndex = 2
rcTrans = New Transients.RegenClearTransient(cPoly, aDoc)
tr.Commit()
End Using
End Sub