public TECSystem AddInstance() { Dictionary <Guid, Guid> guidDictionary = new Dictionary <Guid, Guid>(); var newSystem = new TECSystem(); newSystem.CopyPropertiesFromScope(this); if (this.Location != null) { newSystem.Location = this.Location; } foreach (TECEquipment equipment in Equipment) { var toAdd = new TECEquipment(equipment, guidDictionary, TypicalInstanceDictionary); TypicalInstanceDictionary.AddItem(equipment, toAdd); newSystem.Equipment.Add(toAdd); } foreach (TECController controller in Controllers) { var toAdd = controller.CopyController(guidDictionary); TypicalInstanceDictionary.AddItem(controller, toAdd); newSystem.AddController(toAdd); } foreach (TECPanel panel in Panels) { var toAdd = new TECPanel(panel, guidDictionary); TypicalInstanceDictionary.AddItem(panel, toAdd); newSystem.Panels.Add(toAdd); } foreach (TECMisc misc in MiscCosts) { var toAdd = new TECMisc(misc); TypicalInstanceDictionary.AddItem(misc, toAdd); newSystem.MiscCosts.Add(toAdd); } foreach (TECScopeBranch branch in ScopeBranches) { var toAdd = new TECScopeBranch(branch); TypicalInstanceDictionary.AddItem(branch, toAdd); newSystem.ScopeBranches.Add(toAdd); } //Proposal items are not currently synced to instances //foreach(TECProposalItem propItem in ProposalItems) //{ // var toAdd = new TECProposalItem(propItem); // TypicalInstanceDictionary.AddItem(propItem, toAdd); // newSystem.ProposalItems.Add(toAdd); //} ModelLinkingHelper.LinkSystem(newSystem, guidDictionary); Instances.Add(newSystem); return(newSystem); }
private void handleAdd(TECChangedEventArgs args) { ITypicalable sender = args.Sender as ITypicalable; List <ITECObject> parentInstances = new List <ITECObject>(); if (args.Sender is TECTypical typ) { parentInstances.AddRange(Instances); } else { parentInstances = TypicalInstanceDictionary.GetInstances(sender as ITECObject); } foreach (ITECObject parentInstance in parentInstances) { ITypicalable instanceSender = parentInstance as ITypicalable; if (instanceSender == null) { throw new Exception("Change occured from object which is not typicalable"); } ITECObject instanceValue = args.Value as ITECObject; if (instanceValue == null) { throw new Exception("Value to add is not ITECObject"); } if (args.Value is ITypicalable typicalChild) { if (sender is IRelatable relSender && relSender.IsDirectChildProperty(args.PropertyName)) { instanceValue = typicalChild.CreateInstance(TypicalInstanceDictionary); if (instanceValue != null) { TypicalInstanceDictionary.AddItem(args.Value as ITECObject, instanceValue); } } else { var parentSystem = this.Instances .Where(x => x.IsDirectDescendant(parentInstance)) .FirstOrDefault(); instanceValue = this.TypicalInstanceDictionary.GetInstances(typicalChild) .Where(x => parentSystem.IsDirectDescendant(x)).FirstOrDefault(); } } if (instanceValue != null) { instanceSender.AddChildForProperty(args.PropertyName, instanceValue); } }