Example #1
0
        //------------------------------------
        public override string ToString()
        {
            if (m_strFormat.Length > 0)
            {
                return(ToString(m_strFormat));
            }
            IUnite unite = CGestionnaireUnites.GetUnite(Unite);

            if (unite != null)
            {
                return(Valeur.ToString() + unite.LibelleCourt);
            }
            return(Valeur.ToString() + Unite);
        }
Example #2
0
        public CValeurUnite ConvertTo(string strUnite)
        {
            if (strUnite == Unite)
            {
                return(this);
            }
            string strMyCle    = CUtilUnite.GetIdClasseUnite(Unite);
            string strAutrecle = CUtilUnite.GetIdClasseUnite(strUnite);

            if (strMyCle == null || strAutrecle == null || strMyCle != strAutrecle)
            {
                throw new Exception(I.T("Can not convert from @1 to @2|20017", Unite, strUnite));
            }
            double fValeur = ConvertToBase();

            string[] strComposants = CUtilUnite.Developpe(strUnite);
            foreach (string strComposant in strComposants)
            {
                string strIdUnite = strComposant;
                bool   bDiviser   = false;
                if (strComposant[0] == '/')
                {
                    bDiviser   = true;
                    strIdUnite = strComposant.Substring(1);
                }
                IUnite unite = CGestionnaireUnites.GetUnite(strIdUnite);
                if (unite == null)
                {
                    return(this);
                }
                if (bDiviser)
                {
                    fValeur *= unite.FacteurVersBase;
                }
                else
                {
                    fValeur /= unite.FacteurVersBase;
                }
            }
            return(new CValeurUnite(fValeur, CUtilUnite.Factorise(strComposants)));
        }
Example #3
0
        //-------------------------------------------------------
        public static string GetIdClasseUnite(string strUnite)
        {
            string[]      strUnits   = Developpe(strUnite);
            List <string> lstClasses = new List <string>();
            StringBuilder bl         = new StringBuilder();

            foreach (string strCompo in strUnits)
            {
                bool   bDiviser = false;
                string strCle   = strCompo;
                if (strCompo[0] == '/')
                {
                    bDiviser = true;
                    strCle   = strCompo.Substring(1);
                }
                IUnite unite = CGestionnaireUnites.GetUnite(strCle);
                if (unite == null || unite.Classe == null)
                {
                    return(null);
                }
                string strClasse = unite.Classe.GlobalId;
                if (bDiviser)
                {
                    strClasse = "/" + strClasse;
                }
                else
                {
                    strClasse = "." + strClasse;
                }
                lstClasses.Add(strClasse);
            }
            lstClasses.Sort();
            foreach (string str in lstClasses)
            {
                bl.Append(str);
            }
            return(bl.ToString());
        }
Example #4
0
        //------------------------------------
        public static CValeurUnite GetValeurFromValeurBase(double fValeurDeBase, string strUnite)
        {
            double fValeur = fValeurDeBase;

            string[] strComposants = CUtilUnite.Developpe(strUnite);
            foreach (string strComposant in strComposants)
            {
                string strIdUnite = strComposant;
                bool   bDiviser   = false;
                if (strComposant[0] == '/')
                {
                    bDiviser   = true;
                    strIdUnite = strComposant.Substring(1);
                }
                if (strComposant[0] == '.')
                {
                    strIdUnite = strComposant.Substring(1);
                }
                IUnite unite = CGestionnaireUnites.GetUnite(strIdUnite);
                if (unite == null)
                {
                    return(null);
                }
                if (bDiviser)
                {
                    fValeur *= unite.FacteurVersBase;
                }
                else
                {
                    fValeur /= unite.FacteurVersBase;
                }
                if (strComposant.Length == 1)
                {
                    fValeur -= unite.OffsetVersBase;
                }
            }
            return(new CValeurUnite(fValeur, strUnite));
        }
