Curve.Extend

Curve.Extend

Anonymous
Not applicable
4,241 Views
15 Replies
Message 1 of 16

Curve.Extend

Anonymous
Not applicable

Hey guys, I am programming in C# and I am trying to extend the end point of a curve to another line and am having some trouble.

 

Could you by chance explain the Curve.Extend function to me, as well as what the syntax (bool extendStart, Point3d toPoint) means, and/or give an extend alternative if you have any.

 

0 Likes
Accepted solutions (3)
4,242 Views
15 Replies
Replies (15)
Message 2 of 16

Alexander.Rivilis
Mentor
Mentor
Accepted solution

Sample code:

 

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

#pragma warning disable 0618

[assembly: CommandClass(typeof(ExtendLine.MyCommands))]

namespace ExtendLine
{
  public class MyCommands
  {
    [CommandMethod("ExtendLine")]
    public void MyCommand()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      if (doc == null) return;
      Editor ed = doc.Editor;
      PromptEntityResult rs1 = ed.GetEntity("\nSelect curve for extend operation: ");
      if (rs1.Status != PromptStatus.OK) return;
      if (!rs1.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Curve)))) {
        ed.WriteMessage("\nIt is not a Curve");
        return;
      }
      PromptEntityResult rs2 = ed.GetEntity("\nSelect boundary curve: ");
      if (rs2.Status != PromptStatus.OK) return;
      if (!rs2.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Curve)))) {
        ed.WriteMessage("\nIt is not a Curve");
        return;
      }
      using (Curve cv1 = rs1.ObjectId.Open(OpenMode.ForWrite) as Curve)
      using (Curve cv2 = rs2.ObjectId.Open(OpenMode.ForRead) as Curve) {
        Point3dCollection pts = new Point3dCollection();
        cv1.IntersectWith(cv2, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero);
        if (pts.Count > 0) {
          Point3d p = pts[0];
          Point3d pStart = cv1.StartPoint;
          Point3d pEnd = cv1.EndPoint;
          bool bStart = (p.DistanceTo(pStart) < p.DistanceTo(pEnd));
          try {
            cv1.Extend(bStart, p);
          }
          catch (Autodesk.AutoCAD.Runtime.Exception ex) {
            ed.WriteMessage(ex.ToString());
          }
        }
      }
    }
  }
}

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 16

Anonymous
Not applicable

So, I was able to make this work with my predetermined curves, but is there any way to extend it to the point at which the line WOULD intersect if cv2 kept going, but doesn't due to the line being too short to reach?

 

Example Image:

Example.png

0 Likes
Message 4 of 16

Alexander.Rivilis
Mentor
Mentor
Accepted solution

Try to replace this line of code:

cv1.IntersectWith(cv2, Intersect.ExtendThis, pts, IntPtr.Zero, IntPtr.Zero);

with this line:

cv1.IntersectWith(cv2, Intersect.ExtendBoth, pts, IntPtr.Zero, IntPtr.Zero);

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 5 of 16

Anonymous
Not applicable

If I could give you like ten Kudos per post, I would! Thanks a bunch!

0 Likes
Message 6 of 16

Anonymous
Not applicable

So another question; how would I extend a line to the intersecting point, and then an additional 10 feet in the same direction?

0 Likes
Message 7 of 16

Anonymous
Not applicable

Whoops, that last comment above was meant to be in reply to you.

0 Likes
Message 8 of 16

Alexander.Rivilis
Mentor
Mentor

You have to use this algorithm to calculate point for extend: http://forums.autodesk.com/t5/net/how-to-get-point3d-between-two-point3d/m-p/6587893#M50386

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 9 of 16

Anonymous
Not applicable

In this case, would you modify the Point3dCollection to contact set values from that equation?

0 Likes
Message 10 of 16

Alexander.Rivilis
Mentor
Mentor
Accepted solution

It is easy for me post a code:

 

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

#pragma warning disable 0618

[assembly: CommandClass(typeof(ExtendLine.MyCommands))]

