Esempio n. 1
0
        /// <summary>Adds a new model (as specified by the xml node) to the specified parent.</summary>
        /// <param name="parent">The parent to add the model to</param>
        /// <param name="node">The XML representing the new model</param>
        /// <returns>The newly created model.</returns>
        public static IModel Add(IModel parent, XmlNode node)
        {
            IModel modelToAdd = XmlUtilities.Deserialise(node, Assembly.GetExecutingAssembly()) as Model;

            // Get all child models
            List <IModel> modelsToNotify = Apsim.ChildrenRecursively(modelToAdd);

            // Call deserialised in all models.
            object[] args = new object[] { true };
            CallEventHandler(modelToAdd, "Deserialised", args);
            foreach (IModel modelToNotify in modelsToNotify)
            {
                CallEventHandler(modelToNotify, "Deserialised", args);
            }

            // Corrently parent all models.
            modelToAdd.Parent = parent;
            Apsim.ParentAllChildren(modelToAdd);
            parent.Children.Add(modelToAdd as Model);

            // Ensure the model name is valid.
            Apsim.EnsureNameIsUnique(modelToAdd);

            // Call OnLoaded
            Apsim.CallEventHandler(modelToAdd, "Loaded", null);
            foreach (IModel child in modelsToNotify)
            {
                Apsim.CallEventHandler(child, "Loaded", null);
            }

            Locator(parent).Clear();

            return(modelToAdd);
        }
Esempio n. 2
0
        /// <summary>Adds a new model (as specified by the xml node) to the specified parent.</summary>
        /// <param name="parent">The parent to add the model to</param>
        /// <param name="node">The XML representing the new model</param>
        /// <returns>The newly created model.</returns>
        public static IModel Add(IModel parent, XmlNode node)
        {
            IModel modelToAdd = XmlUtilities.Deserialise(node, Assembly.GetExecutingAssembly()) as Model;

            // Call deserialised
            Events events = new Events(modelToAdd);

            object[] args = new object[] { true };
            events.Publish("Deserialised", args);

            // Correctly parent all models.
            Add(parent, modelToAdd);

            // Ensure the model name is valid.
            Apsim.EnsureNameIsUnique(modelToAdd);

            // Call OnLoaded
            LoadedEventArgs loadedArgs = new LoadedEventArgs();

            events.Publish("Loaded", new object[] { modelToAdd, loadedArgs });

            Locator(parent).Clear();

            return(modelToAdd);
        }