using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
namespace ITAT_Autocad
{
internal class CoordinatesNormalizer
{
public static double RoundOff(double input, int accuracy)
{
double result;
result = Math.Abs(input / accuracy) * accuracy;
return result;
}
public static Point2dCollection NormalizePoint(Point2dCollection vertices)
{
Point2dCollection normVertices = null;
foreach (Point2d vertice in vertices)
{
Point2d normVertice = new Point2d(RoundOff(vertice.X, 5), RoundOff(vertice.Y, 5));
normVertices.Add(normVertice);
}
return normVertices;
}
public static Point2dCollection VerticesToCollection(Polyline polyline)
{
Point2dCollection vertices = null;
for (int i = 0; i < polyline.NumberOfVertices; i++)
{
vertices.Add(polyline.GetPoint2dAt(i));
}
return vertices;
}
}
public static class Linesnormalizer
{
[CommandMethod("0RoundOff", CommandFlags.Modal)]
public static void RoundOffPoints()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Point2dCollection point2DCollection=null;
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite);
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
if (btr.IsFromExternalReference)
{
continue;
}
foreach (ObjectId id in btr)
{
Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
if (ent is Polyline)
{
Polyline pline = (Polyline)ent;
point2DCollection = CoordinatesNormalizer.VerticesToCollection(pline);
var normVertices = CoordinatesNormalizer.NormalizePoint(point2DCollection);
for (int i = 0; i < normVertices.Count; i++)
{
pline.AddVertexAt(i, normVertices[i], 0, 0, 0);
}
}
if (ent is Line)
{
continue;
}
}
}
tr.Commit();
}
ed.Regen();
}
}
}