How to make two polylines eachother.

How to make two polylines eachother.

MarkJamesRogolino
Advocate Advocate
2,093 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,094 Views
23 Replies
Replies (23)
Message 2 of 24

kerry_w_brown
Advisor
Advisor

Please explain in detail.


// 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 3 of 24

MarkJamesRogolino
Advocate
Advocate

Hi, Kdub. Thanks for your reply. This is my problem.

If I have two poly lines in one document, I need to touch the two poly lines.
To do that, I have to stretch left polyline  to the right automatically. Is it possible?

goldhorsemillion_0-1677042137253.png

 

0 Likes
Message 4 of 24

kerry_w_brown
Advisor
Advisor

If you are sure the rectangular polylines will be in the orientation you show, 

perhaps :
change the RIGHT vertex locations.X of the left polyline to be the same as

the  LEFT vertex locations.X of the right polyline.

If your design differs, you will need to explain in detail.

 

added:
Any issues with the rectangles not being planar OR with the edges not being parallel will/may need to be handled.

>>> To do that, I have to stretch left polyline  to the right automatically. Is it possible? <<<

What do you imagine will initiale  initiate this functionality ?

 

 

edit 2023/02/25 : spelling


// 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
Message 5 of 24

MarkJamesRogolino
Advocate
Advocate

Hi, Kdub. How can I move point3d runtime?

0 Likes
Message 6 of 24

kerry_w_brown
Advisor
Advisor

You can't move  a point3D.

added : refer to your post : https://forums.autodesk.com/t5/net/how-to-get-an-objectid-of-a-point3d/m-p/11753625

 

You can relocate a Vertex :

https://help.autodesk.com/view/OARX/2023/ENU/?guid=OARX-ManagedRefGuide-Autodesk_AutoCAD_DatabaseSer...

 

I assume you know how to iterate the vertex 's of a polyline and test for the vertex index with the largest ( or smallest ) X value.

// - - - 

I don't know your use case, but another way to handle this is to ask the user to select the polyline segment to "move" and select the segment to align with.

You can determine the X value end vertex index of the endpoint of each segment and change the "moving" vertex locations accordingly.

 

added:

This may require some google work for you, but you will learn a lot.

 

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 7 of 24

MarkJamesRogolino
Advocate
Advocate

Hello, kdub. Have a good weekend?

Thanks for your hint. But I have got this error.

goldhorsemillion_0-1677441614421.png

This error cause is BlockTableRecord.AppendEntity(Polyline).

How can I solve this problem?

I think it does not requires Appendentity method. Right?

0 Likes
Message 8 of 24

kerry_w_brown
Advisor
Advisor

Without seeing the code we'll only be guessing.

I'd guess you are trying to Add/Append a polyline twice into the database.

You may be able to debug this by adding breakpoints in the editor.

 

 


// 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 9 of 24

MarkJamesRogolino
Advocate
Advocate

Hi, kdub. Thanks for your help. I already move polyline's vertex correctly. But moved points are not on target polyline(target polyline segment's ison property is false).

Check function that one point is on polyline or not is als followings.
This function works correctly about other Polylines but does not work about moved polylines.

 

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;
        }

 

Could you help me to solve this? I need to have the point3d lie on the target polyline.

 

0 Likes
Message 10 of 24

MarkJamesRogolino
Advocate
Advocate

How to add this entity to blocktablerecord after moving point? Should I just save the document?

0 Likes
Message 11 of 24

kerry_w_brown
Advisor
Advisor

 

I can't help without seeing ALL the code.

You seem to do this a lot, expecting others to resolve your problems without sufficient information.

 


// 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 12 of 24

MarkJamesRogolino
Advocate
Advocate

Sorry, my mistake. my code is followings.

public static bool checkTwoPlineTouch(Polyline pl1, Polyline pl2)            // this checks two polyline is close.
        {
            bool btch = false;

            int tchcnt = 0;
            for (int i = 0; i < pl2.NumberOfVertices; i++)
            {
                if (IsPointOnPolyline(pl1, pl2.GetPoint3dAt(i)))
                {
                    tchcnt++;
                }
            }
            if (tchcnt >= 2)
                btch = true;
            tchcnt = 0;
            if (!btch)
            {
                for (int i = 0; i < pl1.NumberOfVertices; i++)
                {
                    if (IsPointOnPolyline(pl2, pl1.GetPoint3dAt(i)))
                    {
                        tchcnt++;
                    }
                }
                if (tchcnt >= 2)
                    btch = true;
            }
            return btch;
        }
 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);      
                    //isOn=seg.
                    if (isOn)
                        break;
                }
            }
            return isOn;
        }

Now moved point is lie on target polyline. But IsOnPolyline function returns false.

0 Likes
Message 13 of 24

kerry_w_brown
Advisor
Advisor

It looks to me you've used

https://www.keanw.com/2012/01/testing-whether-a-point-is-on-an-autocad-polyline-using-net.html

 

This is an "Improved version"

https://through-the-interface.typepad.com/through_the_interface/2012/01/testing-whether-a-point-is-o...

 

 

Have you tested it by passing a polyline and a Point3d that you "KNOW" is exactly on the pline ??

 

//

 

Perhaps there is a rounding issue with the Point3d axis values ?

 

I haven't checked your code ( or the copy ) , just giving options for you to check.

 

 


// 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 14 of 24

MarkJamesRogolino
Advocate
Advocate

