Hello: I have code that converts objects to polylines (e.g. 2d to poly, spline to poly).
When my code converts most of the objects, the object still appears the same as what it did before; however,
a user allerted me of a few 2d polys that once converted, looked way different (see attached pic).
Here's my code for converting - it's fairly staight forward:
Private Shared Function ConvertToPolylines(ByVal ids() As ObjectId, ByVal tr As Transaction) As List(Of Polyline)
Dim polyList As New List(Of Polyline)
Dim blockTable As BlockTable = tr.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead)
Dim modelSpace As BlockTableRecord = tr.GetObject(blockTable(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim sLayerName As String = "'"
Try
For Each id As ObjectId In ids
Dim ent As Entity = tr.GetObject(id, OpenMode.ForWrite)
If TypeOf ent Is Polyline Then
' there is nothing to do
polyList.Add(ent)
ElseIf TypeOf ent Is Polyline2d Then
Dim oldPoly As Polyline2d = ent
Dim newPoly As New Polyline
newPoly.SetDatabaseDefaults()
For Each vertId As ObjectId In oldPoly
Dim vert As Vertex2d = tr.GetObject(vertId, OpenMode.ForRead)
newPoly.AddVertexAt(newPoly.NumberOfVertices, New GE.Point2d(vert.Position.X, vert.Position.Y), 0, 0, 0)
Next
newPoly.Closed = oldPoly.Closed
Dim lLength As Double = newPoly.Length
Dim aArea As Double = newPoly.Area
' add new object
modelSpace.AppendEntity(newPoly)
tr.AddNewlyCreatedDBObject(newPoly, True)
' delete old one
oldPoly.Erase()
polyList.Add(newPoly)
ElseIf TypeOf ent Is Ellipse Or TypeOf ent Is Circle Or TypeOf ent Is Spline Then
Dim curve As Curve = ent
Dim length As Double = AenLoop.GetCurveLength(ent)
Dim segmentNumber As Integer = length / kPolysegmentLength + 1
Dim newPoly As New Polyline
newPoly.SetDatabaseDefaults()
For i As Integer = 0 To segmentNumber
Dim param As Double = curve.StartParam + ((curve.EndParam - curve.StartParam) * i / segmentNumber)
Dim pt As GE.Point3d = curve.GetPointAtParameter(param)
newPoly.AddVertexAt(newPoly.NumberOfVertices, New GE.Point2d(pt.X, pt.Y), 0, 0, 0)
Next
' it it is circle or ellipse then it is closed
newPoly.Closed = TypeOf ent Is Ellipse Or TypeOf ent Is Circle
' add new object
modelSpace.AppendEntity(newPoly)
tr.AddNewlyCreatedDBObject(newPoly, True)
' delete old one
curve.Erase()
polyList.Add(newPoly)
End If
Next
Return polyList
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox("ConvertToPolylines error: " & ex.Message)
tr.Abort()
Finally
End Try
End Function
Is there a way to have the object retain it's original shape when converting? Thanks,
Proctor