Example #5
0
        //------------------------------------
        public double ConvertToBase()
        {
            double fValeur = Valeur;

            string[] strComposants = CUtilUnite.Developpe(Unite);
            foreach (string strComposant in strComposants)
            {
                string strIdUnite = strComposant;
                bool   bDiviser   = false;
                if (strComposant[0] == '/')
                {
                    bDiviser   = true;
                    strIdUnite = strComposant.Substring(1);
                }
                if (strComposant[0] == '.')
                {
                    strIdUnite = strComposant.Substring(1);
                }
                IUnite unite = CGestionnaireUnites.GetUnite(strIdUnite);
                if (unite == null)
                {
                    return(Valeur);
                }
                if (bDiviser)
                {
                    fValeur /= unite.FacteurVersBase;
                }
                else
                {
                    fValeur *= unite.FacteurVersBase;
                }
                if (strComposants.Length == 1)
                {
                    fValeur += unite.OffsetVersBase;
                }
            }
            return(fValeur);
        }
Example #6
0
 //--------------------------------------
 public static void Refresh()
 {
     m_instance = null;
 }
Example #7
0
        //------------------------------------
        //Se débrouille pour que les éléments qui ont la même classe d'unité travaillent dans
        //la même unité
        public static string GetUniteHarmonisee(string strUniteReference, string strUniteAHarmoniser)
        {
            string[]         strComposantsRef         = Developpe(strUniteReference);
            string[]         strComposantsAHarmoniser = Developpe(strUniteAHarmoniser);
            List <string>    lstAHarmoniser           = new List <string>(strComposantsAHarmoniser);
            List <string>    lstFinale     = new List <string>();
            HashSet <string> classesFaites = new HashSet <string>();
            HashSet <int>    setFaits      = new HashSet <int>();

            foreach (string strCompoRef in strComposantsRef)
            {
                string strRef = strCompoRef;
                if (strRef[0] == '/')
                {
                    strRef = strRef.Substring(1);
                }
                IUnite u1 = CGestionnaireUnites.GetUnite(strRef);
                if (u1 == null)
                {
                    return(strUniteAHarmoniser);
                }
                IClasseUnite classe = u1.Classe;
                if (!classesFaites.Contains(classe.GlobalId))
                {
                    classesFaites.Add(classe.GlobalId);
                    for (int n = 0; n < lstAHarmoniser.Count; n++)
                    {
                        if (!setFaits.Contains(n))
                        {
                            string strHar   = lstAHarmoniser[n];
                            bool   bDiviser = false;
                            if (strHar[0] == '/')
                            {
                                bDiviser = true;
                                strHar   = strHar.Substring(1);
                            }
                            IUnite u2 = CGestionnaireUnites.GetUnite(strHar);
                            if (u2 == null)
                            {
                                return(strUniteAHarmoniser);
                            }
                            if (u2.Classe.GlobalId == u1.Classe.GlobalId)
                            {
                                setFaits.Add(n);
                                if (strHar != strRef)
                                {
                                    string strTmp = strRef;
                                    if (bDiviser)
                                    {
                                        strTmp = "/" + strRef;
                                    }
                                    lstAHarmoniser[n] = strTmp;
                                }
                            }
                        }
                    }
                }
            }
            StringBuilder bl = new StringBuilder();

            foreach (string strCompo in lstAHarmoniser)
            {
                if (bl.Length > 0 && strCompo[0] != '/')
                {
                    bl.Append('.');
                }
                bl.Append(strCompo);
            }
            return(bl.ToString());
        }
Example #8
0
        //------------------------------------
        //Se débrouille pour que chaque classe d'unité ne soit
        //représentée que par une seule et même unité
        public static string Harmonise(string strUnite)
        {
            string[]      strComposants = Developpe(strUnite);
            List <string> lstComposants = new List <string>(strComposants);
            HashSet <int> setFaits      = new HashSet <int>();

            for (int nComposant = 0; nComposant < lstComposants.Count; nComposant++)
            {
                if (!setFaits.Contains(nComposant))
                {
                    setFaits.Add(nComposant);
                    string strCompo1 = lstComposants[nComposant];
                    if (strCompo1[0] == '/')
                    {
                        strCompo1 = strCompo1.Substring(1);
                    }
                    IUnite u1 = CGestionnaireUnites.GetUnite(strCompo1);
                    if (u1 == null)
                    {
                        return(strUnite);
                    }
                    //Cherche les autres éléments de la même classe
                    for (int n = nComposant + 1; n < lstComposants.Count; n++)
                    {
                        if (!setFaits.Contains(n))
                        {
                            string strCompo2 = lstComposants[n];
                            bool   bDiviser  = false;
                            if (strCompo2[0] == '/')
                            {
                                bDiviser  = true;
                                strCompo2 = strCompo2.Substring(1);
                            }
                            IUnite u2 = CGestionnaireUnites.GetUnite(strCompo2);
                            if (u2 == null)
                            {
                                return(strUnite);
                            }
                            if (u2.Classe.GlobalId == u1.Classe.GlobalId)
                            {
                                setFaits.Add(n);
                                if (strCompo2 != strCompo1)
                                {
                                    string strTmp = strCompo1;
                                    if (bDiviser)
                                    {
                                        strTmp = "/" + strTmp;
                                    }
                                    lstComposants[n] = strTmp;
                                }
                            }
                        }
                    }
                }
            }
            StringBuilder bl = new StringBuilder();

            foreach (string strCompo in lstComposants)
            {
                if (bl.Length > 0 && strCompo[0] != '/')
                {
                    bl.Append('.');
                }
                bl.Append(strCompo);
            }
            return(bl.ToString());
        }
Example #9
0
        //------------------------------------
        /// <summary>
        /// Décompose une valeur simple dans les unités de l'unité.
        /// Par exemple, 65 min décomposé en h min donne 1h5min
        /// </summary>
        /// <param name="strUnitesAUtiliser"></param>
        /// <returns></returns>
        public string GetStringDecomposeeValeurSimple(params string[] strUnitesAUtiliser)
        {
            List <string> strUnitesCorrigées = new List <string>();

            foreach (string strUnite in strUnitesAUtiliser)
            {
                if (strUnite.Length > 0)
                {
                    strUnitesCorrigées.Add(strUnite.Trim());
                }
            }
            IUnite unite = CGestionnaireUnites.GetUnite(Unite);

            if (unite == null)
            {
                return(null);
            }
            IClasseUnite classe = unite.Classe;

            if (classe == null)
            {
                return(null);
            }
            List <double?> lstVals         = new List <double?>();
            double         fValeurRestante = ConvertToBase();
            StringBuilder  bl = new StringBuilder();

            CValeurUnite valeurTmp = new CValeurUnite(Math.Abs(fValeurRestante), classe.UniteBase);

            for (int nUnite = 0; nUnite < strUnitesCorrigées.Count; nUnite++)
            {
                string strUnite = strUnitesCorrigées[nUnite];
                string strLib   = strUnite;
                IUnite u        = CGestionnaireUnites.GetUnite(strUnite);
                if (u != null)
                {
                    strLib = u.LibelleCourt;
                }
                valeurTmp = valeurTmp.ConvertTo(strUnite);

                int nVal = (int)valeurTmp.Valeur;
                if (nUnite < strUnitesCorrigées.Count - 1)
                {
                    valeurTmp = valeurTmp - (double)nVal;
                    if (nVal > 0)
                    {
                        bl.Append(nVal + strLib + " ");
                    }
                    if (valeurTmp.Valeur == 0)
                    {
                        break;
                    }
                }
                else
                {
                    if (valeurTmp.Valeur != 0)
                    {
                        bl.Append(Math.Round(valeurTmp.Valeur, 5).ToString() + strLib);
                    }
                }
            }
            if (Valeur < 0)
            {
                bl.Insert(0, "-");
            }
            return(bl.ToString());
        }