public Boolean UnitSet(IUnitSystem unitSystem, String unitName, Quantity unitValue, String unitSymbol, out INametableItem unitItem, out string errorMessage)
        {
            // Find identifier
            Boolean Found = FindIdentifier(unitName, out IEnvironment context, out unitItem);

            if (Found && (context == this) && (unitItem.Identifierkind != IdentifierKind.Unit))
            {   // Found locally, but is not a unit; Can't set as unit
                errorMessage = unitName + " is found locally but is not a unit; Can't set as unit";
                return(false);
            }

            if (context == null)
            {
                context = this;
            }

            (bool OK, String errormessage)updateRes;
            if (unitItem == null)
            {
                // Either is identifier an unit in some context; set it to specified value
                // or identifier not found; No local identifier with that name, Declare local unit
                if (unitSystem == null)
                {
                    if (unitValue != null && unitValue.Unit != null)
                    {   // Is same system as values unit
                        unitSystem = unitValue.Unit.ExponentsSystem;
                    }

                    /**
                     * if (unitSystem == null)
                     * {   // Is first unit in a new system
                     *  if (SystemSet(unitName + "_system", out INametableItem SystemItem))
                     *  {
                     *      unitSystem = ((NamedSystem)SystemItem).UnitSystem;
                     *  }
                     * }
                     **/
                }
                unitItem  = new NamedUnit(unitSystem, unitName, unitSymbol, unitValue, this);
                updateRes = (true, "");
            }
            else
            {
                NamedUnit nui = (NamedUnit)unitItem;
                updateRes = nui.UpdateUnit(unitName, unitValue);
            }
            errorMessage = updateRes.errormessage;
            if (!updateRes.OK)
            {
                return(false);
            }

            return(context.SetLocalIdentifier(unitName, unitItem));
        }
        public Boolean UnitGet(String unitName, out Unit unitValue, ref String resultLine)
        {
            // Find identifier
            Boolean Found = FindIdentifier(unitName, out var context, out var Item);

            if (Found && Item.Identifierkind == IdentifierKind.Unit)
            {   // Identifier is a unit in some context; Get it
                NamedUnit nu = Item as NamedUnit;

                unitValue = nu.pu;
                return(true);
            }
            else
            {
                unitValue  = null;
                resultLine = "Unit '" + unitName + "' not found";
                return(false);
            }
        }
Exemplo n.º 3
0
        public Boolean UnitSet(IUnitSystem unitSystem, String unitName, Quantity unitValue, out INametableItem unitItem)
        {
            // Find identifier
            IEnvironment context;
            Boolean      Found = FindIdentifier(unitName, out context, out unitItem);

            if (Found && (context == this) && (unitItem.Identifierkind != IdentifierKind.Unit))
            {   // Found locally but is not a unit; Can't set as unit
                return(false);
            }

            if (context == null)
            {
                context = this;
            }

            // Either is identifier an unit in some context; set it to specified value
            // or identifier not found; No local identifier with that name, Declare local unit
            if (unitSystem == null)
            {
                if (unitValue != null && unitValue.Unit != null)
                {   // Is same system as values unit
                    unitSystem = unitValue.Unit.ExponentsSystem;
                }

                if (unitSystem == null)
                {   // Is first unit in a new system
                    INametableItem SystemItem;
                    if (SystemSet(unitName + "_system", out SystemItem))
                    {
                        unitSystem = ((NamedSystem)SystemItem).UnitSystem;
                    }
                }
            }

            unitItem = new NamedUnit(unitSystem, unitName, unitValue, this);
            return(context.SetLocalIdentifier(unitName, unitItem));
        }