I'm using AutoCAD 2013 x64 and VS2012 Express with ObjectARX 2013.
Now I want to calculate the (projected) intersection point of 2 (intersecting) lines in vb.net
After selecting the lines I have this code:
line1.IntersectWith(line2, Intersect.OnBothOperands, intpts, 0, 0)
In Visual studio express 2012 I get the following warning for the above line of code:
warning BC40000: 'Public Sub IntersectWith(entityPointer As Autodesk.AutoCAD.DatabaseServices.Entity, intersectType As Autodesk.AutoCAD.DatabaseServices.Intersect, points As Autodesk.AutoCAD.Geometry.Point3dCollection, thisGraphicSystemMarker As Long, otherGraphicSystemMarker As Long)' is obsolete: 'Use the overload taking IntPtr instead.'.
So, if I'm reading this correctly, this method is outdated and I should use an other method to calculate the intersection.
But I really don't know what 'Use the overload taking IntPtr instead' means.
I have searched on a lot of forums, but I can't seem to find an answer. All the answers I could find were essentially the same function.
One of the things I found is a function that contains this line of code:
Dim inters As Point3d = line1.IntersectWith(line2)(0)
This one looks almost the same, but doesn't give me the warning when I compile the code, but this method crashes AutoCAD when there is no intersection between the lines. This is despite a try catch block that surrounds the code.
Can anybody point me in the right direction?
For now I just want to use lines, but I need to expand the code so it will also work with circles, arcs and polylines.
Solved! Go to Solution.
Wow that was quick!!!
And it works too! Thanks a lot.
But can you(or anybody else) tell me what the difference is between the two lines of code I posted?
I mean, to my eye it is the same method, but why don't I get a warning on the second line of code?
The second line of code crashes my AutoCAD when the picked lines have no intersectionpoints.
Would it better to check if the lines are parallel before the intersection check?
That's what I have done now to avoid the crash, but I'm sure there is another (maybe better) way.
Not sure about that line of code, as it should give an error at compile time. Unless there was, at one time, on overload method which only accepted the one argument (perhaps the old COM method allowed optional arguments?)
If you find a response which answers your question, please be sure to accept the actual post which helped you as the solution. This will help future searchers quickly find answers to their own questions. Thanks!
I think the second line of code uses the Autodesk.AutoCAD.Geometry.LinearEntity3d.Intersect
Usiing the Autodesk.AutoCAD.Geometry.LinearEntity3d (or Entity2d) underlaying derived classes for geometry calculation is cheaper (it's the way the API internaly does).
The LinearEntity3d.IntersectWith() method return a Point3d array or null (Nothing) if there's none intersection. to avoid the exception, you have to check if the returned value is not null (Nothing), IOW if the lines are parallel (or colinear).
Thanks for your answer.
I have tried to check if the function returns nothing, but unfortunately that does not matter.
AutoCAD just crashes if the lines are parallel.
This is the function I was using:
Function LinesIntersect(ln1 As Line, ln2 As Line) As Point3d Try Dim line1 As New Line3d(ln1.StartPoint, ln1.EndPoint) Dim line2 As New Line3d(ln2.StartPoint, ln2.EndPoint) Dim inters As Point3d = line1.IntersectWith(line2)(0) 'Acad crashes at this line of code If Not inters = Nothing Then GSIntersect = inters Else msgbox("parallel?") End If Catch ex As Exception Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.D
ocumentManager.MdiActiveDocument Dim ed As Editor = doc.Editor ed.WriteMessage("Something went wrong:" & vbLf & vbLf & ex.ToString()) End Try End Function
I get a FATAL ERROR: Unhandled Access Violation....
AutoCAD just crashes before I can even check if the result is nothing.
What I'm trying to achieve is a function that returns a points3DCollection with the coordinates of all the intersections of the two objects I select (lines,polylines, arcs and circles)
As I said, IntersectWith returns a point3d array or null (Nothing), so you have to check if the array is null before trying to get its first item.
If you want to make a function, it may return a nullable<Point3d> (Nullable(Of Point3d) instead of a Point3d because the Point3d structure is not nullable in case there's none intersection.
The following example retruns a Nullable(Of Point3d) (or null if none intersection):
Private Function LinesIntersect(line1 As Line, line2 As Line) As Nullable(Of Point3d) Dim ls1 As New LineSegment3d(line1.StartPoint, line1.EndPoint) Dim ls2 As New LineSegment3d(line2.StartPoint, line2.EndPoint) Dim inters As Point3d() = ls1.IntersectWith(ls2) If inters Is Nothing Then Return Nothing Else Return inters(0) End If
Dim nPt As Nullable(Of Point3d) = LinesIntersect(line1, line2)
If nPt.HasValue Then
Dim pt As Point3d = nPt.Value