예제 #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);
            newSimulation.Locator().Clear();
            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);
        }