public static IfcNamedUnit GetUnitFor(this IfcUnitAssignment ua, IfcPhysicalSimpleQuantity Quantity)
        {
            if (Quantity.Unit != null)
                return Quantity.Unit;

            IfcUnitEnum? requiredUnit = null; 

            // list of possible types taken from:
            // http://www.buildingsmart-tech.org/ifc/IFC2x3/TC1/html/ifcquantityresource/lexical/ifcphysicalsimplequantity.htm
            //
            if (Quantity is IfcQuantityLength)
                requiredUnit = IfcUnitEnum.LENGTHUNIT;
            else if (Quantity is IfcQuantityArea)
                requiredUnit = IfcUnitEnum.AREAUNIT;
            else if (Quantity is IfcQuantityVolume)
                requiredUnit = IfcUnitEnum.VOLUMEUNIT;
            else if (Quantity is IfcQuantityCount) // really not sure what to do here.
                return null;
            else if (Quantity is IfcQuantityWeight)
                requiredUnit = IfcUnitEnum.MASSUNIT;
            else if (Quantity is IfcQuantityTime)
                requiredUnit = IfcUnitEnum.TIMEUNIT;

            if (requiredUnit == null)
                return null;

            IfcNamedUnit nu = ua.Units.OfType<IfcSIUnit>().FirstOrDefault(u => u.UnitType == (IfcUnitEnum)requiredUnit);
            if (nu == null)
                nu = ua.Units.OfType<IfcConversionBasedUnit>().FirstOrDefault(u => u.UnitType == (IfcUnitEnum)requiredUnit);
            return nu;
        }
Exemplo n.º 2
0
        /// <summary>
        /// get the IfcPhysicalSimpleQuantity property value 
        /// </summary>
        /// <param name="p">IfcPhysicalSimpleQuantity</param>
        /// <param name="ifcGlobalUnits">global unit dictionary</param>
        /// <returns>string representing the value with units</returns>
        public static string FormatQuantityValue(IfcPhysicalSimpleQuantity p, ConcurrentDictionary<string, string> ifcGlobalUnits)
        {
            string numberFormat = "{0,0:N2}";
            string unit = string.Empty;

            //get associated unit abbreviation to the IfcPhysicalSimpleQuantity
            if (p.Unit != null)
                unit = GetUnitAbbreviation(p.Unit);

            if (p is IfcQuantityLength)
            {
                string value = string.Format(numberFormat, (p as IfcQuantityLength).LengthValue.Value);
                //get global if unit is null
                if (string.IsNullOrEmpty(unit))
                {
                    if (!ifcGlobalUnits.TryGetValue(IfcUnitEnum.LENGTHUNIT.ToString(), out unit))
                        unit = string.Empty;
                }

                return value + unit;
            }
            if (p is IfcQuantityArea)
            {
                string value = string.Format(numberFormat, (p as IfcQuantityArea).AreaValue.Value);
                if (string.IsNullOrEmpty(unit))
                {
                    if (!ifcGlobalUnits.TryGetValue(IfcUnitEnum.AREAUNIT.ToString(), out unit))
                        unit = string.Empty;
                }

                return value + unit;
            }
            if (p is IfcQuantityVolume)
            {
                string value = string.Format(numberFormat, (p as IfcQuantityVolume).VolumeValue.Value);
                if (string.IsNullOrEmpty(unit))
                {
                    if (!ifcGlobalUnits.TryGetValue(IfcUnitEnum.VOLUMEUNIT.ToString(), out unit))
                        unit = string.Empty;
                }

                return value + unit;
            }
            if (p is IfcQuantityCount)
            {
                string value = string.Format(numberFormat, (p as IfcQuantityCount).CountValue.Value);

                return value + unit;
            }
            if (p is IfcQuantityWeight)
            {
                string value = string.Format(numberFormat, (p as IfcQuantityWeight).WeightValue.Value);
                if (string.IsNullOrEmpty(unit))
                {
                    if (!ifcGlobalUnits.TryGetValue(IfcUnitEnum.MASSUNIT.ToString(), out unit))
                        unit = string.Empty;
                }

                return value + unit;
            }
            if (p is IfcQuantityTime)
            {
                string value = string.Format(numberFormat, (p as IfcQuantityTime).TimeValue.Value);
                if (string.IsNullOrEmpty(unit))
                {
                    if (!ifcGlobalUnits.TryGetValue(IfcUnitEnum.TIMEUNIT.ToString(), out unit))
                        unit = string.Empty;
                }

                return value + unit;
            }
            return string.Empty;
        }