Author Topic: Show block tooltip  (Read 815 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Show block tooltip
« on: October 28, 2012, 09:22:47 PM »
To display block info, please, try to hover mouse right on block,
then tooltip message woul be displayed
Tested on A2010 only

Code: [Select]
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

namespace ToolTips
{
    public class BlockToolTip
    {
        public class Drawing
        {

            Document doc;
            Editor ed;

            public Drawing()
            {
                doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                ed = doc.Editor;
            }



            [CommandMethod("WWS")]
            public void StartToolTip()
            {
                ed.WriteMessage("\n---   To remove handler type: WWE   ---\n");
                ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);
            }


            void ed_PointMonitor(object sender, PointMonitorEventArgs e)
            {
                Editor ed = (Editor)sender;
                try
                {
                    FullSubentityPath[] paths = e.Context.GetPickedEntities();
                    string attdata = "";
                    Transaction tr = doc.TransactionManager.StartTransaction();
                    using (tr)
                    {
                        foreach (FullSubentityPath path in paths)
                        {
                            ObjectId[] ids = path.GetObjectIds();
                            if (ids.Length > 0)
                            {
                                ObjectId id = ids[0];
                                DBObject obj = tr.GetObject(id, OpenMode.ForRead);
                                if (obj != null)
                                {
                                    BlockReference bref = obj as BlockReference;
                                    if (bref != null)
                                    {
                                        BlockTableRecord bdef = (BlockTableRecord)tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead);
                                        if (!bdef.HasAttributeDefinitions) return;
                                        attdata = GetAttributesData(tr, bref);
                                    }
                                }
                            }
                        }
                        tr.Commit();
                    }
                    if (attdata != "")
                        e.AppendToolTipText(attdata);
                }
                catch (System.Exception ex)
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message + "\n" + ex.StackTrace);
                }
            }
            string GetAttributesData(Transaction tr, BlockReference bref)
            {
                int mix = 24;
                int left;
                StringBuilder sb = new StringBuilder();
                left = "Block:".Length;
                sb.AppendLine(String.Format("{0}{1}\n", "Block:".PadRight(mix - 6, ' '), bref.Name));
                var info = (from ObjectId id in bref.AttributeCollection
                            let attref = (AttributeReference)tr.GetObject(id, OpenMode.ForRead, false)
                            select new { key = attref.Tag, value = attref.TextString }).ToArray();
                foreach (var item in info)
                {
                    left = item.key.Length;
                    sb.AppendLine(String.Format("{0}{1}", item.key.PadRight(mix - left, ' '), item.value));
                }
                return sb.ToString();
            }

            [CommandMethod("wwe")]
            public void EndToolTip()
            {
                ed.TurnForcedPickOn();
                ed.PointMonitor -= ed_PointMonitor;
            }
        }
    }
}

Offline BlackBox

  • Newbie
  • *
  • Posts: 4
  • Karma: +1/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Show block tooltip
« Reply #1 on: October 29, 2012, 03:43:38 PM »
Nice one, Oleg... Also good to see you coding more C#.

I started something similar for RasterImage(s) a while back in this thread.
"Potential has a shelf life." - Margaret Atwood

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: Show block tooltip
« Reply #2 on: October 29, 2012, 06:07:19 PM »
To be honestly, most of code is written by Kean Walmsley,
regatds to him for that, mine is just a litlle piece of this routine,
Cheers :)