Highlight part of polyline

Highlight part of polyline

Ertqwa
Advocate Advocate
3,240 Views
10 Replies
Message 1 of 11

Highlight part of polyline

Ertqwa
Advocate
Advocate

Hello Forum,

 

is it possible to highlight a part of a polyline in someway?

 

Thank you.

0 Likes
Accepted solutions (1)
3,241 Views
10 Replies
Replies (10)
Message 2 of 11

Hallex
Advisor
Advisor

Try thsi code

 

        [CommandMethod("Segh")]
        public void SegmentHighLight()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;

            Database db = doc.Database;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect the segment to copy: ");

            peo.SetRejectMessage("\nYou have to select polyline only!");

            peo.AllowNone = false;

            peo.AllowObjectOnLockedLayer = false;

            peo.AddAllowedClass(typeof(Polyline), true);

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK) return;

            ObjectId objId = per.ObjectId;

            if (!objId.ObjectClass.IsDerivedFrom( RXClass.GetClass(typeof(Polyline))))
            {

                ed.WriteMessage(

                    "\nYou didn't select a lwpline, please try again...\n");

                return;

            }

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    Polyline pline = tr.GetObject(objId, OpenMode.ForRead, false) as Polyline;

                    if (pline != null)
                    {
                        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false) as BlockTableRecord;

                        Point3d pickPt = pline.GetClosestPointTo((Point3d)per.PickedPoint, false);

                        double param = pline.GetParameterAtPoint(pickPt);

                        int num = (int)param + 1;

                        ObjectId[] subentIds = { };

                        FullSubentityPath[] ips = pline.GetSubentityPathsAtGraphicsMarker(SubentityType.Edge, num, pickPt, Matrix3d.Identity, 1, subentIds);

                        pline.Highlight(ips[0], true);

                    }
                    tr.Commit();
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                ed.WriteMessage("Pokey");
            }
        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 11

Ertqwa
Advocate
Advocate

Hi, ty for the response.

 

If I try your code,  get an error at the following line:

 

pline.Highlight(ips[0], true);

Error:

eNotApplicable at Autodesk.AutoCAD.DatabaseServices.Entity.Highlight(FullSubentityPath subId, Boolean highlightAll)

 

Trying to find out what it might be but no luck so far.

0 Likes
Message 4 of 11

Hallex
Advisor
Advisor

I tested code on A2010, it's  a reason maybe

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 11

Ertqwa
Advocate
Advocate

Ah I see, I use AutoCAD2009. I'll do some more checking. Ty.

0 Likes
Message 6 of 11

Hallex
Advisor
Advisor
Accepted solution

Here is another way, see maybe it will working on your release

 

        	using Autodesk.AutoCAD.ApplicationServices;
	using Autodesk.AutoCAD.DatabaseServices;
	using Autodesk.AutoCAD.EditorInput;
	using Autodesk.AutoCAD.Runtime;
	using Autodesk.AutoCAD.Geometry;
	using Autodesk.AutoCAD.GraphicsInterface;
	using Autodesk.AutoCAD.Colors;
	//_____________________________________//

        [CommandMethod("colseg")]
        public void ColoredSegment()
        {

            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect Segment of Polyline:");

            peo.SetRejectMessage("\nMust be a Polyline!");

            peo.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), false);

            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)

                return;

            Transaction tr = db.TransactionManager.StartTransaction();

            Autodesk.AutoCAD.DatabaseServices.Polyline pl = new Autodesk.AutoCAD.DatabaseServices.Polyline();

            IntegerCollection ic = new IntegerCollection();

            using (tr)
            {
                Autodesk.AutoCAD.DatabaseServices.Polyline pline = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Autodesk.AutoCAD.DatabaseServices.Polyline;

                if (pline != null)
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(pline.OwnerId, OpenMode.ForWrite);

                    double bulg = 0;

                    Point3d picked = pline.GetClosestPointTo((Point3d)per.PickedPoint, false);

                    double par = pline.GetParameterAtPoint(picked);

                    int m = (int)par;

                    Point2dCollection verts = new Point2dCollection(2);

                    SegmentType stype = pline.GetSegmentType(m);

                    if (stype == SegmentType.Arc)
                    {
                        CircularArc2d arc = pline.GetArcSegment2dAt(m);

                        bulg = pline.GetBulgeAt(m);

                        verts.Add(arc.StartPoint);

                        verts.Add(arc.EndPoint);
                    }
                    else if (stype == SegmentType.Line)
                    {
                        LineSegment2d ln = pline.GetLineSegment2dAt(m);

                        verts.Add(ln.StartPoint);

                        verts.Add(ln.EndPoint);

                        bulg = 0;
                    }

                    pl.AddVertexAt(0, verts[0], bulg, 0, 0);

                    pl.AddVertexAt(1, verts[1], 0, 0, 0);

                    pl.ColorIndex = 121;

                    ObjectIdCollection ids = new ObjectIdCollection();

                    btr.AppendEntity(pl);

                    tr.AddNewlyCreatedDBObject(pl, true);

                    verts = new Point2dCollection();

                    if (pl != null)
                    {
                        TransientManager tm = TransientManager.CurrentTransientManager;
                        tm.AddTransient(pl, TransientDrawingMode.Highlight, 128, ic);

                    }
                }

                if (pl != null)
                {
                    pl.Erase();

                    pl.Dispose();// optional, might be removed
                }

                tr.Commit();
                // pause for user input only
                string key = ed.GetString("\nPress any key: ").StringResult;

                TransientManager.CurrentTransientManager.EraseTransient(pline, ic);

            }

        }

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 7 of 11

Ertqwa
Advocate
Advocate

Hi, that works, thanks a lot!

 

One question tho, if I leave out the "EraseTransient" line at the end, it still disapears after I regenerate. Why should I call it?

 

I'll try and see if I can get the other one to work.

0 Likes
Message 8 of 11

Hallex
Advisor
Advisor

I did it machinally, though it is no problem

to keep them  inside, but who knows?

Of course, if you need to display a segment

in the work time, then don't remove,

 

See you,

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 9 of 11

conveyor1
Enthusiast
Enthusiast

Was wondering if anyone has a VB.net version of this code.  I am having some difficulty modifying it from C#.

 

Please let me know if you can.

0 Likes
Message 10 of 11

_gile
Consultant
Consultant

Hi,

 

Have you simply tried a code converter?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 11 of 11

Norman_Yuan
Mentor
Mentor

I have recently posted an article on this similar topic, showing a similar solution (using Transient Graphics to highlight part/segment of a polyline). But in my case, it is to highlight a segment when mouse cursor hover a polyline. I thought it may also be helpful/useful for your need.

 

https://drive-cad-with-code.blogspot.com/2019/02/visually-select-segment-of-polyline.html

 

Unfortunately, the code is also in C#. As @_gile suggested, you can find one of the online .NET code converter to easily convert the code to VB.NET.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes