// Refactoring on this method is done (logic-wise, could use some cleanup otherwise) private static void DeleteHEWU(ProcessUnitControl he, DrawingCanvas canvas) { Workspace ws = canvas.GetWorkspace(); // Heat exchangers with utilities must also delete the heat stream that's incoming // Normally the heat stream is at index 0 among the incoming streams, but it seems like // this does not always hold true when loading from files so it's just safer to search // for it HeatStream heatStream = null; foreach (AbstractStream incomingStream in he.ProcessUnit.IncomingStreams) { if (incomingStream is HeatStream) { heatStream = incomingStream as HeatStream; break; } } PFD.Streams.StreamControl heatStreamControl = canvas.GetStreamControl(heatStream) as PFD.Streams.StreamControl; List <IUndoRedoAction> undos = new List <IUndoRedoAction>(); undos.Add(new Logic.Undos.AddToWorkspace(heatStream)); undos.Add(new Logic.Undos.AddToWorkspace(he.ProcessUnit)); // We need to check if the attached stream has a source if (null != heatStream.Source) { // Detach with undo undos.Add(new Logic.Undos.AttachOutgoingStream(heatStream.Source, heatStream)); heatStream.Source.DetachOutgoingStream(heatStream); } // Detach all outgoing streams and make undos foreach (AbstractStream s in he.ProcessUnit.OutgoingStreams) { undos.Add(new Logic.Undos.SetStreamSource(s, he.ProcessUnit, null, s.SourceLocation)); s.Source = null; } // Detach all incoming streams and make undos foreach (AbstractStream s in he.ProcessUnit.IncomingStreams) { undos.Add(new Logic.Undos.SetStreamDestination(s, he.ProcessUnit, null, s.DestinationLocation)); s.Destination = null; } ws.AddUndo(new UndoRedoCollection( "Undo deletion of heat exchanger with utility", undos.ToArray())); // Remove the stream and process unit from the workspace. Event handlers will update the UI. ws.RemoveStream(heatStream); ws.RemoveProcessUnit(he.ProcessUnit); }
/// <summary> /// Handles mouse-left-button-down event for placing a heat exchanger with utility /// </summary> public void MLBD_HEWU(object sender, MouseButtonEventArgs e) { // Start by getting the mouse position Point pos = e.GetPosition(m_canvas); // See if we have a DraggableStreamEndpoint where we clicked DraggableStreamEndpoint endpoint = m_canvas.GetChildAt(pos, m_placementIcon) as DraggableStreamEndpoint; // Remove the placement icon and then set it to null to indicate state completion m_canvas.RemoveChild(m_placementIcon); m_placementIcon = null; // If there's not an endpoint: // 1. Create and attach an incoming heat stream. Position the source endpoint just // below the process unit // 2. Create an undo that will remove both // 3. Switch to the appropriate state for moving the source endpoint if (null == endpoint) { // Create the actual process unit AbstractProcessUnit apu = (AbstractProcessUnit)Activator.CreateInstance( m_type, AbstractProcessUnit.GetNextUID()); apu.Location = new MathCore.Vector(pos.X, pos.Y); HeatStream s = new HeatStream(); apu.AttachIncomingStream(s); s.Destination = apu; s.PropertiesTable = new StreamPropertiesTable(s); // Position the source of the heat stream. It's above the process unit by default, unless // that would make it go off the screen if (apu.Location.Y < 150.0) { s.SourceLocation = new MathCore.Vector( apu.Location.X - 10.0, apu.Location.Y + 80.0); s.PropertiesTable.Location = new MathCore.Vector( apu.Location.X + 10.0, apu.Location.Y + 140.0); } else { s.SourceLocation = new MathCore.Vector( apu.Location.X - 10.0, apu.Location.Y - 80.0); s.PropertiesTable.Location = new MathCore.Vector( apu.Location.X + 10.0, apu.Location.Y - 100.0); } // Add the stream and the process unit to the workspace. Event handlers will update // the UI appropriately. m_workspace.AddProcessUnit(apu); m_workspace.AddStream(s); m_workspace.AddUndo(new UndoRedoCollection("Undo creation of heat exchanger with utility", new Logic.Undos.DetachIncomingStream(apu, s), new Logic.Undos.SetStreamDestination(s, null, apu, s.DestinationLocation), new Logic.Undos.RemoveStream(s), new Logic.Undos.RemoveProcessUnit(apu))); // Select nothing on the canvas m_canvas.SelectedElement = null; m_palette.SwitchToSelect(); return; } // Otherwise, if we HAVE clicked on an endpoint, just deny it by doing nothing }