Connecting points to curves by the shortest distance in AutoCAD using .NET Part 2

In the last post we saw a simple command that connects a block with a curve via a line that starts at the insertion point and meets the curve at its closest point. In this post we’re going to see how we can search the modelspace for the nearest curve and connect each block to that.

Here’s the C# code that connects all blocks of a certain type to the nearest curve. The code could easily be changed to only search for certain types or curve – or only curves tagged with a certain bit of XData – but I’ve left that as an exercise for the reader.

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.Runtime;

using System.Linq;

 

namespace ConnectBlocksToCurves

{

  public static class Extensions

  {

    public static double DistanceFrom(this Curve c, Point3d pt)

    {

      return pt.DistanceTo(c.GetClosestPointTo(pt, false));

    }

 

    Read more

Leave a Comment