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

CAD

开发平台:

C#

  1. #region Using directives
  2. using System;
  3. using System.Runtime.Serialization;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.Security.Permissions;
  7. using System.Globalization;
  8. using System.Collections.Generic;
  9. using System.Collections;
  10. using System.Diagnostics;
  11. using System.Reflection;
  12. #endregion
  13. namespace DrawTools
  14. {
  15.     using DrawList = List<DrawObject>;
  16.     /// <summary>
  17.     /// List of graphic objects
  18.     /// </summary>
  19.     [Serializable]
  20.     class GraphicsList : ISerializable
  21.     {
  22.         #region Members
  23.         private DrawList graphicsList;
  24.         private const string entryCount = "Count";
  25.         private const string entryType = "Type";
  26.         #endregion
  27.         #region Constructor
  28.         public GraphicsList()
  29.         {
  30.             graphicsList = new DrawList();
  31.         }
  32.         #endregion
  33.         #region Serialization Support
  34.         protected GraphicsList(SerializationInfo info, StreamingContext context)
  35.         {
  36.             graphicsList = new DrawList();
  37.             int n = info.GetInt32(entryCount);
  38.             string typeName;
  39.             DrawObject drawObject;
  40.             for (int i = 0; i < n; i++)
  41.             {
  42.                 typeName = info.GetString(
  43.                     String.Format(CultureInfo.InvariantCulture,
  44.                         "{0}{1}",
  45.                     entryType, i));
  46.                 drawObject = (DrawObject)Assembly.GetExecutingAssembly().CreateInstance(
  47.                     typeName);
  48.                 drawObject.LoadFromStream(info, i);
  49.                 graphicsList.Add(drawObject);
  50.             }
  51.         }
  52.         /// <summary>
  53.         /// Save object to serialization stream
  54.         /// </summary>
  55.         /// <param name="info"></param>
  56.         /// <param name="context"></param>
  57.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  58.         public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  59.         {
  60.             info.AddValue(entryCount, graphicsList.Count);
  61.             int i = 0;
  62.             foreach (DrawObject o in graphicsList)
  63.             {
  64.                 info.AddValue(
  65.                     String.Format(CultureInfo.InvariantCulture,
  66.                         "{0}{1}",
  67.                         entryType, i),
  68.                     o.GetType().FullName);
  69.                 o.SaveToStream(info, i);
  70.                 i++;
  71.             }
  72.         }
  73.         #endregion
  74.         #region Other functions
  75.         public void Draw(Graphics g)
  76.         {
  77.             int n = graphicsList.Count;
  78.             DrawObject o;
  79.             // Enumerate list in reverse order to get first
  80.             // object on the top of Z-order.
  81.             for (int i = n - 1; i >= 0; i--)
  82.             {
  83.                 o = graphicsList[i];
  84.                 o.Draw(g);
  85.                 if (o.Selected == true)
  86.                 {
  87.                     o.DrawTracker(g);
  88.                 }
  89.             }
  90.         }
  91.         /// <summary>
  92.         /// Dump (for debugging)
  93.         /// </summary>
  94.         public void Dump()
  95.         {
  96.             Trace.WriteLine("");
  97.             foreach ( DrawObject o in graphicsList )
  98.             {
  99.                 o.Dump();
  100.             }
  101.         }
  102.         /// <summary>
  103.         /// Clear all objects in the list
  104.         /// </summary>
  105.         /// <returns>
  106.         /// true if at least one object is deleted
  107.         /// </returns>
  108.         public bool Clear()
  109.         {
  110.             bool result = (graphicsList.Count > 0);
  111.             graphicsList.Clear();
  112.             return result;
  113.         }
  114.         /// <summary>
  115.         /// Count and this [nIndex] allow to read all graphics objects
  116.         /// from GraphicsList in the loop.
  117.         /// </summary>
  118.         public int Count
  119.         {
  120.             get
  121.             {
  122.                 return graphicsList.Count;
  123.             }
  124.         }
  125.         public DrawObject this[int index]
  126.         {
  127.             get
  128.             {
  129.                 if (index < 0 || index >= graphicsList.Count)
  130.                     return null;
  131.                 return graphicsList[index];
  132.             }
  133.         }
  134.         /// <summary>
  135.         /// SelectedCount and GetSelectedObject allow to read
  136.         /// selected objects in the loop
  137.         /// </summary>
  138.         public int SelectionCount
  139.         {
  140.             get
  141.             {
  142.                 int n = 0;
  143.                 foreach (DrawObject o in Selection)
  144.                 {
  145.                     n++;
  146.                 }
  147.                 return n;
  148.             }
  149.         }
  150.         /// <summary>
  151.         /// Returns INumerable object which may be used for enumeration
  152.         /// of selected objects.
  153.         /// 
  154.         /// Note: returning IEnumerable<DrawObject> breaks CLS-compliance
  155.         /// (assembly CLSCompliant = true is removed from AssemblyInfo.cs).
  156.         /// To make this program CLS-compliant, replace 
  157.         /// IEnumerable<DrawObject> with IEnumerable. This requires
  158.         /// casting to object at runtime.
  159.         /// </summary>
  160.         /// <value></value>
  161.         public IEnumerable<DrawObject> Selection
  162.         {
  163.             get
  164.             {
  165.                 foreach (DrawObject o in graphicsList)
  166.                 {
  167.                     if (o.Selected)
  168.                     {
  169.                         yield return o;
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.         public void Add(DrawObject obj)
  175.         {
  176.             // insert to the top of z-order
  177.             graphicsList.Insert(0, obj);
  178.         }
  179.         /// <summary>
  180.         /// Insert object to specified place.
  181.         /// Used for Undo.
  182.         /// </summary>
  183.         public void Insert(int index, DrawObject obj)
  184.         {
  185.             if ( index >= 0  && index < graphicsList.Count )
  186.             {
  187.                 graphicsList.Insert(index, obj);
  188.             }
  189.         }
  190.         /// <summary>
  191.         /// Replace object in specified place.
  192.         /// Used for Undo.
  193.         /// </summary>
  194.         public void Replace(int index, DrawObject obj)
  195.         {
  196.             if (index >= 0 && index < graphicsList.Count)
  197.             {
  198.                 graphicsList.RemoveAt(index);
  199.                 graphicsList.Insert(index, obj);
  200.             }
  201.         }
  202.         /// <summary>
  203.         /// Remove object by index.
  204.         /// Used for Undo.
  205.         /// </summary>
  206.         public void RemoveAt(int index)
  207.         {
  208.             graphicsList.RemoveAt(index);
  209.         }
  210.         /// <summary>
  211.         /// Delete last added object from the list
  212.         /// (used for Undo operation).
  213.         /// </summary>
  214.         public void DeleteLastAddedObject()
  215.         {
  216.             if ( graphicsList.Count > 0 )
  217.             {
  218.                 graphicsList.RemoveAt(0);
  219.             }
  220.         }
  221.         public void SelectInRectangle(Rectangle rectangle)
  222.         {
  223.             UnselectAll();
  224.             foreach (DrawObject o in graphicsList)
  225.             {
  226.                 if (o.IntersectsWith(rectangle))
  227.                     o.Selected = true;
  228.             }
  229.         }
  230.         public void UnselectAll()
  231.         {
  232.             foreach (DrawObject o in graphicsList)
  233.             {
  234.                 o.Selected = false;
  235.             }
  236.         }
  237.         public void SelectAll()
  238.         {
  239.             foreach (DrawObject o in graphicsList)
  240.             {
  241.                 o.Selected = true;
  242.             }
  243.         }
  244.         /// <summary>
  245.         /// Delete selected items
  246.         /// </summary>
  247.         /// <returns>
  248.         /// true if at least one object is deleted
  249.         /// </returns>
  250.         public bool DeleteSelection()
  251.         {
  252.             bool result = false;
  253.             int n = graphicsList.Count;
  254.             for (int i = n - 1; i >= 0; i--)
  255.             {
  256.                 if (((DrawObject)graphicsList[i]).Selected)
  257.                 {
  258.                     graphicsList.RemoveAt(i);
  259.                     result = true;
  260.                 }
  261.             }
  262.             return result;
  263.         }
  264.         /// <summary>
  265.         /// Move selected items to front (beginning of the list)
  266.         /// </summary>
  267.         /// <returns>
  268.         /// true if at least one object is moved
  269.         /// </returns>
  270.         public bool MoveSelectionToFront()
  271.         {
  272.             int n;
  273.             int i;
  274.             DrawList tempList;
  275.             tempList = new DrawList();
  276.             n = graphicsList.Count;
  277.             // Read source list in reverse order, add every selected item
  278.             // to temporary list and remove it from source list
  279.             for (i = n - 1; i >= 0; i--)
  280.             {
  281.                 if ((graphicsList[i]).Selected)
  282.                 {
  283.                     tempList.Add(graphicsList[i]);
  284.                     graphicsList.RemoveAt(i);
  285.                 }
  286.             }
  287.             // Read temporary list in direct order and insert every item
  288.             // to the beginning of the source list
  289.             n = tempList.Count;
  290.             for (i = 0; i < n; i++)
  291.             {
  292.                 graphicsList.Insert(0, tempList[i]);
  293.             }
  294.             return (n > 0);
  295.         }
  296.         /// <summary>
  297.         /// Move selected items to back (end of the list)
  298.         /// </summary>
  299.         /// <returns>
  300.         /// true if at least one object is moved
  301.         /// </returns>
  302.         public bool MoveSelectionToBack()
  303.         {
  304.             int n;
  305.             int i;
  306.             DrawList tempList;
  307.             tempList = new DrawList();
  308.             n = graphicsList.Count;
  309.             // Read source list in reverse order, add every selected item
  310.             // to temporary list and remove it from source list
  311.             for (i = n - 1; i >= 0; i--)
  312.             {
  313.                 if ((graphicsList[i]).Selected)
  314.                 {
  315.                     tempList.Add(graphicsList[i]);
  316.                     graphicsList.RemoveAt(i);
  317.                 }
  318.             }
  319.             // Read temporary list in reverse order and add every item
  320.             // to the end of the source list
  321.             n = tempList.Count;
  322.             for (i = n - 1; i >= 0; i--)
  323.             {
  324.                 graphicsList.Add(tempList[i]);
  325.             }
  326.             return (n > 0);
  327.         }
  328.         /// <summary>
  329.         /// Get properties from selected objects and fill GraphicsProperties instance
  330.         /// </summary>
  331.         /// <returns></returns>
  332.         private GraphicsProperties GetProperties()
  333.         {
  334.             GraphicsProperties properties = new GraphicsProperties();
  335.             bool bFirst = true;
  336.             int firstColor = 0;
  337.             int firstPenWidth = 1;
  338.             bool allColorsAreEqual = true;
  339.             bool allWidthAreEqual = true;
  340.             foreach (DrawObject o in Selection)
  341.             {
  342.                 if (bFirst)
  343.                 {
  344.                     firstColor = o.Color.ToArgb();
  345.                     firstPenWidth = o.PenWidth;
  346.                     bFirst = false;
  347.                 }
  348.                 else
  349.                 {
  350.                     if (o.Color.ToArgb() != firstColor)
  351.                         allColorsAreEqual = false;
  352.                     if (o.PenWidth != firstPenWidth)
  353.                         allWidthAreEqual = false;
  354.                 }
  355.             }
  356.             if (allColorsAreEqual)
  357.             {
  358.                 properties.Color = Color.FromArgb(firstColor);
  359.             }
  360.             if (allWidthAreEqual)
  361.             {
  362.                 properties.PenWidth = firstPenWidth;
  363.             }
  364.             return properties;
  365.         }
  366.         /// <summary>
  367.         /// Apply properties for all selected objects.
  368.         /// Returns TRue if at least one property is changed.
  369.         /// </summary>
  370.         private bool ApplyProperties(GraphicsProperties properties)
  371.         {
  372.             bool changed = false;
  373.             foreach (DrawObject o in graphicsList)
  374.             {
  375.                 if (o.Selected)
  376.                 {
  377.                     if (properties.Color.HasValue)
  378.                     {
  379.                         if (o.Color != properties.Color.Value)
  380.                         {
  381.                             o.Color = properties.Color.Value;
  382.                             DrawObject.LastUsedColor = properties.Color.Value;
  383.                             changed = true;
  384.                         }
  385.                     }
  386.                     if (properties.PenWidth.HasValue)
  387.                     {
  388.                         if (o.PenWidth != properties.PenWidth.Value)
  389.                         {
  390.                             o.PenWidth = properties.PenWidth.Value;
  391.                             DrawObject.LastUsedPenWidth = properties.PenWidth.Value;
  392.                             changed = true;
  393.                         }
  394.                     }
  395.                 }
  396.             }
  397.             return changed;
  398.         }
  399.         /// <summary>
  400.         /// Show Properties dialog. Return true if list is changed
  401.         /// </summary>
  402.         /// <param name="parent"></param>
  403.         /// <returns></returns>
  404.         public bool ShowPropertiesDialog(DrawArea parent)
  405.         {
  406.             if (SelectionCount < 1)
  407.                 return false;
  408.             GraphicsProperties properties = GetProperties();
  409.             PropertiesDialog dlg = new PropertiesDialog();
  410.             dlg.Properties = properties;
  411.             CommandChangeState c = new CommandChangeState(this);
  412.             if (dlg.ShowDialog(parent) != DialogResult.OK)
  413.                 return false;
  414.             if ( ApplyProperties(properties) )
  415.             {
  416.                 c.NewState(this);
  417.                 parent.AddCommandToHistory(c);
  418.             }
  419.             return true;
  420.         }
  421.         #endregion
  422.     }
  423. }