CDomain.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, RectangleF a_rectRegion)
  25. {
  26. m_nShape = a_nShape;
  27. m_rectangle =new Rectangle( (int)a_rectRegion.X,(int)a_rectRegion.Y,(int)a_rectRegion.Width,(int)a_rectRegion.Height);
  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(RectangleF val)
  59. {
  60. m_rectangle= new Rectangle(new Point((int)val.X,(int)val.Y),new Size((int)val.Width,(int)val.Height));
  61. }
  62. public RectangleF 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 PointF GetDomainCenter() { return new PointF(m_rectangle.X + m_rectangle.Width / 2, m_rectangle.Y + m_rectangle.Height / 2); }
  67. public void OffsetDomain(PointF a_poiMove) { m_rectangle.X += (int)a_poiMove.X; m_rectangle.Y += (int)a_poiMove.Y; }
  68. public float GetDiameter() { return m_rectangle.Width; }
  69. public RectangleF GetDomainRect() { return m_rectangle; }
  70. public void SetDomainRect(RectangleF a_rect) { m_rectangle = new Rectangle(new Point((int)a_rect.Location.X,(int)a_rect.Location.Y), new Size((int)a_rect.Size.Width,(int)a_rect.Size.Height)); }
  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. }
  77. public List<Point> GetPolygonPoint() { return m_PolygonPoint; }
  78. public bool IntersectDomain(CDomain a_oDomain)
  79. {
  80. CDomainClr dom = GetClrDomainObj();
  81. var dom1 = a_oDomain.GetClrDomainObj();
  82. bool ifWithIn = dom.IntersectDomain(dom1);
  83. dom.MyDispose();
  84. dom1.MyDispose();
  85. return ifWithIn;
  86. }
  87. public bool PtInDomain( Point a_poiCheck)
  88. {
  89. var dom = GetClrDomainObj();
  90. dom.PtInDomain(a_poiCheck);
  91. dom.MyDispose();
  92. return true;
  93. }
  94. public bool DomainInDomain( CDomain a_oDomain)
  95. {
  96. var dom = GetClrDomainObj();
  97. var dom1 = a_oDomain.GetClrDomainObj();
  98. bool ifWithIn = dom.DomainInDomain(dom1);
  99. dom.MyDispose();
  100. dom1.MyDispose();
  101. return ifWithIn;
  102. }
  103. protected void Init()
  104. {
  105. m_nShape = (otsdataconst.DOMAIN_SHAPE)(-1);
  106. m_rectangle = new Rectangle(0, 0, 0, 0);
  107. m_PolygonPoint = new List<Point>();
  108. }
  109. public override void Serialize(bool isStoring, XmlDocument classDoc, XmlNode rootNode)
  110. {
  111. Slo slo = new Slo();
  112. xString xnShape = new xString();
  113. xRect xRecDomain = new xRect();
  114. xString xPolygonPoint = new xString();
  115. slo.Register("shape", xnShape);
  116. slo.Register("rectDomian", xRecDomain);
  117. slo.Register("PolygonPoint", xPolygonPoint);
  118. if (isStoring)
  119. {
  120. xnShape.AssignValue((int)m_nShape+":"+ m_nShape.ToString());
  121. //we have to save the rectangle parameter according to the previouse rule.(left,top,width,height or centerX centerY,diamiter,0)
  122. System.Drawing.RectangleF rect = new System.Drawing.RectangleF();
  123. rect.X = m_rectangle.X;
  124. rect.Y = m_rectangle.Y;
  125. rect.Width = m_rectangle.Width;
  126. rect.Height = m_rectangle.Height;
  127. xRecDomain.AssignValue(rect, (int)m_nShape);
  128. if (m_PolygonPoint != null)
  129. {
  130. string pointStr;
  131. pointStr = string.Join("_", m_PolygonPoint);
  132. xPolygonPoint.AssignValue(pointStr);
  133. }
  134. slo.Serialize(true, classDoc, rootNode);
  135. }
  136. else
  137. {
  138. slo.Serialize(false, classDoc, rootNode);
  139. m_nShape = (otsdataconst.DOMAIN_SHAPE)Convert.ToInt32( xnShape.value().Split(':')[0]);
  140. List<string> rectDomain = xRecDomain.toString().Split(',').ToList();
  141. m_rectangle = new Rectangle(Convert.ToInt32(rectDomain[0]), Convert.ToInt32(rectDomain[1]), 0, 0);
  142. //do the regulation
  143. float nCentreX = m_rectangle.Left;
  144. float nCentreY = m_rectangle.Top;
  145. int nWidth;
  146. int nHeight;
  147. int nDiameter;
  148. if (m_nShape == otsdataconst.DOMAIN_SHAPE.ROUND)
  149. {
  150. nDiameter = Convert.ToInt32(rectDomain[2]);
  151. // create domain
  152. m_rectangle = new Rectangle(0, 0, nDiameter, nDiameter);
  153. OffsetDomain(new PointF(nCentreX - (nDiameter / 2), nCentreY - (nDiameter / 2)));
  154. }
  155. else if (m_nShape == otsdataconst.DOMAIN_SHAPE.RECTANGLE)
  156. {
  157. nWidth = Convert.ToInt32(rectDomain[2]);
  158. nHeight = Convert.ToInt32(rectDomain[3]);
  159. m_rectangle = new Rectangle(0, 0, nWidth, nHeight);
  160. OffsetDomain(new PointF(nCentreX - (nWidth / 2), nCentreY - (nHeight / 2)));
  161. }
  162. else
  163. {
  164. int left = Convert.ToInt32(rectDomain[0]);
  165. int top = Convert.ToInt32(rectDomain[1]);
  166. int right = Convert.ToInt32(rectDomain[2]);
  167. int bottom = Convert.ToInt32(rectDomain[3]);
  168. nWidth = right - left;
  169. nHeight = bottom - top;
  170. m_rectangle = new Rectangle(0, 0, nWidth, nHeight);
  171. OffsetDomain(new PointF(nCentreX - (nWidth / 2), nCentreY - (nHeight / 2)));
  172. }
  173. if (m_PolygonPoint == null)
  174. {
  175. m_PolygonPoint = new List<Point>();
  176. }
  177. //myDomainClr = new CDomainClr((int)m_nShape, m_rectangle);
  178. if (xPolygonPoint != null)
  179. {
  180. if(xPolygonPoint.value() != null)
  181. {
  182. List<string> pointList = xPolygonPoint.toString().Split('_').ToList();
  183. if (pointList.Count > 2)
  184. {
  185. foreach (var item in pointList)
  186. {
  187. // 先去掉大括号
  188. string str = item.Replace("{", "").Replace("}", "");
  189. int x = Convert.ToInt32(str.Split(',')[0].Split('=')[1]);
  190. int y = Convert.ToInt32(str.Split(',')[1].Split('=')[1]);
  191. Point point = new Point(x, y);
  192. m_PolygonPoint.Add(point);
  193. }
  194. }
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }