Author Topic: Minimum distance between 2 curves and draw a line at those points  (Read 1602 times)

0 Members and 1 Guest are viewing this topic.

themethodman

  • Mosquito
  • Posts: 12
With the below code I can determine the shortest distance between 2 curves. Works great.

How could I then determine the point3d's along each of the curves at which this shortest distance occurs?

This is for the purpose of drawing a line between these 2 points to help the user identify where this shortest distance is located.

https://forums.autodesk.com/t5/net/minimum-distance-between-two-entities/td-p/6381823

Code: [Select]
[CommandMethod("MinDist")]
        public void GetMinimumDistance()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect first object: ");
            options.SetRejectMessage("Must be a curve.");
            options.AddAllowedClass(typeof(Curve), false);
            var result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id1 = result.ObjectId;

            options.Message = "\nSelect second object: ";
            result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id2 = result.ObjectId;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curve1 = (Curve)tr.GetObject(id1, OpenMode.ForRead);
                var curve2 = (Curve)tr.GetObject(id2, OpenMode.ForRead);
                var distance = curve1.GetGeCurve().GetDistanceTo(curve2.GetGeCurve());
                ed.WriteMessage("\nDistance = {0}", distance);
                tr.Commit();
            }
        }

« Last Edit: October 18, 2020, 11:26:54 PM by themethodman »

themethodman

  • Mosquito
  • Posts: 12
Re: Minimum distance between 2 curves and draw a line at those points
« Reply #1 on: October 19, 2020, 12:46:10 AM »
Did some digging and managed to come up with a workable solution.

Code: [Select]
namespace MinDist
{
    public class Class1
    {

        [CommandMethod("MinDist")]
        public void GetMinimumDistance()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var options = new PromptEntityOptions("\nSelect first object: ");
            options.SetRejectMessage("Must be a curve.");
            options.AddAllowedClass(typeof(Curve), false);
            var result = ed.GetEntity(options);

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

            options.Message = "\nSelect second object: ";
            result = ed.GetEntity(options);
            if (result.Status != PromptStatus.OK)
                return;
            var id2 = result.ObjectId;

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curve1 = (Curve)tr.GetObject(id1, OpenMode.ForRead);
                var curve2 = (Curve)tr.GetObject(id2, OpenMode.ForRead);
                var distance = curve1.GetGeCurve().GetDistanceTo(curve2.GetGeCurve());

                ed.WriteMessage("\nDistance = {0}", distance);
                tr.Commit();
            }

            using (var tr = db.TransactionManager.StartTransaction())
            {
                var curve1 = (Curve)tr.GetObject(id1, OpenMode.ForRead);
                var curve2 = (Curve)tr.GetObject(id2, OpenMode.ForRead);
                var shortestpoints = curve1.GetGeCurve().GetClosestPointTo(curve2.GetGeCurve());

                BlockTable bt;
                bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr;
                btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                Polyline pl = new Polyline();
                pl.AddVertexAt(0, new Point2d(shortestpoints[0].Point.X, shortestpoints[0].Point.Y), 0, 0, 0);
                pl.AddVertexAt(0, new Point2d(shortestpoints[1].Point.X, shortestpoints[1].Point.Y), 0, 0, 0);

                btr.AppendEntity(pl);
                tr.AddNewlyCreatedDBObject(pl, true);
                tr.Commit();

            }
           
        }
    }
}
« Last Edit: October 19, 2020, 12:53:30 AM by themethodman »

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Minimum distance between 2 curves and draw a line at those points
« Reply #2 on: October 19, 2020, 12:54:57 AM »
Looks good to me :)
Welcome aboard!

The DWG Geometry API is very rich and if you dig deep enough you'll probably find it :)
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff