Here it is just creating a rectangle around the block reference to clean it up with out all the hard code mess in it.
Was just seeing if it worked
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using AcadGi = Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Colors;
using System.Collections;
using System.Collections.Generic;
[assembly: CommandClass(typeof(AnimatedBlockHover.BlockHover))]
namespace AnimatedBlockHover
{
public class BlockHover
{
public static TempGraphics tgStatic = null;
public static System.Windows.Forms.Control blockHoverCtrl;
public BlockHover()
{
// Create New Windows Form Control
blockHoverCtrl = new System.Windows.Forms.Control();
blockHoverCtrl.CreateControl();
}
// TempGraphics is a helper class at bottom
void SeperateThreadProcess()
{
ThreadsDelegate threadDelegate = new ThreadsDelegate(MainThreadProcess);
TempGraphics tempG = new TempGraphics();
tempG = tgStatic;
tempG.drawAction = 0;
// Invoke Control
if (blockHoverCtrl.InvokeRequired)
{
blockHoverCtrl.Invoke(threadDelegate, tempG);// Draw Temp because tempG.drawAction == 0
}
else
{
MainThreadProcess(tempG);
}
System.Threading.Thread.Sleep(5000);// Sleep 5 Seconds
tempG.drawAction = 1;
if (blockHoverCtrl.InvokeRequired)
{
blockHoverCtrl.Invoke(threadDelegate, tempG);// Erase Temp Graphics because tempG.drawAction != 0
}
else
{
MainThreadProcess(tempG);
}
}
delegate void ThreadsDelegate(object obj); // delegate
void MainThreadProcess(object obj) // Add and remove Temp Graphics
{
TempGraphics tempG = (TempGraphics)obj;
if (tempG.drawAction == 0)
{
foreach (Polyline pLine in tempG.plList)
{
IntegerCollection coll = new IntegerCollection();
AcadGi.TransientManager.CurrentTransientManager.AddTransient(pLine, AcadGi.TransientDrawingMode.DirectShortTerm, 128, coll);
}
}
else
{
foreach (Polyline pLine in tempG.plList)
{
IntegerCollection coll = new IntegerCollection();
AcadGi.TransientManager.CurrentTransientManager.EraseTransient(pLine, coll);
pLine.Dispose();
}
tempG.plList.Clear();
tempG.plList = null;
}
Application.UpdateScreen();
}
[CommandMethod("HighlighBlockReferences")]
public void HighlighBlockReferences()
{
Application.DocumentManager.MdiActiveDocument.Editor.PointMonitor += new PointMonitorEventHandler(HoverHandler);
}
private void HoverHandler(object sender, PointMonitorEventArgs e)// PointMonitorEventHandler
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
FullSubentityPath[] fsps = e.Context.GetPickedEntities();
if (fsps.Length > 0)
{
FullSubentityPath fsp = fsps[0];
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
Entity ent = (Entity)tr.GetObject(fsp.GetObjectIds()[0], OpenMode.ForRead);
if (ent is BlockReference)
{
BlockReference bref = ent.Id.GetObject(OpenMode.ForRead) as BlockReference;
BlockTableRecord btr = bref.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
ObjectIdCollection brefIds = btr.GetBlockReferenceIds(true, false);
TempGraphics tg = new TempGraphics(); // Create new TempGraphics Helper Class
foreach (ObjectId objId in brefIds)
{
Entity ents = objId.GetObject(OpenMode.ForRead) as Entity;
Point3d min = ents.GeometricExtents.MinPoint;
Point3d max = ents.GeometricExtents.MaxPoint;
Polyline pl = new Polyline();// Create new polylines to add to TempGraphics Helper Class
pl.SetDatabaseDefaults();
Point2d p1 = new Point2d(min.X, min.Y);
Point2d p2 = new Point2d(min.X, max.Y);
Point2d p3 = new Point2d(max.X, max.Y);
Point2d p4 = new Point2d(max.X, min.Y);
pl.AddVertexAt(0, p1, 0, 0, 0);
pl.AddVertexAt(1, p2, 0, 0, 0);
pl.AddVertexAt(2, p3, 0, 0, 0);
pl.AddVertexAt(3, p4, 0, 0, 0);
pl.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
pl.Closed = true;
tg.plList.Add(pl); // Add PolyLines
}
tgStatic = tg;
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(SeperateThreadProcess));
thread.Start();// Start New Thread
}
}
catch
{
tr.Dispose();
}
tr.Commit();
}
}
}
}
public class TempGraphics
{
public List<Polyline> plList = new List<Polyline>();
public int drawAction;
public TempGraphics()
{
plList.Clear();
}
}
}