public static void RoundOffPoints()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
double accuracy = SelectionUtilities.GetNumParameter("Set desired accuracy");
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, false, true);
if (ent is Polyline)
{
Polyline pline = (Polyline)ent;
point2DCollection = CoordinatesNormalizer.VerticesToCollection(pline);
Point2dCollection normVertices = CoordinatesNormalizer.NormalizePoint(accuracy, point2DCollection);
for (int i = 0; i < normVertices.Count; i++)
{
pline.SetPointAt(i, normVertices[i]);
}
}
if (ent is Line)
{
Line line = (Line)ent;
point2DCollection = CoordinatesNormalizer.VerticesToCollection(line);
Point2dCollection normVertices = CoordinatesNormalizer.NormalizePoint(accuracy, point2DCollection);
line.StartPoint = new Point3d(normVertices[0].X, normVertices[0].Y, 0);
line.EndPoint = new Point3d(normVertices[1].X, normVertices[1].Y, 0);
}
if (ent is BlockReference)
{
BlockReference bref = (BlockReference)ent;
point2DCollection.Add(new Point2d(bref.Position.X, bref.Position.Y));
Point2dCollection normVertices = CoordinatesNormalizer.NormalizePoint(accuracy, point2DCollection);
bref.Position = new Point3d(normVertices[0].X, normVertices[0].Y, 0);
}
}
}
tr.Commit();
}
ed.Regen();
}
internal class CoordinatesNormalizer
{
public static double RoundOff(double input, double accuracy)
{
double result = Math.Round(input / accuracy) * accuracy;
return result;
}
public static Point2dCollection NormalizePoint(double accuracy, Point2dCollection vertices)
{
Point2dCollection normVertices = new Point2dCollection();
foreach (Point2d vertice in vertices)
{
Point2d normVertice = new Point2d(RoundOff(vertice.X, accuracy), RoundOff(vertice.Y, accuracy));
normVertices.Add(normVertice);
}
return normVertices;
}
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;
}
public static Point2dCollection VerticesToCollection(Line line)
{
Point2dCollection vertices = new Point2dCollection();
vertices.Add(new Point2d(line.StartPoint.X, line.StartPoint.Y));
vertices.Add(new Point2d(line.EndPoint.X, line.EndPoint.Y));
return vertices;
}
public static class MathExtensions
{
public static int RoundUpTo(int number, int nearest)
{
if ((nearest < 10) || (nearest % 10 != 0))
{
throw new ArgumentOutOfRangeException(nameof(nearest), $"{nameof(nearest)} must be a positive multiple of 10, but you specified {nearest}.");
}
int modulo = number % nearest;
if (modulo == 0)
{
return number;
}
if (modulo > 0)
{
return number + (nearest - modulo);
}
return number - modulo;
}
}
}