TheSwamp
Code Red => .NET => Topic started by: MaksimS 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:
(http://www.geoinova.com/examples/merge_before.jpg)
After merging - resulting linestrings converted into polylines:
(http://www.geoinova.com/examples/merge_after.jpg)
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: http://code.google.com/p/tf-net/ (http://code.google.com/p/tf-net/)
Source for merging lines/polylines (again, no error handling whatsoever):
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)
merger.Add(geometry)
Next
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
btr.AppendEntity(outEnt)
tr.AddNewlyCreatedDBObject(outEnt, True)
Next
tr.Commit()
tr.Dispose()
End If
End Sub
End Class