.NET

Reply
Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 1 of 6 (560 Views)
Accepted Solution

Find intersection point of two lines

560 Views, 5 Replies
01-23-2014 04:26 AM

Hello

 

I am new here in autocad programming .I am making an application which ask user to select diagonal line and find their intersection point to draw furthur drawing. please help as earliest.

 

 

Valued Mentor
gasty1001
Posts: 470
Registered: ‎04-11-2010
Message 2 of 6 (548 Views)

Re: Find intersection point of two lines

01-23-2014 06:14 AM in reply to: bvyas89

Hi,

 

Look for Entity.IntersectWith method, there are many examples here and in the swamp too.

 

Gaston Nunez

 

 

 

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 3 of 6 (536 Views)

Re: Find intersection point of two lines

01-23-2014 07:58 AM in reply to: bvyas89

Hi,

With below code you can start to find intersection of two polylines. This code is very simple. Hope you get some idead how to get intersection point. Just one reminder, as I did not change anything in the drawing this is why I did not commit the transaction at the end.

 

  <CommandMethod("INS")> Public Sub InterSectionPoint()
        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor
        Dim pl1 As Polyline = Nothing
        Dim pl2 As Polyline = Nothing
        Dim ent As Entity = Nothing
        Dim peo As PromptEntityOptions = Nothing
        Dim per As PromptEntityResult = Nothing
        Using tx As Transaction = db.TransactionManager.StartTransaction()
'Select first polyline
            peo = New PromptEntityOptions("Select firtst Polyline:")
            per = ed.GetEntity(peo)
            If per.Status <> PromptStatus.OK Then
                Return
            End If
'Get the polyline entity
            ent = tx.GetObject(per.ObjectId, OpenMode.ForRead)
            If TypeOf ent Is Polyline Then
                pl1 = TryCast(ent, Polyline)
            End If
'Select 2nd polyline
            peo = New PromptEntityOptions("Select Second Polyline:")
            per = ed.GetEntity(peo)
            If per.Status <> PromptStatus.OK Then
                Return
            End If
            ent = tx.GetObject(per.ObjectId, OpenMode.ForRead)
            If TypeOf ent Is Polyline Then
                pl2 = TryCast(ent, Polyline)
            End If
            Dim pts3D As Point3dCollection = New Point3dCollection()
'Get the intersection Points between polyline 1 and polyline 2
            pl1.IntersectWith(pl2, Intersect.OnBothOperands, pts3D, IntPtr.Zero, IntPtr.Zero)
            For Each pt As Point3d In pts3D
                ed.WriteMessage(pt.ToString)
            Next
        End Using

    End Sub

 

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 4 of 6 (505 Views)

Re: Find intersection point of two lines

01-23-2014 08:41 PM in reply to: mzakiralam

Thanks mzakiralam for your prompt reply. I tried your code bt it giving me an error.Please find image attach here with for details of error.



Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 5 of 6 (492 Views)

Re: Find intersection point of two lines

01-23-2014 09:34 PM in reply to: bvyas89

ABOVE PROBLEM GET SOLVE BUT NOW NEW ERROR HERE.I NTERSECTION POINT HAS NULL VALUE ITS NT WORKING PROPERLY.

 

CODE AND ERROR MSG IS AS BELOW

 

//intersection
[CommandMethod("INS")]
public void InterSectionPoint()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Line pl1 = null;
Line pl2 = null;
Entity ent = null;
PromptEntityOptions peo = null;
PromptEntityResult per = null;
using (Transaction tx = db.TransactionManager.StartTransaction())
{
//Select first polyline
peo = new PromptEntityOptions("Select firtst line:");
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
//Get the polyline entity
ent = (Entity)tx.GetObject(per.ObjectId, OpenMode.ForRead);
if (ent is Line)
{
pl1 = ent as Line;
}
//Select 2nd polyline
peo = new PromptEntityOptions("\n Select Second line:");
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
ent = (Entity)tx.GetObject(per.ObjectId, OpenMode.ForRead);
if (ent is Line)
{
pl2 = ent as Line;
}
Point3dCollection pts3D = new Point3dCollection();
//Get the intersection Points between line 1 and line 2
pl1.IntersectWith(pl2, Intersect.ExtendBoth, pts3D, IntPtr.Zero, IntPtr.Zero);
foreach (Point3d pt in pts3D)
{
// ed.WriteMessage("\n intersection point :",pt);
ed.WriteMessage("Point number {0}: ({1}, {2}, {3})", pt.X, pt.Y, pt.Z);

}

tx.Commit();
}

}

Active Contributor
bvyas89
Posts: 26
Registered: ‎01-16-2014
Message 6 of 6 (485 Views)

Re: Find intersection point of two lines

01-23-2014 10:21 PM in reply to: bvyas89

Thanx everyone and this is working code;

 

//intersection
[CommandMethod("INS")]
public void InterSectionPoint()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Line pl1 = null;
Line pl2 = null;
Entity ent = null;
PromptEntityOptions peo = null;
PromptEntityResult per = null;
using (Transaction tx = db.TransactionManager.StartTransaction())
{
//Select first polyline
peo = new PromptEntityOptions("Select firtst line:");
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
//Get the polyline entity
ent = (Entity)tx.GetObject(per.ObjectId, OpenMode.ForRead);
if (ent is Line)
{
pl1 = ent as Line;
}
//Select 2nd polyline
peo = new PromptEntityOptions("\n Select Second line:");
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
{
return;
}
ent = (Entity)tx.GetObject(per.ObjectId, OpenMode.ForRead);
if (ent is Line)
{
pl2 = ent as Line;
}
Point3dCollection pts3D = new Point3dCollection();
//Get the intersection Points between line 1 and line 2
pl1.IntersectWith(pl2, Intersect.OnBothOperands, pts3D, IntPtr.Zero, IntPtr.Zero);
foreach (Point3d pt in pts3D)
{
// ed.WriteMessage("\n intersection point :",pt);
// ed.WriteMessage("Point number: ", pt.X, pt.Y, pt.Z);

Application.ShowAlertDialog("\n Intersection Point: " +"\nX = " + pt.X + "\nY = " + pt.Y +"\nZ = " + pt.Z);
}

tx.Commit();
}

}

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.