Highlight Alignement Entities

eirij
Advocate
Advocate

Highlight Alignement Entities

eirij
Advocate
Advocate

Hi,

 

Does anyone know the way to highlight Alignment Entities or sub-entities with .NET.

 

Thank you!

 

Best Regards,

Eirij

0 Likes
Reply
Accepted solutions (1)
550 Views
2 Replies
Replies (2)

Jeff_M
Consultant
Consultant
Looks like you will need to use TransientGraphics for this.
Jeff_M, also a frequent Swamper
EESignature
0 Likes

Jeff_M
Consultant
Consultant
Accepted solution

Here's some code using the TransientGraphics that will highlight the selected Alignment.SubEntity. I only show Lines and Arcs since I don't use Spirals here.

In an Extension method class, which has in the usings "using aGi = Autodesk.AutoCAD.GraphicsInterface;" :

        public static void Highlight(this Alignment align, Point3d pt)
        {
            ClearMarkers();
            AlignmentSubEntity subEnt = align.SelectedSubEnt(pt);
            switch (subEnt.SubEntityType)
            {
                case AlignmentSubEntityType.Arc:
                    HighlightArc((AlignmentSubEntityArc)subEnt);
                    break;
                case AlignmentSubEntityType.Line:
                    HighlightLine((AlignmentSubEntityLine)subEnt);
                    break;
                case AlignmentSubEntityType.Spiral:
                    //ToDo: Add code for the Spiral type
                    break;
            }
        }

        public static Point3d To3D(this Point2d pt)
        {
            return new Point3d(pt.X, pt.Y, 0);
        }

        private static void HighlightLine(AlignmentSubEntityLine ent)
        {
            Line line = new Line(ent.StartPoint.To3D(), ent.EndPoint.To3D());
            line.ColorIndex = 1; //may want to get the actual display color for the Alignment's style...more work :-)
            aGi.TransientManager.CurrentTransientManager.AddTransient(line, aGi.TransientDrawingMode.Highlight, 128, intColl);
            m_mrkers.Add(line);
        }

        private static void HighlightArc(AlignmentSubEntityArc subEnt)
        {
            Vector2d startAng = new Vector2d();
            Vector2d endAng = new Vector2d();

            if(subEnt.Clockwise)
            {
                startAng = subEnt.CenterPoint.GetVectorTo(subEnt.EndPoint);
                endAng = subEnt.CenterPoint.GetVectorTo(subEnt.StartPoint);
            }
            else
            {
                startAng = subEnt.CenterPoint.GetVectorTo(subEnt.StartPoint);
                endAng = subEnt.CenterPoint.GetVectorTo(subEnt.EndPoint);
            }
            Arc arc = new Arc(subEnt.CenterPoint.To3D(), subEnt.Radius, startAng.Angle, endAng.Angle);
            arc.ColorIndex = 2; //may want to get the actual display color for the Alignment's style...more work :-)
            aGi.TransientManager.CurrentTransientManager.AddTransient(arc, aGi.TransientDrawingMode.Highlight, 128, intColl);
            m_mrkers.Add(arc);
        }

        public static void ClearMarkers()
        {
            for (int i = 0; i < m_mrkers.Count; i++)
            {
                aGi.TransientManager.CurrentTransientManager.EraseTransient(m_mrkers[i], intColl);
                m_mrkers[i].Dispose();
            }
            m_mrkers.Clear();
        }

        public static AlignmentSubEntity SelectedSubEnt(this Alignment align, Point3d pt)
        {
            AlignmentSubEntity ent = null;
            double station = double.NaN, offset = double.NaN;
            try
            {
                align.StationOffset(pt.X, pt.Y, ref station, ref offset);
            }
            catch
            {
                return ent;
            }
            foreach (AlignmentEntity e in align.Entities)
            {
                for (int i = 0; i < e.SubEntityCount; i++)
                {
                    AlignmentSubEntity subent = e[i];
                    if (subent.StartStation < station && subent.EndStation > station)
                    {
                        ent= subent;
                        break;
                    }
                }
                if (ent != null)
                    break;
            }
            return ent;
        }

And the Command to test it:

        [CommandMethod("HighliteAlignEnt")]
        public void hilite()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var db = doc.Database;
            var entOpts = new PromptEntityOptions("\nSelect an alignment to offset:");
            entOpts.SetRejectMessage("...not an alignment, try again!");
            entOpts.AddAllowedClass(typeof(Alignment), true);
            var entSel = ed.GetEntity(entOpts);
            if (entSel.Status != PromptStatus.OK)
                return;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var align = (Alignment)tr.GetObject(entSel.ObjectId, OpenMode.ForRead);
                align.Highlight(entSel.PickedPoint);
                align.Unhighlight();
            }
        }

        [CommandMethod("UnHighliteAlignEnt")]
        public void unhilite()
        {
            Extensions.AlignmentExtensions.ClearMarkers();
        }

Hope that helps.

Jeff_M, also a frequent Swamper
EESignature