void EndConnection(Guid nodeId, int portIndex, PortType portType) { bool isInPort = portType == PortType.INPUT; NodeModel node = CurrentWorkspace.GetModelInternal(nodeId) as NodeModel; if (node == null) { return; } PortModel portModel = isInPort ? node.InPorts[portIndex] : node.OutPorts[portIndex]; ConnectorModel connectorToRemove = null; // Remove connector if one already exists if (portModel.Connectors.Count > 0 && portModel.PortType == PortType.INPUT) { connectorToRemove = portModel.Connectors[0]; CurrentWorkspace.Connectors.Remove(connectorToRemove); portModel.Disconnect(connectorToRemove); var startPort = connectorToRemove.Start; startPort.Disconnect(connectorToRemove); } // We could either connect from an input port to an output port, or // another way around (in which case we swap first and second ports). PortModel firstPort, second; if (portModel.PortType != PortType.INPUT) { firstPort = portModel; second = activeStartPort; } else { // Create the new connector model firstPort = activeStartPort; second = portModel; } ConnectorModel newConnectorModel = CurrentWorkspace.AddConnection(firstPort.Owner, second.Owner, firstPort.Index, second.Index, PortType.INPUT); // Record the creation of connector in the undo recorder. var models = new Dictionary <ModelBase, UndoRedoRecorder.UserAction>(); if (connectorToRemove != null) { models.Add(connectorToRemove, UndoRedoRecorder.UserAction.Deletion); } models.Add(newConnectorModel, UndoRedoRecorder.UserAction.Creation); CurrentWorkspace.RecordModelsForUndo(models); activeStartPort = null; }