예제 #1
0
        public void SetFormula(int srcIndex, int trgIndex, IBxFormula fml)
        {
            if (srcIndex == trgIndex)
            {
                return;
            }
            BxSrcUnitFormulas formulas = _formulas[srcIndex];

            formulas.SetFormula(trgIndex, fml);
        }
예제 #2
0
        public void SetFormula(string srcUnit, string trgUnit, IBxFormula fml)
        {
            if (srcUnit == trgUnit)
            {
                return;
            }
            IBxIndexedUnit src = ParseEx(srcUnit);
            IBxIndexedUnit trg = ParseEx(trgUnit);

            if ((src == null) || (trg == null))
            {
                return;
            }
            BxSrcUnitFormulas formulas = _formulas[src.Index];

            formulas.SetFormula(trg.Index, fml);
        }
예제 #3
0
        public IBxFormula GetFormula(int srcIndex, int trgIndex)
        {
            if (srcIndex == trgIndex)
            {
                return(ParsedFormula.s_sameUnitFormula);
            }

            BxSrcUnitFormulas formulas = _formulas[srcIndex];
            IBxFormula        fml      = formulas.GetFormula(trgIndex);

            if (fml == null)
            {
                //取反向的转换关系
                ParsedFormula tempFml = _formulas[trgIndex].GetFormula(srcIndex) as ParsedFormula;
                if (tempFml != null)
                {
                    fml = ParsedFormula.Inverse(tempFml);
                    formulas.SetFormula(trgIndex, fml);
                }
            }
            if (fml == null)
            {
                if ((trgIndex == _nDefaultUnitIndex) || (srcIndex == _nDefaultUnitIndex))
                {
                    throw new Exception(string.Format("缺乏单位{0}和主单位{1}之间的转换关系\n", this[srcIndex].ID, this[trgIndex].ID));
                }
                else
                {
                    IBxFormula fml1 = GetFormula(srcIndex, _nDefaultUnitIndex);
                    IBxFormula fml2 = GetFormula(_nDefaultUnitIndex, trgIndex);
                    fml = new CombinedFormula(fml1, fml2);
                    formulas.SetFormula(trgIndex, fml);
                }
            }
            return(fml);
        }
예제 #4
0
        public void LoadUnitConfigNode(XmlElement cateNode)
        {
            FormulaParser parser = new FormulaParser();

            try
            {
                _id   = cateNode.GetAttribute("id");
                _code = cateNode.GetAttribute("code");

                XmlElement  unitsNode     = (XmlElement)cateNode.SelectSingleNode("Units");
                XmlNodeList unitsNodeList = cateNode.SelectSingleNode("Units").ChildNodes;
                int         count         = unitsNodeList.Count;
                _units    = new CategoryUnits(count);
                _formulas = new BxSrcUnitFormulas[count];
                int    nIndex = 0;
                string id     = null;
                string code   = null;
                int    dd     = 0;
                foreach (XmlElement one in unitsNodeList)
                {
                    id   = one.GetAttribute("id");
                    code = one.GetAttribute("code");
                    if (!Int32.TryParse(one.GetAttribute("decimalDigits"), NumberStyles.Any, CultureInfo.InvariantCulture, out dd))
                    {
                        //throw new Exception("no decimal digis in unit " + id + " !");
                    }

                    _units[nIndex]    = new BxUnit(id, code, dd, this, nIndex);
                    _formulas[nIndex] = new BxSrcUnitFormulas(nIndex, count);
                    nIndex++;
                }
            }
            catch (System.Exception) { }

            string srcUnit;
            string trgUnit;

            try
            {
                XmlElement formulasNode = (XmlElement)cateNode.SelectSingleNode("Formulas");
                if (formulasNode == null)
                {
                    return;
                }
                string primaryUnitID = formulasNode.GetAttribute("primaryUnit");
                if (string.IsNullOrEmpty(primaryUnitID))
                {
                    return;
                }

                _nDefaultUnitIndex = ParseEx(primaryUnitID).Index;

                XmlNodeList formulaNodeList = formulasNode.ChildNodes;

                foreach (XmlElement one in formulaNodeList)
                {
                    srcUnit        = one.GetAttribute("src");
                    trgUnit        = one.GetAttribute("trg");
                    parser.Formula = one.GetAttribute("convertion");
                    parser.Variant = srcUnit;
                    SetFormula(srcUnit, trgUnit, new ParsedFormula(parser.Parse()));
                }
            }
            catch (System.Exception) { }
        }