GasDxfDrawShape.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using System.Globalization;
  11. using System.Windows.Controls;
  12. using Newtonsoft.Json.Linq;
  13. using System.Xml.Linq;
  14. using Microsoft.Win32.SafeHandles;
  15. using System.Windows.Input;
  16. using MECF.Framework.UI.Core.DxfScript.Converter;
  17. using SciChart.Core.Extensions;
  18. namespace MECF.Framework.UI.Core.DxfScript
  19. {
  20. /// <summary>
  21. /// 名称:Draw类
  22. /// 用途:根据自定义对象画页面元素
  23. /// </summary>
  24. public partial class GasDxfDocument
  25. {
  26. //画全部元素
  27. public void Draw()
  28. {
  29. if (Lines.Count == 0)
  30. {
  31. return;
  32. }
  33. draw.ClearDrawing();
  34. CreateGraphics();
  35. InitScale();
  36. foreach (var line in Lines)
  37. {
  38. DrawLine(line, false);
  39. }
  40. foreach (GasCircle circle in Circles)
  41. {
  42. DrawCircle(circle, false);
  43. }
  44. foreach (GasFunnel funnel in Funnels)
  45. {
  46. DrawFunnel(funnel, false);
  47. }
  48. foreach (GasText text in Texts)
  49. {
  50. DrawText(text, false);
  51. }
  52. foreach (GasPolyLine polyLine in PolyLines)
  53. {
  54. DrawPolyLine(polyLine, false);
  55. }
  56. foreach (GasAITValve valve in Valves)
  57. {
  58. DrawValve(valve, false);
  59. }
  60. foreach (GasButton button in Buttons)
  61. {
  62. DrawButton(button, false);
  63. }
  64. foreach (GasAnalogControl4Jet analog in Analogs)
  65. {
  66. DrawAnalog(analog, false);
  67. }
  68. CloseGraphics();
  69. draw.SaveDrawing();
  70. }
  71. //清除所有元素
  72. private void ClearDxf()
  73. {
  74. if (Lines != null)
  75. {
  76. Lines.Clear();
  77. }
  78. else
  79. {
  80. Lines = new List<GasLine>();
  81. }
  82. if (PolyLines != null)
  83. {
  84. PolyLines.Clear();
  85. }
  86. else
  87. {
  88. PolyLines = new List<GasPolyLine>();
  89. }
  90. if (Circles != null)
  91. {
  92. Circles.Clear();
  93. }
  94. else
  95. {
  96. Circles = new List<GasCircle>();
  97. }
  98. if (Funnels != null)
  99. {
  100. Funnels.Clear();
  101. }
  102. else
  103. {
  104. Funnels = new List<GasFunnel>();
  105. }
  106. if (Texts != null)
  107. {
  108. Texts.Clear();
  109. }
  110. else
  111. {
  112. Texts = new List<GasText>();
  113. }
  114. if (Valves != null)
  115. {
  116. Valves.Clear();
  117. }
  118. else
  119. {
  120. Valves = new List<GasAITValve>();
  121. }
  122. if (Buttons != null)
  123. {
  124. Buttons.Clear();
  125. }
  126. else
  127. {
  128. Buttons = new List<GasButton>();
  129. }
  130. if (Analogs != null)
  131. {
  132. Analogs.Clear();
  133. }
  134. else
  135. {
  136. Analogs = new List<GasAnalogControl4Jet>();
  137. }
  138. DxfWidth = 0;
  139. DxfHeight = 0;
  140. MinX = 100000;
  141. MinY = 100000;
  142. MaxX = 0;
  143. MaxY = 0;
  144. Scale = 1;
  145. dragX = 0;
  146. dragY = 0;
  147. }
  148. //开始画元素
  149. public void DrawShape(GasBaseShape shape, bool isSelected)
  150. {
  151. CreateGraphics();
  152. draw.LoadDrawing(dragX, dragY);
  153. if (shape is GasFunnel)
  154. {
  155. DrawFunnel(shape as GasFunnel, isSelected);
  156. }
  157. if (shape is GasLine)
  158. {
  159. DrawLine(shape as GasLine, isSelected);
  160. }
  161. else if (shape is GasCircle)
  162. {
  163. DrawCircle(shape as GasCircle, isSelected);
  164. }
  165. else if (shape is GasText)
  166. {
  167. DrawText(shape as GasText, isSelected);
  168. }
  169. else if (shape is GasPolyLine)
  170. {
  171. DrawPolyLine(shape as GasPolyLine, isSelected);
  172. }
  173. else if (shape is GasAITValve)
  174. {
  175. DrawValve(shape as GasAITValve, isSelected);
  176. }
  177. else if (shape is GasButton)
  178. {
  179. DrawButton(shape as GasButton, isSelected);
  180. }
  181. else if (shape is GasAnalogControl4Jet)
  182. {
  183. DrawAnalog(shape as GasAnalogControl4Jet, isSelected);
  184. }
  185. CloseGraphics();
  186. }
  187. //设置画笔颜色
  188. private Brush GetGasShapeBrush(GasBaseShape shape, bool isSelected)
  189. {
  190. if (shape == null) return NormalBrush;
  191. if (!IsExecuted)
  192. {
  193. return GetDevelopmentBrush(shape, isSelected);
  194. }
  195. else
  196. {
  197. return GetExecutionBrush(shape);
  198. }
  199. }
  200. private Pen GetPenByBrush(Brush brush)
  201. {
  202. if (brush == NormalBrush)
  203. {
  204. return NormalPen;
  205. }
  206. else if (brush == SelectedBrush)
  207. {
  208. return SelectedPen;
  209. }
  210. else if (brush == WithConditionBrush)
  211. {
  212. return WithConditionPen;
  213. }
  214. else if (brush == ReturnTrueBrush)
  215. {
  216. return ReturnTruePen;
  217. }
  218. else if (brush == WithErrorBrush)
  219. {
  220. return WithErrorPen;
  221. }
  222. else if (brush == TransparentBrush)
  223. {
  224. return TransparentPen;
  225. }
  226. else
  227. {
  228. return null;
  229. }
  230. }
  231. //画单线
  232. private void DrawLine(GasLine line, bool isSelected, Brush brush = null)
  233. {
  234. if (brush == null)
  235. brush = GetGasShapeBrush(line, isSelected);
  236. Pen pen = GetPenByBrush(brush);
  237. double x1 = line.X1 * rate * Scale + XSPACE + dragX;
  238. double x2 = line.X2 * rate * Scale + XSPACE + dragX;
  239. double y1 = (DxfHeight - line.Y1) * rate * Scale + YSPACE + dragY;
  240. double y2 = (DxfHeight - line.Y2) * rate * Scale + YSPACE + dragY;
  241. dc.DrawLine(pen, new Point(x1, y1), new Point(x2, y2));
  242. }
  243. //画漏斗
  244. private void DrawFunnel(GasFunnel funnel, bool isSelected, Brush brush = null)
  245. {
  246. if (funnel == null) return;
  247. foreach (var line in funnel.GasLines)
  248. {
  249. DrawLine(line, isSelected, brush);
  250. }
  251. }
  252. //画圆
  253. private void DrawCircle(GasCircle circle, bool isSelected, Brush brush = null)
  254. {
  255. if (brush == null) brush = GetGasShapeBrush(circle, isSelected); ;
  256. Pen pen = GetPenByBrush(brush);
  257. if (brush == NormalBrush)
  258. {
  259. pen = NormalPen;
  260. brush = TransparentBrush;
  261. }
  262. else
  263. {
  264. pen = NormalPen;
  265. //brush不变
  266. }
  267. double x = (circle.X) * rate * Scale + XSPACE + dragX;//圆心x
  268. double y = (DxfHeight - circle.Y) * rate * Scale + YSPACE + dragY;//圆心y
  269. double r = circle.R * rate * Scale;
  270. if (circle.StartAngle > 0)
  271. {
  272. DrawGeometry(circle, isSelected, brush);
  273. }
  274. else
  275. {
  276. dc.DrawEllipse(brush, pen, new Point(x, y), r, r);
  277. }
  278. }
  279. //画弧线
  280. public void DrawGeometry(GasCircle circle, bool isSelected, Brush brush = null)
  281. {
  282. if (brush == null) brush = GetGasShapeBrush(circle, isSelected); ;
  283. Pen pen = GetPenByBrush(brush);
  284. if (brush == NormalBrush)
  285. {
  286. pen = NormalPen;
  287. brush = TransparentBrush;
  288. }
  289. else
  290. {
  291. pen = NormalPen;
  292. //brush不变
  293. }
  294. double x = (circle.X) * rate * Scale + XSPACE + dragX;//圆心x
  295. double y = (DxfHeight - circle.Y) * rate * Scale + YSPACE + dragY;//圆心y
  296. double r = circle.R * rate * Scale;
  297. Rect arcRect = new Rect(x - r, y - r, 2 * r, 2 * r);
  298. // 定义圆弧的起始角度和扫过角度
  299. double startAngle = circle.StartAngle; // 起始角度
  300. double sweepAngle = circle.EndAngle - circle.StartAngle; // 扫过角度
  301. // 将角度转换为弧度
  302. double startAngleRad = startAngle * Math.PI / 180;
  303. double sweepAngleRad = sweepAngle * Math.PI / 180;
  304. // 计算起始点和结束点
  305. Point startPoint = new Point(
  306. arcRect.X + arcRect.Width / 2 + arcRect.Width / 2 * Math.Cos(startAngleRad),
  307. arcRect.Y + arcRect.Height / 2 - arcRect.Height / 2 * Math.Sin(startAngleRad));
  308. Point endPoint = new Point(
  309. arcRect.X + arcRect.Width / 2 + arcRect.Width / 2 * Math.Cos(startAngleRad + sweepAngleRad),
  310. arcRect.Y + arcRect.Height / 2 - arcRect.Height / 2 * Math.Sin(startAngleRad + sweepAngleRad));
  311. // 定义弧形段
  312. ArcSegment arcSegment = new ArcSegment
  313. {
  314. Point = endPoint,
  315. Size = new Size(arcRect.Width / 2, arcRect.Height / 2),
  316. RotationAngle = 0,
  317. IsLargeArc = (sweepAngle - startAngle) > 180,
  318. SweepDirection = SweepDirection.Counterclockwise
  319. };
  320. // 定义路径段
  321. PathFigure pathFigure = new PathFigure
  322. {
  323. StartPoint = startPoint,
  324. Segments = new PathSegmentCollection { arcSegment }
  325. };
  326. // 定义路径几何
  327. PathGeometry pathGeometry = new PathGeometry();
  328. pathGeometry.Figures.Add(pathFigure);
  329. // 使用 DrawingContext 绘制路径
  330. dc.DrawGeometry(brush, pen, pathGeometry);
  331. }
  332. //画文本
  333. private void DrawText(GasText text, bool isSelected, Brush brush = null)
  334. {
  335. if (brush == null) brush = GetGasShapeBrush(text, isSelected); ;
  336. Pen pen = GetPenByBrush(brush);
  337. if (string.IsNullOrEmpty(text.Text)) return;
  338. string[] textLines = Regex.Split(text.Text, "\\\\P");
  339. for (int i = 0; i < textLines.Length; i++)
  340. {
  341. double x = text.X;
  342. double y = text.Y;
  343. double rectWidth = text.RectWidth;
  344. double rectHeight = text.RectHeight;
  345. double textWidth = GetFontWidth(textLines[i], text.FontSize);
  346. double textHeight = text.FontSize;
  347. double fontSize = text.FontSize;
  348. int alignment = text.Alignment;
  349. double lineSpace = text.LineSpace;
  350. switch (alignment)
  351. {
  352. case 1:
  353. x += (rectWidth - textWidth) / 2;
  354. y = y - (rectHeight - textHeight) / 2;
  355. y -= (textHeight + lineSpace) * i;
  356. break;
  357. case 4:
  358. x += (rectWidth - textWidth) / 2;
  359. y += textHeight / 2;
  360. y -= (textHeight + lineSpace) * i;
  361. break;
  362. case 7:
  363. x += (rectWidth - textWidth) / 2;
  364. y += (rectHeight + textHeight) / 2;
  365. y -= (textHeight + lineSpace) * i;
  366. break;
  367. case 2:
  368. x -= textWidth / 2;
  369. y = y - (rectHeight - textHeight) / 2;
  370. y -= (textHeight + lineSpace) * i;
  371. break;
  372. case 5:
  373. x -= textWidth / 2;
  374. y += textHeight / 2;
  375. y -= (textHeight + lineSpace) * i;
  376. break;
  377. case 8:
  378. x -= textWidth / 2;
  379. y += (rectHeight + textHeight) / 2;
  380. y -= (textHeight + lineSpace) * i;
  381. break;
  382. case 3:
  383. x -= (rectWidth + textWidth) / 2;
  384. y = y - (rectHeight - textHeight) / 2;
  385. y -= (textHeight + lineSpace) * i;
  386. break;
  387. case 6:
  388. x -= (rectWidth + textWidth) / 2;
  389. y += textHeight / 2;
  390. y -= (textHeight + lineSpace) * i;
  391. break;
  392. case 9:
  393. x -= (rectWidth + textWidth) / 2;
  394. y += (rectHeight + textHeight) / 2;
  395. y -= (textHeight + lineSpace) * i;
  396. break;
  397. }
  398. x = x * rate * Scale + XSPACE + dragX;
  399. y = (DxfHeight - y) * rate * Scale + YSPACE + dragY;
  400. dc.DrawText(GetFormattedText(textLines[i], text.FontSize * rate * Scale, brush), new Point(x, y));
  401. }
  402. }
  403. //画多线
  404. private void DrawPolyLine(GasPolyLine polyLine, bool isSelected, Brush brush = null)
  405. {
  406. if (brush == null) brush = GetGasShapeBrush(polyLine, isSelected); ;
  407. Pen pen = GetPenByBrush(brush);
  408. foreach (GasLine line in polyLine.InnerLines)
  409. {
  410. DrawLine(line, isSelected, brush);
  411. }
  412. }
  413. //画按钮
  414. private void DrawButton(GasButton button, bool isSelected, Brush brush = null)
  415. {
  416. if (brush == null) brush = GetGasShapeBrush(button, isSelected); ;
  417. Pen pen = GetPenByBrush(brush);
  418. if (brush == NormalBrush)
  419. {
  420. pen = NormalPen;
  421. brush = TransparentBrush;
  422. }
  423. else
  424. {
  425. pen = NormalPen;
  426. }
  427. double x, y, width, height;
  428. x = Math.Min(button.InnerPolyLine.InnerLines[0].X1, button.InnerPolyLine.InnerLines[0].X2);
  429. y = Math.Max(button.InnerPolyLine.InnerLines[0].Y1, button.InnerPolyLine.InnerLines[0].Y2);
  430. width = Math.Abs(button.InnerPolyLine.InnerLines[0].X1 - button.InnerPolyLine.InnerLines[0].X2);
  431. height = Math.Abs(button.InnerPolyLine.InnerLines[1].Y1 - button.InnerPolyLine.InnerLines[1].Y2);
  432. x = x * rate * Scale + XSPACE + dragX;
  433. y = (DxfHeight - y) * rate * Scale + YSPACE + dragY;
  434. width = width * rate * Scale;
  435. height = height * rate * Scale;
  436. dc.DrawRectangle(brush, pen, new Rect(x, y, width, height));
  437. DrawText(button.InnerText, isSelected, NormalBrush);
  438. }
  439. //画阀
  440. private void DrawValve(GasAITValve valve, bool isSelected, Brush brush = null)
  441. {
  442. if (brush == null) brush = GetGasShapeBrush(valve, isSelected); ;
  443. Pen pen = GetPenByBrush(brush);
  444. DrawCircle(valve.InnerCircle, isSelected, brush);
  445. DrawText(valve.InnerText, isSelected, NormalBrush);
  446. if (valve.InnerPolyLine != null && valve.InnerPolyLine.InnerLines != null)
  447. {
  448. DrawPolyLine(valve.InnerPolyLine, isSelected, NormalBrush);
  449. }
  450. }
  451. //画按钮
  452. private void DrawAnalog(GasAnalogControl4Jet analog, bool isSelected, Brush brush = null)
  453. {
  454. if (brush == null) brush = GetGasShapeBrush(analog, isSelected); ;
  455. Pen pen = GetPenByBrush(brush);
  456. DrawButton(analog.TopButton, isSelected, brush);
  457. DrawButton(analog.LeftButton, isSelected, brush);
  458. DrawButton(analog.RightButton, isSelected, brush);
  459. }
  460. //创建自定义画布对象
  461. private void CreateGraphics()
  462. {
  463. if (dc == null) dc = draw.CreateGraphics();
  464. }
  465. //清除画布对象
  466. private void CloseGraphics()
  467. {
  468. if (dc != null)
  469. {
  470. draw.Dispose();
  471. }
  472. dc = null;
  473. }
  474. private double GetFontWidth(string text, double fontSize)
  475. {
  476. if (computeSizeTextBlock == null)
  477. {
  478. computeSizeTextBlock = new TextBlock();
  479. PixelsPerDip = DpiHelper.GetPixelsPerDip(Application.Current.MainWindow);
  480. }
  481. computeSizeTextBlock.Text = text;
  482. computeSizeTextBlock.FontSize = fontSize == 0 ? 15 : fontSize;
  483. computeSizeTextBlock.FontFamily = new FontFamily("宋体");
  484. FormattedText formattedText = new FormattedText(
  485. computeSizeTextBlock.Text,
  486. CultureInfo.GetCultureInfo("zh-CN"),
  487. FlowDirection.LeftToRight,
  488. new Typeface(computeSizeTextBlock.FontFamily, computeSizeTextBlock.FontStyle, computeSizeTextBlock.FontWeight, computeSizeTextBlock.FontStretch),
  489. computeSizeTextBlock.FontSize,
  490. Brushes.Black);
  491. return formattedText.Width;
  492. }
  493. private FormattedText GetFormattedText(string text, double fontSize, Brush brush)
  494. {
  495. if (computeSizeTextBlock == null)
  496. {
  497. computeSizeTextBlock = new TextBlock();
  498. }
  499. computeSizeTextBlock.Text = text;
  500. computeSizeTextBlock.FontSize = fontSize == 0 ? 15 : fontSize;
  501. computeSizeTextBlock.FontFamily = new FontFamily("宋体");
  502. FormattedText formattedText = new FormattedText(
  503. computeSizeTextBlock.Text,
  504. CultureInfo.GetCultureInfo("zh-CN"),
  505. FlowDirection.LeftToRight,
  506. new Typeface(computeSizeTextBlock.FontFamily, computeSizeTextBlock.FontStyle, computeSizeTextBlock.FontWeight, computeSizeTextBlock.FontStretch),
  507. computeSizeTextBlock.FontSize,
  508. brush);
  509. return formattedText;
  510. }
  511. #region 根据不同环境对画刷处理
  512. //主方法 GetGasShapeBrush:根据 IsExecuted 的状态分为开发态和执行态处理。
  513. //开发态处理 GetDevelopmentBrush:处理开发态下的逻辑,包括条件检查和选择画刷。
  514. //如果形状未启用,直接返回 DisabledBrush。
  515. //依次检查 BoolCondition 和 StringCondition,如果有错误,直接返回 WithErrorBrush。
  516. //处理特定类型的形状逻辑。
  517. //条件检查 CheckAndHandleCondition:统一处理 BoolCondition 和 StringCondition 的检查逻辑,减少代码重复。
  518. //执行态处理 GetExecutionBrush:处理执行态下的逻辑,包括条件执行和选择画刷。
  519. //如果形状未启用,直接返回 DisabledBrush。
  520. //依次执行 BoolCondition 和 StringCondition,如果有错误,直接返回 WithErrorBrush。
  521. //条件执行 ExecuteAndHandleCondition:统一处理 BoolCondition 和 StringCondition 的执行逻辑,减少代码重复。
  522. //处理开发环境下的特定形状 HandleSpecialShapesDevelopment:处理开发环境下特定形状的逻辑。
  523. //处理执行环境下的特定形状 HandleSpecialShapesExecution:处理执行环境下特定形状的逻辑。
  524. private Brush GetDevelopmentBrush(GasBaseShape shape, bool isSelected)
  525. {
  526. if (isSelected) return SelectedBrush;
  527. if (!shape.Enable) return DisabledBrush;
  528. Brush brush = NormalBrush;
  529. brush = CheckAndHandleCondition(shape.BoolCondition, brush, shape.Id, BoolConditions);
  530. if (brush == WithErrorBrush) return brush;
  531. brush = CheckAndHandleCondition(shape.StringCondition, brush, shape.Id, StringConditions);
  532. if (brush == WithErrorBrush) return brush;
  533. brush = CheckAndHandleCondition(shape.ClickCondition, brush, shape.Id, ClickConditions);
  534. if (brush == WithErrorBrush) return brush;
  535. brush = HandleSpecialShapesDevelopment(shape, brush);
  536. return brush;
  537. }
  538. private Brush HandleSpecialShapesDevelopment(GasBaseShape shape, Brush currentBrush)
  539. {
  540. if (shape is GasAITValve)
  541. {
  542. // 处理 GasAITValve 的开发环境逻辑
  543. return currentBrush;
  544. }
  545. else if (shape is GasAnalogControl4Jet)
  546. {
  547. // 处理 GasAnalogControl4Jet 的开发环境逻辑
  548. return currentBrush;
  549. }
  550. return currentBrush;
  551. }
  552. private Brush CheckAndHandleCondition(Script condition, Brush currentBrush, int shapeId, Dictionary<int, string> conditionsDict)
  553. {
  554. if (condition == null || condition.Result == ScriptResult.EMPTY)
  555. {
  556. return currentBrush;
  557. }
  558. else if (condition.Result == ScriptResult.SUCCESS)
  559. {
  560. return WithConditionBrush;
  561. }
  562. else if (condition.Result == ScriptResult.ERROR)
  563. {
  564. if (conditionsDict.ContainsKey(shapeId))
  565. {
  566. if (!conditionsDict[shapeId].EndsWith("TODO"))
  567. {
  568. conditionsDict[shapeId] += "\n//" + condition.Error + ":TODO";
  569. }
  570. }
  571. return WithErrorBrush;
  572. }
  573. return currentBrush;
  574. }
  575. private Brush GetExecutionBrush(GasBaseShape shape)
  576. {
  577. bool isExec = shape.ClickCondition == null && shape.BoolCondition == null && shape.StringCondition == null;
  578. if (!shape.Enable&& isExec)
  579. {
  580. return DisabledBrush;
  581. }
  582. Brush brush = NormalBrush;
  583. brush = ExecuteAndHandleCondition(shape.ClickCondition, brush, shape);
  584. brush = ExecuteAndHandleCondition(shape.BoolCondition, brush, shape);
  585. if (brush == WithErrorBrush) return brush;
  586. brush = ExecuteAndHandleCondition(shape.StringCondition, brush, shape);
  587. if (brush == WithErrorBrush) return brush;
  588. return brush;
  589. }
  590. private Brush ExecuteAndHandleCondition(Script condition, Brush currentBrush, GasBaseShape shape)
  591. {
  592. if (condition == null) return currentBrush;
  593. if (condition.Result == ScriptResult.EMPTY)
  594. {
  595. return currentBrush;
  596. }
  597. //由于一直在Draw(),会不断的执行脚本 导致十分卡顿
  598. //而且由于采用切换key的方法读取值,所以这部分不需要一直执行脚本
  599. //if (isExecute)
  600. //{
  601. // condition.Execute();
  602. //}
  603. if (condition.Result == ScriptResult.SUCCESS)
  604. {
  605. return HandleSpecialShapes(condition, currentBrush, shape);
  606. }
  607. else if (condition.Result == ScriptResult.ERROR)
  608. {
  609. // return WithErrorBrush;
  610. return NormalBrush;
  611. }
  612. return currentBrush;
  613. }
  614. private Brush HandleSpecialShapes(Script condition, Brush currentBrush, GasBaseShape shape)
  615. {
  616. // 提取重复的逻辑到一个方法
  617. Brush defaultBrush = GetDefaultBrush(shape, currentBrush);
  618. switch (shape)
  619. {
  620. case GasAITValve aiTValve:
  621. var result = condition.ReadValue<bool>(aiTValve.Key);
  622. return result ? ReturnTrueBrush : defaultBrush;
  623. case GasAnalogControl4Jet mfc:
  624. var value = condition.ReadValue<double>(mfc.ValueKey);
  625. mfc.LeftButton.InnerText.Text = value.ToString("f2");
  626. mfc.RightButton.InnerText.Text = condition.ReadValue<string>(mfc.UnitKey);
  627. return value>0?GasMapProvider.ChangeMFCColor:defaultBrush;
  628. case GasButton btn:
  629. return condition.ReadValue<bool>(btn.DataKey) ? ReturnTrueBrush : defaultBrush;
  630. case GasCircle cycle:
  631. return !string.IsNullOrEmpty(cycle.Key) && condition.ReadValue<bool>(cycle.Key) ? ReturnTrueBrush : defaultBrush;
  632. default:
  633. return defaultBrush;
  634. }
  635. }
  636. private Brush GetDefaultBrush(GasBaseShape shape, Brush currentBrush)
  637. {
  638. // 如果 shape 自身有 Brush,则使用它;否则,使用传入的 currentBrush
  639. return shape.Brush ?? currentBrush;
  640. }
  641. #endregion
  642. }
  643. }