.NET

.NET

Reply
Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 1 of 7 (613 Views)
Accepted Solution

Create a Polyline with Insertion Point Prompt

613 Views, 6 Replies
03-13-2012 10:34 AM

Hey Everyone,

 

I've been going through some examples on http://through-the-interface.typepad.com to create polylines and i've managed to make some code run. One thing that I can't seem to find information on though, is how to use the default AutoCAD command line options in my code. For instance, I would like to be able to prompt the user whether they would like to snap the polyline to an insertion point, everytime they create a new verticie.  I'm not sure how to do this, does anyone have an suggestions or experience with this?

 

Cheers

Vince

 

 

Below is some example code from the site that i'm working from:

 

namespace MyPlineApp
{
public class MyPlineCmds
{
[CommandMethod("MYPOLY")]
public void MyPoly()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;

// Get the current color, for our temp graphics
Color col = doc.Database.Cecolor;

// Create a point collection to store our vertices
Point3dCollection pts = new Point3dCollection();

// Set up the selection options
// (used for all vertices)
PromptPointOptions opt =
new PromptPointOptions(
"\nSelect polyline vertex: "
);
opt.AllowNone = true;

// Get the start point for the polyline
PromptPointResult res = ed.GetPoint(opt);
while (res.Status == PromptStatus.OK)
{
// Add the selected point to the list
pts.Add(res.Value);

// Drag a temp line during selection
// of subsequent points
opt.UseBasePoint = true;
opt.BasePoint = res.Value;
res = ed.GetPoint(opt);
if (res.Status == PromptStatus.OK)
{
// For each point selected,
// draw a temporary segment
ed.DrawVector(
pts[pts.Count - 1], // start point
res.Value, // end point
col.ColorIndex, // current color
false); // highlighted?
}
}
if (res.Status == PromptStatus.None)
{
// Get the current UCS
Matrix3d ucs =
ed.CurrentUserCoordinateSystem;
Point3d origin = new Point3d(0, 0, 0);
Vector3d normal = new Vector3d(0, 0, 1);
normal = normal.TransformBy(ucs);

// Create a temporary plane, to help with calcs
Plane plane = new Plane(origin, normal);

// Create the polyline, specifying
// the number of vertices up front
Polyline pline = new Polyline(pts.Count);
pline.Normal = normal;
foreach (Point3d pt in pts)
{
Point3d transformedPt = pt.TransformBy(ucs);
pline.AddVertexAt(pline.NumberOfVertices, plane.ParameterOf(transformedPt),0, 0, 0);
}

// Now let's add the polyline to the modelspace
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
ObjectId plineId = btr.AppendEntity(pline);
tr.AddNewlyCreatedDBObject(pline, true);
tr.Commit();
ed.WriteMessage("\nPolyline entity is: " + plineId.ToString());
}
}
// Clear the temp graphics (polyline should be in
// the same location, if selection was not cancelled)
// We could "redraw" instead of "regen" here
ed.Regen();
}
}
}

*Expert Elite*
norman.yuan
Posts: 987
Registered: ‎04-27-2009
Message 2 of 7 (598 Views)

Re: Create a Polyline with Insertion Point Prompt

03-13-2012 11:41 AM in reply to: vince1327

You probably want to use Editor.Snap() in conjunction with Editor.GetPoint(). That is, with Editor.getPoint(), user pick a point, which may not be the exact point your code expected, then you call Editor.Snap() with user-picked point as input parameter and with appropriate snap mode, Editor.Snap() then returns a snapped point, based on snap mode.

 

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 3 of 7 (600 Views)

Re: Create a Polyline with Insertion Point Prompt

03-13-2012 11:56 AM in reply to: norman.yuan

Thanks, that sounds like what i'm looking for. You wouldn't happen to have an example of this in action would you? 

*Expert Elite*
norman.yuan
Posts: 987
Registered: ‎04-27-2009
Message 4 of 7 (593 Views)

Re: Create a Polyline with Insertion Point Prompt

03-13-2012 12:46 PM in reply to: vince1327

OK, here is code sample:

 

class MyCommands
    {

        [CommandMethod("MyCmd", CommandFlags.Session)]
        public static void MyCmd()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            PromptPointOptions opt = new PromptPointOptions("Pick line at one of its end: ");
            PromptPointResult res = ed.GetPoint(opt);
            if (res.Status == PromptStatus.OK)
            {
                Point3d pt = ed.Snap("ENDP", res.Value);

                ed.WriteMessage("\nPicked point: {0}, {1}, {2}", res.Value.X, res.Value.Y, res.Value.Z);
                ed.WriteMessage("\nSnapped point: {0}, {1}, {2}", pt.X, pt.Y, pt.Z);
            }
        }
    }

To try the code, you can draw a line, and then pick anywhere at the line (but not the end), it will get one of the end point, depending on the picked poin is closer to which end.

 

Other snap mode parameter would be like "NEA", "INS", "INT", "CEN"...

 

However, you need to be careful: user may be very accustomed at using Acad with object snap turned on. In this case, when GetPoint() is called, AutoCAD display auto-snap grip to whatever snap modes that are turned on. But the snap mode in Editor.Snap() method wins. If the snap mode in Editor.Snap() method is different from user sees (AutoCAD object snap prompted snap grip), the result may confuse user to mislead user...

 

So, if you use Editor.Snap() for specific reason, you might as well check if Object SNAP is turned on, if it is, you may want to save the snap mode (system variable OSMODE) in a variable; then turn it off; then after Editor.Snap() call, you restore the object snap mode back to its original mode.

 

HTH

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 5 of 7 (562 Views)

Re: Create a Polyline with Insertion Point Prompt

03-16-2012 05:27 AM in reply to: vince1327

Thanks for the response and the sample code...it was a good guideline. I found the example of calculating area in the .NET developers handbook and have implemented that. A few questions though are how to get the polyline to remain static until the last point is picked, and how to modify the code so that it continiously requests new points until the user press a certain key or clicks back on the origin point. I'm working on the second part right now and i'm sure it'll go. Any suggestions or assistance would be great though.

 

Cheers

Vince

*Expert Elite*
norman.yuan
Posts: 987
Registered: ‎04-27-2009
Message 6 of 7 (549 Views)

Re: Create a Polyline with Insertion Point Prompt

03-16-2012 01:18 PM in reply to: vince1327

If you want to continuously let user to pick point to build up your polyline (similar as "POLYLINE" command in ACAD), you begin with Editor.GetPoint() and go into t loop. Kean Wamsley has posted an article on creating a polyline interactively a few year ago:

 

http://through-the-interface.typepad.com/through_the_interface/2006/11/controlling_int.html

 

It might be what you are looking for.

 

Distinguished Contributor
vince1327
Posts: 117
Registered: ‎11-02-2011
Message 7 of 7 (514 Views)

Re: Create a Polyline with Insertion Point Prompt

03-20-2012 12:22 PM in reply to: vince1327

Hey, 

 

Thanks for the tip. That helped immensley.

 

Cheers

Vince

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 or visit the Installation and Licensing Forum to get help installing your software.