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 System.Diagnostics; using Uestc.Auto6.Dso.MathExt; using Uestc.Auto6.Dso.ComModel; using ScottPlot.Renderable; using Uestc.Auto6.Dso.Core.Tools; using Uni_Trend.MSO7000X.Common.Structs; using EventBus; using System.Collections.ObjectModel; namespace Uestc.Auto6.Dso.U2 { public partial class WaveformFigure : BaseDisplayForm, ITimebaseView, ITriggerView, IDisplayView { public WaveformFigure() { InitializeComponent(); } private static readonly Object _ObjectLock = new(); private System.Windows.Forms.Timer _TimerRefresh = new System.Windows.Forms.Timer(); private Boolean _IsRefreshing = false; private Boolean _IsDraging = false; private TimeDomainViewSettingForm _TimeDomainViewSettingForm = null; #region WavePlot private WavePlot wavePlot; private VRIndicationArea _VRIndicationAreaA; private VRIndicationArea _VRIndicationAreaB; private HTIndicationArea _HTIndicationArea; private ZoomRectangleArea zoomRectangleArea; public Dictionary<(ChannelId, ChannelId), ZoomRectangleArea> ZoomRectangleAreaDictionary = new Dictionary<(ChannelId, ChannelId), ZoomRectangleArea>(); //private List _AnalogWaveIdList = new List() //{ // ChannelId.C1, // ChannelId.C2, // ChannelId.C3, // ChannelId.C4 //}; //private List _DigitalWaveIdList = new List() //{ // ChannelId.D1, // ChannelId.D2, // ChannelId.D3, // ChannelId.D4 //}; private Dictionary _WavePlotDictionary = new Dictionary(); private Dictionary _VLIndicationAreaDictionary = new Dictionary(); private Dictionary _DigitalWavePlotDictionary = new Dictionary(); private Dictionary _VLArrowIndicationAreaDictionary = new Dictionary(); private Dictionary _PlotBaseDictionary = new Dictionary(); /// /// 光标层级计算使用 /// private Dictionary _IndicationAreaDictionary = new Dictionary(); /// /// 更新波形 /// private void DrawWave() { _IsRefreshing = true; AnalogWaveUpdate(); DigitalWaveUpdate(); TriggerIndicationAreaUpdate(); if (_HTIndicationArea != null && _IsDraging == false) { double position6 = ScottPlotFormControl.Plot.XAxis.Dims.GetPixel(TmbPresenter.PosIndexBymDiv); _HTIndicationArea.SetPosition(position6); } if (_WavePlotDictionary.Count == 0) { AddWavePlot(); } SetPlotRenderZIndex(); ////更新通道刻度信息 //if (Program.Oscilloscope.TryGetChannel(Program.Oscilloscope.FocusId, out IChnlPrsnt focusChannel)) // ScottPlotFormControl.Plot.ResetChannelParameter((int)(focusChannel as AnalogPrsnt).PosIndex, (focusChannel as AnalogPrsnt).Scale, (focusChannel as AnalogPrsnt).UnitPrefix.ToString(), focusChannel.Unit, Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.White)/*channel.DrawColor*/); 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; } private void TriggerIndicationAreaUpdate() { if (_VRIndicationAreaA != null && _VRIndicationAreaB != null /*&& _IsDraging == false*/) { switch (TriggerPrsnt.Type) { case TriggerType.Edge: case TriggerType.PulseWidth: case TriggerType.TimeOut: case TriggerType.Glitch: { _VRIndicationAreaB.IsVisible = false; if (TrgPresenter is TrigSingleSrcPrsnt trgp) { if (trgp.Source.IsAnalog() == true) { _VRIndicationAreaA.IsVisible = true; double position1 = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-trgp.RelPosIndex); _VRIndicationAreaA.SetPosition(position1); } else { _VRIndicationAreaA.IsVisible = false; } } } break; case TriggerType.Video: case TriggerType.Pattern: case TriggerType.State: case TriggerType.MultiQulified: case TriggerType.Serial: case TriggerType.SetupHold: { _VRIndicationAreaA.IsVisible = false; _VRIndicationAreaB.IsVisible = false; } break; case TriggerType.Runt: case TriggerType.Transition: case TriggerType.Window: { //两个 if (TrgPresenter is TrigMultiLevelPrsnt trgp) { if (trgp.Source.IsAnalog() == true) { _VRIndicationAreaA.IsVisible = true; _VRIndicationAreaB.IsVisible = true; double positiona = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-trgp.RelPosUpperIndex); _VRIndicationAreaA.SetPosition(positiona); double positionb = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-trgp.RelPosLowerIndex); _VRIndicationAreaB.SetPosition(positionb); } else { _VRIndicationAreaA.IsVisible = false; _VRIndicationAreaB.IsVisible = false; } } } break; default: break; } } } private void DigitalWaveUpdate() { foreach (var id in ChannelIdExt.GetDigitals()) { if (Program.Oscilloscope.TryGetChannel(id, out IChnlPrsnt ch) && ch.Pack!=null) { var dch = (DigitalPrsnt)ch; if (!_DigitalWavePlotDictionary.ContainsKey(id)) { DigitalWavePlot digitalWavePlot = ScottPlotFormControl.Plot.AddDigitalWave(dch.Pack.Buffer, sampleRate: dch.Pack.Properties.Factor, color: dch.DrawColor/*, label: channel.Label*/); digitalWavePlot.IsAfterglowOn = false; _DigitalWavePlotDictionary.Add(id, digitalWavePlot); _PlotBaseDictionary.Add(id, digitalWavePlot); } _DigitalWavePlotDictionary[id].IsVisible = dch.Active; if (dch.Active == true) { for (int i = 0; i < dch.BitLength; i++) { Boolean isactive = dch.GetActiveAt(i); _DigitalWavePlotDictionary[id].SetBitActive(i, isactive); _DigitalWavePlotDictionary[id].SetBitColor(i, dch.GetColorAt(i)); VLArrowIndicationArea vlarrowindicationarea = _VLArrowIndicationAreaDictionary[ChannelId.D1 + i]; vlarrowindicationarea.Color = dch.GetColorAt(i); vlarrowindicationarea.Label = dch.GetLabelAt(i); vlarrowindicationarea.IsVisible = isactive; if (vlarrowindicationarea.PosIndexEnabled == false && isactive == true) { float offsety = (float)vlarrowindicationarea.GetIndicationValue() - ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-dch.GetPosIndexAt(i)) + vlarrowindicationarea.IndicationImage.Height / 2; _DigitalWavePlotDictionary[id].SetBitOffsetY(i, offsety); } else { _DigitalWavePlotDictionary[id].SetBitOffsetY(i, 0); if (isactive == true) { vlarrowindicationarea.SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-dch.GetPosIndexAt(i) /** dch.BitHeight*/) - vlarrowindicationarea.IndicationImage.Height / 2); } } } //更新数据 if (dch.Pack != null) _DigitalWavePlotDictionary[id].YTs = dch.VuDatabase.Current.Buffer; _DigitalWavePlotDictionary[id].SampleRate = dch.Pack.Properties.Factor; //更新通道刻度信息 if (DsoPrsnt.FocusId.IsDigital() == true) ScottPlotFormControl.Plot.YAxis.IsVisible = false; else if (AxisTickVisible == true) { ScottPlotFormControl.Plot.YAxis.IsVisible = true; } } else { for (int i = 0; i < dch.BitLength; i++) { _VLArrowIndicationAreaDictionary[ChannelId.D1 + i].IsVisible = false; } } } } } private void AnalogWaveUpdate() { if (_WavePlotDictionary.Count > 3) { foreach (var id in ChannelIdExt.GetAnalogs()) { if (Program.Oscilloscope.TryGetChannel(id, out IChnlPrsnt channel)) { _WavePlotDictionary[id].IsVisible = channel.Active; if (channel.Active == true) { //更新数据 //if (channel.Pack != null) // _WavePlotDictionary[id].YTs = channel.Pack.Buffer; if (channel.VuDatabase.Current != null) { _WavePlotDictionary[id].YTs = (double[,])channel.VuDatabase.Current.Buffer; _WavePlotDictionary[id].SampleRate = channel.VuDatabase.Current.ZoomRatio; _WavePlotDictionary[id].OffsetX = channel.VuDatabase.Current.Start; } //更新通道刻度信息 if (channel.Id == DsoPrsnt.FocusId) { ScottPlotFormControl.Plot.ResetChannelParameter((int)(channel as AnalogPrsnt).PosIndexBymDiv, (channel as AnalogPrsnt).ScaleBymV, (channel as AnalogPrsnt).Prefix.ToString(), channel.Unit, Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.White)/*channel.DrawColor*/); if (AxisTickVisible == true) { ScottPlotFormControl.Plot.YAxis.IsVisible = true; } } if (_VLIndicationAreaDictionary.ContainsKey(id)) { if (_IsDraging == false) { _VLIndicationAreaDictionary[id].IsVisible = true; _VLIndicationAreaDictionary[id].Color = channel.DrawColor; _VLIndicationAreaDictionary[id].Label = channel.Label; _VLIndicationAreaDictionary[id].SetPosition(ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(-(int)(channel as AnalogPrsnt).PosIndexBymDiv)); } if (channel.Id == DsoPrsnt.FocusId) { if (_VLIndicationAreaDictionary[id].IsMoved) { _VRIndicationAreaA.IsRenderDottedLine = false; _VRIndicationAreaB.IsRenderDottedLine = false; } else { _VRIndicationAreaA.IsRenderDottedLine = true; _VRIndicationAreaB.IsRenderDottedLine = true; } } } List<(ChannelId, ChannelId)> keylist = ZoomRectangleAreaDictionary.Keys.Where((x) => { return x.Item2 == channel.Id; }).ToList(); foreach (var item in keylist) { if (ZoomRectangleAreaDictionary.TryGetValue(item, out ZoomRectangleArea zoomRectangleArea) == true) { zoomRectangleArea.YPosIndexBymDiv = (channel as AnalogPrsnt).PosIndexBymDiv; if (item.Item1 == DsoPrsnt.FocusId) zoomRectangleArea.IsFocused = true; else zoomRectangleArea.IsFocused = false; if (Program.Oscilloscope.TryGetChannel(item.Item1, out IChnlPrsnt mathchannel)) { double width =(mathchannel as MathPrsnt).Sampling.Scale / TmbPresenter.ScaleByus * Constants.MAX_XPOS_IDX; //double start = TmbPresenter.PosIndexBymDiv +(channel.Pack!.Properties.TmbPosition.Index + channel.Pack!.Properties.TmbPosition.Value - (mathchannel as MathPrsnt).Sampling.PosIndexBymDiv) * (mathchannel as MathPrsnt).Sampling.Scale / TmbPresenter.ScaleByus - width / 2; double start = channel.Pack!.Properties.TmbPosition.Index + channel.Pack!.Properties.TmbPosition.Value + (channel.Pack!.Properties.TmbPosition.Index + channel.Pack!.Properties.TmbPosition.Value - (mathchannel as MathPrsnt).Sampling.PosIndexBymDiv) * (mathchannel as MathPrsnt).Sampling.Scale / TmbPresenter.ScaleByus - width / 2; zoomRectangleArea.SetAreaBoundaryOnly(start,start+width,1,1); } } } } else { _VLIndicationAreaDictionary[id].IsVisible = false; } } } } if (DisplayPresenter.Persist == WfmPersist.Auto) ScottPlotFormControl.WfmPersist = WaveFormPersist.Auto; else if (DisplayPresenter.Persist == WfmPersist.Infinity) ScottPlotFormControl.WfmPersist = WaveFormPersist.Infinity; else ScottPlotFormControl.WfmPersist = WaveFormPersist.Close; } /// /// 设置渲染层级 /// private void SetPlotRenderZIndex() { if (_PlotBaseDictionary.ContainsKey(DsoPrsnt.FocusId)) { List> waveplotlist = _PlotBaseDictionary.ToList(); waveplotlist.Sort((a, b) => { return a.Key > b.Key ? 1 : -1; }); Int32 index = waveplotlist.Count; Int32 currentfocusindex = waveplotlist.FindIndex((a) => { return a.Key == DsoPrsnt.FocusId; }); for (int i = currentfocusindex; i < waveplotlist.Count; i++) { waveplotlist[i].Value.ZIndex = index; index--; } for (int j = 0; j < currentfocusindex; j++) { waveplotlist[j].Value.ZIndex = index; index--; } } if (_IndicationAreaDictionary.ContainsKey(DsoPrsnt.FocusId)) { List> vlindicationarealist = _IndicationAreaDictionary.ToList(); vlindicationarealist.Sort((a, b) => { return a.Key > b.Key ? 1 : -1; }); ChannelId currentchannelid = DsoPrsnt.FocusId; if (DsoPrsnt.FocusId.IsDigital() == true) { if (Program.Oscilloscope.TryGetChannel(DsoPrsnt.FocusId, out IChnlPrsnt ch)) { var dch = (DigitalPrsnt)ch; currentchannelid = ChannelId.D1 + dch.FocusBitId; } } Int32 index = vlindicationarealist.Count; Int32 currentfocusindex = vlindicationarealist.FindIndex((a) => { return a.Key == currentchannelid; }); for (int i = currentfocusindex; i < vlindicationarealist.Count; i++) { if (index == vlindicationarealist.Count) { vlindicationarealist[i].Value.IsFocused = true; } else { vlindicationarealist[i].Value.IsFocused = false; } vlindicationarealist[i].Value.ZIndex = index; index--; } for (int j = 0; j < currentfocusindex; j++) { vlindicationarealist[j].Value.IsFocused = false; vlindicationarealist[j].Value.ZIndex = index; index--; } } else { foreach (var item in _IndicationAreaDictionary) { item.Value.IsFocused = false; } } foreach (var item in ZoomRectangleAreaDictionary) { if (item.Key.Item1 == DsoPrsnt.FocusId) { item.Value.ZIndex = 1; } else { item.Value.ZIndex = 0; } } } /// /// 配置显示信息 /// 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.Gray, titleLabel: Color.Gray); //网格属性配置 ScottPlotFormControl.Plot.Grid(enable: true, color: Color.Gray, lineStyle: LineStyle.Dot); ScottPlotFormControl.BackColor = Color.Black; ScottPlotFormControl.Plot.ResetChannelParameter(50, 6.6, "", "", Color.White); ScottPlotFormControl.Plot.ResetTimebaseParameter(50, 7.7, "", "", Color.White); AddWavePlot(); AddDragArea(); //AddZoomArea(); ScottPlotFormControl.PlottableDropped += ScottPlotFormControl_PlottableDropped; ScottPlotFormControl.PlottableDragged += ScottPlotFormControl_PlottableDragged; ScottPlotFormControl.Plot.RenderEventHandler += ScottPlotFormControlPlot_RenderEventHandler; (Program.Oscilloscope.View as DsoForm).RemoveZoomRectangleAreaEventArgs += DsoForm_ZoomRectangleAreaRemoveEventArgs; (Program.Oscilloscope.View as DsoForm).AddZoomRectangleAreaEventArgs += DsoForm_AddZoomRectangleAreaEventArgs; } private void DsoForm_ZoomRectangleAreaRemoveEventArgs(object sender, ChannelId e) { List<(ChannelId, ChannelId)> removekeylist = ZoomRectangleAreaDictionary.Keys.Where((x) => { return x.Item1 == e; }).ToList(); foreach (var item in removekeylist) { ZoomRectangleAreaDictionary.Remove(item, out ZoomRectangleArea zoomrectanglearea); if (zoomrectanglearea != null) { ScottPlotFormControl.Plot.Remove(zoomrectanglearea); } } } private void ScottPlotFormControlPlot_RenderEventHandler(object sender, (Bitmap bmp, PlotDimensions plotDimensions, bool lowQuality) e) { CursorApp.Default?.DrawCursor(sender, e); MeasureApp.Default?.DrawIndicator(sender, e); return; //String message = Guid.NewGuid().ToString(); //using (var gfx = ScottPlot.Drawing.GDI.Graphics(e.bmp, e.plotDimensions, e.lowQuality, false)) //using (var font = ScottPlot.Drawing.GDI.Font("微软雅黑", 10)) //using (var fontBrush = new SolidBrush(Color.Black)) //using (var fillBrush = new SolidBrush(Color.White)) //using (var borderPen = new Pen(Color.White, 2)) //{ // SizeF textSize = ScottPlot.Drawing.GDI.MeasureString(gfx, message, font); // float textHeight = textSize.Height; // float textWidth = textSize.Width; // float textY = 0; // textY = e.plotDimensions.DataOffsetY + e.plotDimensions.DataHeight / 2 - textHeight / 2; // float textX = 0; // textX = e.plotDimensions.DataOffsetX + e.plotDimensions.DataWidth / 2 - textWidth / 2; // RectangleF textRect = new RectangleF(textX, textY, textWidth, textHeight); // gfx.FillRectangle(fillBrush, textRect); // gfx.DrawRectangle(borderPen, Rectangle.Round(textRect)); // gfx.DrawString(message, font, fontBrush, textX, textY); //} } private void ScottPlotFormControl_PlottableDragged(object sender, EventArgs e) { if (sender != null && sender is VLArrowIndicationArea vlarrowindicationarea) { //数字位光标在拖动时,P层PosIndex无效 vlarrowindicationarea.PosIndexEnabled = false; } } private void ScottPlotFormControl_PlottableDropped(object sender, EventArgs e) { if (sender != null && sender is VLArrowIndicationArea vlarrowindicationarea) { //数字位光标拖动结束时,计算位置替换 if (Program.Oscilloscope.TryGetChannel(ChannelId.D1, out IChnlPrsnt ch)) { var dch = (DigitalPrsnt)ch; Int32 curchannelidindex = 0; foreach (var item in _VLArrowIndicationAreaDictionary) { if (item.Value.Equals(vlarrowindicationarea)) { curchannelidindex = item.Key - ChannelId.D1; break; } } dch.SetPosIndexAt(curchannelidindex, ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)vlarrowindicationarea.GetIndicationValue())); vlarrowindicationarea.PosIndexEnabled = true; } } } /// /// 增加模拟波形 /// private void AddWavePlot() { foreach (var id in ChannelIdExt.GetAnalogs()) if (Program.Oscilloscope.TryGetChannel(id, out IChnlPrsnt channel)) if (channel.Pack != null) { wavePlot = ScottPlotFormControl.Plot.AddWave(channel.Pack.Buffer, sampleRate: channel.Pack.Properties.Factor, isAfterglowOn: true, channel.DrawColor/*, label: channel.Label*/); _WavePlotDictionary.Add(id, wavePlot); _PlotBaseDictionary.Add(id, wavePlot); } } /// /// 增加拖拽指示区域 /// private void AddDragArea() { PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1); foreach (var item in ChannelIdExt.GetAnalogs())//模拟通道 { VLIndicationArea vLIndicationAreaCH1 = ScottPlotFormControl.Plot.AddVLIndicationArea(plotDimensions, color: Color.Red, label: item.ToString(), name: item.ToString()); vLIndicationAreaCH1.Margin = (0, 3, 0, 3); _VLIndicationAreaDictionary.Add(item, vLIndicationAreaCH1); _IndicationAreaDictionary.Add(item, vLIndicationAreaCH1); double position0 = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(0); vLIndicationAreaCH1.SetPosition(position0); vLIndicationAreaCH1.Dragged += (sender, e) => { if (Program.Oscilloscope.TryGetChannel(item, out IChnlPrsnt channel)) { (channel as AnalogPrsnt).PosIndexBymDiv = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)vLIndicationAreaCH1.GetIndicationValue()); } ScottPlotFormControl.ClearWfmCache();//清理无限余晖缓存测试,不影响其他业务 }; vLIndicationAreaCH1.MouseDown += (sender, e) => { DsoPrsnt.FocusId = item; }; } for (int i = 0; i < 16; i++)//数字通道 { VLArrowIndicationArea vlarrowindicationarea = ScottPlotFormControl.Plot.AddVLArrowIndicationArea(plotDimensions, color: Color.Yellow, label: "D" + (i + 1).ToString(), name: "D" + (i + 1).ToString()); vlarrowindicationarea.IsVisible = false; _VLArrowIndicationAreaDictionary.Add(ChannelId.D1 + i, vlarrowindicationarea); _IndicationAreaDictionary.Add(ChannelId.D1 + i, vlarrowindicationarea); int bitindex = i; vlarrowindicationarea.MouseDown += (sender, e) => { DsoPrsnt.FocusId = ChannelId.D1; if (Program.Oscilloscope.TryGetChannel(DsoPrsnt.FocusId, out IChnlPrsnt ch)) { var dch = (DigitalPrsnt)ch; dch.FocusBitId = bitindex; } }; } _VRIndicationAreaB = ScottPlotFormControl.Plot.AddVRIndicationArea(plotDimensions, color: Color.OrangeRed/*, name: "B"*/); //触发 _VRIndicationAreaB.Margin = (6, 3, 0, 3); _VRIndicationAreaA = ScottPlotFormControl.Plot.AddVRIndicationArea(plotDimensions, color: Color.OrangeRed/*, name: "A"*/); //触发 _VRIndicationAreaA.Margin = (0, 3, 5, 3); _VRIndicationAreaA.ZIndex = 1; _HTIndicationArea = ScottPlotFormControl.Plot.AddHTIndicationArea(plotDimensions, color: Color.OrangeRed, name: "T");//时基 _HTIndicationArea.Margin = (3, 0, 3, 0); double position1 = ScottPlotFormControl.Plot.YAxis.Dims.GetPixel(0); double position2 = ScottPlotFormControl.Plot.XAxis.Dims.GetPixel(5000); _VRIndicationAreaA.SetPosition(position1); _VRIndicationAreaB.SetPosition(position1); _HTIndicationArea.SetPosition(position2); _VRIndicationAreaA.Dragged += VRIndicationArea_Dragged; _VRIndicationAreaB.Dragged += VRIndicationArea_Dragged; _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); } private void VRIndicationArea_Dragged(object sender, EventArgs e) { if (sender is VRIndicationArea vrindicationarea) { Boolean isdraggedupperarea = true; if (vrindicationarea.Equals(_VRIndicationAreaB)) { isdraggedupperarea = false; } switch (TriggerPrsnt.Type) { case TriggerType.Edge: case TriggerType.PulseWidth: case TriggerType.TimeOut: case TriggerType.Glitch: { if (TrgPresenter is TrigSingleSrcPrsnt trgp && isdraggedupperarea == true) { trgp.RelPosIndex = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)_VRIndicationAreaA.GetIndicationValue()); } } break; case TriggerType.Video: case TriggerType.Pattern: case TriggerType.State: case TriggerType.MultiQulified: case TriggerType.Serial: case TriggerType.SetupHold: break; case TriggerType.Runt: case TriggerType.Transition: case TriggerType.Window: { //两个 if (TrgPresenter is TrigMultiLevelPrsnt trgp) { if (isdraggedupperarea == true) { double posa = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)_VRIndicationAreaA.GetIndicationValue()); trgp.RelPosUpperIndex = posa; } else { double posb = ScottPlotFormControl.Plot.YAxis.Dims.GetUnit((float)_VRIndicationAreaB.GetIndicationValue()); trgp.RelPosLowerIndex = posb; } } } break; default: break; } } } private void AddZoomArea() { PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1); AxisDimensions axisDimensionsX = ScottPlotFormControl.Plot.XAxis.Dims; AxisDimensions axisDimensionsY = ScottPlotFormControl.Plot.YAxis.Dims; zoomRectangleArea = ScottPlotFormControl.Plot.AddDraggableZoomRectangleArea(plotDimensions, axisDimensionsX, axisDimensionsY, label: "Math1"); } /// /// 增加拓展区域 /// /// /// /// /// private void AddZoomArea((ChannelId, ChannelId) id, Color channeCcolor, Int64 idKey, (double xLeft, double xRight, double yTop, double yBottom) zoomunitarea, Color labelColor) { PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1); AxisDimensions axisDimensionsX = ScottPlotFormControl.Plot.XAxis.Dims; AxisDimensions axisDimensionsY = ScottPlotFormControl.Plot.YAxis.Dims; ZoomRectangleArea zoomRectangle = ScottPlotFormControl.Plot.AddDraggableZoomRectangleArea(plotDimensions, axisDimensionsX, axisDimensionsY, color: channeCcolor, label: id.Item1.ToString(), labelColor: labelColor); ZoomRectangleAreaDictionary.Add(id, zoomRectangle); } #endregion #region 创建Zoom private void ScottPlotFormControl_ZoomCreated(object sender, EventArgs e) { isZooming = false; ScottPlotFormControl.SetZoomingState(isZooming); (double xLeft, double xRight, double yTop, double yBottom) zoomunitarea = ScottPlotFormControl.Plot.GetZoomUnitArea(); //此操作获取焦点通道的水平、垂直档位,以此为依据创建新的数学通道,通道的数学运算属性为zoom //1.垂直档位 = Math.Abs(yTop - yBottom)/10000*focusedchannel.YScale //2.水平档位 = Math.Abs(xLeft - xRight)/10000*focusedchannel.XScale //3.垂直位移 //4.水平位移 ChannelId channelId = ChannelId.C1; if (DsoPrsnt.FocusId.IsAnalog() == true) { channelId = DsoPrsnt.FocusId; } //5.创建ZoomArea (Program.Oscilloscope.View as DsoForm).Presenter.TryGetChannel(channelId, out var prsnt); if ((Program.Oscilloscope.View as DsoForm).TryAddMathInfo( MathType.Zoom) && prsnt != null) { ////此处需要Core公开部分属性的写权限 (Program.Oscilloscope.View as DsoForm).Presenter.TryGetChannel(DsoPrsnt.FocusId, out var mprsnt); AddZoomArea((DsoPrsnt.FocusId, channelId), mprsnt.DrawColor, IdFactory.NextId, zoomunitarea, mprsnt.DrawColor); //channel.timebase = Math.Abs(zoomunitarea.xLeft - zoomunitarea.xRight) / 10000 * timebase.Scale; //channel.Scale = Math.Abs(zoomunitarea.yTop - zoomunitarea.yBottom) / 10000 * prsnt.YScale; } //6.属性绑定(AreaUnit,unit边界) //7.资源释放 数学通道关闭后,将对应zoom区域清除 } private void DsoForm_AddZoomRectangleAreaEventArgs(object sender, (ChannelId, ChannelId) e) { if (ZoomRectangleAreaDictionary.ContainsKey(e) == false) { PlotDimensions plotDimensions = ScottPlotFormControl.Plot.GetSettings().GetPlotDimensions(0, 0, 1); AxisDimensions axisDimensionsX = ScottPlotFormControl.Plot.XAxis.Dims; AxisDimensions axisDimensionsY = ScottPlotFormControl.Plot.YAxis.Dims; if (Program.Oscilloscope.TryGetChannel(e.Item1, out IChnlPrsnt mathchannel)) { ZoomRectangleArea zoomRectangle = ScottPlotFormControl.Plot.AddDraggableZoomRectangleArea(plotDimensions, axisDimensionsX, axisDimensionsY, color: mathchannel.DrawColor); ZoomRectangleAreaDictionary.Add(e, zoomRectangle); } } } #endregion #region 标记 private VLine _VLine; private HLine _HLine; private Boolean _IsCursorTracking = false; public Boolean IsCursorTracking { get => _IsCursorTracking; set => _IsCursorTracking = value; } private void ConfigureMouseTracker() { _VLine = ScottPlotFormControl.Plot.AddVerticalLine(0, Color.Yellow, 1, LineStyle.Dash); _HLine = ScottPlotFormControl.Plot.AddHorizontalLine(0, Color.Yellow, 1, LineStyle.Dash); } private void ScottPlotFormControl_MouseMove(object sender, MouseEventArgs e) { //#region 鼠标移动,显示最近点的坐标值 ////// determine point nearest the cursor ////(double mouseCoordX, double mouseCoordY) = ScottPlotFormControl.GetMouseCoordinates(); ////double xyRatio = ScottPlotFormControl.Plot.XAxis.Dims.PxPerUnit / ScottPlotFormControl.Plot.YAxis.Dims.PxPerUnit; ////(double pointX, double pointY, int pointIndex) = _MyScatterPlot.GetPointNearest(mouseCoordX, mouseCoordY, xyRatio); ////// place the highlight over the point of interest ////_HighlightedPoint.Xs[0] = pointX; ////_HighlightedPoint.Ys[0] = pointY; ////_HighlightedPoint.IsVisible = true; ////// render if the highlighted point chnaged ////if (_LastHighlightedIndex != pointIndex) ////{ //// _LastHighlightedIndex = pointIndex; //// ScottPlotFormControl.Render(); ////} ////// update the GUI to describe the highlighted point ////LblShowResult.Text = $"Closest point to ({e.X:N0}, {e.Y:N0}) " + //// $"is index {pointIndex} ({pointX:N2}, {pointY:N2})"; //#endregion //#region 线标 //if (_IsCursorTracking) //{ // _VLine.IsVisible = true; // _HLine.IsVisible = true; // (double coordinateX, double coordinateY) = ScottPlotFormControl.GetMouseCoordinates(); // LblShowResult.Text = $"Mouse Pixel ({e.X:0.000}, {e.Y:0.000}) " + // $"Coordinate ({coordinateX:0.00000000}, {coordinateY:0.00000000})"; // _VLine.X = coordinateX; // _HLine.Y = coordinateY; //} //else //{ // _VLine.IsVisible = false; // _HLine.IsVisible = false; //} //#endregion } #endregion #region 窗口显示属性及配置 /// /// 波形显示风格 /// public WfmDrawMode DrawMode { get => DisplayPresenter.DrawMode; set => DisplayPresenter.DrawMode = value; } /// /// 波形亮度 /// public Int32 WfmIntensity { get => DisplayPresenter.WfmIntensity; set => DisplayPresenter.WfmIntensity = value; } /// /// 网格风格 /// public GridType GridStyle { get => DisplayPresenter.GridStyle; set => DisplayPresenter.GridStyle = value; } /// /// 网格亮度 /// public Int32 GridIntensity { get => DisplayPresenter.GridIntensity; set => DisplayPresenter.GridIntensity = value; } /// /// 标记是否可见 /// public Boolean AxisTickVisible { get => DisplayPresenter.AxisTickVisible; set => DisplayPresenter.AxisTickVisible = value; } /// /// 水平标记是否显示在下方 /// public Boolean XAxisTickBottom { get => DisplayPresenter.XAxisTickBottom; set => DisplayPresenter.XAxisTickBottom = value; } /// /// 垂直标记是否显示在右方 /// public Boolean YAxisTickRight { get => DisplayPresenter.YAxisTickRight; set => DisplayPresenter.YAxisTickRight = value; } /// /// 配置波形显示风格 /// /// 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; } } /// /// 配置波形亮度 /// /// private void ConfigWfmIntensity(Int32 wfmIntensity) { ScottPlotFormControl.Plot.WavePlotStyle(wfmIntensity); } /// /// 配置网格样式 /// /// 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; } } /// /// 配置网格亮度 /// /// private void ConfigGridIntensity(Int32 GridIntensity) { ScottPlotFormControl.Plot.Grid(color: Color.FromArgb((Int32)(GridIntensity / 100d * 255), Color.Gray)); } /// /// 配置水平标记的显示位置 /// /// private void ConfigHorizontalTickLabelDirection(Boolean horizontalBottom = true) { ScottPlotFormControl.Plot.XAxis.Edge = horizontalBottom == true ? ScottPlot.Renderable.Edge.Bottom : ScottPlot.Renderable.Edge.Top; } /// /// 配置垂直标记的显示位置 /// /// private void ConfigVirticalTickLabelDirection(Boolean virticalRight = true) { ScottPlotFormControl.Plot.YAxis.Edge = virticalRight == true ? ScottPlot.Renderable.Edge.Right : ScottPlot.Renderable.Edge.Left; } /// /// 配置标记label的显示 /// /// private void ConfigTickLabelVisible(Boolean visible = true) { ScottPlotFormControl.Plot.XAxis.IsVisible = visible; ScottPlotFormControl.Plot.YAxis.IsVisible = visible; } private void ToolStripMenuItemTimeWave_Click(object sender, EventArgs e) { if (_TimeDomainViewSettingForm != null) { _TimeDomainViewSettingForm.Close(); } _TimeDomainViewSettingForm = new TimeDomainViewSettingForm(); _TimeDomainViewSettingForm.SetGridComboBoxSelectType(Enum.GetValues()); XAxisTickType xaxisticktype = XAxisTickBottom ? XAxisTickType.Bottom : XAxisTickType.Top; YAxisTickType yaxisticktype = YAxisTickRight ? YAxisTickType.Right : YAxisTickType.Left; _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; //locationx = locationx + dsoForm.Left; //locationy = locationy + dsoForm.Top; _TimeDomainViewSettingForm.Location = new Point(locationx, locationy); } _TimeDomainViewSettingForm.Presenter = DisplayPresenter; _TimeDomainViewSettingForm.Presenter.TryAddView(_TimeDomainViewSettingForm); _TimeDomainViewSettingForm.FormClosed += (sender, e) => { _TimeDomainViewSettingForm.ParameterChangedEventHandler -= TimeDomainViewSettingForm_ParameterChangedEventHandler; _TimeDomainViewSettingForm = null; }; FormEventArgs formeventargs = new FormEventArgs() { Current = _TimeDomainViewSettingForm, Type = FormType.SettingForm }; EventBroker.Instance.GetEvent().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; } } private void ToolStripMenuItemXY_Click(object sender, EventArgs e) { XYViewSettingForm xysettingform = new XYViewSettingForm(); if (Program.Oscilloscope.View != null && Program.Oscilloscope.View is DsoForm dsoForm) { Int32 locationx = (dsoForm.Width - xysettingform.Width) / 2; locationx = locationx > 0 ? locationx : 0; Int32 locationy = (dsoForm.Height - xysettingform.Height) / 2; locationy = locationy > 0 ? locationy : 0; //locationx = locationx + dsoForm.Left; //locationy = locationy + dsoForm.Top; xysettingform.Location = new Point(locationx, locationy); } FormEventArgs formeventargs = new FormEventArgs() { Current = xysettingform, Type = FormType.SettingForm }; EventBroker.Instance.GetEvent().Publish(this, formeventargs); } private void OnLostFocusClick_FloatForm(object obj, EventArgs e) { var form = obj as Uni_Trend.MSO7000X.UserControls.FloatForm; if (form != null) { form.Close(); } } #endregion #region 事件 private void TimerRefresh_Tick(object sender, EventArgs e) { if (_IsRefreshing == false) { DrawWave(); } } private void WaveformViewForm_Load(object sender, EventArgs e) { ConfigureWavePlot(); ConfigGridIntensity(GridIntensity); ConfigGridStyle(GridStyle); ConfigWfmIntensity(WfmIntensity); ConfigDrawMode(DrawMode); ConfigTickLabelVisible(AxisTickVisible); ConfigVirticalTickLabelDirection(XAxisTickBottom); ConfigHorizontalTickLabelDirection(YAxisTickRight); ConfigScottPlotFormControlTouch(); _TimerRefresh.Interval = 10; _TimerRefresh.Tick += TimerRefresh_Tick; _TimerRefresh.Enabled = true; } private void ScottPlotFormControl_MouseDown(object sender, MouseEventArgs e) { _IsDraging = true; } private void ScottPlotFormControl_MouseLeave(object sender, EventArgs e) { _IsDraging = false; } private void ScottPlotFormControl_MouseUp(object sender, MouseEventArgs e) { _IsDraging = false; } private void ScottPlotFormControl_AxesChanged(object sender, EventArgs e) { //ConfigAxis(); } protected override void OnFormClosed(FormClosedEventArgs e) { _TimerRefresh.Dispose(); TmbPresenter.TryRemoveView(this); TrgPresenter.TryRemoveView(this); base.OnFormClosed(e); } ~WaveformFigure() { if (_TimerRefresh != null) _TimerRefresh.Dispose(); } #endregion #region IDsoView public MeasPrsnt MeasurePresenter { get; set; } public DisplayPrsnt DisplayPresenter { get; set; } IDisplayPrsnt IView.Presenter { get => DisplayPresenter; set => DisplayPresenter = (DisplayPrsnt)value; } public TimebasePrsnt TmbPresenter { get; set; } ITimebasePrsnt IView.Presenter { get => TmbPresenter; set => TmbPresenter = (TimebasePrsnt)value; } public TriggerPrsnt TrgPresenter { get; set; } ITriggerPrsnt IView.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 private Boolean isZooming = false; private void uestcIconButton1_MouseDown(object sender, MouseEventArgs e) { isZooming = !isZooming; ScottPlotFormControl.SetZoomingState(isZooming); } //缩放的累计量集合元组,包含了:水平放大,水平缩小,垂直放大,垂直缩小; private (float ZoomInH, float ZoomOutH, float ZoomInV, float ZoomOutV) _ScaleCumulative = (1F, 1F, 1F, 1F); /// /// 对波形显示控件的触摸事件进行配置处理; /// private void ConfigScottPlotFormControlTouch() { //处理TouchDown事件 ScottPlotFormControl.TouchDown += (x, y) => { _ScaleCumulative = (1F, 1F, 1F, 1F); _IsDraging = true; }; //处理TouchUp事件 ScottPlotFormControl.TouchUp += (x, y) => { _ScaleCumulative = (1F, 1F, 1F, 1F); _IsDraging = false; }; //处理TouchScale事件;分水平和垂直方向 ScottPlotFormControl.TouchScale += (x, y) => { switch(y) { case Uni_Trend.MSO7000X.Touch.ScaleDirection.Horizontal: OnScaleHorizontal(x); break; case Uni_Trend.MSO7000X.Touch.ScaleDirection.Veritcal: OnScaleVertical(x); break; default:// Uni_Trend.MSO7000X.Touch.ScaleDirection.Both OnScaleHorizontal(x); OnScaleVertical(x); break; } }; } /// /// 处理水平缩放 /// /// 水平缩放的比例 private void OnScaleHorizontal(float scale) { if (scale > 1F)//放大 { _ScaleCumulative.ZoomInH *= scale; if (_ScaleCumulative.ZoomInH > 1.25F) { TmbPresenter.ScaleIndex--; _ScaleCumulative.ZoomInH = 1F; } } else//缩小 { _ScaleCumulative.ZoomOutH *= scale; if (_ScaleCumulative.ZoomOutH < 0.8F) { TmbPresenter.ScaleIndex++; _ScaleCumulative.ZoomOutH = 1F; } } } /// /// 处理垂直缩放 /// /// 垂直缩放的比例 private void OnScaleVertical(float scale) { //找到当前选中通道的P层数据 AnalogPrsnt channelPrsnt = null; if(DsoPrsnt.FocusId.IsAnalog()) { Program.Oscilloscope.TryGetChannel(DsoPrsnt.FocusId, out IChnlPrsnt channel); if(channel != null && channel.Active == true) { channelPrsnt = channel as AnalogPrsnt; } } //波形进行缩放 if(channelPrsnt != null) { if (scale > 1F)//放大 { _ScaleCumulative.ZoomInV *= scale; if (_ScaleCumulative.ZoomInV > 1.25F) { channelPrsnt.AnaScaleIndex--; _ScaleCumulative.ZoomInV = 1F; } } else//缩小 { _ScaleCumulative.ZoomOutV *= scale; if (_ScaleCumulative.ZoomOutV < 0.8F) { channelPrsnt.AnaScaleIndex++; _ScaleCumulative.ZoomOutV = 1F; } } } } } }