Code:
<CommandMethod("CNC_PLINE_TEST", CommandFlags.Modal)> _
Public Shared Sub createBreakpoints() Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim ucs As Matrix3d = ed.CurrentUserCoordinateSystem
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim id As ObjectId
Dim ent As Entity
Try
Using tr As Transaction = db.TransactionManager.StartTransaction()
'Blocktablerecord öffnen / schreiben
Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
' Let the user select a curve
id = ed.GetEntity(vbLf & "Polylinie wählen: ").ObjectId
If id = ObjectId.Null Then
Return
End If
ent = DirectCast(tr.GetObject(id, OpenMode.ForRead, False), Entity)
If ent Is Nothing Then
Return
End If
' Check that it is really a curve
Dim Curve As Polyline = TryCast(ent, Polyline)
If Curve Is Nothing Then
Return
End If
Dim breakPnt As Point3d 'ersten Brechpunkt wählen
breakPnt = ed.GetPoint(vbLf & "Startpunkt wählen: ").Value
Dim RichtungsPt As Point3d 'Richtungsvektor angeben
RichtungsPt = ed.GetPoint(vbLf & "Punkt auf Polylinie zur Richtungsbestimmung wählen: ").Value
breakPnt.TransformBy(ucs.Inverse())
' Check that the point is on the curve
breakPnt = Curve.GetClosestPointTo(breakPnt, False)
Dim breakPoints As New Point3dCollection()
Dim newCurves As New DBObjectCollection()
' Get the segments according to the trim points
breakPoints.Add(breakPnt)
newCurves = Curve.GetSplitCurves(breakPoints)
If newCurves Is Nothing Then
ed.WriteMessage(vbLf & "Fehler in der gewählten Kurve!")
Return
End If
' Here we add the segments to the database with different colors
For i As Integer = 0 To newCurves.Count - 1
Dim pent As Polyline = TryCast(DirectCast(newCurves(i), Polyline), Polyline)
If pent.StartPoint <> breakPnt Then
ReversePolylineDirection(pent)
End If
pent.SetPropertiesFrom(ent)
pent.ColorIndex = i + 1
btr.AppendEntity(pent)
tr.AddNewlyCreatedDBObject(pent, True)
Next
ent.UpgradeOpen()
tr.Commit()
End Using
Catch ex As System.Exception
ed.WriteMessage(ex.Message)
End Try
End Sub
Private Structure PerVertexData
Public pt As Point2d
Public bulge As Double
Public startWidth As Double
Public endWidth As Double
End Structure
Public Shared Sub ReversePolylineDirection(ByVal pl As Polyline)
Dim doc = Application.DocumentManager.MdiActiveDocument
Dim ed = doc.Editor
Dim tr = doc.TransactionManager.StartTransaction()
Using tr
If pl IsNot Nothing Then
' Collect our per-vertex data
Dim vertData As New List(Of PerVertexData)(pl.NumberOfVertices)
For i As Integer = 0 To pl.NumberOfVertices - 1
Dim pvd As New PerVertexData()
pvd.bulge = (If(i > 0, pl.GetBulgeAt(i - 1), 0))
pvd.startWidth = (If(i > 0, pl.GetStartWidthAt(i - 1), 0))
pvd.endWidth = (If(i > 0, pl.GetEndWidthAt(i - 1), 0))
pvd.pt = pl.GetPoint2dAt(i)
vertData.Add(pvd)
Next
For i As Integer = 0 To pl.NumberOfVertices - 1
Dim pvd As PerVertexData = vertData(pl.NumberOfVertices - (i + 1))
pl.SetPointAt(i, pvd.pt)
pl.SetBulgeAt(i, -pvd.bulge)
pl.SetStartWidthAt(i, pvd.endWidth)
pl.SetEndWidthAt(i, pvd.startWidth)
Next
End If
tr.Commit()
End Using
End Sub