How to make two polylines eachother.

How to make two polylines eachother.

MarkJamesRogolino
Advocate Advocate
2,162 Views
23 Replies
Message 1 of 24

How to make two polylines eachother.

MarkJamesRogolino
Advocate
Advocate

Hello, everyone. Now I have to make two polylines (these are separated each other) touch.Is there a way to implement this process

0 Likes
2,163 Views
23 Replies
Replies (23)
Message 21 of 24

kerry_w_brown
Advisor
Advisor

@MarkJamesRogolino 

The partial code you posted is of no use to me for resolving your issue.

At first glance it seems adequate. 

What is the difference between the moved location and your expected location ?


I did test the original version of IsPointOnPolyline ( similar to the one you used )

 

 

// (C) CodeHimBelonga: kdub 2023/03/03
//
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Runtime.CompilerServices;

//using Kdub.Common;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

[assembly: CommandClass(typeof(TouchingPolylines.TestCommands_02))]

namespace TouchingPolylines
{
   public class TestCommands_02
   {
      private static Document _doc => AcadApp.DocumentManager.MdiActiveDocument;
      private static Database _db => _doc.Database;
      private static Editor _ed => _doc.Editor;

      public static void WriteMessage(string msg) => _ed.WriteMessage(msg);


      [CommandMethod("plSegment")]
      public static void PlineSegment()
      {
         PromptEntityOptions peo = new PromptEntityOptions("\nSelect a Polyline Segment: ");
         peo.SetRejectMessage("\nMust be a Polyline...");
         peo.AddAllowedClass(typeof(Polyline), true);
         PromptEntityResult per = _ed.GetEntity(peo);

         if (per.Status == PromptStatus.Cancel) return;
         using (Transaction tr = _doc.TransactionManager.StartTransaction())
         {
            var pline = (Polyline)per.ObjectId.GetObject(OpenMode.ForRead);
            var wcsPickedPoint = per.PickedPoint.TransformBy(_ed.CurrentUserCoordinateSystem);
            var wcspointOnPline = pline.GetClosestPointTo(wcsPickedPoint, false);
            var segmentIndex = (int)pline.GetParameterAtPoint(wcspointOnPline);

            if (pline.GetSegmentType(segmentIndex) == SegmentType.Line)
            {
               _ed.WriteMessage("\n--------------------------------\n");
               LineSegment2d segment = pline.GetLineSegment2dAt(segmentIndex);
               _ed.WriteMessage($"segmentIndex: {segmentIndex}\n");
               _ed.WriteMessage($"segment Length: {segment.Length}\n");
               _ed.WriteMessage($"Angle: {( segment.Direction.Angle * 180 / Math.PI )}°\n");
               _ed.WriteMessage($"Start-WCS: {segment.StartPoint} , End-WCS: {segment.EndPoint}\n");

               _ed.WriteMessage($"per.PickedPoint(ucs): {per.PickedPoint}\n");
               _ed.WriteMessage($"Pline_Vertex2D(wcs): {pline.GetPoint2dAt(segmentIndex)} , 3D: {pline.GetPoint3dAt(segmentIndex)}\n");
            }
            else
            {
               _ed.WriteMessage($"\nsegment Type: {pline.GetSegmentType(segmentIndex)}\n");
            }
            tr.Commit();

            bool isOn = IsPointOnPolyline(pline, wcspointOnPline);
            _ed.WriteMessage($"IsPointOnPolyline: {isOn}\n");
         }

      }

      public static bool IsPointOnPolyline(Polyline pl, Point3d pt)           // this returns point is on polyline or not.
      {
         bool isOn = false;
         for (int i = 0; i < pl.NumberOfVertices; i++)
         {
            Curve3d seg = null;
            SegmentType segType = pl.GetSegmentType(i);
            if (segType == SegmentType.Arc)
               seg = pl.GetArcSegmentAt(i);
            else if (segType == SegmentType.Line)
               seg = pl.GetLineSegmentAt(i);
            if (seg != null)
            {
               isOn = seg.IsOn(pt);
               if (isOn)
                  break;
            }
         }
         return isOn;
      }
   }
}

 

 

VertexValues_2023-03-04_07-31-39.png

 

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 22 of 24

kerry_w_brown
Advisor
Advisor

In Fact, all versions test OK.

            _ed.WriteMessage($"IsPointOnPolyline: {IsPointOnPolyline(pline, wcspointOnPline)}\n");
            _ed.WriteMessage($"IsPointOnCurveGDAP: {IsPointOnCurveGDAP((Curve)pline, wcspointOnPline)}\n");
            _ed.WriteMessage($"IsPointOnCurveGCP: {IsPointOnCurveGCP((Curve)pline, wcspointOnPline)}\n");

 

      // A generalized IsPointOnCurve function that works on all
      // types of Curve (including PolyLines), but catches an
      // Exception on failure

      public static bool IsPointOnCurveGDAP(Curve cv, Point3d pt)
      {
         try
         {            // Return true if operation succeeds
            cv.GetDistAtPoint(pt);
            return true;
         }
         catch { }

         // Otherwise we return false
         return false;
      }

      // A generalized IsPointOnCurve function that works on all
      // types of Curve (including PolyLines), and checks the position
      // of the returned point rather than relying on catching an
      // exception

      public static bool IsPointOnCurveGCP(Curve cv, Point3d pt)
      {
         try
         {            // Return true if operation succeeds
            Point3d p = cv.GetClosestPointTo(pt, false);
            return ( p - pt ).Length <= Tolerance.Global.EqualPoint;
         }
         catch { }

         // Otherwise we return false
         return false;
      }


      public static bool IsPointOnPolyline(Polyline pl, Point3d pt)        
      {
         bool isOn = false;
         for (int i = 0; i < pl.NumberOfVertices; i++)
         {
            Curve3d seg = null;
            SegmentType segType = pl.GetSegmentType(i);
            if (segType == SegmentType.Arc)
               seg = pl.GetArcSegmentAt(i);
            else if (segType == SegmentType.Line)
               seg = pl.GetLineSegmentAt(i);
            if (seg != null)
            {
               isOn = seg.IsOn(pt);
               if (isOn)
                  break;
            }
         }
         return isOn;
      }

 

From this I'd conclude that the fault is with your "Moving" code, not with these methods.

Regards,

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 23 of 24

kerry_w_brown
Advisor
Advisor

Perhaps this will help

 

https://www.theswamp.org/index.php?topic=58137.msg613490#msg613490

 

stretch polyline2023-03-07_13-46-18.png


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 24 of 24

kerry_w_brown
Advisor
Advisor

Any joy Vladimir ??

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes