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

obatin nearest endpoint of selected object...?

7 REPLIES 7
Reply
Message 1 of 8
C_Witt
2744 Views, 7 Replies

obatin nearest endpoint of selected object...?

I'm trying to convert a simple lisp command over to .net (vb).. but can't seem to figure out how to do it...

 

lisp:

; force point to be on selected entity
(setq pt1 (osnap (cadr en) "quick,near"))
; grab nearest endpoint of entity
(setq pt2 (osnap pt1 "end"))

 

I can do the first part easily enough (pt1), but how do I obtain pt2 in vb.net?

 

TIA

7 REPLIES 7
Message 2 of 8
cadMeUp
in reply to: C_Witt

you can look into using the  'Snap' method of the Editor object:

Public Function Snap(ByVal snapMode As String, ByVal input As Autodesk.AutoCAD.Geometry.Point3d) As Autodesk.AutoCAD.Geometry.Point3d
     Member of Autodesk.AutoCAD.EditorInput.Editor

Message 3 of 8
giskumar
in reply to: cadMeUp

Hi,

 

Follow this link, i hope it will help you.

 

http://forums.autodesk.com/t5/NET/Insert-vertex-to-polyline/td-p/2845062

 

 

Thanks,

Kumar.

Message 4 of 8
C_Witt
in reply to: cadMeUp

That sounds like if would be what I need, but i'm at a loss as to "how" to use it...  ?

Message 5 of 8
norman.yuan
in reply to: C_Witt

If I interprete the lisp code correctly, it is used in this scenario:

 

There is an entity, user picks it, then the routine will get 2 points:

 

The first one is the point that on the entity closest to where user picks (the pick box could be quite big so that the pick box's centre may not fall on the entity, thus the snap "NEA");

 

The second point would be an point snapped in "END" mode. depnding on the type of entity, it could be Line's startpoint or endpoint, or LwPolyline's vertex...

 

So, here is the code to do that:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;

[assembly: CommandClass(typeof(SnapPickPoints.MyCommands))]

namespace SnapPickPoints
{
    public class MyCommands
    {
        [CommandMethod("SnapPick")]
        public static void SnapPickTest()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            Point3d pt1, pt2;
            if (PickPoints(ed, out pt1, out pt2))
            {
                ed.WriteMessage("\nFirst Point: x={0}\ty={1}\tz={2}", pt1.X, pt1.Y, pt1.Z);
                ed.WriteMessage("\nSecond Point: x={0}\ty={1}\tz={2}", pt2.X, pt2.Y, pt2.Z);
                ed.WriteMessage("\n");
            }
            else
            {
                ed.WriteMessage("\nCommand: *Cancel*");
            }
        }

        private static bool PickPoints(Editor ed, out Point3d pt1, out Point3d pt2)
        {
            pt1 = new Point3d();
            pt2 = new Point3d();

            //Pick an entity
            PromptEntityOptions opt = new PromptEntityOptions("\nPick an entity:");
            PromptEntityResult res = ed.GetEntity(opt);
            if (res.Status != PromptStatus.OK) return false;

            //Pick first point with "nea" snap
            pt1 = ed.Snap("NEA", res.PickedPoint);

            //Pick second point with "end" snap
            //AutoCAD will find a closest point (start/end point of Line) 
            //or vertex of a polyline
            pt2 = ed.Snap("END", pt1);

            return true;
        }
    }
}

 

Start AutoCAD and draw a line/polyline, then run the command.

 

Is this what you want?

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 6 of 8
C_Witt
in reply to: C_Witt

"pt2 = ed.Snap("END", pt1)"

 

That is what I was after.

 

cadMeUp's post had me thinking I had to create the "snap" function, thus the confustion.  Thank you for the clarification.

Message 7 of 8
norman.yuan
in reply to: norman.yuan

On a second though.

 

We do not really have to pick an entity to use Editor.Snap(). You can pick anywhere (as point), and pass it to the Snap() method. However, you'd better wrap Editor.Snap() with try..catch block: AutoCAD will try to find a point from a closest entity, based on the snap mode. If the point is too far away from any entity, eInvalidInput exception occurs.

 

Here is the code:

 

        [CommandMethod("SnapPick1")]
        public static void SnapPickTest_1()
        {
            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;

            Point3d pt1, pt2;
            if (PickPoints_1(ed, out pt1, out pt2))
            {
                ed.WriteMessage("\nFirst Point: x={0}\ty={1}\tz={2}", pt1.X, pt1.Y, pt1.Z);
                ed.WriteMessage("\nSecond Point: x={0}\ty={1}\tz={2}", pt2.X, pt2.Y, pt2.Z);
                ed.WriteMessage("\n");
            }
            else
            {
                ed.WriteMessage("\nCommand: *Cancel*");
            }
        }

        private static bool PickPoints_1(Editor ed, out Point3d pt1, out Point3d pt2)
        {
            pt1 = new Point3d();
            pt2 = new Point3d();

            //Pick an point near an entity
            PromptPointOptions opt = new PromptPointOptions("\nPick a point near an entity:");
            PromptPointResult res = ed.GetPoint(opt);
            if (res.Status != PromptStatus.OK) return false;

            try
            {
                //Pick first point with "nea" snap
                pt1 = ed.Snap("NEA", res.Value);
            }
            catch
            {
                ed.WriteMessage("\nPicked point is too far away from an entity. \"NEA\" snap failed.");
                return false;
            }

            //Pick second point with "end" snap
            //AutoCAD will find a closest point (start/end point of Line) 
            //or vertex of a polyline
            pt2 = ed.Snap("END", pt1);

            return true;
        }

 

When you run this code (make sure you have some lines/polylines drawn), if you pick a point very close to an entity, you get 2 point3d returned; if the picked point is too far away from an entity, the exception is caught in "catch..." clause.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 8 of 8
kbarnettza
in reply to: norman.yuan

THANK YOU NORMAN! here I was going nowhere with OSMODE then I found your post here ... did not know about Editor.Snap ... THANK YOU!

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