コード例 #1
0
        public static IElement2D RoundCoordinates(this IElement2D element2d, int decimalPlaces = 6)
        {
            bool planar = element2d.IIsPlanar();

            if (planar)
            {
                Vector normal = element2d.FitPlane().Normal.Normalise();

                //If the element is planar AND aligned with one of the main coordinate system's planes then rounded element will get projected on this plane to keep it's planarity.
                if (Math.Abs(Math.Abs(normal.X) - 1) < Tolerance.Angle ||
                    Math.Abs(Math.Abs(normal.Y) - 1) < Tolerance.Angle ||
                    Math.Abs(Math.Abs(normal.Z) - 1) < Tolerance.Angle)
                {
                    Plane plane = new Plane()
                    {
                        Origin = Geometry.Modify.RoundCoordinates(element2d.OutlineCurve().StartPoint(), decimalPlaces), Normal = normal.RoundCoordinates(0)
                    };

                    element2d = element2d.ISetOutlineElements1D(element2d.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry().IProject(plane), decimalPlaces))).ToList());

                    return(element2d.ISetInternalElements2D(element2d.IInternalElements2D().Select(y => y.ISetOutlineElements1D(y.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry().IProject(plane), decimalPlaces))).ToList())).ToList()));
                }
            }

            //Here is the part with the default way of rounding element's coordinates:

            IElement2D newElement2d = element2d.ISetOutlineElements1D(element2d.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry(), decimalPlaces))).ToList());

            newElement2d = newElement2d.ISetInternalElements2D(newElement2d.IInternalElements2D().Select(y => y.ISetOutlineElements1D(y.IOutlineElements1D().Select(x => x.ISetGeometry(Geometry.Modify.IRoundCoordinates(x.IGeometry(), decimalPlaces))).ToList())).ToList());

            if (planar && !newElement2d.IsPlanar()) //If the original element was planar we need to ensure that result is planar as well.
            {
                Reflection.Compute.RecordWarning("Rounding the coordinates of an IElement2D couldn't be achieved without losing planarity. No action has been taken.");
                return(element2d);
            }

            return(newElement2d);
        }