public NodeViewModel(Main.Nodes.Node model, double left, double top, WorkAreaViewModel parent) { Parent = parent; NodeModel = model; Name = NodeModel.Name; NodeTypeName = model.GetType().GetField("typeName").GetValue(null) as string; IsUpToDate = false; // Get ports var _ports = NodeModel.PortManager.GetPorts(); InputPorts = new ObservableCollection <InputPortViewModel>(); OutputPorts = new ObservableCollection <OutputPortViewModel>(); foreach (Main.Ports.Port p in _ports) { switch (p.PortType) { case Main.Ports.PortType.Input: InputPortViewModel inst = new InputPortViewModel(p, this); inst.Port.ValueManager.UpToDateChanged += ValueManager_UpToDateChanged; InputPorts.Add(inst); break; case Main.Ports.PortType.Output: OutputPortViewModel inst2 = new OutputPortViewModel(p, this); inst2.Port.ValueManager.UpToDateChanged += ValueManager_UpToDateChanged; OutputPorts.Add(inst2); break; } } Left = left; Top = top; }
/// <summary> /// Deserialize object /// </summary> public void Load(Stream stream, NodeTypesManager _nodeTypes) { this.Connections.Clear(); this.Nodes.Clear(); XmlDocument doc = new XmlDocument(); doc.Load(stream); Scheme = new Main.Sceme.Scheme(doc.DocumentElement.Attributes["name"].Value); List <Tuple <string, string> > _connectionsToCreate = new List <Tuple <string, string> >(); // First create all nodes foreach (XmlNode t in doc.DocumentElement.ChildNodes) { string nodeName = t.Attributes["name"].Value; string nodeType = t.Attributes["nodeType"].Value; string x = t.Attributes["x"].Value; string y = t.Attributes["y"].Value; var node = CreateNode(NodeTypes.GetNodeType(nodeType), Convert.ToDouble(x), Convert.ToDouble(y), false, nodeName); foreach (XmlNode z in t.ChildNodes) { string portName = z.Name; string value = z.Attributes["value"] == null ? null : z.Attributes["value"].Value; string connected = z.Attributes["connected"] == null ? null : z.Attributes["connected"].Value; if (connected != null) { _connectionsToCreate.Add(new Tuple <string, string>(nodeName + "+" + portName, connected)); } else { // We can set const value here InputPortViewModel port = node.InputPorts.FirstOrDefault((u) => u.PortName == portName); if (port.Port.DataType == typeof(bool)) { port.Val = Convert.ToBoolean(value); continue; } if (port.Port.DataType == typeof(int)) { port.Val = Convert.ToInt32(value); continue; } throw new ArgumentException(string.Format("{0} can't be desirialized", value.GetType())); } } } // Create connections (all nodes are created before) foreach (Tuple <string, string> conn in _connectionsToCreate) { string firstPortNode = conn.Item1.Split('+')[0]; string firstPort = conn.Item1.Split('+')[1]; string secondPortNode = conn.Item2.Split('+')[0]; string secondPort = conn.Item2.Split('+')[1]; PortViewModel first = Nodes.FirstOrDefault((r) => r.Name == firstPortNode).InputPorts.FirstOrDefault((u) => u.PortName == firstPort); if (first == null) { first = Nodes.FirstOrDefault((r) => r.Name == firstPortNode).OutputPorts.FirstOrDefault((u) => u.PortName == firstPort); } PortViewModel second = Nodes.FirstOrDefault((r) => r.Name == secondPortNode).InputPorts.FirstOrDefault((u) => u.PortName == secondPort); if (second == null) { second = Nodes.FirstOrDefault((r) => r.Name == secondPortNode).OutputPorts.FirstOrDefault((u) => u.PortName == secondPort); } CreateConnection(first, second); } }