GasConditions.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using MECF.Framework.UI.Core.DxfScript;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. namespace MECF.Framework.UI.Core.DxfScript
  9. {
  10. /// <summary>
  11. /// 名称:事件类
  12. /// 用途:BoolConditions/StringConditions 逻辑实现
  13. /// </summary>
  14. public partial class GasDxfDocument
  15. {
  16. //解析全部事件
  17. public void ParseConditions()
  18. {
  19. foreach (var node in BoolConditions)
  20. {
  21. string boolCondition = node.Value;
  22. if (boolCondition == null || boolCondition.Equals(""))
  23. {
  24. continue;
  25. }
  26. int id = node.Key;
  27. GasBaseShape shape = FindShapeById(id);
  28. if (shape != null)
  29. {
  30. shape.BoolCondition = new Script(boolCondition);
  31. }
  32. }
  33. foreach (var node in StringConditions)
  34. {
  35. string stringCondition = node.Value;
  36. if (stringCondition == null || stringCondition.Equals(""))
  37. {
  38. continue;
  39. }
  40. int id = node.Key;
  41. GasBaseShape shape = FindShapeById(id);
  42. if (shape != null)
  43. {
  44. shape.StringCondition = new Script(stringCondition);
  45. }
  46. }
  47. foreach (var node in ClickConditions)
  48. {
  49. string clickCondition = node.Value;
  50. if (clickCondition == null || clickCondition.Equals(""))
  51. {
  52. continue;
  53. }
  54. int id = node.Key;
  55. GasBaseShape shape = FindShapeById(id);
  56. if (shape != null)
  57. {
  58. shape.ClickCondition = new Script(clickCondition);
  59. }
  60. }
  61. }
  62. //保存conditions的公共函数
  63. private void SaveCondition(XmlDocument xmlDoc, XmlElement nodeBoolConditions, XmlElement nodeStringConditions, XmlElement nodeClickConditions, GasBaseShape shape)
  64. {
  65. if (shape.Id > 0)
  66. {
  67. string boolCondition = null;
  68. if (BoolConditions.ContainsKey(shape.Id))
  69. {
  70. boolCondition = BoolConditions[shape.Id];
  71. }
  72. string stringCondition = null;
  73. if (StringConditions.ContainsKey(shape.Id))
  74. {
  75. stringCondition = StringConditions[shape.Id];
  76. }
  77. string clickCondition = null;
  78. if (ClickConditions.ContainsKey(shape.Id))
  79. {
  80. clickCondition = ClickConditions[shape.Id];
  81. }
  82. if (boolCondition != null && !boolCondition.Equals(""))
  83. {
  84. XmlElement nodeBoolCondition = xmlDoc.CreateElement("BoolCondition");
  85. nodeBoolCondition.SetAttribute("Id", shape.Id.ToString());
  86. nodeBoolCondition.InnerText = boolCondition;
  87. nodeBoolConditions.AppendChild(nodeBoolCondition);
  88. }
  89. if (stringCondition != null && !stringCondition.Equals(""))
  90. {
  91. XmlElement nodeStringCondition = xmlDoc.CreateElement("StringCondition");
  92. nodeStringCondition.SetAttribute("Id", shape.Id.ToString());
  93. nodeStringCondition.InnerText = stringCondition;
  94. nodeStringConditions.AppendChild(nodeStringCondition);
  95. }
  96. if (clickCondition != null && !clickCondition.Equals(""))
  97. {
  98. XmlElement nodeClickCondition = xmlDoc.CreateElement("ClickCondition");
  99. nodeClickCondition.SetAttribute("Id", shape.Id.ToString());
  100. nodeClickCondition.InnerText = clickCondition;
  101. nodeClickConditions.AppendChild(nodeClickCondition);
  102. }
  103. }
  104. }
  105. private void LoadCondition(XmlDocument xmlDoc)
  106. {
  107. XmlNodeList nodeBoolConditions = xmlDoc.SelectNodes("GasDxfDocument/BoolConditions/BoolCondition");
  108. foreach (XmlElement nodeBoolCondition in nodeBoolConditions)
  109. {
  110. int id = int.Parse(nodeBoolCondition.GetAttribute("Id"));
  111. if (id > 0)
  112. {
  113. string boolCondition = nodeBoolCondition.InnerText;
  114. if (boolCondition != null && !boolCondition.Equals(""))
  115. {
  116. BoolConditions.Add(id, boolCondition);
  117. }
  118. GasBaseShape shape = FindShapeById(id);
  119. if (shape != null)
  120. {
  121. shape.BoolCondition = new Script(boolCondition);
  122. }
  123. }
  124. }
  125. XmlNodeList nodeStringConditions = xmlDoc.SelectNodes("GasDxfDocument/StringConditions/StringCondition");
  126. foreach (XmlElement nodeStringCondition in nodeStringConditions)
  127. {
  128. int id = int.Parse(nodeStringCondition.GetAttribute("Id"));
  129. if (id > 0)
  130. {
  131. string stringCondition = nodeStringCondition.InnerText;
  132. if (stringCondition != null && !stringCondition.Equals(""))
  133. {
  134. StringConditions.Add(id, stringCondition);
  135. }
  136. GasBaseShape shape = FindShapeById(id);
  137. if (shape != null)
  138. {
  139. shape.StringCondition = new Script(stringCondition);
  140. }
  141. }
  142. }
  143. XmlNodeList nodeClickConditions = xmlDoc.SelectNodes("GasDxfDocument/ClickConditions/ClickCondition");
  144. foreach (XmlElement nodeClickCondition in nodeClickConditions)
  145. {
  146. int id = int.Parse(nodeClickCondition.GetAttribute("Id"));
  147. if (id > 0)
  148. {
  149. string clickCondition = nodeClickCondition.InnerText;
  150. if (clickCondition != null && !clickCondition.Equals(""))
  151. {
  152. ClickConditions.Add(id, clickCondition);
  153. }
  154. GasBaseShape shape = FindShapeById(id);
  155. if (shape != null)
  156. {
  157. shape.ClickCondition = new Script(clickCondition);
  158. }
  159. }
  160. }
  161. }
  162. /// <summary>
  163. /// 清除所有事件
  164. /// </summary>
  165. public void ClearAll()
  166. {
  167. ClearDxf();
  168. if (BoolConditions != null)
  169. {
  170. BoolConditions.Clear();
  171. }
  172. else
  173. {
  174. BoolConditions = new Dictionary<int, string>();
  175. }
  176. if (StringConditions != null)
  177. {
  178. StringConditions.Clear();
  179. }
  180. else
  181. {
  182. StringConditions = new Dictionary<int, string>();
  183. }
  184. if (ClickConditions != null)
  185. {
  186. ClickConditions.Clear();
  187. }
  188. else
  189. {
  190. ClickConditions = new Dictionary<int, string>();
  191. }
  192. InitScriptContent = "";
  193. InitScriptExpress = null;
  194. IsGasFileModified = false;
  195. SelectedShape = null;
  196. }
  197. //把boolcondition和stringConditon的文本内容更新到字典中,并且刷新shape的脚本对象
  198. public void UpdateBoolCondition(GasBaseShape shape, string boolCondition)
  199. {
  200. shape.BoolCondition = new Script(boolCondition);
  201. if (BoolConditions.ContainsKey(shape.Id))
  202. {
  203. BoolConditions[shape.Id] = boolCondition;
  204. }
  205. else
  206. {
  207. BoolConditions.Add(shape.Id, boolCondition);
  208. }
  209. }
  210. public void UpdateStringCondition(GasBaseShape shape, string stringCondition)
  211. {
  212. shape.StringCondition = new Script(stringCondition);
  213. if (StringConditions.ContainsKey(shape.Id))
  214. {
  215. StringConditions[shape.Id] = stringCondition;
  216. }
  217. else
  218. {
  219. StringConditions.Add(shape.Id, stringCondition);
  220. }
  221. }
  222. public void UpdateClickCondition(GasBaseShape shape, string clickCondition)
  223. {
  224. shape.ClickCondition = new Script(clickCondition);
  225. if (ClickConditions.ContainsKey(shape.Id))
  226. {
  227. ClickConditions[shape.Id] = clickCondition;
  228. }
  229. else
  230. {
  231. ClickConditions.Add(shape.Id, clickCondition);
  232. }
  233. }
  234. public void UpdateInitScript(string content)
  235. {
  236. InitScriptContent = content;
  237. InitScriptExpress = new Script(content);
  238. }
  239. //根据Id找元件
  240. public GasBaseShape FindShapeById(int id)
  241. {
  242. switch (id / 1000000)
  243. {
  244. case (int)GasBaseShape.ShapeType.ACUTE:
  245. case (int)GasBaseShape.ShapeType.OBTUSE:
  246. case (int)GasBaseShape.ShapeType.FUNNEL:
  247. case (int)GasBaseShape.ShapeType.HLINE:
  248. case (int)GasBaseShape.ShapeType.POLY:
  249. case (int)GasBaseShape.ShapeType.VLINE:
  250. //线段
  251. GasLine line = Lines.Find(x => x.Id == id);
  252. if (line != null)
  253. {
  254. return line;
  255. }
  256. else
  257. {
  258. GasPolyLine polyLine = PolyLines.Find(x => x.Id == id);
  259. if (polyLine != null)
  260. {
  261. return polyLine;
  262. }
  263. else
  264. {
  265. GasButton button = Buttons.Find(x => x.Id == id);
  266. if (button != null)
  267. {
  268. return button;
  269. }
  270. else
  271. {
  272. GasAnalogControl4Jet analog = Analogs.Find(x => x.Id == id);
  273. if (analog != null)
  274. {
  275. return analog;
  276. }
  277. else
  278. {
  279. return null;
  280. }
  281. }
  282. }
  283. }
  284. break;
  285. case (int)GasBaseShape.ShapeType.CIRCLE:
  286. //圆
  287. GasCircle circle = Circles.Find(x => x.Id == id);
  288. if (circle != null)
  289. {
  290. return circle;
  291. }
  292. else
  293. {
  294. GasAITValve valve = Valves.Find(x => x.Id == id);
  295. if (valve != null)
  296. {
  297. return valve;
  298. }
  299. else
  300. {
  301. return null;
  302. }
  303. }
  304. break;
  305. case (int)GasBaseShape.ShapeType.TEXT:
  306. //文本
  307. GasText text = Texts.Find(x => x.Id == id);
  308. if (text != null)
  309. {
  310. return text;
  311. }
  312. else
  313. {
  314. return null;
  315. }
  316. break;
  317. default:
  318. return null;
  319. }
  320. }
  321. //根据位置找元件
  322. public GasBaseShape GetShapeByPosition(double x, double y)
  323. {
  324. foreach (GasBaseShape shape in Analogs)
  325. {
  326. if (shape.Contains(x, y))
  327. {
  328. return shape;
  329. }
  330. }
  331. foreach (GasBaseShape shape in Valves)
  332. {
  333. if (shape.Contains(x, y))
  334. {
  335. return shape;
  336. }
  337. }
  338. foreach (GasBaseShape shape in Buttons)
  339. {
  340. if (shape.Contains(x, y))
  341. {
  342. return shape;
  343. }
  344. }
  345. foreach (GasBaseShape shape in Texts)
  346. {
  347. if (shape.Contains(x, y))
  348. {
  349. return shape;
  350. }
  351. }
  352. foreach (GasBaseShape shape in Circles)
  353. {
  354. if (shape.Contains(x, y))
  355. {
  356. return shape;
  357. }
  358. }
  359. foreach (GasBaseShape shape in Funnels)
  360. {
  361. if (shape.Contains(x, y))
  362. {
  363. return shape;
  364. }
  365. }
  366. foreach (GasBaseShape shape in Lines)
  367. {
  368. if (shape.Contains(x, y))
  369. {
  370. return shape;
  371. }
  372. }
  373. foreach (GasBaseShape shape in PolyLines)
  374. {
  375. if (shape.Contains(x, y))
  376. {
  377. return shape;
  378. }
  379. }
  380. return null;
  381. }
  382. }
  383. }