Revit secondary development knowledge sharing (5) unit conversion issues

The unit of the data we read with the code in the actual project is in feet. If you don't know this, you will get an error when you do some functions that input values ​​and specify values. Therefore, the method of unit conversion is provided in RevitAPI.
Insert picture description here

DisplayUnitType is an enumeration class, there are different unit choices, you can find out by yourself. Below I put some methods that I use frequently below.

/// <summary>
        /// 英尺转毫米
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double ToMM(this double b)
        {
    
    
            return UnitUtils.Convert(b, DisplayUnitType.DUT_DECIMAL_FEET, DisplayUnitType.DUT_MILLIMETERS);
        }
        /// <summary>
        /// 毫米转英尺
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double ToFeet<T>(this T b)where T:struct
        {
    
    
            double.TryParse(b.ToString(), out var d);
            return UnitUtils.Convert(d, DisplayUnitType.DUT_MILLIMETERS, DisplayUnitType.DUT_DECIMAL_FEET);
        }
        /// <summary>
        /// 平方英尺转平方米
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double ToSquareMeters(this double b)
        {
    
    
            return UnitUtils.Convert(b, DisplayUnitType.DUT_SQUARE_FEET, DisplayUnitType.DUT_SQUARE_METERS);
        }
        /// <summary>
        /// 平方米转平方英尺
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double ToSquareFeet(this double b)
        {
    
    
            return UnitUtils.Convert(b, DisplayUnitType.DUT_SQUARE_METERS, DisplayUnitType.DUT_SQUARE_FEET);
        }

Hope that can solve your problem.

Guess you like

Origin blog.csdn.net/Oneal5354/article/details/108433294