Author Topic: Highlight closed polyline via mouse cursor hover  (Read 1566 times)

0 Members and 1 Guest are viewing this topic.

themethodman

  • Mosquito
  • Posts: 12
Highlight closed polyline via mouse cursor hover
« on: January 16, 2022, 10:03:36 PM »
I'm trying to generate a temporary hatch and text when I hover the cursor over a polylines/lines that make up a closed polyline. Text will display area etc.

For now, a simple white polyline along the perimeter is shown for example.

See GIF:

I'm successfully using the Editor.TraceBoundary() however it generates messages in the command editor box whenever a closed boundary can/cannot be found

Is there anyway to switch this off? Otherwise is there an alternative way of achieving this?

Code: [Select]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using aGi = Autodesk.AutoCAD.GraphicsInterface;

namespace Test
{
    public class PointMonitorTooltips
    {
        private static DBObjectCollection m_markers = new DBObjectCollection();
        private static IntegerCollection collectints = new IntegerCollection();

        [CommandMethod("starto")]
        public static void StartMonitor()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.PointMonitor += new PointMonitorEventHandler(ed_PointMonitor);
        }

        [CommandMethod("stopo")]
        public static void stopmonitor()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.PointMonitor -= ed_PointMonitor;
            ClearMarkers();
        }
        static void ed_PointMonitor(object sender, PointMonitorEventArgs e)
        {
            showRegionHatch(e.Context.RawPoint);
        }

        static void showRegionHatch(Point3d pt)
        {
            ClearMarkers();

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor edt = doc.Editor;

            var objs = edt.TraceBoundary(pt, false);
            if (objs == null || objs.Count == 0)
                return;

            // If we have some geometry, create a Region from it
            try
            {
                var regstest = Region.CreateFromCurves(objs);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception Ex)
            {
                Application.ShowAlertDialog("Error");
            }
            var regs = Region.CreateFromCurves(objs);
            if (regs == null || regs.Count == 0)
                return;

            foreach (Region reg in regs)
            {
                DBObjectCollection dbocRegionPolylines = Extensions.PolylineFromRegion(reg);
                foreach (Entity ent in dbocRegionPolylines)
                {
                    m_markers.Add(ent);
                    aGi.TransientManager.CurrentTransientManager.AddTransient(ent, aGi.TransientDrawingMode.DirectShortTerm, 128, collectints);
                }
            }
        }

        static void ClearMarkers()
        {
            for (int i = 0; i < m_markers.Count; i++)
            {
                aGi.TransientManager.CurrentTransientManager.EraseTransient(m_markers[i], collectints);
                m_markers[i].Dispose();
            }
            m_markers.Clear();
        }
    }
}
« Last Edit: January 16, 2022, 10:51:34 PM by themethodman »

Jeff_M

  • King Gator
  • Posts: 4095
  • C3D user & customizer
Re: Highlight closed polyline via mouse cursor hover
« Reply #1 on: January 17, 2022, 04:00:38 PM »
One way would be to get a selectionset of all modelspace closed polylines, check if the current cursor location is inside one of them, then Highlight the polyline and display the hatch/text. I would probably also get the geometric extents of the polylines so it isn't constantly checking for the point being inside when elsewhere in the dwg. I do s similar thing for highlighting regions in a C3D Corridor.

themethodman

  • Mosquito
  • Posts: 12
Re: Highlight closed polyline via mouse cursor hover
« Reply #2 on: January 18, 2022, 05:15:17 AM »
After some trial and error I used SendStringToExecute to set CLIPROMPTLINES to 0. After the program finishes I've coded it to revert back to default. Not a very clean solution but seems to work.

Thanks Jeff_M for the suggestion - I will look at doing that next  :nerdystraight:
« Last Edit: January 18, 2022, 05:32:02 AM by themethodman »

Jeff_M

  • King Gator
  • Posts: 4095
  • C3D user & customizer
Re: Highlight closed polyline via mouse cursor hover
« Reply #3 on: January 18, 2022, 10:35:54 AM »
You could set that sysvar in code without resorting to sendstringtoexecute. A better sysvar for this may be NOMUTT.

Application.SetSystemVariable("NOMUTT", 1); //reset to 0 when done