.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Find intersection point of two lines

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
bvyas89
8834 Views, 6 Replies

Find intersection point of two lines

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.

 

 

6 REPLIES 6
Message 2 of 7
hgasty1001
in reply to: bvyas89

Hi,

 

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

 

Gaston Nunez

 

 

 

Message 3 of 7
mzakiralam
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

 

Message 4 of 7
bvyas89
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.



Message 5 of 7
bvyas89
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();
}

}

Message 6 of 7
bvyas89
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();
}

}

Message 7 of 7
BKSpurgeon
in reply to: bvyas89

For future readers:

 

No expert here, but if you're using a line, why not use: LinearEntity2d.IntersectWith method rather than using the baseclass Entity intersectWith method.

 

The objectarx documentation says this:

This method calls LinearEnt2d.IntersectWith(LinearEntity2d line, Tolerance tolerance) with tolerance set to Global.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost