.NET

.NET

Reply
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 1 of 6 (1,385 Views)
Accepted Solution

Getting an intersecting point coordinate of two polylines(lines)

1385 Views, 5 Replies
07-12-2012 12:24 AM

Hi, I wonder if there is a way to get an intersecting point coordinate of two polylines(lines) programmatically.

For example, like in the below sample picture, I need to get coordinates of the points in the red cycles.

 

sample.PNG

 

 

Is there any method that I can easily use?

Could I get a sample code snippet that computes the coordinates? (in C# & ObjectARX 2010 if possible?)

 

I've looked up "Through the Interface" and this disscussion group. couldn't find any useful yet.

Please someone help me. =(

 

 

Thank you for your time.

 

Jake

Check this out

 

 [CommandMethod("checkinters")]
public void CheckInterference()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
try
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
PromptEntityOptions peo = new PromptEntityOptions("\nSelect a first polyline >>");
peo.SetRejectMessage("\nSelected object might be of type polyline only >>");
peo.AddAllowedClass(typeof(Polyline), false);
PromptEntityResult res;
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK)
return;
Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
if (ent == null)
return;
Polyline poly = (Polyline)ent as Polyline;
peo = new PromptEntityOptions("\nSelect second polyline >>");
peo.SetRejectMessage("\nSelected object might be of type polyline only >>");
peo.AddAllowedClass(typeof(Polyline), false);
res = ed.GetEntity(peo);
if (res.Status != PromptStatus.OK)
return;
ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
if (ent == null)
return;
Polyline poly2 = (Polyline)ent as Polyline;
Point3dCollection pts = new Point3dCollection();
poly.IntersectWith(poly2, Intersect.OnBothOperands, pts, (int)IntPtr.Zero, (int)IntPtr.Zero);
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format("Hint:\nNumber of Verices: {0}\nNumber of Intersections: {1}", poly.NumberOfVertices, pts.Count));
// test for visulization only
foreach (Point3d pt in pts)
{
Circle circ = new Circle(pt,Vector3d.ZAxis,10 * db.Dimtxt);
circ.ColorIndex = 1;
btr.AppendEntity(circ);
tr.AddNewlyCreatedDBObject(circ, true);
}
tr.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
}
}
}

 

 

~'J'~

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 6 (1,379 Views)

Re: Getting an intersecting point coordinate of two polylines(lines)

07-12-2012 12:57 AM in reply to: dynamicscope

Check this out

 

        [CommandMethod("checkinters")]
        public void CheckInterference()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
    
                Transaction tr = db.TransactionManager.StartTransaction();

                using (tr)
                {
                    try
                    {
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                        PromptEntityOptions peo = new PromptEntityOptions("\nSelect a first polyline  >>");

                        peo.SetRejectMessage("\nSelected object might be of type polyline only >>");

                        peo.AddAllowedClass(typeof(Polyline), false);

                        PromptEntityResult res;

                        res = ed.GetEntity(peo);

                        if (res.Status != PromptStatus.OK)

                            return;

                        Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);

                        if (ent == null)

                            return;

                        Polyline poly = (Polyline)ent as Polyline;
                        peo = new PromptEntityOptions("\nSelect second polyline  >>");

                        peo.SetRejectMessage("\nSelected object might be of type polyline only >>");

                        peo.AddAllowedClass(typeof(Polyline), false);

                        res = ed.GetEntity(peo);

                        if (res.Status != PromptStatus.OK)

                            return;

                        ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);

                        if (ent == null)

                            return;

                        Polyline poly2 = (Polyline)ent as Polyline;


                        Point3dCollection pts = new Point3dCollection();

                        poly.IntersectWith(poly2, Intersect.OnBothOperands, pts, (int)IntPtr.Zero, (int)IntPtr.Zero);

                        Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(string.Format("Hint:\nNumber of Verices: {0}\nNumber of Intersections: {1}", poly.NumberOfVertices, pts.Count));
                     // test for visulization only
                        foreach (Point3d pt in pts)
                        {
                            Circle circ = new Circle(pt,Vector3d.ZAxis,10 * db.Dimtxt);
                            circ.ColorIndex = 1;
                            btr.AppendEntity(circ);
                            tr.AddNewlyCreatedDBObject(circ, true);

                        }
                        tr.Commit();
                    }
                    catch (System.Exception ex)
                    {
                        ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace);
                    }
                }

            }

 

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 3 of 6 (1,376 Views)

Re: Getting an intersecting point coordinate of two polylines(lines)

07-12-2012 01:06 AM in reply to: dynamicscope

Can you upload the piece of this drawing

for testing?

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
dynamicscope
Posts: 95
Registered: ‎04-04-2012
Message 4 of 6 (1,333 Views)

Re: Getting an intersecting point coordinate of two polylines(lines)

07-16-2012 10:34 PM in reply to: Hallex

It does work!! Thank you!! =)

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 6 (1,329 Views)

Re: Getting an intersecting point coordinate of two polylines(lines)

07-16-2012 11:38 PM in reply to: dynamicscope

You're welcome

Happy coding :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
New Member
applie_3
Posts: 1
Registered: ‎12-14-2012
Message 6 of 6 (1,063 Views)

Re: Getting an intersecting point coordinate of two polylines(lines)

12-14-2012 11:16 PM in reply to: dynamicscope

Hi, i need to get coordinates of an image in autocad for my final year project. I am a student of computer engineering so dont have autocad background. Any help will highly be appriciated, thankyou.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.