public class PolylineSerializer
{
[CommandMethod("2SerializePolylines")]
public void SerializePolylines()
{
//--------------------------------------------------------------------Mandatory variables
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
//--------------------------------------------------------------------Mandatory variables
List < Polyline > polylines = new List < Polyline >();
using ( tr )
{
BlockTable bt = (BlockTable) tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
BlockTableRecord mainBtr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
List < SerializablePolyline > serializablePolylines = new List < SerializablePolyline >();
foreach ( ObjectId btrId in bt )
{
BlockTableRecord btr = (BlockTableRecord) tr.GetObject(btrId, OpenMode.ForWrite);
foreach ( ObjectId id in btr )
{
if ( id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(BlockReference))) )
{
continue;
}
Entity ent = (Entity) tr.GetObject(id, OpenMode.ForWrite, false, true);
if ( (ent != null) && (ent is Polyline) )
{
polylines.Add((Polyline) ent);
}
}
}
Vertex vertex;
foreach ( var pline in polylines )
{
var plHandle = pline.Handle.Value;
SerializablePolyline sp = new SerializablePolyline(plHandle, new List < Vertex >(), pline.Linetype, pline.Closed);
for ( int i = 0; i < pline.NumberOfVertices; i++ )
{
vertex = new Vertex(pline.GetPoint2dAt(i).X, pline.GetPoint2dAt(i).Y, pline.GetBulgeAt(i));
sp.VerticesCollection.Add(vertex);
}
serializablePolylines.Add(sp);
}
var currentDate = DateTime.Now.ToLongDateString();
AutocadSerializer.PolylineToJson(serializablePolylines, $"D:\\{currentDate}.json");
var list2 = AutocadSerializer.GetPolylineListFromJson($"D:\\{currentDate}.json");
for ( int i = 0; i < list2.Count; i++ )
{
SerializablePolyline item = list2[ i ];
Polyline polyline = new Polyline();
polyline.SetDatabaseDefaults();
for ( int j = 0; j < item.VerticesCollection.Count; j++ )
{
polyline.AddVertexAt(j, new Point2d(item.VerticesCollection[ j ].X, item.VerticesCollection[ j ].Y), item.VerticesCollection[ j ].Bulge, 0, 0);
}
polyline.LineWeight = LineWeight.LineWeight060;
polyline.Layer = "0";
polyline.ColorIndex = 10;
polyline.Closed = item.Contour;
try
{
polyline.Linetype = item.Linetype;
}
catch ( System.Exception ex )
{
//Application.ShowAlertDialog("The following exception was caught:\n" + ex.Message);
}
finally
{
polyline.Linetype = "ByLayer";
}
mainBtr.AppendEntity(polyline);
tr.AddNewlyCreatedDBObject(polyline, true);
}
tr.Commit();
}
}
public static Point2dCollection VerticesToCollection(Polyline polyline)
{
Point2dCollection vertices = new Point2dCollection();
for ( int i = 0; i < polyline.NumberOfVertices; i++ )
{
vertices.Add(polyline.GetPoint2dAt(i));
}
return vertices;
}
}