Hi,
Here's a little sample (C#)
Code:
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace PickPoint
{
public class Class1
{
[CommandMethod("Test")]
public void Test()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a polyline: ");
peo.SetRejectMessage("You must select a polyline\n");
peo.AddAllowedClass(typeof(Polyline), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId id = per.ObjectId;
Point3d picked = per.PickedPoint;
Polyline pline = tr.GetObject(id, OpenMode.ForRead) as Polyline;
Point3d onPlinePt = pline.GetClosestPointTo(picked, ed.GetCurrentView().ViewDirection, false);
MessageBox.Show("Picked point: " + picked.ToString() + "\nClosest point on polyline: " + onPlinePt.ToString());
tr.Commit();
}
}
}
}
}
A VB convertion (made with a .net code converter)
Code:
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports acadApp = Autodesk.AutoCAD.ApplicationServices.Application
Namespace PickPoint
Public Class Class1
<CommandMethod("Test")> _
Public Sub Test()
Dim doc As Document = acadApp.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim peo As New PromptEntityOptions(vbLf & "Select a polyline: ")
peo.SetRejectMessage("You must select a polyline" & vbLf)
peo.AddAllowedClass(GetType(Polyline), False)
Dim per As PromptEntityResult = ed.GetEntity(peo)
If per.Status = PromptStatus.OK Then
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim id As ObjectId = per.ObjectId
Dim picked As Point3d = per.PickedPoint
Dim pline As Polyline = TryCast(tr.GetObject(id, OpenMode.ForRead), Polyline)
Dim onPlinePt As Point3d = pline.GetClosestPointTo(picked, ed.GetCurrentView().ViewDirection, False)
MessageBox.Show(("Picked point: " & picked.ToString() & vbLf & "Closest point on polyline: ") + onPlinePt.ToString())
tr.Commit()
End Using
End If
End Sub
End Class
End Namespace
Bookmarks