Interactive polyline while drawing it.

Interactive polyline while drawing it.

kmkxperia
Advocate Advocate
327 Views
1 Reply
Message 1 of 2

Interactive polyline while drawing it.

kmkxperia
Advocate
Advocate

 

 

While we normally use 'PL'  command for polyline we are able to see the line as we select the points. but when I programmatically try this it shows me the message to select the ployline points but it does not show the line. so its difficult to find the start point to close it. is there any way to do this

 

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

// Prompt for the polyline points
PromptPointOptions ppo = new PromptPointOptions("\nEnter the first point of the polyline: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK) return;
Point3d startPoint = ppr.Value;

Polyline poly = new Polyline();
poly.AddVertexAt(0, new Point2d(startPoint.X, startPoint.Y), 0, 0, 0);

while (true)
{
    ppo.Message = "\nEnter the next point of the polyline (or press Enter to finish): ";
    ppr = ed.GetPoint(ppo);
    if (ppr.Status != PromptStatus.OK) break;

    Point3d nextPoint = ppr.Value;
    poly.AddVertexAt(poly.NumberOfVertices, new Point2d(nextPoint.X, nextPoint.Y), 0, 0, 0);
}

if (poly.NumberOfVertices < 2)
{
    ed.WriteMessage("\nA polyline needs at least two points.");
    return;
}

// Create the polyline
using (Transaction tr = db.TransactionManager.StartTransaction())
{
    doc.LockDocument();
    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
    btr.AppendEntity(poly);
    tr.AddNewlyCreatedDBObject(poly, true);

    

    tr.Commit();
}

 

 

0 Likes
Accepted solutions (1)
328 Views
1 Reply
Reply (1)
Message 2 of 2

_gile
Consultant
Consultant
Accepted solution

Hi,

You can do this using a class deriving from EntityJig.

Here's an example:

 

    [CommandMethod("JIGPL")]
    public static void JigPolyline()
    {
        var doc = Application.DocumentManager.MdiActiveDocument;
        var db = doc.Database;
        var ed = doc.Editor;
        var ppr = ed.GetPoint("\nFirst point: ");
        if (ppr.Status != PromptStatus.OK)
            return;
        using (var tr = db.TransactionManager.StartTransaction())
        {
            using (var pline = new Polyline())
            {
                pline.AddVertexAt(0, ppr.Value.Convert2d(new Plane()), 0.0, 0.0, 0.0);
                pline.AddVertexAt(1, Point2d.Origin, 0.0, 0.0, 0.0);
                pline.TransformBy(ed.CurrentUserCoordinateSystem);

                void Append()
                {
                    var cSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    cSpace.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);
                    tr.Commit();
                }

                var jig = new PlineJig(pline);
                while (true)
                {
                    var pr = ed.Drag(jig);
                    switch (pr.Status)
                    {
                        case PromptStatus.Keyword:
                            switch (pr.StringResult)
                            {
                                case "Close":
                                    pline.RemoveVertexAt(pline.NumberOfVertices - 1);
                                    pline.Closed = true;
                                    Append();
                                    return;
                                case "Undo":
                                    pline.RemoveVertexAt(pline.NumberOfVertices - 1);
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case PromptStatus.OK:
                            pline.AddVertexAt(pline.NumberOfVertices, Point2d.Origin, 0.0, 0.0, 0.0);
                            db.TransactionManager.QueueForGraphicsFlush();
                            break;
                        default:
                            pline.RemoveVertexAt(pline.NumberOfVertices - 1);
                            if (pline.NumberOfVertices < 2)
                            {
                                return;
                            }
                            else
                            {
                                Append();
                                return;
                            }
                    }
                }
            }
        }
    }

    class PlineJig : EntityJig
    {
        Polyline pline;
        Point3d dragPt;
        Plane plane;

        public PlineJig(Polyline pline) : base(pline)
        {
            this.pline = pline;
            plane = new Plane(Point3d.Origin, pline.Normal);
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            var options = new JigPromptPointOptions("\nSpecify the next point: ");
            options.BasePoint = pline.GetPoint3dAt(pline.NumberOfVertices - 2);
            options.UseBasePoint = true;
            if (2 < pline.NumberOfVertices)
            {
                options.Keywords.Add("Undo");
                options.AppendKeywordsToMessage = true;
            }
            if (3 < pline.NumberOfVertices)
            {
                options.Keywords.Add("Close");
            }
            options.UserInputControls =
                UserInputControls.Accept3dCoordinates |
                UserInputControls.GovernedByOrthoMode |
                UserInputControls.GovernedByUCSDetect |
                UserInputControls.NullResponseAccepted;
            var result = prompts.AcquirePoint(options);
            if (result.Value.IsEqualTo(dragPt))
                return SamplerStatus.NoChange;
            dragPt = result.Value;
            return SamplerStatus.OK;
        }
        protected override bool Update()
        {
            pline.SetPointAt(pline.NumberOfVertices - 1, dragPt.Convert2d(plane));
            return true;
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub