예제 #1
0
        /// <summary>
        /// Replace the object specified by 'path' in 'newSimulation' with the specified 'value'
        /// </summary>
        private static void ApplyModelReplacement(Simulation newSimulation, string path, IModel value)
        {
            IModel newModel = Apsim.Clone(value);
            IModel modelToReplace = newSimulation.Get(path) as IModel;
            if (modelToReplace == null)
                throw new Exception("Cannot find model to replace. Model path: " + path);

            int index = modelToReplace.Parent.Children.IndexOf(modelToReplace as Model);
            if (index == -1)
                throw new Exception("Cannot find model to replace. Model path: " + path);

            modelToReplace.Parent.Children.RemoveAt(index);
            modelToReplace.Parent.Children.Insert(index, newModel as Model);
            newModel.Name = modelToReplace.Name;
            newModel.Parent = modelToReplace.Parent;

            Apsim.CallEventHandler(newModel, "Loaded", null);
        }
예제 #2
0
 /// <summary>
 /// Use the name of this object as a value to insert into the specified 'newSimulation'
 /// </summary>
 private static void ApplyStringAsValue(Simulation newSimulation, string path, string name)
 {
     object originalValue = newSimulation.Get(path);
     object newValue;
     if (originalValue is DateTime)
         newValue = DateTime.Parse(name, CultureInfo.InvariantCulture);
     else if (originalValue is float)
         newValue = Convert.ToSingle(name, CultureInfo.InvariantCulture);
     else if (originalValue is double)
         newValue = Convert.ToDouble(name, CultureInfo.InvariantCulture);
     else if (originalValue is int)
         newValue = Convert.ToInt32(name, CultureInfo.InvariantCulture);
     else if (originalValue is string)
         newValue = Convert.ToString(name);
     else
         newValue = name;
     newSimulation.Set(path, newValue);
 }