private void CleanupOldPorts() { //clear all the inputs but the first one //which is the family instance //first kill all the connectors for (int i = 1; i < InPortData.Count; i++) { dynPort p = InPorts[i]; //must remove the connectors iteratively //do not use a foreach here! while (p.Connectors.Count > 0) { dynConnector c = p.Connectors[p.Connectors.Count - 1] as dynConnector; c.Kill(); } } //then remove all the ports while (InPorts.Count > 1) { InPorts.RemoveAt(InPorts.Count - 1); InPortData.RemoveAt(InPortData.Count - 1); } while (gridLeft.RowDefinitions.Count > 1) { //remove the port from the children list gridLeft.Children.RemoveAt(gridLeft.RowDefinitions.Count - 1); gridLeft.RowDefinitions.RemoveAt(gridLeft.RowDefinitions.Count - 1); } }
void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseEventArgs e) { //Keyboard.Focus(this); hitResultsList.Clear(); TestClick(e.GetPosition(workBench)); #region test for a port dynPort p = null; if (hitResultsList.Count > 0) { foreach (DependencyObject depObj in hitResultsList) { //traverse the tree through all the //hit elements to see if you get a port p = ElementClicked(depObj, typeof(dynPort)) as dynPort; if (p != null) { break; } } } if (p != null) { if (!isConnecting) { //test if port already has a connection if so grab it //and begin connecting to somewhere else //don't allow the grabbing of the start connector if (p.Connectors.Count > 0 && p.Connectors[0].Start != p) { activeConnector = p.Connectors[0]; activeConnector.Disconnect(p); isConnecting = true; workBench.isConnecting = true; } else { try { //you've begun creating a connector dynConnector c = new dynConnector(p, workBench, e.GetPosition(workBench)); activeConnector = c; isConnecting = true; workBench.isConnecting = true; } catch (Exception ex) { Debug.WriteLine(ex.Message); } } } else { //attempt a connection between the port //and the connector if (!activeConnector.Connect(p)) { activeConnector.Kill(); isConnecting = false; workBench.isConnecting = false; activeConnector = null; } else { //you've already started connecting //now you're going to stop isConnecting = false; workBench.isConnecting = false; activeConnector = null; } } //set the handled flag so that the element doesn't get dragged e.Handled = true; } else { //if you click on the canvas and you're connecting //then drop the connector, otherwise do nothing if (activeConnector != null) { activeConnector.Kill(); isConnecting = false; workBench.isConnecting = false; activeConnector = null; } } #endregion #region test for canvas hitResultsList.Clear(); TestClick(e.GetPosition(workBench)); DragCanvas dc = null; if (hitResultsList.Count > 0) { foreach (DependencyObject depObj in hitResultsList) { //traverse the tree through all the //hit elements to see if you get a port dc = ElementClicked(depObj, typeof(DragCanvas)) as DragCanvas; if (dc != null) { Debug.WriteLine("Canvas clicked"); ClearSelection(); break; } } } #endregion #region test for dyn element hitResultsList.Clear(); TestClick(e.GetPosition(workBench)); dynElement element = null; if (hitResultsList.Count > 0) { foreach (DependencyObject depObj in hitResultsList) { //traverse the tree through all the //hit elements to see if you get an element element = ElementClicked(depObj, typeof(dynElement)) as dynElement; if (element != null) { SelectElement(element); break; } } } #endregion }
bool OpenWorkbench(string xmlPath) { Log("Opening workbench " + xmlPath + "..."); CleanWorkbench(); try { #region read xml file XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlPath); XmlNodeList elNodes = xmlDoc.GetElementsByTagName("dynElements"); XmlNodeList cNodes = xmlDoc.GetElementsByTagName("dynConnectors"); XmlNode elNodesList = elNodes[0] as XmlNode; XmlNode cNodesList = cNodes[0] as XmlNode; foreach (XmlNode elNode in elNodesList.ChildNodes) { XmlAttribute typeAttrib = elNode.Attributes[0]; XmlAttribute guidAttrib = elNode.Attributes[1]; XmlAttribute nicknameAttrib = elNode.Attributes[2]; XmlAttribute xAttrib = elNode.Attributes[3]; XmlAttribute yAttrib = elNode.Attributes[4]; string typeName = typeAttrib.Value.ToString(); Guid guid = new Guid(guidAttrib.Value.ToString()); string nickname = nicknameAttrib.Value.ToString(); double x = Convert.ToDouble(xAttrib.Value.ToString()); double y = Convert.ToDouble(yAttrib.Value.ToString()); Type t = Type.GetType(typeName); dynElement el = AddDynElement(t, nickname, guid, x, y); //read the sub elements //set any numeric values foreach (XmlNode subNode in elNode.ChildNodes) { if (subNode.Name == "System.Double") { double val = Convert.ToDouble(subNode.Attributes[0].Value); el.OutPortData[0].Object = val; el.Update(); } else if (subNode.Name == "System.Int32") { int val = Convert.ToInt32(subNode.Attributes[0].Value); el.OutPortData[0].Object = val; el.Update(); } } } dynElementSettings.SharedInstance.Workbench.UpdateLayout(); foreach (XmlNode connector in cNodesList.ChildNodes) { XmlAttribute guidStartAttrib = connector.Attributes[0]; XmlAttribute intStartAttrib = connector.Attributes[1]; XmlAttribute guidEndAttrib = connector.Attributes[2]; XmlAttribute intEndAttrib = connector.Attributes[3]; XmlAttribute portTypeAttrib = connector.Attributes[4]; Guid guidStart = new Guid(guidStartAttrib.Value.ToString()); Guid guidEnd = new Guid(guidEndAttrib.Value.ToString()); int startIndex = Convert.ToInt16(intStartAttrib.Value.ToString()); int endIndex = Convert.ToInt16(intEndAttrib.Value.ToString()); int portType = Convert.ToInt16(portTypeAttrib.Value.ToString()); //find the elements to connect dynElement start = null; dynElement end = null; foreach (dynElement e in dynElementSettings.SharedInstance.Bench.Elements) { if (e.GUID == guidStart) { start = e; } else if (e.GUID == guidEnd) { end = e; } if (start != null && end != null) { break; } } //don't connect if the end element is an instance map //those have a morphing set of inputs //dynInstanceParameterMap endTest = end as dynInstanceParameterMap; //if (endTest != null) //{ // continue; //} if (start != null && end != null && start != end) { dynConnector newConnector = new dynConnector(start, end, startIndex, endIndex, portType); //connectors.Add(newConnector); } } #endregion } catch (Exception ex) { Log("There was an error opening the workbench."); Log(ex.Message); Log(ex.StackTrace); Debug.WriteLine(ex.Message + ":" + ex.StackTrace); CleanWorkbench(); return(false); } return(true); }