CommandChangeState.cs
上传用户:sxsgcs
上传日期:2013-10-21
资源大小:110k
文件大小:2k
源码类别:

CAD

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace DrawTools
  5. {
  6.     /// <summary>
  7.     /// Changing state of existing objects:
  8.     /// move, resize, change properties.
  9.     /// </summary>
  10.     class CommandChangeState : Command
  11.     {
  12.         // Selected object(s) before operation
  13.         List<DrawObject> listBefore;
  14.         // Selected object(s) after operation
  15.         List<DrawObject> listAfter;
  16.         
  17.         // Create this command BEFORE operation.
  18.         public CommandChangeState(GraphicsList graphicsList)
  19.         {
  20.             // Keep objects state before operation.
  21.             FillList(graphicsList, ref listBefore);
  22.         }
  23.         // Call this function AFTER operation.
  24.         public void NewState(GraphicsList graphicsList)
  25.         {
  26.             // Keep objects state after operation.
  27.             FillList(graphicsList, ref listAfter);
  28.         }
  29.         public override void Undo(GraphicsList list)
  30.         {
  31.             // Replace all objects in the list with objects from listBefore
  32.             ReplaceObjects(list, listBefore);
  33.         }
  34.         public override void Redo(GraphicsList list)
  35.         {
  36.             // Replace all objects in the list with objects from listAfter
  37.             ReplaceObjects(list, listAfter);
  38.         }
  39.         // Replace objects in graphicsList with objects from list
  40.         private void ReplaceObjects(GraphicsList graphicsList, List<DrawObject> list)
  41.         {
  42.             for ( int i = 0; i < graphicsList.Count; i++ )
  43.             {
  44.                 DrawObject replacement = null;
  45.                 foreach(DrawObject o in list)
  46.                 {
  47.                     if ( o.ID == graphicsList[i].ID )
  48.                     {
  49.                         replacement = o;
  50.                         break;
  51.                     }
  52.                 }
  53.                 if ( replacement != null )
  54.                 {
  55.                     graphicsList.Replace(i, replacement);
  56.                 }
  57.             }
  58.         }
  59.         // Fill list from selection
  60.         private void FillList(GraphicsList graphicsList, ref List<DrawObject> listToFill)
  61.         {
  62.             listToFill = new List<DrawObject>();
  63.             foreach (DrawObject o in graphicsList.Selection)
  64.             {
  65.                 listToFill.Add(o.Clone());
  66.             }
  67.         }
  68.     }
  69. }