123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using ScottPlot;
- using ScottPlot.Plottable;
- using Uestc.Auto6.Dso.Core;
- using System.Threading;
- using Uestc.Auto6.Dso.ComModel;
- using System.Diagnostics;
- using ScottPlot.Renderable;
- using Uni_Trend.MSO7000X.Common.Structs;
- using EventBus;
- namespace Uestc.Auto6.Dso.U2
- {
- public partial class ReferenceFigure : BaseDisplayForm, ITimebaseView, ITriggerView, IChnlView
- {
- public ReferenceFigure(DisplayPrsnt displayPrsnt)
- {
- InitializeComponent();
- ConfigureWavePlot();
- _TimerRefresh.Interval = 50;
- _TimerRefresh.Tick += TimerRefresh_Tick;
- _TimerRefresh.Enabled = true;
- _DrawMode = displayPrsnt.DrawMode;
- _WfmIntensity = displayPrsnt.WfmIntensity;
- _GridStyle = displayPrsnt.GridStyle;
- _GridIntensity = displayPrsnt.GridIntensity;
- _AxisTickVisible = displayPrsnt.AxisTickVisible;
- _XAxisTickBottom = displayPrsnt.XAxisTickBottom;
- _YAxisTickRight = displayPrsnt.YAxisTickRight;
- }
- private System.Windows.Forms.Timer _TimerRefresh = new System.Windows.Forms.Timer();
- private Boolean _IsRefreshing = false;
- private static readonly Object _ObjectLock = new();
- private Boolean _IsDraging = false;
- private WavePlot _WavePlot;
- public ChannelId Id;
- private VLIndicationArea vLIndicationArea;
- //private VRIndicationArea vRIndicationArea;
- //private HTIndicationArea hTIndicationArea;
- /// <summary>
- /// 视图设置窗口
- /// </summary>
- private TimeDomainViewSettingForm _TimeDomainViewSettingForm = null;
- #region WavePlot
- /// <summary>
- /// 更新波形
- /// </summary>
- private void DrawWave()
- {
- _IsRefreshing = true;
- AddWavePlot();
- if (Program.Oscilloscope.TryGetChannel(Id, out IChnlPrsnt channel))
- {
- //更新数据
- //if (channel.Pack != null)
- // _WavePlot.YTs = channel.Pack.Buffer;
- if (channel.VuDatabase.Current != null)
- {
- _WavePlot.YTs = (double[,])channel.VuDatabase.Current.Buffer;
- _WavePlot.SampleRate = channel.VuDatabase.Current.ZoomRatio;
- _WavePlot.OffsetX = channel.VuDatabase.Current.Start;
- }
- _WavePlot.Color = Color.FromArgb((Int32)(_WfmIntensity / 100d * 255), channel.DrawColor);
- //更新通道刻度信息
- if (Id == DsoPrsnt.FocusId)
- {
- ScottPlotFormControl.Plot.ResetChannelParameter((int)(channel as ReferencePrsnt).PosIndexBymDiv, (channel as ReferencePrsnt).Scale, (channel as ReferencePrsnt).Prefix.ToString(), channel.Unit, Color.FromArgb((Int32)(_GridIntensity / 100d * 255), Color.White/*channel.DrawColor*/));
- if (vLIndicationArea != null)
- {
- vLIndicationArea.IsFocused = true;
- }
- }
- else
- {
- if (vLIndicationArea != null)
- {
- vLIndicationArea.IsFocused = false;
- }
- }
- if (vLIndicationArea != null && _IsDraging == false)
- {
- vLIndicationArea.IsVisible = true;
- vLIndicationArea.Color = channel.DrawColor;
- vLIndicationArea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-(int)(channel as ReferencePrsnt).PosIndexBymDiv));
- }
- if (vLIndicationArea != null)
- {
- vLIndicationArea.Label = channel.Label;
- vLIndicationArea.Name = Id.ToString();
- }
- }
- //if (hTIndicationArea != null && _IsDraging == false)
- //{
- // double position = ScottPlotFormControl.Plot.XAxis.Dims.GetPixel(TmbPresenter.PosIndexBymDiv);
- // hTIndicationArea.SetPosition(position);
- //}
- ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PosIndexBymDiv - 5000, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.FromArgb((Int32)(_GridIntensity / 100d * 255), Color.White));
- ScottPlotFormControl.Render(skipIfCurrentlyRendering: true);
- _IsRefreshing = false;
- }
- /// <summary>
- /// 添加波形
- /// </summary>
- private void AddWavePlot()
- {
- if (_WavePlot == null)
- if (Program.Oscilloscope.TryGetChannel(Id, out IChnlPrsnt channel))
- if (channel.Pack != null)
- _WavePlot = ScottPlotFormControl.Plot.AddWave(channel.Pack.Buffer, sampleRate: 1, isAfterglowOn: false, channel.DrawColor/*, label: channel.Id.ToString()*/);
- }
- /// <summary>
- /// 添加可拖拽指示区域
- /// </summary>
- private void AddDragArea()
- {
- PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1);
- vLIndicationArea = ScottPlotFormControl.Plot.AddVLIndicationArea(plotDimensions, color: Color.Red);
- //vRIndicationArea = ScottPlotFormControl.Plot.AddVRIndicationArea(plotDimensions, color: Color.Yellow);
- //hTIndicationArea = ScottPlotFormControl.Plot.AddHTIndicationArea(plotDimensions, color: Color.OrangeRed,label:"T");
- double position0 = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(0);
- double position1 = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(0);
- double position2 = ScottPlotFormControl.Plot.XAxis.Dims.GetPixel(5000);
- vLIndicationArea.SetPosition(position0);
- //vRIndicationArea.SetPosition(position1);
- //hTIndicationArea.SetPosition(position2);
- vLIndicationArea.LabelFont = new Font("微软雅黑", 6);
- vLIndicationArea.Dragged += (sender, e) =>
- {
- if (Program.Oscilloscope.TryGetChannel(Id, out IChnlPrsnt channel))
- {
- double s = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)vLIndicationArea.GetIndicationValue());
- (channel as ReferencePrsnt).PosIndexBymDiv = s;
- }
- };
- vLIndicationArea.MouseDown += (sender, e) =>
- {
- DsoPrsnt.FocusId = Id;
- };
- //vRIndicationArea.Dragged += (sender, e) =>
- //{
- // // TrgPresenter.PosIndex = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)vRIndicationArea.GetIndicationValue());
- //};
- //hTIndicationArea.Dragged += (sender, e) =>
- //{
- // TmbPresenter.PosIndexBymDiv = ScottPlotFormControl.Plot.XAxis.Dims.GetUnit((float)hTIndicationArea.GetIndicationValue());
- //};
- ////更新拖动图片
- //vLIndicationArea.IndicationImage = new Bitmap(19,19);
- //vRIndicationArea.IndicationImage = new Bitmap(19,19);
- //hTIndicationArea.IndicationImage = new Bitmap(19,19);
- }
- /// <summary>
- /// 配置显示信息
- /// </summary>
- private void ConfigureWavePlot()
- {
- //控件属性配置
- ScottPlotFormControl.Configuration.MiddleClickAutoAxisMarginX = 0;
- ScottPlotFormControl.Configuration.Quality = 0;
- //波形线路名称
- ScottPlot.Renderable.Legend legend = ScottPlotFormControl.Plot.Legend();
- legend.FontColor = Color.Gray;
- legend.OutlineColor = Color.White;
- legend.FillColor = Color.Black;
- ScottPlotFormControl.Plot.AxisAutoX(margin: 0);
- ScottPlotFormControl.Plot.SetAxisLimits(xMin: 0, xMax: 10000, yMin: -5000, yMax: 5000);//初始视阈
- ScottPlotFormControl.Plot.SetViewLimits(xMin: -32768, xMax: 32768, yMin: -10000, yMax: 10000);//视阈限制
- ScottPlotFormControl.Dock = DockStyle.Fill;
- //图形颜色设置
- ScottPlotFormControl.Plot.Style(figureBackground: Color.Transparent, dataBackground: Color.Transparent, grid: Color.Gray, tick: Color.Gray, axisLabel: Color.White, titleLabel: Color.Gray);
- //网格属性配置
- ScottPlotFormControl.Plot.Grid(enable: true, color: Color.Gray, lineStyle: LineStyle.Dot);
- ScottPlotFormControl.BackColor = Color.Black;
- AddWavePlot();
- AddDragArea();
- ScottPlotFormControl.Plot.RenderEventHandler += ScottPlotFormControlPlot_RenderEventHandler;
- }
- private void ScottPlotFormControlPlot_RenderEventHandler(object sender, (Bitmap bmp, PlotDimensions plotDimensions, bool lowQuality) e)
- {
- CursorApp.Default?.DrawCursor(sender, e);
- }
- #endregion
- #region 窗口显示属性及配置
- private WfmDrawMode _DrawMode;
- /// <summary>
- /// 波形显示风格
- /// </summary>
- public WfmDrawMode DrawMode
- {
- get => _DrawMode;
- set => _DrawMode = value;
- }
- private Int32 _WfmIntensity;
- /// <summary>
- /// 波形亮度
- /// </summary>
- public Int32 WfmIntensity
- {
- get => _WfmIntensity;
- set => _WfmIntensity = value;
- }
- private GridType _GridStyle;
- /// <summary>
- /// 网格风格
- /// </summary>
- public GridType GridStyle
- {
- get => _GridStyle;
- set => _GridStyle = value;
- }
- private Int32 _GridIntensity;
- /// <summary>
- /// 网格亮度
- /// </summary>
- public Int32 GridIntensity
- {
- get => _GridIntensity;
- set => _GridIntensity = value;
- }
- private Boolean _AxisTickVisible;
- /// <summary>
- /// 标记是否可见
- /// </summary>
- public Boolean AxisTickVisible
- {
- get => _AxisTickVisible;
- set => _AxisTickVisible = value;
- }
- private Boolean _XAxisTickBottom;
- /// <summary>
- /// 水平标记是否显示在下方
- /// </summary>
- public Boolean XAxisTickBottom
- {
- get => _XAxisTickBottom;
- set => _XAxisTickBottom = value;
- }
- private Boolean _YAxisTickRight;
- /// <summary>
- /// 垂直标记是否显示在右方
- /// </summary>
- public Boolean YAxisTickRight
- {
- get => _YAxisTickRight;
- set => _YAxisTickRight = value;
- }
- /// <summary>
- /// 配置波形显示风格
- /// </summary>
- /// <param name="wfmDrawMode"></param>
- private void ConfigDrawMode(WfmDrawMode wfmDrawMode)
- {
- switch (wfmDrawMode)
- {
- case WfmDrawMode.Vector:
- ScottPlotFormControl.Plot.WavePlotStyle(WavePlotLineStyle.Vector);
- break;
- case WfmDrawMode.Dot:
- ScottPlotFormControl.Plot.WavePlotStyle(WavePlotLineStyle.Dot);
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 配置波形亮度
- /// </summary>
- /// <param name="wfmIntensity"></param>
- private void ConfigWfmIntensity(Int32 wfmIntensity)
- {
- ScottPlotFormControl.Plot.WavePlotStyle(wfmIntensity);
- }
- /// <summary>
- /// 配置水平标记的显示位置
- /// </summary>
- /// <param name="horizontalBottom"></param>
- private void ConfigHorizontalTickLabelDirection(Boolean horizontalBottom = true)
- {
- ScottPlotFormControl.Plot.XAxis.Edge = horizontalBottom == true ? ScottPlot.Renderable.Edge.Bottom : ScottPlot.Renderable.Edge.Top;
- }
- /// <summary>
- /// 配置垂直标记的显示位置
- /// </summary>
- /// <param name="virticalRight"></param>
- private void ConfigVirticalTickLabelDirection(Boolean virticalRight = true)
- {
- ScottPlotFormControl.Plot.YAxis.Edge = virticalRight == true ? ScottPlot.Renderable.Edge.Right : ScottPlot.Renderable.Edge.Left;
- }
- /// <summary>
- /// 配置标记label的显示
- /// </summary>
- /// <param name="visible"></param>
- private void ConfigTickLabelVisible(Boolean visible = true)
- {
- ScottPlotFormControl.Plot.XAxis.IsVisible = visible;
- ScottPlotFormControl.Plot.YAxis.IsVisible = visible;
- }
- /// <summary>
- /// 配置网格样式
- /// </summary>
- /// <param name="gridType"></param>
- private void ConfigGridStyle(GridType gridType)
- {
- switch (gridType)
- {
- case GridType.Full:
- ScottPlotFormControl.Plot.Grid(enable: true, lineStyle: LineStyle.Dot);
- ScottPlotFormControl.Plot.MinorTickVisible(true);
- break;
- case GridType.Brief:
- ScottPlotFormControl.Plot.Grid(enable: true, lineStyle: LineStyle.Dot);
- ScottPlotFormControl.Plot.MinorTickVisible(false);
- break;
- case GridType.None:
- ScottPlotFormControl.Plot.Grid(enable: false);
- ScottPlotFormControl.Plot.MinorTickVisible(false);
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 配置网格亮度
- /// </summary>
- /// <param name="GridIntensity"></param>
- private void ConfigGridIntensity(Int32 GridIntensity)
- {
- ScottPlotFormControl.Plot.Grid(color: Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.Gray));
- }
- private void OnLostFocusClick_FloatForm(object obj, EventArgs e)
- {
- var form = obj as Uni_Trend.MSO7000X.UserControls.FloatForm;
- if (form != null)
- {
- form.Close();
- }
- }
- private void ToolStripMenuItemTimeWave_Click(object sender, EventArgs e)
- {
- //if ((Program.Oscilloscope.View as DsoForm)?.OperateForm != null)
- //{
- // (Program.Oscilloscope.View as DsoForm)?.OperateForm.Close();
- //}
- if (_TimeDomainViewSettingForm != null)
- {
- _TimeDomainViewSettingForm.Close();
- }
- _TimeDomainViewSettingForm = new TimeDomainViewSettingForm();
- _TimeDomainViewSettingForm.SetGridComboBoxSelectType(Enum.GetValues<GridType>());
- XAxisTickType xaxisticktype = ScottPlotFormControl.Plot.XAxis.Edge == Edge.Bottom ? XAxisTickType.Bottom : XAxisTickType.Top;
- YAxisTickType yaxisticktype = ScottPlotFormControl.Plot.YAxis.Edge == Edge.Left ? YAxisTickType.Left : YAxisTickType.Right;
- _TimeDomainViewSettingForm.SetMutexButtonModeType(xaxisticktype, yaxisticktype, DrawMode);
- _TimeDomainViewSettingForm.SetParameterValue(TimeEventParameterType.TickIntensity, GridIntensity);
- _TimeDomainViewSettingForm.SetParameterValue(TimeEventParameterType.WFMIntensity, WfmIntensity);
- _TimeDomainViewSettingForm.SetParameterValue(TimeEventParameterType.AxisTick, ScottPlotFormControl.Plot.XAxis.IsVisible);
- _TimeDomainViewSettingForm.ParameterChangedEventHandler += TimeDomainViewSettingForm_ParameterChangedEventHandler;
- if (Program.Oscilloscope.View != null && Program.Oscilloscope.View is DsoForm dsoForm)
- {
- Int32 locationx = (dsoForm.Width - _TimeDomainViewSettingForm.Width) / 2;
- locationx = locationx > 0 ? locationx : 0;
- Int32 locationy = (dsoForm.Height - _TimeDomainViewSettingForm.Height) / 2;
- locationy = locationy > 0 ? locationy : 0;
- _TimeDomainViewSettingForm.Location = new Point(locationx, locationy);
- }
- _TimeDomainViewSettingForm.FormClosed += (sender, e) =>
- {
- _TimeDomainViewSettingForm.ParameterChangedEventHandler -= TimeDomainViewSettingForm_ParameterChangedEventHandler;
- _TimeDomainViewSettingForm = null;
- };
- FormEventArgs formeventargs = new FormEventArgs() { Current = _TimeDomainViewSettingForm, Type = FormType.SettingForm };
- EventBroker.Instance.GetEvent<FormEventArgs>().Publish(this, formeventargs);
- }
- private void TimeDomainViewSettingForm_ParameterChangedEventHandler(TimeEventParameterType type, object obj)
- {
- switch (type)
- {
- case TimeEventParameterType.AxisTick:
- {
- if (obj is Boolean visible)
- {
- AxisTickVisible = visible;
- ConfigTickLabelVisible(visible);
- }
- }
- break;
- case TimeEventParameterType.XAxisTick:
- {
- if (obj is XAxisTickType timeBaseMarkerType)
- {
- XAxisTickBottom = timeBaseMarkerType == XAxisTickType.Bottom;
- ConfigHorizontalTickLabelDirection(timeBaseMarkerType == XAxisTickType.Bottom);
- }
- }
- break;
- case TimeEventParameterType.YAxisTick:
- {
- if (obj is YAxisTickType verticalGearMarkerType)
- {
- YAxisTickRight = verticalGearMarkerType == YAxisTickType.Right;
- ConfigVirticalTickLabelDirection(verticalGearMarkerType == YAxisTickType.Right);
- }
- }
- break;
- case TimeEventParameterType.WFMType:
- if (obj is WfmDrawMode mode)
- {
- DrawMode = mode;
- ConfigDrawMode(DrawMode);
- }
- break;
- case TimeEventParameterType.WFMIntensity:
- if (obj is Int32 int32)
- {
- WfmIntensity = int32;
- ConfigWfmIntensity(WfmIntensity);
- }
- break;
- case TimeEventParameterType.TickType:
- if (obj is GridType value)
- {
- GridStyle = value;
- ConfigGridStyle(GridStyle);
- }
- break;
- case TimeEventParameterType.TickIntensity:
- if (obj is Int32 intensity)
- {
- GridIntensity = intensity;
- ConfigGridIntensity(GridIntensity);
- }
- break;
- default:
- break;
- }
- }
- #endregion
- #region 事件
- protected override void OnLoad(EventArgs e)
- {
- base.OnLoad(e);
- ConfigGridIntensity(GridIntensity);
- ConfigGridStyle(GridStyle);
- ConfigWfmIntensity(WfmIntensity);
- ConfigDrawMode(DrawMode);
- ConfigTickLabelVisible(AxisTickVisible);
- ConfigVirticalTickLabelDirection(XAxisTickBottom);
- ConfigHorizontalTickLabelDirection(YAxisTickRight);
- }
- private void TimerRefresh_Tick(object sender, EventArgs e)
- {
- if (_IsRefreshing == false)
- DrawWave();
- }
- protected override void OnFormClosed(FormClosedEventArgs e)
- {
- TmbPresenter.TryRemoveView(this);
- TrgPresenter.TryRemoveView(this);
- Presenter.TryRemoveView(this);
- Presenter.Active = false;
- (Program.Oscilloscope.View as DsoForm).RemoveBadge(Presenter);
- //(Program.Oscilloscope.View as DsoForm).DestroyDisplayForm(this);
- }
- ~ReferenceFigure()
- {
- if (_TimerRefresh != null)
- _TimerRefresh.Dispose();
- }
- #endregion
- #region IDsoView
- public ReferencePrsnt Presenter
- {
- get;
- set;
- }
- IBadge IView<IBadge>.Presenter
- {
- get => Presenter;
- set => Presenter = (ReferencePrsnt)value;
- }
- public TimebasePrsnt TmbPresenter
- {
- get;
- set;
- }
- ITimebasePrsnt IView<ITimebasePrsnt>.Presenter
- {
- get => TmbPresenter;
- set => TmbPresenter = (TimebasePrsnt)value;
- }
- public TriggerPrsnt TrgPresenter
- {
- get;
- set;
- }
- ITriggerPrsnt IView<ITriggerPrsnt>.Presenter
- {
- get => TrgPresenter;
- set => TrgPresenter = (TriggerPrsnt)value;
- }
- public void UpdateView(String propertyName)
- {
- if (String.IsNullOrEmpty(propertyName))
- {
- UpdateView();
- return;
- }
- switch (propertyName)
- {
- //case nameof(TrgPresenter.):
- // break;
- //case nameof(TmbPresenter.Length):
- // break;
- case nameof(TmbPresenter.ScaleByus):
- ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PositionByus, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.White);
- break;
- case nameof(TmbPresenter.PositionByus):
- ScottPlotFormControl.Plot.ResetTimebaseParameter((Int32)TmbPresenter.PositionByus, TmbPresenter.ScaleByus, TmbPresenter.Prefix.ToString(), TmbPresenter.Unit, Color.White);
- break;
- }
- }
- protected void UpdateView()
- {
- }
- #endregion
- }
- }
|