Exemple #1
0
 public WorkAreaViewModel(NodeTypesManager _nodeTypes)
 {
     Scheme      = new Main.Sceme.Scheme("test");
     Nodes       = new ObservableCollection <NodeViewModel>();
     Connections = new ObservableCollection <object>();
     NodeTypes   = _nodeTypes;
 }
Exemple #2
0
        /// <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);
            }
        }