using Autodesk.Revit.DB; using Autodesk.Revit.DB.Structure; using System; namespace Sai.Toolkit.Revit.Assist { /// /// 单位转换 /// public static class UnitAssist { /// /// 小数四舍五入 /// /// /// 保留小数位数 /// public static double FloatRounding(this double a, int digits) { return (double)Math.Round((decimal)a, digits, MidpointRounding.AwayFromZero); } /// /// 向上取整 /// /// /// public static int GetCeiling(this double a) { return (int)Math.Ceiling(a); } public static double GetDiameterInMillimeter(this RebarBarType rbt) { #if REVIT2025 var d = rbt.BarNominalDiameter.ToMillimeter(); #else var d = rbt.BarDiameter.ToMillimeter(); #endif return Math.Round(d, 1, MidpointRounding.ToEven); } /// /// 向下取整 /// /// /// public static int GetFloor(this double a) { return (int)Math.Floor(a); //return (int)a; } public static double? GetValueFromString(this string text, Units units) { #if USE_FORGETYPEID if (UnitFormatUtils.TryParse(units, SpecTypeId.Length, text, out var value)) #elif REVIT2018 || REVIT2020 if (UnitFormatUtils.TryParse(units, UnitType.UT_Length, text, out var value)) #elif REVIT2025 if (UnitFormatUtils.TryParse(units, SpecTypeId.Length, text, out var value)) #endif { return value; } return null; } /// /// 基于Revit的精度,判断值是否近似相等 /// /// /// /// public static bool IsAlmostEqual(double a, double b) { return IsZero(b - a); } /// /// 基于Revit的精度,判断值是否为零 /// /// /// /// public static bool IsZero(double a, double tolerance = 1.0e-9) { return tolerance > Math.Abs(a); } /// /// 四舍五入取整 /// /// /// public static int Rounding(this double a) { return (int)Math.Round(a, MidpointRounding.AwayFromZero); } /// /// 弧度转角度 /// /// /// public static double ToDegree(this double radian) { return radian * 180.0 / Math.PI; } /// /// 毫米转英尺 /// /// /// public static double ToFeet(this int mm) { return mm / 304.8; } /// /// 毫米转英尺 /// /// /// public static double ToFeet(this double mm) { return mm / 304.8; } /// /// 英尺转毫米 /// /// /// public static double ToMillimeter(this double ft) { return ft * 304.8; } /// /// 角度转弧度 /// /// /// public static double ToRadian(this double degree) { return degree * Math.PI / 180.0; } /// /// 平方毫米转平方英尺 /// /// /// public static double ToSquareFeet(this int squaremm) { return squaremm / (304.8 * 304.8); } /// /// 平方英尺转平方毫米 /// /// /// public static double ToSquareMillimeter(this double squareft) { return squareft * (304.8 * 304.8); } } }