Author Topic: Polyline Highlighted as its being drawn  (Read 2558 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Polyline Highlighted as its being drawn
« on: May 20, 2015, 02:47:57 PM »
I thought I saw at one time here; a lisp that would highlight a polyline as it was being drawn. I am starting to trace xref's in paperspace, but there is so much clutter its sometimes hard to see where it is.

I know I can change xref colors and all that, but didn't know if that was still around or not.

Thanks.
Civil3D 2020

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Polyline Highlighted as its being drawn
« Reply #1 on: May 21, 2015, 03:44:34 PM »
Draw your pline with a width.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

mjfarrell

  • Seagull
  • Posts: 14444
  • Every Student their own Lesson
Re: Polyline Highlighted as its being drawn
« Reply #2 on: May 21, 2015, 03:45:53 PM »
I thought I saw at one time here; a lisp that would highlight a polyline as it was being drawn. I am starting to trace xref's in paperspace, but there is so much clutter its sometimes hard to see where it is.

I know I can change xref colors and all that, but didn't know if that was still around or not.

Thanks.

Why are you doing this?  These is probably a better way forward for you....
Be your Best


Michael Farrell
http://primeservicesglobal.com/

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Polyline Highlighted as its being drawn
« Reply #3 on: May 21, 2015, 04:11:19 PM »
its for tracing erosion control plan for hatching! YEA! sarcasm.... Its a dog to do, no body likes doing it....
Civil3D 2020

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Polyline Highlighted as its being drawn
« Reply #4 on: August 06, 2015, 10:05:24 AM »
I am going to share that I started using parcels for this stuff. Works great for areas ad hatching.

But I still am curious if there is a way you can hoover your cursor over a line arc what ever and then traces it like a polyline.
Civil3D 2020

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Polyline Highlighted as its being drawn
« Reply #5 on: August 06, 2015, 10:33:37 AM »
Ok. I searched theswamp one more time and I think I found it.

http://drive-cad-with-code.blogspot.ca/2014/09/moving-mouse-cursor-to-trace-polyline.html

Just having a brain issue not remembering how to convert this to a netload...

Code: [Select]
using System;
using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using CadDb = Autodesk.AutoCAD.DatabaseServices;
 
namespace TraceAlongPolyline
{
    public class PolylineTracer : IDisposable
    {
        private Document _dwg;
        private Editor _ed;
        private TransientManager _tsManager;
        private CadDb.Polyline _polyline = null;
        private CadDb.Polyline _drawable = null;
        private Point3d _startPoint;
        private double _allowedOffset = 1.0;
 
        private int _colorIndex = 2;
        private LineWeight _lineWeight = LineWeight.LineWeight050;
 
        public PolylineTracer(Document dwg)
        {
            _dwg = dwg;
            _ed = dwg.Editor;
            _tsManager = TransientManager.CurrentTransientManager;
        }
 
        public void Dispose()
        {
            ClearTransientGraphics();
        }
 
        public void Trace(
            int colorIndex=2, LineWeight lineweight=LineWeight.LineWeight050)
        {
            _colorIndex = colorIndex;
            _lineWeight = lineweight;
 
            ObjectId selectedId = SelectPolyline();
            if (selectedId.IsNull)
            {
                _ed.WriteMessage("\n*Cancel*");
                return;
            }
 
            using (Transaction tran =
                _dwg.TransactionManager.StartTransaction())
            {
                _polyline = (CadDb.Polyline)
                    tran.GetObject(selectedId, OpenMode.ForRead);
                _allowedOffset = _polyline.Length / 50.0;
 
                _polyline.Highlight();
 
                try
                {
                    if (SelectTraceStartPoint())
                    {
                        int showLineWeight =
                            Convert.ToInt32(
                            CadApp.GetSystemVariable("LWDISPLAY"));
 
                        CadApp.SetSystemVariable("LWDISPLAY", 1);
 
                        try
                        {
                            _ed.PointMonitor += Editor_PointMonitor;
                            PromptPointOptions opt = new PromptPointOptions(
                                "\nClick on the polyline for path length:");
                            opt.AllowNone = true;
                            opt.Keywords.Add("eXit");
                            opt.Keywords.Default = "eXit";
 
                            PromptPointResult res = _ed.GetPoint(opt);
                            if (res.Status == PromptStatus.OK)
                            {
                                if (_drawable != null)
                                {
                                    _ed.WriteMessage(
                                        "\nLength of picked path: {0}",
                                        _drawable.Length);
                                }
                            }
                        }
                        finally
                        {
                            _ed.PointMonitor -= Editor_PointMonitor;
                            CadApp.SetSystemVariable(
                                "LWDISPLAY", showLineWeight);
                        }
                    }
                }
                finally
                {
                    _polyline.Unhighlight();
                }
 
                tran.Commit();
            }
        }
 
        #region private methods
 
        private ObjectId SelectPolyline()
        {
            PromptEntityOptions opt = new PromptEntityOptions(
                "\nSelect a polyline:");
            opt.SetRejectMessage("\nInvalid selection: not a polyline.");
            opt.AddAllowedClass(typeof(CadDb.Polyline), true);
            PromptEntityResult res = _ed.GetEntity(opt);
            if (res.Status == PromptStatus.OK)
                return res.ObjectId;
            else
                return ObjectId.Null;
        }
 
        private bool SelectTraceStartPoint()
        {
            while (true)
            {
                PromptPointOptions opt = new PromptPointOptions(
                    "\nTrace starts at -> " +
                    "pick a point on the selected polyline, " +
                    "or use polyline's start point:");
                opt.AllowNone = true;
                opt.Keywords.Add("Start point");
                opt.Keywords.Default = "Start point";
 
                PromptPointResult res = _ed.GetPoint(opt);
                if (res.Status == PromptStatus.OK ||
                    res.Status == PromptStatus.Keyword)
                {
                    if (res.Status == PromptStatus.Keyword)
                    {
                        _startPoint = _polyline.StartPoint;
                        return true;
                    }
                    else
                    {
                        Point3d pt = _ed.Snap("NEA", res.Value);
                        if (IsPointOnPolyline(pt, _polyline))
                        {
                            _startPoint = pt;
                            return true;
                        }
                    }
 
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
 
        private bool IsPointOnPolyline(Point3d pt, CadDb.Polyline poly)
        {
            Point3d closest = poly.GetClosestPointTo(pt, false);
            return IsTheSamePoint(closest, pt);
        }
 
        private bool IsTheSamePoint(Point3d pt1, Point3d pt2)
        {
            double dist = pt1.DistanceTo(pt2);
            return dist <= Tolerance.Global.EqualPoint;
        }
 
        #endregion
 
        #region private methods: draw transientgraphics along the polyline
 
        private void Editor_PointMonitor(
            object sender, PointMonitorEventArgs e)
        {
            ClearTransientGraphics();
 
            //Current mouse cursor point
            Point3d pt = e.Context.RawPoint;
 
            //Closest point on the polyline
            Point3d closest = _polyline.GetClosestPointTo(pt, false);
 
            //If the mouse cursor is to far off the polyline
            if (pt.DistanceTo(closest) > _allowedOffset) return;
 
            //Draw TransientGraphics
            DrawTransientGraphics(closest);
 
            double l = _drawable.Length;
            string tip = string.Format("Traced length: {0}", l);
            e.AppendToolTipText(tip);
        }
 
        private void ClearTransientGraphics()
        {
            if (_drawable!=null)
            {
                _tsManager.EraseTransient(_drawable, new IntegerCollection());
                _drawable.Dispose();
                _drawable = null;
            }
        }
 
        private void DrawTransientGraphics(Point3d endPoint)
        {
            List<Point2d> vertices = CollectVerticesFromPolyline(endPoint);
            _drawable = CreatePolylineFromPoints(vertices);
            _tsManager.AddTransient(
                _drawable,
                TransientDrawingMode.DirectTopmost,
                128,
                new IntegerCollection());
        }
 
        private List<Point2d> CollectVerticesFromPolyline(Point3d endPoint)
        {
            List<Point2d> lst = new List<Point2d>();
 
            double startDistance = _polyline.GetDistAtPoint(_startPoint);
            double endDistance = _polyline.GetDistAtPoint(endPoint);
 
            lst.Add(new Point2d(_startPoint.X, _startPoint.Y));
 
            bool stop = false;
 
            for (int i = 1; i < _polyline.NumberOfVertices; i++)
            {
                Point3d vertex = _polyline.GetPoint3dAt(i);
                double dist = _polyline.GetDistAtPoint(vertex);
 
                if (dist>startDistance)
                {
                    if (dist<endDistance)
                    {
                        lst.Add(new Point2d(vertex.X, vertex.Y));
                    }
                    else
                    {
                        lst.Add(new Point2d(endPoint.X, endPoint.Y));
                        stop = true;
                    }
                }
 
                if (stop) break;
            }
               
            return lst;
        }
 
        private CadDb.Polyline CreatePolylineFromPoints(List<Point2d> points)
        {
            CadDb.Polyline poly = new CadDb.Polyline(points.Count);
            int i = 0;
            foreach (var p in points)
            {
                poly.AddVertexAt(i, p, 0.0, 0.0, 0.0);
                i++;
            }
 
            poly.ColorIndex = _colorIndex;
            poly.LineWeight = _lineWeight;
 
            return poly;
        }
 
        #endregion
    }
}


 Here is the command method to run it:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assembly: CommandClass(typeof(TraceAlongPolyline.MyCadCommands))]
 
namespace TraceAlongPolyline
{
    public class MyCadCommands
    {
        [CommandMethod("TracePoly")]
        public static void RunMyCadCommand()
        {
            Document dwg = CadApp.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;
 
            try
            {
                using (PolylineTracer tracer=new PolylineTracer(dwg))
                {
                    tracer.Trace();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}\n{1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }
    }
}

Civil3D 2020

ChrisCarlson

  • Guest
Re: Polyline Highlighted as its being drawn
« Reply #6 on: August 06, 2015, 11:06:15 AM »
That starts to deal with .NET and Visual Studio. I'd find someone who can compile it unless you want to delve into that world.

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: Polyline Highlighted as its being drawn
« Reply #7 on: August 10, 2015, 02:09:46 PM »
Just a thought, if it's an xRef, have you tried using ncopy to copy the lines you are tracing instead of redrawing them?

MSTG007

  • Gator
  • Posts: 2598
  • I can't remeber what I already asked! I need help!
Re: Polyline Highlighted as its being drawn
« Reply #8 on: August 10, 2015, 02:11:53 PM »
Yup i have... just thought by using my mouse to hoover over the line would be a easy thing to do rather than clicking on all the time.
Civil3D 2020