.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Drawing polyine

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
Anonymous
663 Views, 5 Replies

Drawing polyine

Hi everyone

I'm trying to write something that gets the user to draw a closed polyline after a button is pressed on a form.

I tried using the CommandAsync method to send the PLINE command to autocad but using that didn't allow me to execute code after the polyline was drawn so I am currently using the method of creating a polyline object and adding vertices to it based on where the user clicks, however the problem with this method is that it does not show the line as I'm drawing it which would make it difficult for users to know where exactly they've drawn.

My question is, can I get autocad to show the lines of the polyline in the drawing as I add vertices?

Thanks
Joel

5 REPLIES 5
Message 2 of 6
hgasty1001
in reply to: Anonymous

Hi,

 

Try this article from Kean's blog: Polyline Jig  also this article from Norman Yuang's excelent blog it's very insightful: Tracing over a Polyline

 

Gaston Nunez

Message 3 of 6
Anonymous
in reply to: hgasty1001

Thanks Gaston. I think this is exactly what I was looking for!

Message 4 of 6
_gile
in reply to: Anonymous

Hi,

 

Here's a simpler exampe just using a base point in a GetPoint loop (works with straight segments on the current UCS plane).

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace DrawPline
{
    public class Commands
    {
        [CommandMethod("TEST")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptPointOptions opts = new PromptPointOptions("\nSpecify the first point: ");
            PromptPointResult result = ed.GetPoint(opts);
            if (result.Status != PromptStatus.OK)
                return;

            opts.Message = "\nSpecify next point: ";
            opts.AllowNone = true;
            opts.UseBasePoint = true;

            Matrix3d ucsMat = ed.CurrentUserCoordinateSystem;
            CoordinateSystem3d ucs = ucsMat.CoordinateSystem3d;
            Plane plane = new Plane(Point3d.Origin, ucs.Zaxis);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord space =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                using (Polyline pline = new Polyline())
                {
                    pline.Normal = ucs.Zaxis;
                    pline.Elevation = ucs.Origin.Z;
                    pline.AddVertexAt(0, result.Value.TransformBy(ucsMat).Convert2d(plane), 0.0, 0.0, 0.0);
                    space.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);
                    int i = 1;
                    while (true)
                    {
                        opts.BasePoint = result.Value;
                        result = ed.GetPoint(opts);
                        if (result.Status == PromptStatus.Cancel)
                        {
                            return;
                        }
                        if (result.Status == PromptStatus.Keyword)
                        {
                            pline.Closed = true;
                            break;
                        }
                        if (result.Status != PromptStatus.OK)
                        {
                            break;
                        }
                        if (i == 2)
                        {
                            opts.SetMessageAndKeywords("\nSpecify next point [Close]: ", "Close");
                        }
                        pline.AddVertexAt(i++, result.Value.TransformBy(ucsMat).Convert2d(plane), 0.0, 0.0, 0.0);
                        db.TransactionManager.QueueForGraphicsFlush();
                        opts.BasePoint = result.Value;
                    }
                }
                tr.Commit();
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 6
_gile
in reply to: _gile

The same with an Undo option.

 

 

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.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace DrawPline
{
    public class Commands
    {
        [CommandMethod("TEST")]
        public void Test()
        {
            Document doc = AcAp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            PromptPointOptions opts = new PromptPointOptions("\nSpecify the first point: ");
            PromptPointResult result = ed.GetPoint(opts);

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

            Stack<Point3d> points = new Stack<Point3d>();
            points.Push(result.Value);

            Matrix3d ucsMat = ed.CurrentUserCoordinateSystem;
            CoordinateSystem3d ucs = ucsMat.CoordinateSystem3d;
            Plane plane = new Plane(Point3d.Origin, ucs.Zaxis);
            Func<Point3d, Point2d> conv2d = p => p.TransformBy(ucsMat).Convert2d(plane);

            Autodesk.AutoCAD.DatabaseServices.TransactionManager transMgr = db.TransactionManager;
            using (Transaction tr = transMgr.StartTransaction())
            {
                BlockTableRecord space =
                    (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                using (Polyline pline = new Polyline())
                {
                    pline.Normal = ucs.Zaxis;
                    pline.Elevation = ucs.Origin.Z;
                    pline.AddVertexAt(0, conv2d(points.Peek()), 0.0, 0.0, 0.0);
                    space.AppendEntity(pline);
                    tr.AddNewlyCreatedDBObject(pline, true);

                    while (true)
                    {
                        switch (points.Count)
                        {
                            case 1:
                                opts = new PromptPointOptions("\nSpecify next point: ");
                                opts.AllowNone = true;
                                opts.UseBasePoint = true;
                                break;
                            case 2:
                                opts.SetMessageAndKeywords("\nSpecify next point [Undo]: ", "Undo");
                                break;
                            default:
                                opts.SetMessageAndKeywords("\nSpecify next point [Undo/Close]: ", "Undo Close");
                                break;
                        }
                        opts.BasePoint = points.Peek();
                        result = ed.GetPoint(opts);

                        if (result.Status == PromptStatus.Cancel) return;

                        if (result.Status == PromptStatus.Keyword)
                        {
                            if (result.StringResult.ToUpper() == "CLOSE")
                            {
                                pline.Closed = true;
                                break;
                            }
                            else
                            {
                                points.Pop();
                                pline.RemoveVertexAt(points.Count);
                                db.TransactionManager.QueueForGraphicsFlush();
                                continue;
                            }
                        }

                        if (result.Status != PromptStatus.OK)
                        {
                            if (points.Count > 1) break;
                            else return;
                        }

                        points.Push(result.Value);
                        pline.AddVertexAt(points.Count - 1, conv2d(points.Peek()), 0.0, 0.0, 0.0);
                        db.TransactionManager.QueueForGraphicsFlush();
                    }
                }
                tr.Commit();
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 6
Anonymous
in reply to: _gile

Thanks Gilles. That's really helpful too.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost