Author Topic: Hover Trouble  (Read 2604 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Hover Trouble
« on: October 03, 2010, 07:22:56 PM »
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 link

The Code
Code: [Select]

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

kaefer

  • Guest
Re: Hover Trouble
« Reply #1 on: October 04, 2010, 06:47:00 AM »
Any ideas on how to temporarily add a hatch without committing it?
Hi, Jeff!

The obvious issue is your hatch border: You haven't added the polyline to the database, so you can't use its ID to add a hatch loop. Perhaps p/invoking AcDbHatch::appendLoop with vertices and bulges as array parameters might work better.

What about a Solid? Since you're only interested in a rectangular hatch over the extension corner points, it would fit the bill.

Cheers, Thorsten

(BTW, you are opening the hover-over entity twice, when casting it to its tested type should suffice.)

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Hover Trouble
« Reply #2 on: October 04, 2010, 09:12:08 AM »
Hey kaefer I thought I would have that problem needing the objectId, but the solid idea is good one I did not think of that.
Thanks for the idea kaefer.