The code will add the tool-tip to show the block name and how many times it is inserted.
It also draws a temporary bounding box around all the block references when you hover over a block reference. I just can not get it to draw a temporary hatch.
I threw in to show a circles diameter and pline's length if anyone wanted idea what you could do with it.
Any ideas on how to temporarily add a hatch without committing it?
Unless I am wrong I do not see how to do it without adding it to the database so you can get it's ObjectId.
Thanks to Fenton Webb I got the idea from DevTV: Introduction to AutoCAD .NET Programming at
Here is the linkThe Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
namespace BlockReferenceHover
{
public class BlockHover
{
[CommandMethod("HighlighBlockReferences")]
public void HighlighBlockReferences()
{
Application.DocumentManager.MdiActiveDocument.Editor.PointMonitor += new PointMonitorEventHandler(HoverHandler);
}
private void HoverHandler(object sender, PointMonitorEventArgs e)
{
FullSubentityPath[] fsps = e.Context.GetPickedEntities();
if (fsps.Length > 0)
{
FullSubentityPath fsp = fsps[0];
using (Transaction tr = Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
{
try
{
Entity ent = (Entity)tr.GetObject(fsp.GetObjectIds()[0], OpenMode.ForRead);
if (ent is Circle)
{
Circle cir = ent.Id.GetObject(OpenMode.ForRead) as Circle;
double diam = cir.Diameter;
e.AppendToolTipText("Circle\nDiameter: " + diam);
}
else if (ent is Polyline)
{
Polyline pl = ent.Id.GetObject(OpenMode.ForRead) as Polyline;
double plneLength = pl.Length;
e.AppendToolTipText("Polyline\nLength: " + plneLength);
}
else if (ent is BlockReference)
{
BlockTableRecord btr;
BlockReference bref = ent.Id.GetObject(OpenMode.ForRead) as BlockReference;
if (bref.IsDynamicBlock)
{// Add in code for dynamic blocks
//btr = bref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
//string btrDyName = btr.Name;
//int numDyBrefs = btr.GetAnonymousBlockIds().Count;
//e.AppendToolTipText("You are a bitch \n " + btrDyName + "\nThere are " + numDyBrefs + " inserted");
}
else
{
btr = bref.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
string btrName = btr.Name;
ObjectIdCollection brefIds = btr.GetBlockReferenceIds(true, false);
int numBrefs = brefIds.Count;
e.AppendToolTipText(btrName + "\nThere are " + numBrefs + " inserted");
ObjectIdCollection objIdColl = new ObjectIdCollection();
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();
pl.SetDatabaseDefaults();
pl.AddVertexAt(0, new Point2d(min.X, min.Y), 0, 0, 0);
pl.AddVertexAt(1, new Point2d(max.X, min.Y), 0, 0, 0);
pl.AddVertexAt(2, new Point2d(max.X, max.Y), 0, 0, 0);
pl.AddVertexAt(3, new Point2d(min.X, max.Y), 0, 0, 0);
pl.Color = Color.FromColorIndex(ColorMethod.ByAci,2);
pl.Closed = true;
e.Context.DrawContext.Geometry.Draw(pl);
//Hatch hat = new Hatch();
//objIdColl.Add(pl.ObjectId);
//hat.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
//hat.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
//hat.Transparency = new Transparency(127);
//e.Context.DrawContext.Geometry.Draw(hat);
//hat.Associative = true;
//hat.AppendLoop(HatchLoopTypes.Default, objIdColl);
//hat.EvaluateHatch(true);
//objIdColl.Clear();
}
}
}
else
// Commit out Else statement if you want Autocad to handle it normally
{
e.AppendToolTipText("Your a bitch");
}
}
catch
{
tr.Dispose();
}
}
}
}
}
}
The Picture is when you hover over a block reference