The reason the ClearGraphics() method does not clear the Transient (and even cause crashing) is the drawable (referenced by class-level variable transientEntity ) has been disposed in the scope of the ClearGraphics() method when TransientManager calls EraseTransient(), because the drawable is created in a "using..." block!
You need to keep the drawable alive after the transient graphics is drawn, in order to update it, or erase it. Of cause, since the drawable is a non-database-residing entity, you need to dispose it when all the Transient thing is done.
So, one of the proper approach to do this type of work is to implement the class as IDisposable. Something like:
public class TransientGraphics : IDisposable
{
private TransientManager tm = TransientManager.CurrentTransientManager;
private Entity transientEntity;
public void Dispose()
{
if (transientEntity!=null)
{
ClearGraphics(true);
}
}
public void DrawTriangle(Point3d location, double size, short colorIndex)
{
// Clear any existing triangle before drawing a new one
ClearGraphics();
triangle.AddVertexAt(0, new Point2d(location.X, location.Y + size), 0, 0, 0);
triangle.AddVertexAt(1, new Point2d(location.X - size, location.Y - size), 0, 0, 0);
triangle.AddVertexAt(2, new Point2d(location.X + size, location.Y - size), 0, 0, 0);
triangle.Closed = true;
triangle.ColorIndex = colorIndex;
transientEntity = triangle; // Store the new triangle entity
tm.AddTransient(triangle, TransientDrawingMode.DirectShortTerm, 128, new IntegerCollection());
}
public void ClearGraphics(bool disposeDrawble = false)
{
if (transientEntity != null)
{
try
{
// Erase the transient graphic
tm.EraseTransient(transientEntity, new IntegerCollection());
// Ensure it is disposed of safely
if (disposeDrawable)
{
transientEntity.Dispose();
transientEntity = null; // Reset reference
}
// Logging for debug
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nTransient graphics cleared successfully.");
}
catch (Exception ex)
{
// Catch and log any potential issues during the erase process
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\nError clearing transient graphics: {ex.Message}");
}
}
else
{
// Log when there are no transient graphics to clear
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nNo transient graphics to clear.");
}
}
}
You would use the class like:
...
using (var graphics = new TransientGraphics())
{
// draw the transient
// get user input
// update the transient, or erase it and redraw a new one
// get user input
} // at the end, the transient is erased automatically because of the Dispose()
Notice
1. The drawable is created and should be alive during the entire instance of TransientGraphics class.
2. I added an optional input for the ClearGraphics() to allow it decide whether the drawable is to be disposed or not.
3. You can also create the drawable entity in the TransientGraphics's constructor (i.e. only create it once and let it to be disposed at the end of "using..." block. This way, you can change/transform the drawable entity inside the scope of the "using..." and call TransientManager.UpdateTransient(), or call ClearGraphics(false) to clear the graphcs without dispose the entity, so you can redraw it.
4. Of course you can always create a new drawable and show it, and then clear the graphics and also dispose it at the same time. Just remember, the drawable should be alive before the graphics is erased.