TheSwamp

Code Red => .NET => Topic started by: themethodman on October 18, 2020, 10:09:27 PM

Title: Minimum distance between 2 curves and draw a line at those points
Post by: themethodman on October 18, 2020, 10:09:27 PM
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 (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();
            }
        }

Title: Re: Minimum distance between 2 curves and draw a line at those points
Post by: themethodman 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();

            }
           
        }
    }
}
Title: Re: Minimum distance between 2 curves and draw a line at those points
Post by: MickD 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 :)
Title: Re: Minimum distance between 2 curves and draw a line at those points
Post by: nobody on October 19, 2020, 06:26:55 PM
If you happen to have Civil 3D there's a command in there that does this:

https://knowledge.autodesk.com/support/civil-3d/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/Civil3D-UserGuide/files/GUID-26094B8A-1A5F-462E-AAF1-AF1716DE526A-htm.html