private ConnectionItemData[] ConvertConnectionItems(Connection connection) { GraphicConnection graphicConnection = connection.LinkedObject as GraphicConnection; List <ConnectionItemData> itemdatalist = new List <ConnectionItemData>(graphicConnection.Children.Count); foreach (IConnectionItem item in graphicConnection.Children) { if (item is ConnectionLine) { ConnectionLine line = item as ConnectionLine; LineItemData linedata = new LineItemData(line.Nodes[0].Name, line.Nodes[1].Name); itemdatalist.Add(linedata); } if (item is ConnectionNode) { ConnectionNode node = item as ConnectionNode; //if (node.Parent is GraphicTerminal) //impossible //{ // NodeItemData nodedata = new NodeItemData(node.Name); // nodedata.IsTerminalNode = true; //not needed // itemdatalist.Add(nodedata); //} else if (node.Parent is GraphicConnection) { NodeItemData nodedata = new NodeItemData(node.Name); //nodedata.IsTerminalNode = false; //not needed nodedata.X = node.Location.X; nodedata.Y = node.Location.Y; itemdatalist.Add(nodedata); } } } return(itemdatalist.ToArray()); }
/// <summary> /// Merges two connection objects into one /// </summary> /// <param name="fromConnection">the remaining connection</param> /// <param name="toConnection">the connection that gets merged</param> private void MergeConnections(GraphicConnection fromConnection, PointF fromPoint, GraphicConnection toConnection, PointF toPoint) { if (fromConnection.Equals(toConnection)) { return; } if (MessageBox.Show(String.Format("Sollen die Verbindungen \"{0}\" und \"{1}\" miteinander verbunden werden?", fromConnection.Name, toConnection.Name), "Frage", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { //derive all Terminals and ConnectionItems (lines, nodes) fromConnection.Merge(toConnection); IConnectionItem item1 = fromConnection.GetItemAt(fromPoint); IConnectionItem item2 = fromConnection.GetItemAt(toPoint); if (item1 is ConnectionLine) { item1 = SplitConnectionLine(item1 as ConnectionLine, fromPoint); } if (item2 is ConnectionLine) { item2 = SplitConnectionLine(item2 as ConnectionLine, fromPoint); } ConnectionLine line = new ConnectionLine(item1 as ConnectionNode, item2 as ConnectionNode); fromConnection.AddChild(line); m_Editor.RemoveElement(toConnection); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } }
private void CreateElements(Circuit circuit, CircuitData circuitData, SignalList signals, bool createGraphics) { Dictionary <BaseElement, BaseElementData> elemDict = new Dictionary <BaseElement, BaseElementData>(); foreach (BaseElementData elemData in circuitData.Elements) { InputOutputElement element = null; element = ConvertElementData(elemData, createGraphics, elemDict, signals); if (element != null) { circuit.AddElement(element); } } foreach (ConnectionData connectionData in circuitData.Connections) { Connection connection = new Connection(); connection.Name = connectionData.Name; ConnectTerminals(connection, circuit, elemDict); if (connection.Terminals.Count > 0) { if (createGraphics) { GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), connection) as GraphicConnection; CreateConnectionLines(circuit, graphicConnection, connectionData); } circuit.AddElement(connection); } } }
public override void MouseClick(PointF location, Keys controlKeys) { GraphicBaseElement element = m_Editor.GetElementAt(location); if (element == null) { if (m_FromElement != null) { //floating connection - create a new connection line FloatingConnection(location); m_LastMouseLocation = location; } return; } if (element is GraphicInputOutputElement == false && element is GraphicConnection == false) { return; } //user clicked a terminal of an element GraphicInputOutputElement ioelem = element as GraphicInputOutputElement; GraphicTerminal graphicTerminal = null; if (ioelem != null) { graphicTerminal = ioelem.GetTerminalAt(location); if (graphicTerminal == null || graphicTerminal.Equals(m_FromElement)) { return; } if (m_FromElement == null) { m_FromElement = graphicTerminal; m_LastMouseLocation = location; return; } TryConnectToTerminal(graphicTerminal, location); m_LastMouseLocation = location; } //user clicked a connection GraphicConnection connection = element as GraphicConnection; if (connection != null) { if (connection.Equals(m_FromElement)) { return; } if (m_FromElement == null) { m_FromElement = connection; m_LastMouseLocation = location; return; } TryConnectToConnection(connection, location); m_LastMouseLocation = location; } }
public override void MouseClick(PointF location, Keys controlKeys) { GraphicBaseElement element = m_Editor.GetElementAt(location); if (element == null) { return; } if (element is GraphicInputOutputElement == false && element is GraphicConnection == false) { return; } GraphicConnection graphicConnection = null; //user clicked a terminal of an element GraphicInputOutputElement ioelem = element as GraphicInputOutputElement; if (ioelem != null) { GraphicTerminal graphicTerminal = ioelem.GetTerminalAt(location); if (graphicTerminal != null) { Terminal terminal = (graphicTerminal.LinkedObject as Terminal); if (terminal.IsConnected) { Connection connection = terminal.Connection; graphicConnection = terminal.Connection.LinkedObject as GraphicConnection; graphicConnection.RemoveChild(graphicTerminal.ConnectionNode.Lines[0]); //terminal.Disconnect(); connection.DisconnectTerminal(terminal); if (connection.Terminals.Count == 0) { m_Editor.RemoveElement(connection.LinkedObject as GraphicConnection); } m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); m_Editor.Invalidate(); } } } //user clicked a connection graphicConnection = element as GraphicConnection; if (graphicConnection != null) { foreach (GraphicBaseElement child in graphicConnection.Children) { graphicConnection.RemoveChild(child); } Connection connection = graphicConnection.LinkedObject as Connection; foreach (Terminal terminal in connection.Terminals) { //terminal.Disconnect(); connection.DisconnectTerminal(terminal); } m_Editor.RemoveElement(graphicConnection); } }
// Connection Creation --- private GraphicConnection CreateGCon(NeuralConnection c, GraphicNode ingn, GraphicNode outgn) { if (c == null || ingn == null || outgn == null) { return(null); } GraphicConnection gc = new GraphicConnection(c, ingn, outgn); gCons.Add(gc); return(gc); }
/// <summary> /// Splits the connection line at the given point and places a node there /// </summary> /// <param name="line">the line to split</param> /// <param name="atPoint">the point to place a node</param> /// <returns>the placed node</returns> private ConnectionNode SplitConnectionLine(ConnectionLine line, PointF atPoint) { GraphicConnection connection = line.Parent as GraphicConnection; ConnectionNode node = new ConnectionNode(line.NearestPointOnLine(m_Editor.AlignToGrid(atPoint))); ConnectionLine line1 = new ConnectionLine(line.Nodes[0], node); ConnectionLine line2 = new ConnectionLine(line.Nodes[1], node); connection.RemoveChild(line); connection.AddChild(node); connection.AddChild(line1); connection.AddChild(line2); return(node); }
private ConnectionNode SearchNode(string nodeName, Circuit circuit, GraphicConnection graphicConnection) { //node is already part of the connection if (graphicConnection.Children != null) { foreach (GraphicBaseElement element in graphicConnection.Children) { if (element is ConnectionNode && element.Name.Equals(nodeName)) { return(element as ConnectionNode); } } } //so the node must be part of a terminal, the name gives the clue string[] str = nodeName.Split(new char[] { '/' }, 2); if (str.Length == 2) { string elementName = str[0]; string terminalName = str[1]; foreach (BaseElement element in circuit) { //for security reasons check whether the found terminal is connected to the connection too //although the name should be all that's needed Connection connection = graphicConnection.LinkedObject as Connection; if (element is InputOutputElement && element.Name.Equals(elementName)) { InputOutputElement io = element as InputOutputElement; foreach (Terminal terminal in io.Inputs) { if (connection.Terminals.Contains(terminal) && terminal.Name.Equals(terminalName)) { return((terminal.LinkedObject as GraphicTerminal).ConnectionNode); } } foreach (Terminal terminal in io.Outputs) { if (connection.Terminals.Contains(terminal) && terminal.Name.Equals(terminalName)) { return((terminal.LinkedObject as GraphicTerminal).ConnectionNode); } } } } } return(null); }
private void CreateConnectionLines(Circuit circuit, GraphicConnection graphicConnection, ConnectionData connectionData) { if (connectionData.Items == null || connectionData.Items.Length == 0) { //downward compatibility - no detailed information available: Connection connection = graphicConnection.LinkedObject as Connection; GraphicTerminal previous = null; //displays straight lines from one terminal to another foreach (Terminal terminal in connection.Terminals) { GraphicTerminal graphicTerminal = terminal.LinkedObject as GraphicTerminal; if (previous != null) { graphicConnection.AddChild(new ConnectionLine(previous.ConnectionNode, graphicTerminal.ConnectionNode)); } previous = graphicTerminal; } return; } //create nodes foreach (ConnectionItemData itemData in connectionData.Items) { if (itemData is NodeItemData) { NodeItemData nodeData = itemData as NodeItemData; ConnectionNode node = new ConnectionNode(nodeData.X, nodeData.Y); graphicConnection.AddChild(node); } } //create lines foreach (ConnectionItemData itemData in connectionData.Items) { if (itemData is LineItemData) { LineItemData lineData = itemData as LineItemData; ConnectionNode node1 = SearchNode(lineData.Node1, circuit, graphicConnection); ConnectionNode node2 = SearchNode(lineData.Node2, circuit, graphicConnection); if (node1 != null && node2 != null) { ConnectionLine line = new ConnectionLine(node1, node2); graphicConnection.AddChild(line); } } } }
/// <summary> /// Connect to a Connection /// </summary> /// <param name="connection"></param> private void TryConnectToConnection(GraphicConnection connection, PointF location) { if (m_FromElement is GraphicTerminal) { Terminal fromTerminal = m_FromElement.LinkedObject as Terminal; if (fromTerminal.Connection != null) { MergeConnections(fromTerminal.Connection.LinkedObject as GraphicConnection, m_LastMouseLocation, connection, location); } else { IConnectionItem connectionItem = connection.GetItemAt(location); if (connectionItem is ConnectionNode) { //user clicked near a node - connect to this ConnectionLine line = new ConnectionLine((m_FromElement as GraphicTerminal).ConnectionNode, connectionItem as ConnectionNode); connection.AddChild(line); } else if (connectionItem is ConnectionLine) { //user clicked near a line ConnectionLine prevLine = connectionItem as ConnectionLine; //place a connection node at location - split connection line ConnectionNode node = SplitConnectionLine(prevLine, location); ConnectionLine line = new ConnectionLine(node, (m_FromElement as GraphicTerminal).ConnectionNode); connection.AddChild(line); } connection.ConnectTerminal(m_FromElement as GraphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } } else if (m_FromElement is GraphicConnection) { MergeConnections(m_FromElement as GraphicConnection, m_LastMouseLocation, connection, location); } else if (m_FromElement is ConnectionNode) { MergeConnections(m_FromElement.Parent as GraphicConnection, m_LastMouseLocation, connection, location); //GraphicConnection fromConnection = m_FromElement.Parent as GraphicConnection; } m_FromElement = null; m_Editor.Invalidate(); }
/// <summary> /// Returns the element at a given location /// </summary> /// <param name="location">Location of interest</param> /// <returns>Element at the given location or null respectively</returns> public GraphicBaseElement GetElementAt(PointF location) { //foreach (GraphicBaseElement element in m_Elements) foreach (BaseElement logic in m_Circuit) { GraphicBaseElement element = logic.LinkedObject as GraphicBaseElement; RectangleF bounds = element.Bounds; if (element is GraphicConnection) { GraphicConnection connection = element as GraphicConnection; if (connection.BelongsTo(location)) { return(element); } } if (bounds.Contains(location)) { return(element); } } return(null); }
/// <summary> /// Connect to a Terminal (resp. its connection) /// </summary> /// <param name="graphicTerminal"></param> private void TryConnectToTerminal(GraphicTerminal graphicTerminal, PointF location) { Terminal toTerminal = graphicTerminal.LinkedObject as Terminal; if (toTerminal.Connection != null) { //merging connections not supported by clicking terminals return; } if (m_FromElement is GraphicTerminal) { Terminal fromTerminal = m_FromElement.LinkedObject as Terminal; //if (fromTerminal.Connection != null && toTerminal.Connection != null) //{ // MergeConnections(fromTerminal.Connection.LinkedObject as GraphicConnection, m_LastMouseLocation, // toTerminal.Connection.LinkedObject as GraphicConnection, location); //} //else if (fromTerminal.Connection != null) //{ // //IDEE: suche über allen linien die kürzeste entfernung vom ziel zur verbindung // GraphicConnection graphicConnection = fromTerminal.Connection.LinkedObject as GraphicConnection; // ConnectionLine line = new ConnectionLine((m_FromElement as GraphicTerminal).ConnectionNode, graphicTerminal.ConnectionNode); // graphicConnection.AddChild(line); // graphicConnection.ConnectTerminal(graphicTerminal); // m_Editor.UpdateDrawing(); // m_Editor.RaiseChangedEvent(); //} //else if (toTerminal.Connection != null) //{ // GraphicConnection graphicConnection = toTerminal.Connection.LinkedObject as GraphicConnection; // graphicConnection.ConnectTerminal(m_FromElement as GraphicTerminal); // m_Editor.UpdateDrawing(); // m_Editor.RaiseChangedEvent(); //} //else if (fromTerminal.Connection == null && toTerminal.Connection == null) { GraphicTerminal fromGraphicTerminal = m_FromElement as GraphicTerminal; if (IsOrthogonal(fromGraphicTerminal.ConnectionNode.Location, graphicTerminal.ConnectionNode.Location) == false) { return; } GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), new Connection()) as GraphicConnection; graphicConnection.Name = UniqueName.GetUniqueName(m_Editor.Circuit, typeof(Connection)); graphicConnection.ConnectTerminal(fromGraphicTerminal); graphicConnection.ConnectTerminal(graphicTerminal); ConnectionLine line = new ConnectionLine(fromGraphicTerminal.ConnectionNode, graphicTerminal.ConnectionNode); graphicConnection.AddChild(line); m_Editor.AddElement(graphicConnection); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } } if (m_FromElement is GraphicConnection) { GraphicConnection fromConnection = m_FromElement as GraphicConnection; IConnectionItem connectionItem = fromConnection.GetItemAt(m_LastMouseLocation); if (connectionItem is ConnectionNode) { ConnectionLine line = new ConnectionLine(connectionItem as ConnectionNode, graphicTerminal.ConnectionNode); fromConnection.AddChild(line); } else if (connectionItem is ConnectionLine) { ConnectionLine prevLine = connectionItem as ConnectionLine; ConnectionNode node = new ConnectionNode(prevLine.NearestPointOnLine(m_LastMouseLocation)); ConnectionLine line1 = new ConnectionLine(prevLine.Nodes[0], node); ConnectionLine line2 = new ConnectionLine(prevLine.Nodes[1], node); fromConnection.RemoveChild(prevLine); fromConnection.AddChild(node); fromConnection.AddChild(line1); fromConnection.AddChild(line2); ConnectionLine line = new ConnectionLine(node, graphicTerminal.ConnectionNode); fromConnection.AddChild(line); } fromConnection.ConnectTerminal(graphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } if (m_FromElement is ConnectionNode) { m_FromElement.Location = ForceOrthogonality(graphicTerminal.ConnectionNode.Location, m_FromElement.Location); ConnectionLine line = new ConnectionLine(m_FromElement as ConnectionNode, graphicTerminal.ConnectionNode); m_FromElement.Parent.AddChild(line); (m_FromElement.Parent as GraphicConnection).ConnectTerminal(graphicTerminal); m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); } m_FromElement = null; m_Editor.Invalidate(); }
/// <summary> /// Create a floating connnection, i.e. not completely connected everywhere /// </summary> /// <param name="location"></param> private void FloatingConnection(PointF location) { if (m_FromElement is GraphicConnection) { GraphicConnection graphicConnection = m_FromElement as GraphicConnection; IConnectionItem connectionItem = graphicConnection.GetItemAt(m_LastMouseLocation); if (connectionItem is ConnectionNode) { //user clicked near a node at fist, connect to this ConnectionNode prevNode = connectionItem as ConnectionNode; location = ForceOrthogonality(prevNode.Location, m_Editor.AlignToGrid(location)); bool skip = false; //check whether the new line would just lenghten the previous line if (prevNode.Lines.Length == 1) { ConnectionLine prevLine = prevNode.Lines[0]; if (prevLine.LineStyle == ConnectionLine.DetermineLineStyle(prevNode.Location, location)) { //place the node at the new location in this case prevNode.Location = location; skip = true; } } if (skip == false) { //place a new line ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(prevNode, node); m_FromElement.AddChild(node); m_FromElement.AddChild(line); m_FromElement = node; } } else if (connectionItem is ConnectionLine) { //user clicked near a line at first ConnectionLine prevLine = connectionItem as ConnectionLine; //place a connection node at location - split connection line ConnectionNode node = SplitConnectionLine(prevLine, m_LastMouseLocation); //the new node/line location = ForceOrthogonality(node.Location, m_Editor.AlignToGrid(location)); ConnectionNode fnode = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(node, fnode); m_FromElement.AddChild(fnode); m_FromElement.AddChild(line); m_FromElement = fnode; } } else if (m_FromElement is GraphicTerminal) { //floating connection from a terminal GraphicTerminal graphicTerminal = m_FromElement as GraphicTerminal; location = ForceOrthogonality(graphicTerminal.ConnectionNode.Location, m_Editor.AlignToGrid(location)); ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(node, graphicTerminal.ConnectionNode); GraphicConnection graphicConnection = GraphicObjectFactory.CreateInstance(typeof(Connection), new Connection()) as GraphicConnection; graphicConnection.Name = UniqueName.GetUniqueName(m_Editor.Circuit, typeof(Connection)); graphicConnection.AddChild(node); graphicConnection.AddChild(line); graphicConnection.ConnectTerminal(graphicTerminal); m_Editor.AddElement(graphicConnection); //proceed connecting with this node m_FromElement = node; } else if (m_FromElement is ConnectionNode) { //special case -> could be covered by "is GraphicConnection" //sequential floating lines ConnectionNode prevNode = m_FromElement as ConnectionNode; location = ForceOrthogonality(prevNode.Location, m_Editor.AlignToGrid(location)); bool skip = false; //check whether the new line would just lenghten the previous line if (prevNode.Lines.Length == 1) { ConnectionLine prevLine = prevNode.Lines[0]; if (prevLine.LineStyle == ConnectionLine.DetermineLineStyle(prevNode.Location, location)) { //place the node at the new location in this case prevNode.Location = location; skip = true; } } if (skip == false) { //place a new line ConnectionNode node = new ConnectionNode(location); ConnectionLine line = new ConnectionLine(prevNode, node); GraphicConnection graphicConnection = prevNode.Parent as GraphicConnection; graphicConnection.AddChild(node); graphicConnection.AddChild(line); m_FromElement = node; } } m_Editor.UpdateDrawing(); m_Editor.RaiseChangedEvent(); m_Editor.Invalidate(); }