Thanks, I used https://www.keanw.com/2012/01/testing-whether-a-point-is-on-an-autocad-polyline-using-net.html.

But how can I get Curve from polyline?

IsPointOnCurveGDAP and IsPointOnCurveGCP function use Curve entity as a parameter.

0 Likes
Message 15 of 24

_gile
Consultant
Consultant

@MarkJamesRogolino  a écrit :

But how can I get Curve from polyline?


The Polyline class is derived from the Curve one, so all Curve members apply to Polyline. You should learn Inheritance which is one of the basics of OOP.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 16 of 24

MarkJamesRogolino
Advocate
Advocate

I know already Inheritence of OOP. I mean how to get Curve(line segment) from polyline entity.

 

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);      
                    //isOn= IsPointOnCurveGDAP(seg, pt);
                    if (isOn)
                        break;
                }
            }
            return isOn;
        }
private bool IsPointOnCurveGCP(Curve cv, Point3d pt)
        {
            try
            {
                Point3d p = cv.GetClosestPointTo(pt, false);
                return (p - pt).Length <= Tolerance.Global.EqualPoint;
            }
            catch { }
            return false;
        }

 

Here seg should be Curve type.

0 Likes
Message 17 of 24

_gile
Consultant
Consultant

Are not you confusing Curve and Curve3d?

Both methods you show work with a Polyline.

IsPointOnPolyline(pline, point) should return the same result as IsPointOnCurveGCP(pline, point)



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 18 of 24

MarkJamesRogolino
Advocate
Advocate

Hi, gile. In my opinion cv of IsPointOnCurveGCP(Curve cv, point) function is linesegment of pline of IsPointOnPolyline(pline, point). I want to get  curve as a linesegment of pline.

0 Likes
Message 19 of 24

kerry_w_brown
Advisor
Advisor

@MarkJamesRogolino 

>>> I want to get  curve as a linesegment of pline. <<<

 

Sorry, I can't understand those requitements.

 

See if this helps your understanding :

 

// (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 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 pointOnPline = pline.GetClosestPointTo(wcsPickedPoint, false);
            var segmentIndex = (int)pline.GetParameterAtPoint(pointOnPline);

            if (pline.GetSegmentType(segmentIndex) == SegmentType.Line)
            {
               LineSegment2d segment = pline.GetLineSegment2dAt(segmentIndex);
               _ed.WriteMessage($"\nsegmentIndex: {segmentIndex}\n");
               _ed.WriteMessage($"segment Length: {segment.Length}\n");
               _ed.WriteMessage($"Angle: {( segment.Direction.Angle * 180 / Math.PI )}°\n" );
               _ed.WriteMessage($"Startpoint: {(segment.StartPoint)}\n");
            }
            else
            {
               _ed.WriteMessage($"\nsegment Type: {pline.GetSegmentType(segmentIndex)}\n");
            }
            tr.Commit();
         }
      }
   }
}

 


PolyLineSegment 2023-03-03_08-55-52.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 20 of 24

MarkJamesRogolino
Advocate
Advocate

Thanks, kdub. I mean I should know one point is lie on one linesegment. I enclosed my working video.

As you can see in my video I moved two points to one Polyline. But my code returns two polylines are not touched eachother. Followings are my code. 

BlockTable acBlkTbl;
acBlkTbl = acCurrTrans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acCurrTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;
Point3d acPt3d = new Point3d(right1.X, 0, 0);
Vector3d acVec3d = acPt3d.GetVectorTo(new Point3d(left2.X, 0, 0));
for (int i = 0; i < pln1.NumberOfVertices; i++)
{
    Point3d pt = pln1.GetPoint3dAt(i);
    if (pt.X == right1.X)
    {
       pln1.SetPointAt(i, new Point2d(left2.X, pt.Y));
    }
}
bool istch = NBCrelate.checkTwoPlineTouch(pln2, pln1);
if (istch)
  Application.ShowAlertDialog("Two Polyline touched");
public static bool checkTwoPlineTouch(Polyline pl1, Polyline pl2)            // this checks two polyline is close.
        {
            bool btch = false;

            int tchcnt = 0;
            for (int i = 0; i < pl2.NumberOfVertices; i++)
            {
                if (IsPointOnPolyline(pl1, pl2.GetPoint3dAt(i)))
                {
                    tchcnt++;
                }
            }
            if (tchcnt >= 2)
                btch = true;
            tchcnt = 0;
            if (!btch)
            {
                for (int i = 0; i < pl1.NumberOfVertices; i++)
                {
                    if (IsPointOnPolyline(pl2, pl1.GetPoint3dAt(i)))
                    {
                        tchcnt++;
                    }
                }
                if (tchcnt >= 2)
                    btch = true;
            }
            return btch;
        }
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;
                Curve seg1 = null;
                SegmentType segType = pl.GetSegmentType(i);
                if (segType == SegmentType.Arc)
                    seg = pl.GetArcSegmentAt(i);
                else
                if (segType == SegmentType.Line)
                    seg = pl.GetLineSegmentAt(i);
                    //seg1 = pl.GetLineSegment2dAt(i);
                if (seg != null)
                {
                    //isOn = seg.IsOn(pt);      
                    isOn= IsPointOnCurveGDAP(seg, pt);
                    if (isOn)
                        break;
                }
            }
            return isOn;
        }

I saw moved Polyline's Point is equal to target polylines's left vertex X coordinate.

But IsPointOnPolyline function does not return true. I dont know cause. Could you tell me?

0 Likes