MathFigure.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using ScottPlot;
  11. using ScottPlot.Plottable;
  12. using Uestc.Auto6.Dso.Core;
  13. using System.Threading;
  14. using Uestc.Auto6.Dso.ComModel;
  15. using System.Diagnostics;
  16. using ScottPlot.Renderable;
  17. using Uni_Trend.MSO7000X.Common.Structs;
  18. using EventBus;
  19. namespace Uestc.Auto6.Dso.U2
  20. {
  21. public partial class MathFigure : BaseDisplayForm, ITimebaseView, ITriggerView, IChnlView
  22. {
  23. public MathFigure(DisplayPrsnt displayPrsnt)
  24. {
  25. InitializeComponent();
  26. ConfigureWavePlot();
  27. _TimerRefresh.Interval = 20;
  28. _TimerRefresh.Tick += TimerRefresh_Tick;
  29. _TimerRefresh.Enabled = true;
  30. _DrawMode = displayPrsnt.DrawMode;
  31. _WfmIntensity = displayPrsnt.WfmIntensity;
  32. _GridStyle = displayPrsnt.GridStyle;
  33. _GridIntensity = displayPrsnt.GridIntensity;
  34. _AxisTickVisible = displayPrsnt.AxisTickVisible;
  35. _XAxisTickBottom = displayPrsnt.XAxisTickBottom;
  36. _YAxisTickRight = displayPrsnt.YAxisTickRight;
  37. }
  38. private System.Windows.Forms.Timer _TimerRefresh = new System.Windows.Forms.Timer();
  39. private Boolean _IsRefreshing = false;
  40. private static readonly Object _ObjectLock = new();
  41. private Boolean _IsDraging = false;
  42. private WavePlot _WavePlot;
  43. public ChannelId Id;
  44. private VLIndicationArea vLIndicationArea;
  45. //private VRIndicationArea vRIndicationArea;
  46. //private HTIndicationArea hTIndicationArea;
  47. /// <summary>
  48. /// 视图设置窗口
  49. /// </summary>
  50. private TimeDomainViewSettingForm _TimeDomainViewSettingForm = null;
  51. #region WavePlot
  52. /// <summary>
  53. /// 更新波形
  54. /// </summary>
  55. private void DrawWave()
  56. {
  57. _IsRefreshing = true;
  58. AddWavePlot();
  59. if (Program.Oscilloscope.TryGetChannel(Id, out IChnlPrsnt channel))
  60. {
  61. //更新数据
  62. //if (channel.Pack != null)
  63. // _WavePlot.YTs = channel.Pack.Buffer;
  64. //if ((channel as MathPrsnt).CalcType == MathType.FFT)
  65. //{
  66. // ScottPlotFormControl.Plot.XAxis.MinorLogScale(true);
  67. // ScottPlotFormControl.Plot.YAxis.MinorLogScale(true);
  68. //}
  69. //else
  70. {
  71. ScottPlotFormControl.Plot.XAxis.MinorLogScale(false);
  72. ScottPlotFormControl.Plot.YAxis.MinorLogScale(false);
  73. }
  74. if (channel.VuDatabase.Current != null)
  75. {
  76. _WavePlot.YTs = (double[,])channel.VuDatabase.Current.Buffer;
  77. _WavePlot.SampleRate = channel.VuDatabase.Current.ZoomRatio;
  78. _WavePlot.OffsetX = channel.VuDatabase.Current.Start;
  79. }
  80. _WavePlot.Color = Color.FromArgb((Int32)(_WfmIntensity / 100d * 255), channel.DrawColor);
  81. ScottPlotFormControl.Plot.ResetChannelParameter(channel.PosIndexBymDiv, (channel as MathPrsnt).Scale, channel.Prefix.ToString(), channel.Unit, Color.FromArgb((Int32)(_GridIntensity / 100d * 255), Color.White/*channel.DrawColor*/));
  82. //更新通道刻度信息
  83. if (Id == DsoPrsnt.FocusId)
  84. {
  85. if (vLIndicationArea != null)
  86. {
  87. vLIndicationArea.IsFocused = true;
  88. }
  89. }
  90. else
  91. {
  92. if (vLIndicationArea != null)
  93. {
  94. vLIndicationArea.IsFocused = false;
  95. }
  96. }
  97. if (vLIndicationArea != null && _IsDraging == false)
  98. {
  99. vLIndicationArea.IsVisible = true;
  100. vLIndicationArea.Color = channel.DrawColor;
  101. vLIndicationArea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-(int)(channel as MathPrsnt).PosIndexBymDiv));
  102. }
  103. if (vLIndicationArea != null)
  104. {
  105. vLIndicationArea.Label = channel.Label;
  106. vLIndicationArea.Name = Id.ToString();
  107. }
  108. }
  109. ScottPlotFormControl.Plot.ResetTimebaseParameter(Presenter.Sampling.PosIndexBymDiv - 5000, Presenter.Sampling.Scale, Presenter.Sampling.Prefix.ToString(), Presenter.Sampling.Unit, Color.FromArgb((Int32)(_GridIntensity / 100d * 255), Color.White));
  110. ScottPlotFormControl.Render(skipIfCurrentlyRendering: true);
  111. _IsRefreshing = false;
  112. }
  113. /// <summary>
  114. /// 添加波形
  115. /// </summary>
  116. private void AddWavePlot()
  117. {
  118. if (_WavePlot == null)
  119. if (Program.Oscilloscope.TryGetChannel(Id, out IChnlPrsnt channel))
  120. if (channel.Pack != null)
  121. _WavePlot = ScottPlotFormControl.Plot.AddWave(channel.Pack.Buffer, sampleRate: 1, isAfterglowOn: false, channel.DrawColor/*, label: channel.Id.ToString()*/);
  122. }
  123. /// <summary>
  124. /// 添加可拖拽指示区域
  125. /// </summary>
  126. private void AddDragArea()
  127. {
  128. PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1);
  129. vLIndicationArea = ScottPlotFormControl.Plot.AddVLIndicationArea(plotDimensions, color: Color.Red);
  130. //vLIndicationArea.LabelFont = new Font("微软雅黑", 8);
  131. vLIndicationArea.Dragged += (sender, e) =>
  132. {
  133. if (Program.Oscilloscope.TryGetChannel(Id, out IChnlPrsnt channel))
  134. {
  135. (channel as MathPrsnt).PosIndexBymDiv = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)vLIndicationArea.GetIndicationValue());
  136. }
  137. };
  138. vLIndicationArea.MouseDown += (sender, e) =>
  139. {
  140. DsoPrsnt.FocusId = Id;
  141. };
  142. ////更新拖动图片
  143. //vLIndicationArea.IndicationImage = new Bitmap(19,19);
  144. //vRIndicationArea.IndicationImage = new Bitmap(19,19);
  145. //hTIndicationArea.IndicationImage = new Bitmap(19,19);
  146. }
  147. /// <summary>
  148. /// 配置显示信息
  149. /// </summary>
  150. private void ConfigureWavePlot()
  151. {
  152. //控件属性配置
  153. ScottPlotFormControl.Configuration.MiddleClickAutoAxisMarginX = 0;
  154. ScottPlotFormControl.Configuration.Quality = 0;
  155. //波形线路名称
  156. ScottPlot.Renderable.Legend legend = ScottPlotFormControl.Plot.Legend();
  157. legend.FontColor = Color.Gray;
  158. legend.OutlineColor = Color.White;
  159. legend.FillColor = Color.Black;
  160. ScottPlotFormControl.Plot.AxisAutoX(margin: 0);
  161. ScottPlotFormControl.Plot.SetAxisLimits(xMin: 0, xMax: 10000, yMin: -5000, yMax: 5000);//初始视阈
  162. ScottPlotFormControl.Plot.SetViewLimits(xMin: -32768, xMax: 32768, yMin: -10000, yMax: 10000);//视阈限制
  163. ScottPlotFormControl.Dock = DockStyle.Fill;
  164. //图形颜色设置
  165. ScottPlotFormControl.Plot.Style(figureBackground: Color.Transparent, dataBackground: Color.Transparent, grid: Color.Gray, tick: Color.Gray, axisLabel: Color.White, titleLabel: Color.Gray);
  166. //网格属性配置
  167. ScottPlotFormControl.Plot.Grid(enable: true, color: Color.Gray, lineStyle: LineStyle.Dot);
  168. ScottPlotFormControl.BackColor = Color.Black;
  169. AddWavePlot();
  170. AddDragArea();
  171. ScottPlotFormControl.Plot.RenderEventHandler += ScottPlotFormControlPlot_RenderEventHandler;
  172. }
  173. private void ScottPlotFormControlPlot_RenderEventHandler(object sender, (Bitmap bmp, PlotDimensions plotDimensions, bool lowQuality) e)
  174. {
  175. CursorApp.Default?.DrawCursor(sender, e);
  176. }
  177. #endregion
  178. #region 窗口显示属性及配置
  179. private WfmDrawMode _DrawMode;
  180. /// <summary>
  181. /// 波形显示风格
  182. /// </summary>
  183. public WfmDrawMode DrawMode
  184. {
  185. get => _DrawMode;
  186. set => _DrawMode = value;
  187. }
  188. private Int32 _WfmIntensity;
  189. /// <summary>
  190. /// 波形亮度
  191. /// </summary>
  192. public Int32 WfmIntensity
  193. {
  194. get => _WfmIntensity;
  195. set => _WfmIntensity = value;
  196. }
  197. private GridType _GridStyle;
  198. /// <summary>
  199. /// 网格风格
  200. /// </summary>
  201. public GridType GridStyle
  202. {
  203. get => _GridStyle;
  204. set => _GridStyle = value;
  205. }
  206. private Int32 _GridIntensity;
  207. /// <summary>
  208. /// 网格亮度
  209. /// </summary>
  210. public Int32 GridIntensity
  211. {
  212. get => _GridIntensity;
  213. set => _GridIntensity = value;
  214. }
  215. private Boolean _AxisTickVisible;
  216. /// <summary>
  217. /// 标记是否可见
  218. /// </summary>
  219. public Boolean AxisTickVisible
  220. {
  221. get => _AxisTickVisible;
  222. set => _AxisTickVisible = value;
  223. }
  224. private Boolean _XAxisTickBottom;
  225. /// <summary>
  226. /// 水平标记是否显示在下方
  227. /// </summary>
  228. public Boolean XAxisTickBottom
  229. {
  230. get => _XAxisTickBottom;
  231. set => _XAxisTickBottom = value;
  232. }
  233. private Boolean _YAxisTickRight;
  234. /// <summary>
  235. /// 垂直标记是否显示在右方
  236. /// </summary>
  237. public Boolean YAxisTickRight
  238. {
  239. get => _YAxisTickRight;
  240. set => _YAxisTickRight = value;
  241. }
  242. /// <summary>
  243. /// 配置波形显示风格
  244. /// </summary>
  245. /// <param name="wfmDrawMode"></param>
  246. private void ConfigDrawMode(WfmDrawMode wfmDrawMode)
  247. {
  248. switch (wfmDrawMode)
  249. {
  250. case WfmDrawMode.Vector:
  251. ScottPlotFormControl.Plot.WavePlotStyle(WavePlotLineStyle.Vector);
  252. break;
  253. case WfmDrawMode.Dot:
  254. ScottPlotFormControl.Plot.WavePlotStyle(WavePlotLineStyle.Dot);
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. /// <summary>
  261. /// 配置波形亮度
  262. /// </summary>
  263. /// <param name="wfmIntensity"></param>
  264. private void ConfigWfmIntensity(Int32 wfmIntensity)
  265. {
  266. ScottPlotFormControl.Plot.WavePlotStyle(wfmIntensity);
  267. }
  268. /// <summary>
  269. /// 配置水平标记的显示位置
  270. /// </summary>
  271. /// <param name="horizontalBottom"></param>
  272. private void ConfigHorizontalTickLabelDirection(Boolean horizontalBottom = true)
  273. {
  274. ScottPlotFormControl.Plot.XAxis.Edge = horizontalBottom == true ? ScottPlot.Renderable.Edge.Bottom : ScottPlot.Renderable.Edge.Top;
  275. }
  276. /// <summary>
  277. /// 配置垂直标记的显示位置
  278. /// </summary>
  279. /// <param name="virticalRight"></param>
  280. private void ConfigVirticalTickLabelDirection(Boolean virticalRight = true)
  281. {
  282. ScottPlotFormControl.Plot.YAxis.Edge = virticalRight == true ? ScottPlot.Renderable.Edge.Right : ScottPlot.Renderable.Edge.Left;
  283. }
  284. /// <summary>
  285. /// 配置标记label的显示
  286. /// </summary>
  287. /// <param name="visible"></param>
  288. private void ConfigTickLabelVisible(Boolean visible = true)
  289. {
  290. ScottPlotFormControl.Plot.XAxis.IsVisible = visible;
  291. ScottPlotFormControl.Plot.YAxis.IsVisible = visible;
  292. }
  293. /// <summary>
  294. /// 配置网格样式
  295. /// </summary>
  296. /// <param name="gridType"></param>
  297. private void ConfigGridStyle(GridType gridType)
  298. {
  299. switch (gridType)
  300. {
  301. case GridType.Full:
  302. ScottPlotFormControl.Plot.Grid(enable: true, lineStyle: LineStyle.Dot);
  303. ScottPlotFormControl.Plot.MinorTickVisible(true);
  304. break;
  305. case GridType.Brief:
  306. ScottPlotFormControl.Plot.Grid(enable: true, lineStyle: LineStyle.Dot);
  307. ScottPlotFormControl.Plot.MinorTickVisible(false);
  308. break;
  309. case GridType.None:
  310. ScottPlotFormControl.Plot.Grid(enable: false);
  311. ScottPlotFormControl.Plot.MinorTickVisible(false);
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. /// <summary>
  318. /// 配置网格亮度
  319. /// </summary>
  320. /// <param name="GridIntensity"></param>
  321. private void ConfigGridIntensity(Int32 GridIntensity)
  322. {
  323. ScottPlotFormControl.Plot.Grid(color: Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.Gray));
  324. }
  325. private void OnLostFocusClick_FloatForm(object obj, EventArgs e)
  326. {
  327. var form = obj as Uni_Trend.MSO7000X.UserControls.FloatForm;
  328. if (form != null)
  329. {
  330. form.Close();
  331. }
  332. }
  333. private void ToolStripMenuItemTimeWave_Click(object sender, EventArgs e)
  334. {
  335. //if ((Program.Oscilloscope.View as DsoForm)?.OperateForm != null)
  336. //{
  337. // (Program.Oscilloscope.View as DsoForm)?.OperateForm.Close();
  338. //}
  339. if (_TimeDomainViewSettingForm != null)
  340. {
  341. _TimeDomainViewSettingForm.Close();
  342. }
  343. _TimeDomainViewSettingForm = new TimeDomainViewSettingForm();
  344. _TimeDomainViewSettingForm.SetGridComboBoxSelectType(Enum.GetValues<GridType>());
  345. XAxisTickType xaxisticktype = ScottPlotFormControl.Plot.XAxis.Edge == Edge.Bottom ? XAxisTickType.Bottom : XAxisTickType.Top;
  346. YAxisTickType yaxisticktype = ScottPlotFormControl.Plot.YAxis.Edge == Edge.Left ? YAxisTickType.Left : YAxisTickType.Right;
  347. _TimeDomainViewSettingForm.SetMutexButtonModeType(xaxisticktype, yaxisticktype, DrawMode);
  348. _TimeDomainViewSettingForm.SetParameterValue(TimeEventParameterType.TickIntensity, GridIntensity);
  349. _TimeDomainViewSettingForm.SetParameterValue(TimeEventParameterType.WFMIntensity, WfmIntensity);
  350. _TimeDomainViewSettingForm.SetParameterValue(TimeEventParameterType.AxisTick, ScottPlotFormControl.Plot.XAxis.IsVisible);
  351. _TimeDomainViewSettingForm.ParameterChangedEventHandler += TimeDomainViewSettingForm_ParameterChangedEventHandler;
  352. if (Program.Oscilloscope.View != null && Program.Oscilloscope.View is DsoForm dsoForm)
  353. {
  354. Int32 locationx = (dsoForm.Width - _TimeDomainViewSettingForm.Width) / 2;
  355. locationx = locationx > 0 ? locationx : 0;
  356. Int32 locationy = (dsoForm.Height - _TimeDomainViewSettingForm.Height) / 2;
  357. locationy = locationy > 0 ? locationy : 0;
  358. _TimeDomainViewSettingForm.Location = new Point(locationx, locationy);
  359. }
  360. _TimeDomainViewSettingForm.FormClosed += (sender, e) =>
  361. {
  362. _TimeDomainViewSettingForm.ParameterChangedEventHandler -= TimeDomainViewSettingForm_ParameterChangedEventHandler;
  363. _TimeDomainViewSettingForm = null;
  364. };
  365. FormEventArgs formeventargs = new FormEventArgs() { Current = _TimeDomainViewSettingForm, Type = FormType.SettingForm };
  366. EventBroker.Instance.GetEvent<FormEventArgs>().Publish(this, formeventargs);
  367. }
  368. private void TimeDomainViewSettingForm_ParameterChangedEventHandler(TimeEventParameterType type, object obj)
  369. {
  370. switch (type)
  371. {
  372. case TimeEventParameterType.AxisTick:
  373. {
  374. if (obj is Boolean visible)
  375. {
  376. AxisTickVisible = visible;
  377. ConfigTickLabelVisible(visible);
  378. }
  379. }
  380. break;
  381. case TimeEventParameterType.XAxisTick:
  382. {
  383. if (obj is XAxisTickType timeBaseMarkerType)
  384. {
  385. XAxisTickBottom = timeBaseMarkerType == XAxisTickType.Bottom;
  386. ConfigHorizontalTickLabelDirection(timeBaseMarkerType == XAxisTickType.Bottom);
  387. }
  388. }
  389. break;
  390. case TimeEventParameterType.YAxisTick:
  391. {
  392. if (obj is YAxisTickType verticalGearMarkerType)
  393. {
  394. YAxisTickRight = verticalGearMarkerType == YAxisTickType.Right;
  395. ConfigVirticalTickLabelDirection(verticalGearMarkerType == YAxisTickType.Right);
  396. }
  397. }
  398. break;
  399. case TimeEventParameterType.WFMType:
  400. if (obj is WfmDrawMode mode)
  401. {
  402. DrawMode = mode;
  403. ConfigDrawMode(DrawMode);
  404. }
  405. break;
  406. case TimeEventParameterType.WFMIntensity:
  407. if (obj is Int32 int32)
  408. {
  409. WfmIntensity = int32;
  410. ConfigWfmIntensity(WfmIntensity);
  411. }
  412. break;
  413. case TimeEventParameterType.TickType:
  414. if (obj is GridType value)
  415. {
  416. GridStyle = value;
  417. ConfigGridStyle(GridStyle);
  418. }
  419. break;
  420. case TimeEventParameterType.TickIntensity:
  421. if (obj is Int32 intensity)
  422. {
  423. GridIntensity = intensity;
  424. ConfigGridIntensity(GridIntensity);
  425. }
  426. break;
  427. default:
  428. break;
  429. }
  430. }
  431. #endregion
  432. #region 事件
  433. private void MathViewForm_Load(object sender, EventArgs e)
  434. {
  435. ConfigGridIntensity(GridIntensity);
  436. ConfigGridStyle(GridStyle);
  437. ConfigWfmIntensity(WfmIntensity);
  438. ConfigDrawMode(DrawMode);
  439. ConfigTickLabelVisible(AxisTickVisible);
  440. ConfigVirticalTickLabelDirection(XAxisTickBottom);
  441. ConfigHorizontalTickLabelDirection(YAxisTickRight);
  442. }
  443. private void TimerRefresh_Tick(object sender, EventArgs e)
  444. {
  445. if (_IsRefreshing == false)
  446. DrawWave();
  447. }
  448. protected override void OnFormClosed(FormClosedEventArgs e)
  449. {
  450. TmbPresenter.TryRemoveView(this);
  451. TrgPresenter.TryRemoveView(this);
  452. Presenter.TryRemoveView(this);
  453. Presenter.Active = false;
  454. (Program.Oscilloscope.View as DsoForm).RemoveBadge(Presenter);
  455. //(Program.Oscilloscope.View as DsoForm).DestroyDisplayForm(this);
  456. }
  457. ~MathFigure()
  458. {
  459. if (_TimerRefresh != null)
  460. _TimerRefresh.Dispose();
  461. }
  462. #endregion
  463. #region IDsoView
  464. private MathPrsnt _Presenter;
  465. public MathPrsnt Presenter
  466. {
  467. get => _Presenter;
  468. set
  469. {
  470. _Presenter = value;
  471. }
  472. }
  473. //IChnlPrsnt IView<IChnlPrsnt>.Presenter
  474. //{
  475. // get => Presenter;
  476. // set => Presenter = (MathPrsnt)value;
  477. //}
  478. IBadge IView<IBadge>.Presenter
  479. {
  480. get => Presenter;
  481. set => Presenter = (MathPrsnt)value;
  482. }
  483. public TimebasePrsnt TmbPresenter
  484. {
  485. get;
  486. set;
  487. }
  488. ITimebasePrsnt IView<ITimebasePrsnt>.Presenter
  489. {
  490. get => TmbPresenter;
  491. set => TmbPresenter = (TimebasePrsnt)value;
  492. }
  493. public TriggerPrsnt TrgPresenter
  494. {
  495. get;
  496. set;
  497. }
  498. ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
  499. {
  500. get => TrgPresenter;
  501. set => TrgPresenter = (TriggerPrsnt)value;
  502. }
  503. public void UpdateView(String propertyName)
  504. {
  505. if (String.IsNullOrEmpty(propertyName))
  506. {
  507. UpdateView();
  508. return;
  509. }
  510. switch (propertyName)
  511. {
  512. //case nameof(TrgPresenter.):
  513. // break;
  514. //case nameof(TmbPresenter.Length):
  515. // break;
  516. case nameof(TmbPresenter.ScaleByus):
  517. ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PositionByus, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.White);
  518. break;
  519. case nameof(TmbPresenter.PositionByus):
  520. ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PositionByus, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.White);
  521. break;
  522. }
  523. }
  524. protected void UpdateView()
  525. {
  526. }
  527. #endregion
  528. }
  529. }