Message 1 of 9
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class IntersectionHelper
<CommandMethod("GetIntersections")>
Public Sub GetIntersections()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
' Select first entity
Dim peo1 As New PromptEntityOptions("Select first polyline")
peo1.SetRejectMessage("Entity must be a polyline.")
peo1.AddAllowedClass(GetType(Polyline), False)
Dim per1 As PromptEntityResult = ed.GetEntity(peo1)
If per1.Status <> PromptStatus.OK Then
Return
End If
' Select second entity
Dim peo2 As New PromptEntityOptions("Select second polyline")
peo2.SetRejectMessage("Entity must be a polyline.")
peo2.AddAllowedClass(GetType(Polyline), False)
Dim per2 As PromptEntityResult = ed.GetEntity(peo2)
If per2.Status <> PromptStatus.OK Then
Return
End If
Dim ent1 As Entity = Nothing
Dim ent2 As Entity = Nothing
Using tr As Transaction = db.TransactionManager.StartTransaction()
ent1 = tr.GetObject(per1.ObjectId, OpenMode.ForRead)
ent2 = tr.GetObject(per2.ObjectId, OpenMode.ForRead)
Dim intersectionPoints As Point3dCollection = GetIntersectionPoints(ent1, ent2)
ed.WriteMessage(Environment.NewLine & "Intersection Points: ")
For Each pt As Point3d In intersectionPoints
ed.WriteMessage(Environment.NewLine & pt.ToString())
Next
tr.Commit()
End Using
End Sub
Public Function GetIntersectionPoints(ent1 As Entity, ent2 As Entity) As Point3dCollection
Dim intersectionPoints As New Point3dCollection()
If Not TypeOf ent1 Is Curve OrElse Not TypeOf ent2 Is Curve Then
Return intersectionPoints
End If
Dim curve1 As Curve = CType(ent1, Curve)
Dim curve2 As Curve = CType(ent2, Curve)
curve1.IntersectWith(curve2, Intersect.OnBothOperands, intersectionPoints, IntPtr.Zero, IntPtr.Zero)
Return intersectionPoints
End Function
Public Function GetLineSegmentsFromEntity(ent As Entity) As List(Of LineSegment3d)
Dim segments As New List(Of LineSegment3d)
If TypeOf ent Is Polyline Then
Dim pl As Polyline = CType(ent, Polyline)
For i As Integer = 0 To pl.NumberOfVertices - 2
Dim startPoint As Point3d = pl.GetPoint3dAt(i)
Dim endPoint As Point3d = pl.GetPoint3dAt(i + 1)
segments.Add(New LineSegment3d(startPoint, endPoint))
Next
If pl.Closed Then
segments.Add(New LineSegment3d(pl.GetPoint3dAt(pl.NumberOfVertices - 1), pl.GetPoint3dAt(0)))
End If
End If
Return segments
End Function
End Class
I'm at my wits end with this script, I can't for the life of me figure out a way to click a polyline and then have it calculate the slope and intersection point with the nearest relevant polyline on a specific layer. I can't find a good reference on the methods that might make something like this easier, would anybody be able to tell me what's wrong with the code or at least more info on methods resources?
Solved! Go to Solution.