namespace ExtendLine
{
  public class MyCommands
  {
    [CommandMethod("ExtendLineWithOffset")]
    public void ExtendLineWithOffset()
    {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      if (doc == null) return;
      Editor ed = doc.Editor;
      PromptEntityResult rs1 =
        ed.GetEntity("\nSelect curve to extend: ");
      if (rs1.Status != PromptStatus.OK) return;
      if (!rs1.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Curve))))
      {
        ed.WriteMessage("\nIt is not a curve!");
        return;
      }
      PromptEntityResult rs2 =
        ed.GetEntity("\nSelect boundary: ");
      if (rs2.Status != PromptStatus.OK) return;
      if (!rs2.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(Curve))))
      {
        ed.WriteMessage("\nIt is not a Curve");
        return;
      }

      PromptDoubleResult rsOff = ed.GetDistance("\nOffset <0>: ");
      double offset = (rsOff.Status == PromptStatus.OK) ? rsOff.Value : 0;

      using (Curve cv1 = rs1.ObjectId.Open(OpenMode.ForWrite) as Curve)
      using (Curve cv2 = rs2.ObjectId.Open(OpenMode.ForRead) as Curve)
      {
        Point3dCollection pts = new Point3dCollection();
        cv1.IntersectWith(cv2, Intersect.ExtendBoth, pts, IntPtr.Zero, IntPtr.Zero);
        if (pts.Count > 0)
        {
          Point3d pStart = cv1.StartPoint;
          Point3d pEnd = cv1.EndPoint;
          Point3d p = pts[0];
          double dist = Math.Min(p.DistanceTo(pStart), p.DistanceTo(pEnd));
          bool bStart = (p.DistanceTo(pStart) < p.DistanceTo(pEnd));
          for (int i = 1; i < pts.Count; i++)
          {
            if (pts[i].DistanceTo(pStart) < dist)
            {
              p = pts[i]; bStart = true;
              dist = p.DistanceTo(pStart);
            }
            else if (pts[i].DistanceTo(pEnd) < dist)
            {
              p = pts[i]; bStart = false;
              dist = p.DistanceTo(pEnd);
            }
          }
          try
          {
            Vector3d dir = (p - ((bStart) ? cv1.StartPoint : cv1.EndPoint)).GetNormal();
            p += dir * offset;
            cv1.Extend(bStart, p);
          }
          catch (Autodesk.AutoCAD.Runtime.Exception ex)
          {
            ed.WriteMessage(ex.ToString());
          }
        }
      }
    }
  }
}

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 11 of 16

Anonymous
Not applicable

So I am running into an issue with the code, once I alter it to extend PAST the point, it refuses to extend that side at all.

 

                                    Curve ptLiveEnd = resCV3.ObjectId.Open(OpenMode.ForRead) as Curve;
                                    Point3dCollection ptss = new Point3dCollection();
                                    subCurv.IntersectWith(ptLiveEnd, Intersect.ExtendBoth, ptss, IntPtr.Zero, IntPtr.Zero);
                                    if (ptss.Count > 0)
                                    {
                                        Point3d p2 = ptss[0];
                                        Point3d pStart2 = subCurv.StartPoint;
                                        Point3d pEnd2 = ptLiveEnd.EndPoint;
                                        bool bStart2 = (p2.DistanceTo(pStart2) < p2.DistanceTo(pEnd2));
                                        double dist = Math.Min(p2.DistanceTo(pStart2), p2.DistanceTo(pEnd2));
                                        double offst = 24;
                                        for (int i = 1; i < ptss.Count; i++)
                                        {
                                            if (ptss[i].DistanceTo(pStart2) < dist)
                                            {
                                                p2 = ptss[i];
                                                bStart2 = true;
                                                dist = p2.DistanceTo(pStart2);
                                            }
                                            else if (ptss[i].DistanceTo(pEnd2) < dist)
                                            {
                                                p2 = ptss[i];
                                                bStart2 = false;
                                                dist = p2.DistanceTo(pEnd2);
                                            }
                                        }
                                        try
                                        {
                                            Vector3d dir = (p2 - ((bStart2) ? subCurv.StartPoint : subCurv.EndPoint)).GetNormal();
                                            p2 += dir*offst;
                                            subCurv.Extend(bStart2, p2);
                                        }
                                        catch (Autodesk.AutoCAD.Runtime.Exception exx)
                                        {
                                            acEd.WriteMessage(exx.ToString());
                                        }
                                    }
0 Likes
Message 12 of 16

Alexander.Rivilis
Mentor
Mentor

I think it's time you learn how to use a debugger to see what wrong with your's code.

 

Look like you can not rewrite my code. Why you use:

Point3d pEnd2 = ptLiveEnd.EndPoint;

???

 

Maybe you have to use:

Point3d pEnd2 = subCurv.EndPoint;

???

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 13 of 16

Anonymous
Not applicable

Ah; ptLiveEnd is cv2, and subCurv is cv1 essentially, they aren't chosen, but pre-determined by earlier parts of the code.;

0 Likes
Message 14 of 16

Alexander.Rivilis
Mentor
Mentor

@Anonymous wrote:

Ah; ptLiveEnd is cv2, and subCurv is cv1 essentially, they aren't chosen, but pre-determined by earlier parts of the code.;


Compare with my code.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 15 of 16

Anonymous
Not applicable

You sir, are very patient and helpful.

I am, however, an idiot.

 

That fixed it xD

0 Likes
Message 16 of 16

Anonymous
Not applicable

One last problem that I am running into. It works perfectly in all cases except for if the line to extend to is already the end point. How would I have it extend two feet past the line of extend even if it is already at that line.

0 Likes