.NET

Reply
Valued Contributor
Ertqwa
Posts: 88
Registered: ‎10-03-2011
Message 1 of 8 (390 Views)
Accepted Solution

Highlight part of polyline

390 Views, 7 Replies
07-30-2012 11:01 AM

Hello Forum,

 

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

 

Thank you.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 8 (381 Views)

Re: Highlight part of polyline

07-30-2012 12:58 PM in reply to: Ertqwa

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
Valued Contributor
Ertqwa
Posts: 88
Registered: ‎10-03-2011
Message 3 of 8 (354 Views)

Re: Highlight part of polyline

08-01-2012 11:32 AM in reply to: Hallex

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 8 (350 Views)

Re: Highlight part of polyline

08-01-2012 11:57 AM in reply to: Ertqwa

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

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
Ertqwa
Posts: 88
Registered: ‎10-03-2011
Message 5 of 8 (343 Views)

Re: Highlight part of polyline

08-01-2012 01:27 PM in reply to: Hallex

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

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 8 (322 Views)

Re: Highlight part of polyline

08-02-2012 05:15 AM in reply to: Ertqwa

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
Valued Contributor
Ertqwa
Posts: 88
Registered: ‎10-03-2011
Message 7 of 8 (304 Views)

Re: Highlight part of polyline

08-02-2012 12:41 PM in reply to: Hallex

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 8 (300 Views)

Re: Highlight part of polyline

08-02-2012 12:50 PM in reply to: Ertqwa

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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community