CDomain.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Xml;
  9. using OTSCLRINTERFACE;
  10. namespace OTSDataType
  11. {
  12. public class CDomain : ISlo
  13. {
  14. protected const int DOMAIN_ITEM_NUMBER = 5;
  15. protected otsdataconst.DOMAIN_SHAPE m_nShape;
  16. protected Rectangle m_rectangle;
  17. protected List<Point> m_PolygonPoint;
  18. public CDomain()
  19. {
  20. m_nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  21. m_rectangle = new Rectangle(0, 0, 0, 0);
  22. m_PolygonPoint = new List<Point>();
  23. }
  24. public CDomain(otsdataconst.DOMAIN_SHAPE a_nShape, Rectangle a_rectRegion)
  25. {
  26. m_nShape = a_nShape;
  27. m_rectangle = a_rectRegion;
  28. m_PolygonPoint = new List<Point>();
  29. }
  30. public CDomainClr GetClrDomainObj()
  31. {
  32. var myDomainClr = new CDomainClr((int)m_nShape, m_rectangle);
  33. myDomainClr.SetPolygonPoint(m_PolygonPoint);
  34. return myDomainClr;
  35. }
  36. public bool equals(CDomain a_source)
  37. {
  38. return m_nShape == a_source.m_nShape && m_rectangle == a_source.m_rectangle;
  39. }
  40. public CDomain(CDomain d)
  41. {
  42. Duplicate(d);
  43. }
  44. protected void Duplicate(CDomain a_oSource)
  45. {
  46. m_nShape = a_oSource.m_nShape;
  47. m_rectangle = new Rectangle(a_oSource.m_rectangle.Location, a_oSource.m_rectangle.Size);
  48. m_PolygonPoint = new List<Point>();
  49. foreach (var p in a_oSource.GetPolygonPoint())
  50. {
  51. m_PolygonPoint.Add(p);
  52. }
  53. }
  54. public bool IsInvalid()
  55. {
  56. return (m_nShape < otsdataconst.DOMAIN_SHAPE.ROUND) || m_nShape > otsdataconst.DOMAIN_SHAPE.RECTANGLE || m_rectangle.IsEmpty;
  57. }
  58. public void SetRectDomain(Rectangle val)
  59. {
  60. m_rectangle= val;
  61. }
  62. public Rectangle GetRectDomain() { return m_rectangle; }
  63. public bool IsEmpty() { return m_rectangle.IsEmpty; }
  64. public bool IsRound() { return m_nShape == otsdataconst.DOMAIN_SHAPE.ROUND; }
  65. public bool IsRect() { return m_nShape == otsdataconst.DOMAIN_SHAPE.RECTANGLE; }
  66. public Point GetDomainCenter() { return new Point(m_rectangle.X + m_rectangle.Width / 2, m_rectangle.Y + m_rectangle.Height / 2); }
  67. public void OffsetDomain(Point a_poiMove) { m_rectangle.X += a_poiMove.X; m_rectangle.Y += a_poiMove.Y; }
  68. public long GetDiameter() { return m_rectangle.Width; }
  69. public Rectangle GetDomainRect() { return m_rectangle; }
  70. public void SetDomainRect(Rectangle a_rect) { m_rectangle = new Rectangle(a_rect.Location, a_rect.Size); }
  71. public otsdataconst.DOMAIN_SHAPE GetShape() { return m_nShape; }
  72. public void SetShape(otsdataconst.DOMAIN_SHAPE a_nShape) { m_nShape = a_nShape; }
  73. public void SetPolygonPoint(List<Point> a_PolygonPoint)
  74. {
  75. m_PolygonPoint = a_PolygonPoint;
  76. //myDomainClr = new CDomainClr((int)m_nShape, m_rectangle);
  77. }
  78. public List<Point> GetPolygonPoint() { return m_PolygonPoint; }
  79. public bool IntersectDomain(CDomain a_oDomain)
  80. {
  81. CDomainClr dom = GetClrDomainObj();
  82. var dom1 = a_oDomain.GetClrDomainObj();
  83. bool ifWithIn = dom.IntersectDomain(dom1);
  84. dom.MyDispose();
  85. dom1.MyDispose();
  86. return ifWithIn;
  87. }
  88. public bool PtInDomain( Point a_poiCheck)
  89. {
  90. var dom = GetClrDomainObj();
  91. dom.PtInDomain(a_poiCheck);
  92. dom.MyDispose();
  93. return true;
  94. }
  95. public bool DomainInDomain( CDomain a_oDomain)
  96. {
  97. var dom = GetClrDomainObj();
  98. var dom1 = a_oDomain.GetClrDomainObj();
  99. bool ifWithIn = dom.DomainInDomain(dom1);
  100. dom.MyDispose();
  101. dom1.MyDispose();
  102. return ifWithIn;
  103. }
  104. protected void Init()
  105. {
  106. m_nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  107. m_rectangle = new Rectangle(0, 0, 0, 0);
  108. m_PolygonPoint = new List<Point>();
  109. }
  110. public override void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  111. {
  112. Slo slo = new Slo();
  113. xString xnShape = new xString();
  114. xRect xRecDomain = new xRect();
  115. xString xPolygonPoint = new xString();
  116. slo.Register("shape", xnShape);
  117. slo.Register("rectDomian", xRecDomain);
  118. slo.Register("PolygonPoint", xPolygonPoint);
  119. if (isStoring)
  120. {
  121. xnShape.AssignValue((int)m_nShape+":"+ m_nShape.ToString());
  122. //we have to save the rectangle parameter according to the previouse rule.(left,top,width,height or centerX centerY,diamiter,0)
  123. System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
  124. rect.X = m_rectangle.X;
  125. rect.Y = m_rectangle.Y;
  126. rect.Width = m_rectangle.Width;
  127. rect.Height = m_rectangle.Height;
  128. xRecDomain.AssignValue(rect, (int)m_nShape);
  129. if (m_PolygonPoint != null)
  130. {
  131. string pointStr;
  132. pointStr = string.Join("_", m_PolygonPoint);
  133. xPolygonPoint.AssignValue(pointStr);
  134. }
  135. slo.Serialize(true, classDoc, rootNode);
  136. }
  137. else
  138. {
  139. slo.Serialize(false, classDoc, rootNode);
  140. m_nShape = (otsdataconst.DOMAIN_SHAPE)Convert.ToInt32( xnShape.value().Split(':')[0]);
  141. List<string> rectDomain = xRecDomain.toString().Split(',').ToList();
  142. m_rectangle = new Rectangle(Convert.ToInt32(rectDomain[0]), Convert.ToInt32(rectDomain[1]), 0, 0);
  143. //do the regulation
  144. int nCentreX = m_rectangle.Left;
  145. int nCentreY = m_rectangle.Top;
  146. int nWidth;
  147. int nHeight;
  148. int nDiameter;
  149. if (m_nShape == otsdataconst.DOMAIN_SHAPE.ROUND)
  150. {
  151. nDiameter = Convert.ToInt32(rectDomain[2]);
  152. // create domain
  153. m_rectangle = new Rectangle(0, 0, nDiameter, nDiameter);
  154. OffsetDomain(new Point(nCentreX - (nDiameter / 2), nCentreY - (nDiameter / 2)));
  155. }
  156. else if (m_nShape == otsdataconst.DOMAIN_SHAPE.RECTANGLE)
  157. {
  158. nWidth = Convert.ToInt32(rectDomain[2]);
  159. nHeight = Convert.ToInt32(rectDomain[3]);
  160. m_rectangle = new Rectangle(0, 0, nWidth, nHeight);
  161. OffsetDomain(new Point(nCentreX - (nWidth / 2), nCentreY - (nHeight / 2)));
  162. }
  163. if (m_PolygonPoint == null)
  164. {
  165. m_PolygonPoint = new List<Point>();
  166. }
  167. //myDomainClr = new CDomainClr((int)m_nShape, m_rectangle);
  168. if (xPolygonPoint != null)
  169. {
  170. if(xPolygonPoint.value() != null)
  171. {
  172. List<string> pointList = xPolygonPoint.toString().Split('_').ToList();
  173. if (pointList.Count > 2)
  174. {
  175. foreach (var item in pointList)
  176. {
  177. // 先去掉大括号
  178. string str = item.Replace("{", "").Replace("}", "");
  179. int x = Convert.ToInt32(str.Split(',')[0].Split('=')[1]);
  180. int y = Convert.ToInt32(str.Split(',')[1].Split('=')[1]);
  181. Point point = new Point(x, y);
  182. m_PolygonPoint.Add(point);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }