Author Topic: Merging (poly)lines with TF.NET  (Read 1488 times)

0 Members and 1 Guest are viewing this topic.


  • Guest
Merging (poly)lines with TF.NET
« on: November 29, 2007, 08:30:55 AM »
Linework merging can be a tricky job. Here's how it's simply done with some help of TF.NET:

Before merging - linework consists of lines only:

After merging - resulting linestrings converted into polylines:

Resulting linework should be noded correctly. Correct noding is very important if you're planning to build any sort of network topology.

Topology Framework .NET (TF.NET) libraries are free, and can be downloaded here:

Source for merging lines/polylines (again, no error handling whatsoever):

Code: [Select]
Imports Topology.Geometries
Imports Topology.IO.Dwg
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput

Public Class Test

    <CommandMethod("MERGELINEWORK")> _
    Public Shared Sub MergeLinework()
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

        Dim values() As TypedValue = {New TypedValue(DxfCode.Start, "*LINE")}
        Dim filter As New SelectionFilter(values)

        Dim selOptions As PromptSelectionOptions = New PromptSelectionOptions
        selOptions.MessageForAdding = "Select objects:"
        selOptions.AllowDuplicates = False
        selOptions.SingleOnly = False

        Dim result As PromptSelectionResult = ed.GetSelection(selOptions, filter)
        If result.Status = PromptStatus.OK Then
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Dim reader As New DwgReader
            Dim writer As New DwgWriter

            Dim selSet As SelectionSet = result.Value
            Dim merger As New Topology.Operation.Linemerge.LineMerger
            For Each objId As ObjectId In selSet.GetObjectIds
                Dim ent As Entity = tr.GetObject(objId, OpenMode.ForRead)
                Dim geometry As Geometry = reader.ReadGeometry(ent)

            Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
            Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)

            For Each lineString As LineString In merger.MergedLineStrings
                Dim outEnt As Entity = writer.WritePolyline(lineString)
                outEnt.ColorIndex = 1
                tr.AddNewlyCreatedDBObject(outEnt, True)

        End If
    End Sub

End Class