Пример #1
0
        static bool getNamedUnitRepStr(IIfcUnit unitDef, out IfcUnitEnum unitType, out string unitRepStr)
        {
            // Initialize the static Dicts, if it is still empty upon the first use. These Dicts do not need to be reset
            setupUnitRep();

            unitType   = IfcUnitEnum.LENGTHUNIT; // initial value
            unitRepStr = string.Empty;
            if (unitDef is IIfcContextDependentUnit)
            {
                // Not supported yet at this time
            }
            else if (unitDef is IIfcConversionBasedUnit)
            {
                unitType   = ((IIfcConversionBasedUnit)unitDef).UnitType;
                unitRepStr = getConversionBasedUnitRepStr(unitDef);
                if (!string.IsNullOrEmpty(unitRepStr))
                {
                    return(true);
                }
            }
            else if (unitDef is IIfcSIUnit)
            {
                unitType   = ((IIfcSIUnit)unitDef).UnitType;
                unitRepStr = getSIUnitRepStr(unitDef);
                if (!string.IsNullOrEmpty(unitRepStr))
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #2
0
        public static string getIfcUnitStr(IIfcUnit unitDef)
        {
            // Initialize the static Dicts, if it is still empty upon the first use. These Dicts do not need to be reset
            setupUnitRep();

            IfcUnitEnum        namedUnitType;
            IfcDerivedUnitEnum derivedUnitType;
            string             unitRepStr = string.Empty;

            if (unitDef is IIfcMonetaryUnit)
            {
                return(m_IfcProjectMonetaryUnit);
            }
            else if (unitDef is IIfcNamedUnit)
            {
                if (getNamedUnitRepStr(unitDef, out namedUnitType, out unitRepStr))
                {
                    return(unitRepStr);
                }
            }
            else if (unitDef is IIfcDerivedUnit)
            {
                if (getDerivedUnitRepStr(unitDef, out derivedUnitType, out unitRepStr))
                {
                    return(unitRepStr);
                }
            }
            return(null);
        }
Пример #3
0
        static string getConversionBasedUnitRepStr(IIfcUnit unitDef)
        {
            // Initialize the static Dicts, if it is still empty upon the first use. These Dicts do not need to be reset
            setupUnitRep();

            IIfcConversionBasedUnit convUnit = unitDef as IIfcConversionBasedUnit;
            string convUnitStr = convUnit.Name.ToString().ToUpper();
            string unitRepStr  = string.Empty;

            m_ConversionBasedNameRep.TryGetValue(convUnitStr, out unitRepStr);
            //string unitRepStr = getIfcUnitStr(convUnit.ConversionFactor.UnitComponent);
            return(unitRepStr);
        }
Пример #4
0
        static string getSIUnitRepStr(IIfcUnit unitDef)
        {
            // Initialize the static Dicts, if it is still empty upon the first use. These Dicts do not need to be reset
            setupUnitRep();

            IIfcSIUnit siUnit     = unitDef as IIfcSIUnit;
            string     unitRepStr = string.Empty;

            m_SIUnitNameRep.TryGetValue(siUnit.Name, out unitRepStr);
            if (siUnit.Prefix.HasValue)
            {
                string prefixStr = string.Empty;
                m_SIPrefixRep.TryGetValue(siUnit.Prefix.Value, out prefixStr);
                unitRepStr = prefixStr + unitRepStr;
            }
            return(unitRepStr);
        }
Пример #5
0
        public static void AddIfcProjectUnitDict(IIfcUnit unitDef)
        {
            // Initialize the static Dicts, if it is still empty upon the first use. These Dicts do not need to be reset
            setupUnitRep();

            IfcUnitEnum        namedUnitType;
            IfcDerivedUnitEnum derivedUnitType;
            string             unitRepStr = string.Empty;

            try
            {
                if (unitDef is IIfcMonetaryUnit)
                {
                    m_IfcProjectMonetaryUnit = (unitDef as IIfcMonetaryUnit).Currency.ToString();
                }
                else if (unitDef is IIfcNamedUnit)
                {
                    if (getNamedUnitRepStr(unitDef, out namedUnitType, out unitRepStr))
                    {
                        if (!m_IfcProjectNamedUnitRep.ContainsKey(namedUnitType))
                        {
                            m_IfcProjectNamedUnitRep.Add(namedUnitType, unitRepStr);
                        }
                    }
                }
                else if (unitDef is IIfcDerivedUnit)
                {
                    if (getDerivedUnitRepStr(unitDef, out derivedUnitType, out unitRepStr))
                    {
                        if (!m_IfcProjectDerivedUnitRep.ContainsKey(derivedUnitType))
                        {
                            m_IfcProjectDerivedUnitRep.Add(derivedUnitType, unitRepStr);
                        }
                    }
                }
            }
            catch
            {
                // Ignore error (likely due to existing entry)
            }
        }
Пример #6
0
 public static string GetSymbol(IIfcUnit unit)
 {
     if (unit is IIfcDerivedUnit du)
     {
         return(GetSymbol(du));
     }
     else if (unit is IIfcContextDependentUnit cdu)
     {
         return(cdu.Name);
     }
     else if (unit is IIfcConversionBasedUnit cbu)
     {
         return(cbu.Name);
     }
     else if (unit is IIfcSIUnit si)
     {
         return(GetSymbol(si));
     }
     else if (unit is IIfcMonetaryUnit mu)
     {
         return(mu.Currency);
     }
     throw new ArgumentOutOfRangeException(nameof(unit), "Unexpected unit type " + unit.GetType().Name);
 }
Пример #7
0
        static bool getDerivedUnitRepStr(IIfcUnit unitDef, out IfcDerivedUnitEnum unitType, out string unitRepStr)
        {
            // Initialize the static Dicts, if it is still empty upon the first use. These Dicts do not need to be reset
            setupUnitRep();

            unitType = IfcDerivedUnitEnum.USERDEFINED;   // initial value
            IIfcDerivedUnit derivedUnit = unitDef as IIfcDerivedUnit;

            unitType = derivedUnit.UnitType;

            unitRepStr = string.Empty;
            IList <string> positiveExpUnits = new List <string>();
            IList <string> negativeExpUnits = new List <string>();

            foreach (IIfcDerivedUnitElement dUnitElem in derivedUnit.Elements)
            {
                IfcUnitEnum elemUnitType;
                string      elemUnitRepStr = string.Empty;
                int         exponent       = (int)dUnitElem.Exponent;
                if (getNamedUnitRepStr(dUnitElem.Unit, out elemUnitType, out elemUnitRepStr))
                {
                    if (exponent >= 0)
                    {
                        if (exponent > 1)
                        {
                            elemUnitRepStr = elemUnitRepStr + "^" + exponent.ToString();
                        }
                        //elemUnitRepStr = elemUnitRepStr + unicodeSuperScript(exponent);
                        positiveExpUnits.Add(elemUnitRepStr);
                    }
                    else
                    {
                        if (exponent < -1)
                        {
                            elemUnitRepStr = elemUnitRepStr + "^" + Math.Abs(exponent).ToString();
                        }
                        //elemUnitRepStr = elemUnitRepStr + unicodeSuperScript(Math.Abs(exponent));
                        negativeExpUnits.Add(elemUnitRepStr);
                    }
                }
            }

            if (positiveExpUnits.Count > 0)
            {
                foreach (string elemUnit in positiveExpUnits)
                {
                    BIMRLCommon.appendToString(elemUnit, ".", ref unitRepStr);
                    //BAIFCCommon.appendToString(elemUnit, "\u22C5", ref negUnitRepStr);
                }
            }
            else
            {
                if (negativeExpUnits.Count > 0)
                {
                    unitRepStr = "1";
                }
            }

            string negUnitRepStr = string.Empty;

            if (negativeExpUnits.Count > 0)
            {
                foreach (string elemUnit in negativeExpUnits)
                {
                    BIMRLCommon.appendToString(elemUnit, "·", ref negUnitRepStr);
                    //BAIFCCommon.appendToString(elemUnit, "\u22C5", ref negUnitRepStr);
                }
            }
            if (!string.IsNullOrEmpty(negUnitRepStr))
            {
                unitRepStr += "/" + negUnitRepStr;
            }

            if (!string.IsNullOrEmpty(unitRepStr))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #8
0
        private ValidationResult CheckUnit(string measureType, IIfcUnit unit, IPersistEntity entity)
        {
            if (string.IsNullOrWhiteSpace(measureType))
            {
                return(null);
            }

            measureType = measureType.ToUpperInvariant();

            // no kind is defined for some measures (boolean, text, monetary, date etc.)
            if (!MeasureUnitMaps.UnitKinds.TryGetValue(measureType, out UnitKind kind))
            {
                return(null);
            }

            switch (kind)
            {
            case UnitKind.IfcUnitEnum:
            {
                if (!MeasureUnitMaps.Units.TryGetValue(measureType, out IfcUnitEnum unitType))
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (unit != null)
                {
                    if (!(unit is IIfcNamedUnit namedUnit) || namedUnit.UnitType != unitType)
                    {
                        return(new ValidationResult
                            {
                                IssueType = ValidationFlags.Properties,
                                Item = entity,
                                Message = $"Named unit of type {unitType.ToString()} must be defined for {measureType}",
                                IssueSource = "Units to Measure Types validation"
                            });
                    }
                }
                else
                {
                    // try to find unit assignment
                    unit = entity.Model.Instances
                           .OfType <IIfcUnitAssignment>()
                           .Select(a => a.Units.FirstOrDefault <IIfcNamedUnit>(u => u.UnitType == unitType))
                           .FirstOrDefault();
                    if (unit == null)
                    {
                        return(new ValidationResult
                            {
                                IssueType = ValidationFlags.Properties,
                                Item = entity,
                                Message = $"Named unit of type {unitType.ToString()} must be defined for {measureType} but it was not found directly or in global units assignment.",
                                IssueSource = "Units to Measure Types validation"
                            });
                    }
                }
            }
            break;

            case UnitKind.IfcDerivedUnitEnum:
            {
                if (!MeasureUnitMaps.DerivedUnits.TryGetValue(measureType, out IfcDerivedUnitEnum unitType))
                {
                    throw new ArgumentOutOfRangeException();
                }
                if (unit != null)
                {
                    if (!(unit is IIfcDerivedUnit derivedUnit) || derivedUnit.UnitType != unitType)
                    {
                        return(new ValidationResult
                            {
                                IssueType = ValidationFlags.Properties,
                                Item = entity,
                                Message = $"Derived unit of type {unitType.ToString()} must be defined for {measureType}",
                                IssueSource = "Units to Measure Types validation"
                            });
                    }
                }
                else
                {
                    // try to find unit assignment
                    unit = entity.Model.Instances
                           .OfType <IIfcUnitAssignment>()
                           .Select(a => a.Units.FirstOrDefault <IIfcDerivedUnit>(u => u.UnitType == unitType))
                           .FirstOrDefault();
                    if (unit == null)
                    {
                        return(new ValidationResult
                            {
                                IssueType = ValidationFlags.Properties,
                                Item = entity,
                                Message = $"Derived unit of type {unitType.ToString()} must be defined for {measureType} but it was not found directly or in global units assignment.",
                                IssueSource = "Units to Measure Types validation"
                            });
                    }
                }
            }
            break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(null);
        }