123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904 |
- // Copyright (c) UESTC. All Rights Reserved
- // <author>QC</author>
- // <date>2022/4/20</date>
- namespace Uestc.Auto6.Dso.Core
- {
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using Uestc.Auto6.Dso.ComModel;
- using Uestc.Auto6.Dso.Core.Tools;
- /// <summary>
- /// Defines the WfmFormat.
- /// </summary>
- public enum WfmFormat
- {
- [Alias("bin")]
- Binary,
- [Alias("txt")]
- Text,
- [Alias("mat")]
- Matlab,
- [Alias("xls")]
- Excel,
- [Alias("csv")]
- CSV,
- [Alias("tsv")]
- TSV,
- [Alias("wfm")]
- WFM,
- [Alias("h5")]
- HDF5,
- }
- /// <summary>
- /// Defines the PicFormat.
- /// </summary>
- public enum PicFormat
- {
- Bmp,
- Tiff,
- Gif,
- Png,
- Jpeg,
- }
- /// <summary>
- /// Defines the PicArea.
- /// </summary>
- public enum PicArea
- {
- FullScreen = 0,
- Window = 1,
- Application = 2
- }
- public enum PicColor
- {
- Standard = 0,
- BlackWhite = 1,
- Reverse = 2
- }
- public enum TxtFormat
- {
- ASCII,
- GB2312,
- UTF8,
- UTF32,
- Unicode,
- }
- /// <summary>
- /// Defines the <see cref="FilePrsnt" />.
- /// </summary>
- public class FilePrsnt : MulticastPrsnt<IFileView>, IFilePrsnt
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="FilePrsnt"/> class.
- /// </summary>
- /// <param name="view">The view<see cref="IFileView?"/>.</param>
- /// <param name="mco">The mco<see cref="ModelCreateOptions"/>.</param>
- public FilePrsnt(IFileView? view, ModelCreateOptions mco = ModelCreateOptions.Dependant)
- {
- Model = mco switch
- {
- ModelCreateOptions.Dependant => DsoModel.Default.File,
- ModelCreateOptions.Standalone => new(),
- _ => throw new ArgumentException($"Argument '{nameof(mco)}' can not assign to '{nameof(ModelCreateOptions.InitializedByChild)}'."),
- };
- Model.PropertyChanged += OnPropertyChanged;
- if (view != null)
- {
- view.Presenter = this;
- TryAddView(view);
- }
- }
- /// <summary>
- /// Gets or sets the GetImageStreamHandler.
- /// </summary>
- public static Func<PicFormat, PicArea, PicColor, MemoryStream>? GetImageStreamHandler { get; set; }
- public static Func<String, String, ChannelId, Boolean>? SaveLabNoteBookHandler { get; set; }
- /// <summary>
- /// Gets the DefaultPrefixName.
- /// </summary>
- public String DefaultPrefixName => Model.DefaultPrefixName;
- /// <summary>
- /// Gets or sets the FileName.
- /// </summary>
- public String FileName { get => Model.FileName; set => Model.FileName = value; }
- /// <summary>
- /// Gets or sets the IfAppendDatetime.
- /// </summary>
- public Boolean IfAppendDatetime { get => Model.IfAppendDatetime; set => Model.IfAppendDatetime = value; }
- /// <summary>
- /// Gets or sets the IsDefaultSetting.
- /// </summary>
- public Boolean IsDefaultSetting { get => Model.IsDefaultSetting; set => Model.IsDefaultSetting = value; }
- /// <summary>
- /// Gets or sets the PicFormat.
- /// </summary>
- public PicFormat PicFormat { get => Model.PicFormat; set => Model.PicFormat = value; }
- /// <summary>
- /// Gets or sets the PicPath.
- /// </summary>
- public String PicPath { get => Model.PicPath; set => Model.PicPath = value; }
- /// <summary>
- /// Gets or sets the PicRegion.
- /// </summary>
- public PicArea PicRegion { get => Model.PicRegion; set => Model.PicRegion = value; }
- /// <summary>
- /// Gets or sets the SettingLoadFullPath.
- /// </summary>
- public String SettingLoadFullPath { get => Model.SettingLoadFullPath; set => Model.SettingLoadFullPath = value; }
- /// <summary>
- /// Gets or sets the SettingSavePath.
- /// </summary>
- public String SettingSavePath { get => Model.SettingSavePath; set => Model.SettingSavePath = value; }
- /// <summary>
- /// Gets or sets the WfmFormat.
- /// </summary>
- public WfmFormat WfmFormat { get => Model.WfmFormat; set => Model.WfmFormat = value; }
- /// <summary>
- /// Gets or sets the WfmPath.
- /// </summary>
- public String WfmPath { get => Model.WfmPath; set => Model.WfmPath = value; }
- /// <summary>
- /// Gets or sets the WfmSource.
- /// </summary>
- public ChannelId WfmSource { get => Model.WfmSource; set => Model.WfmSource = value; }
- /// <summary>
- /// Gets or sets the PicColor.
- /// </summary>
- public PicColor PicColor { get => Model.PicColor; set => Model.PicColor = value; }
- public TxtFormat WfmTxtFormat { get => Model.WfmTxtFormat; set => Model.WfmTxtFormat = value; }
- /// <summary>
- /// Gets the Model.
- /// </summary>
- private protected override FileModel Model { get; }
- /// <summary>
- /// The GetDateTimeString.
- /// </summary>
- /// <returns>The <see cref="String"/>.</returns>
- public static String GetDateTimeString() => "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
- /// <summary>
- /// The GetImageBase64String.
- /// </summary>
- /// <returns>The <see cref="String?"/>.</returns>
- public static String? GetImageBase64String()
- {
- var ms = GetImageStreamHandler?.Invoke(PicFormat.Jpeg, PicArea.Application, PicColor.Standard);
- if (ms is not null)
- {
- try
- {
- var b64string = Convert.ToBase64String(ms.ToArray());
- ms.Close();
- return b64string;
- }
- catch (Exception e)
- {
- Logger.Error("Failed to convert image to Base64 string: " + e.ToString());
- }
- }
- return null;
- }
- public static Encoding GetEncoding(TxtFormat format)
- {
- try
- {
- System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
- return format switch
- {
- TxtFormat.ASCII => Encoding.ASCII,
- TxtFormat.UTF8 => Encoding.UTF8,
- TxtFormat.UTF32 => Encoding.UTF32,
- TxtFormat.GB2312 => Encoding.GetEncoding("GB2312"),
- TxtFormat.Unicode => Encoding.Unicode,
- _ => Encoding.Default
- };
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- return Encoding.Default;
- }
- }
- /// <summary>
- /// The GetPicFileExtName.
- /// </summary>
- /// <param name="pf">The pf<see cref="PicFormat"/>.</param>
- /// <returns>The <see cref="String"/>.</returns>
- public static String GetPicFileExtName(PicFormat pf) => pf switch
- {
- PicFormat.Bmp => ".bmp",
- PicFormat.Gif => ".gif",
- PicFormat.Jpeg => ".jpeg",
- PicFormat.Png => ".png",
- PicFormat.Tiff => ".tiff",
- _ => ".dat"
- };
- /// <summary>
- /// The LoadDefSetting.
- /// </summary>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean LoadDefSetting() =>
- //Load Factory *.set
- LoadSetting(Constants.SET_DEF_PATH + "\\" + Constants.FACTORY_SET_NAME + ".set");
- /// <summary>
- /// The LoadFromText.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <param name="reader">The reader<see cref="Action{StreamReader}"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean LoadFromText(String fullpath, Action<StreamReader> reader)
- {
- try
- {
- using var fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
- using StreamReader sr = new(fs, Encoding.UTF8);
- reader(sr);
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The LoadFromText.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <param name="buffer">The buffer<see cref="List{Double}"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean LoadFromText(String fullpath, out List<Double> buffer)
- {
- buffer = new List<Double>();
- try
- {
- using var fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
- using StreamReader sr = new(fs, Encoding.UTF8);
- String? line;
- while ((line = sr.ReadLine()) != null)
- {
- buffer.Add(Double.Parse(line));
- }
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The LoadSetting.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean LoadSetting(String fullpath)
- {
- try
- {
- if (!File.Exists(fullpath))
- {
- WeakTip.Default.Write(nameof(LoadSetting), MsgTipId.ReadingFailed);
- return false;
- }
- using MemoryStream memorystream = new(File.ReadAllBytes(fullpath));
- var setting = BinaryConvert.Deserialize<SysSettings>(memorystream);
- if (setting is null)
- {
- return false;
- }
- setting.OnDeserialized();
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The LoadWaveform.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <param name="id">The id<see cref="ChannelId"/>.</param>
- /// <param name="dso">The dso<see cref="DsoPrsnt"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean LoadWaveform(String fullpath, ChannelId id, DsoPrsnt dso)
- {
- ReferencePrsnt? rprsnt = null;
- if (ReferencePrsnt.TryRead(id, fullpath, ref rprsnt))
- {
- //Add a new reference channel presenter
- dso.AddChannel(id, rprsnt!);
- DsoPrsnt.FocusId = rprsnt!.Id;
- return true;
- }
- return false;
- }
- /// <summary>
- /// The SaveImage.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <param name="pf">The pf<see cref="PicFormat"/>.</param>
- /// <param name="region">The region<see cref="PicArea"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveImage(String fullpath, PicFormat pf, PicArea region, PicColor color = PicColor.Standard)
- {
- var path = Path.GetDirectoryName(fullpath);
- var file = Path.GetFileName(fullpath);
- if (String.IsNullOrEmpty(file))
- {
- return false;
- }
- if (path?.Length == 0)
- {
- path = Directory.GetCurrentDirectory();
- }
- return SaveImage(path!, file, pf, region, false, color);
- }
- /// <summary>
- /// The SaveImage.
- /// </summary>
- /// <param name="path">The path<see cref="String"/>.</param>
- /// <param name="name">The name<see cref="String"/>.</param>
- /// <param name="pf">The pf<see cref="PicFormat"/>.</param>
- /// <param name="region">The region<see cref="PicArea"/>.</param>
- /// <param name="postfix">The postfix<see cref="Boolean"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveImage(String path, String name, PicFormat pf, PicArea region, Boolean postfix, PicColor color = PicColor.Standard)
- {
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- if (!Directory.Exists(path))
- {
- Logger.Error($"Path does not exist: '{path}'");
- return false;
- }
- }
- String fullfilename = path + "\\" + name + (postfix ? GetDateTimeString() : "") + GetPicFileExtName(pf);
- if (File.Exists(fullfilename))
- {
- if (!StrongTip.Default.Show(MsgTipId.Warning, MsgTipId.FileExisted, MessageType.Warning))
- {
- return false;
- }
- File.Delete(fullfilename);
- }
- var ms = GetImageStreamHandler?.Invoke(pf, region, color);
- if (ms is not null)
- {
- try
- {
- using FileStream fs = new(fullfilename, FileMode.Create, FileAccess.Write);
- ms.WriteTo(fs);
- ms.Close();
- return true;
- }
- catch (Exception e)
- {
- Logger.Error("Failed to save full screen: " + e.ToString());
- }
- }
- return false;
- }
- public static Boolean? SaveLabNoteBook(String LabNotePath, String LabNoteName, ChannelId channelId) => SaveLabNoteBookHandler?.Invoke(LabNotePath, LabNoteName, channelId);
- /// <summary>
- /// The SaveSetting.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveSetting(String fullpath)
- {
- var path = Path.GetDirectoryName(fullpath);
- var file = Path.GetFileName(fullpath);
- if (String.IsNullOrEmpty(file))
- {
- return false;
- }
- if (path?.Length == 0)
- {
- path = Directory.GetCurrentDirectory();
- }
- return SaveSetting(path!, file, false, true);
- }
- /// <summary>
- /// The SaveSetting.
- /// </summary>
- /// <param name="path">The path<see cref="String"/>.</param>
- /// <param name="name">The name<see cref="String"/>.</param>
- /// <param name="postfix">The postfix<see cref="Boolean"/>.</param>
- /// <param name="dumb">The dumb<see cref="Boolean"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveSetting(String path, String name, Boolean postfix, Boolean dumb = false)
- {
- try
- {
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- if (!Directory.Exists(path))
- {
- Logger.Error($"Path does not exist: '{path}'");
- return false;
- }
- }
- String fullfilename = path + "\\" + name + (postfix ? GetDateTimeString() : "") + ".set";
- if (File.Exists(fullfilename))
- {
- if (!dumb && !StrongTip.Default.Show(MsgTipId.Warning, MsgTipId.FileExisted, MessageType.Warning))
- {
- return false;
- }
- File.Delete(fullfilename);
- }
- SysSettings settings = new();
- settings.OnSerializing();
- using var fs = new FileStream(fullfilename, FileMode.Create, FileAccess.Write);
- BinaryConvert.Serialize(settings, fs);
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The SaveToText.
- /// </summary>
- /// <param name="path">The path<see cref="String"/>.</param>
- /// <param name="name">The name<see cref="String"/>.</param>
- /// <param name="writer">The writer<see cref="Action{StreamWriter}"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveToText(String path, String name, Action<StreamWriter> writer)
- {
- try
- {
- String fullfilename = path + "\\" + name + ".txt";
- using var fs = new FileStream(fullfilename, FileMode.Create, FileAccess.Write);
- using StreamWriter sw = new(fs, Encoding.UTF8);
- writer(sw);
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The SaveToText.
- /// </summary>
- /// <param name="path">The path<see cref="String"/>.</param>
- /// <param name="name">The name<see cref="String"/>.</param>
- /// <param name="buffer">The buffer<see cref="IEnumerable{Double}"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveToText(String path, String name, IEnumerable<Double> buffer)
- {
- try
- {
- String fullfilename = path + "\\" + name + ".txt";
- using var fs = new FileStream(fullfilename, FileMode.OpenOrCreate, FileAccess.Write);
- using StreamWriter sw = new(fs, Encoding.UTF8);
- foreach (var d in buffer)
- {
- sw.WriteLine(d);
- }
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The SaveWaveByBin.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByBin(Stream stm, WfmPack pkg)
- {
- try
- {
- BinaryConvert.Serialize(pkg, stm);
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
-
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The SaveWaveByCSV.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByCSV(Stream stm, WfmPack pkg) => SaveWaveByText(stm, pkg, (x, y) => x.ToString("E") + "," + String.Join(",", y.Select(o => o.ToString("G"))));
- /// <summary>
- /// The SaveWaveByExcel.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByExcel(Stream stm, WfmPack pkg)
- {
- try
- {
- HSSFWorkbook workbook = new();
- ISheet sheet = workbook.CreateSheet("Sheet1");
- Int32 length = pkg.Buffer.GetLength(1);
- Int32 wfmcnt = pkg.Buffer.GetLength(0);
- Double sp = pkg.Properties.TmbScale.Value * Constants.VIS_XDIVS_NUM / length;
- Double pos0 = pkg.Properties.TmbPosition.Index;
- Double time;
- IRow head = sheet.CreateRow(0);
- head.CreateCell(0).SetCellValue($"Time({pkg.Properties.TmbUnit.Prefix.ToPfxString()}{pkg.Properties.TmbUnit.Name})");
- for (Int32 i = 0; i < wfmcnt; i++)
- {
- head.CreateCell(i + 1).SetCellValue($"Ampl({pkg.Properties.ChnlUnit.Prefix.ToPfxString()}{pkg.Properties.ChnlUnit.Name})");
- }
- for (Int32 j = 0; j < length; j++)
- {
- var row = (HSSFRow)sheet.CreateRow(j + 1);
- time = (j - pos0) * sp + pkg.Properties.TrigErrorTime;
- row.CreateCell(0).SetCellValue(time);
- for (Int32 i = 0; i < wfmcnt; i++)
- {
- row.CreateCell(i + 1).SetCellValue(pkg.Buffer[i, j]);
- //if (row.GetCell(i) == null)
- //{
- // row.CreateCell(i).SetCellValue(data);
- //}
- //else
- //{
- // row.GetCell(i).SetCellValue(data);
- //}
- }
- }
-
- workbook.Write(stm);
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The SaveWaveByMatlab.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByMatlab(Stream stm, WfmPack pkg) => SaveWaveByText(stm, pkg, (x, y) => x.ToString("E") + " " + String.Join(" ", y.Select(o => o.ToString("G"))));
- /// <summary>
- /// The SaveWaveByText.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <param name="cbfunc">The cbfunc<see cref="Func{Double, Double[], String}"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByText(Stream stm, WfmPack pkg, Func<Double, Double[], String> cbfunc, TxtFormat wfmtxtformat = TxtFormat.UTF8)
- {
- try
- {
- using StreamWriter sw = new(stm, GetEncoding(wfmtxtformat));
- Int32 length = pkg.Buffer.GetLength(1);
- Int32 wfmcnt = pkg.Buffer.GetLength(0);
- Double sp = pkg.Properties.TmbScale.Value * Constants.VIS_XDIVS_NUM / length;
- Double pos0 = pkg.Properties.TmbPosition.Index;
- Double time;
- Double[] ampls = new Double[wfmcnt];
- var header = $"Ampl({pkg.Properties.ChnlUnit.Prefix.ToPfxString()}{pkg.Properties.ChnlUnit.Name})";
- for (Int32 i = 1; i < wfmcnt; i++)
- {
- header = header + ", " + header;
- }
- header = $"Time({pkg.Properties.TmbUnit.Prefix.ToPfxString()}{pkg.Properties.TmbUnit.Name}), " + header;
- sw.WriteLine(header);
- for (Int32 j = 0; j < length; j++)
- {
- time = (j - pos0) * sp + pkg.Properties.TrigErrorTime;
- for (Int32 i = 0; i < wfmcnt; i++)
- {
- ampls[i] = pkg.Buffer[i, j];
- }
- sw.WriteLine(cbfunc(time, ampls));
- }
- }
- catch (Exception e)
- {
- Logger.Error(e.ToString());
- #if DEBUG
- throw;
- #else
- return false;
- #endif
- }
- return true;
- }
- /// <summary>
- /// The SaveWaveByTSV.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByTSV(Stream stm, WfmPack pkg) => SaveWaveByText(stm, pkg, (x, y) => x.ToString("E") + "\t" + String.Join("\t", y.Select(o => o.ToString("G"))));
- /// <summary>
- /// The SaveWaveByTxt.
- /// </summary>
- /// <param name="stm">The stm<see cref="Stream"/>.</param>
- /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveByTxt(Stream stm, WfmPack pkg, TxtFormat fotmat) => SaveWaveByText(stm, pkg, (x, y) => x.ToString("E") + "," + String.Join(",", y.Select(o => o.ToString("G"))), fotmat);
- /// <summary>
- /// The SaveWaveform.
- /// </summary>
- /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
- /// <param name="wf">The wf<see cref="WfmFormat"/>.</param>
- /// <param name="id">The id<see cref="ChannelId"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveform(String fullpath, WfmFormat wf, ChannelId id, TxtFormat wfmtxtfotmat = TxtFormat.UTF8)
- {
- var path = Path.GetDirectoryName(fullpath);
- var file = Path.GetFileName(fullpath);
- if (String.IsNullOrEmpty(file))
- {
- return false;
- }
- if (path?.Length == 0)
- {
- path = Directory.GetCurrentDirectory();
- }
- return SaveWaveform(path!, file, wf, id, false, wfmtxtfotmat);
- }
- /// <summary>
- /// The SaveWaveform.
- /// </summary>
- /// <param name="path">The path<see cref="String"/>.</param>
- /// <param name="name">The name<see cref="String"/>.</param>
- /// <param name="wf">The wf<see cref="WfmFormat"/>.</param>
- /// <param name="id">The id<see cref="ChannelId"/>.</param>
- /// <param name="postfix">The postfix<see cref="Boolean"/>.</param>
- /// <returns>The <see cref="Boolean"/>.</returns>
- public static Boolean SaveWaveform(String path, String name, WfmFormat wf, ChannelId id, Boolean postfix, TxtFormat wfmtxtfotmat = TxtFormat.UTF8)
- {
- if (DsoModel.Default.TryGetChannel(id, out var cm) && cm.Pack is not null)
- {
- //if (cm.Pack.Properties.Version != "U2.0")
- // return false;
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- if (!Directory.Exists(path))
- {
- Logger.Error($"Path does not exist: '{path}'");
- return false;
- }
- }
- String fullfilename = path + "\\" + name + (postfix ? GetDateTimeString() : "") + "." + wf.GetAlias();
- if (File.Exists(fullfilename))
- {
- if (!StrongTip.Default.Show(MsgTipId.Warning, MsgTipId.FileExisted, MessageType.Warning))
- {
- return false;
- }
- File.Delete(fullfilename);
- }
- using var fs = new FileStream(fullfilename, FileMode.OpenOrCreate, FileAccess.Write);
- switch (wf)
- {
- case WfmFormat.Binary:
- return FilePrsnt.SaveWaveByBin(fs, cm.Pack);
- case WfmFormat.Text:
- return FilePrsnt.SaveWaveByTxt(fs, cm.Pack, wfmtxtfotmat);
- case WfmFormat.Matlab:
- return FilePrsnt.SaveWaveByMatlab(fs, cm.Pack);
- case WfmFormat.Excel:
- return FilePrsnt.SaveWaveByExcel(fs, cm.Pack);
- case WfmFormat.CSV:
- return FilePrsnt.SaveWaveByCSV(fs, cm.Pack);
- case WfmFormat.TSV:
- return FilePrsnt.SaveWaveByTSV(fs, cm.Pack);
- case WfmFormat.WFM:
- case WfmFormat.HDF5:
- default:
- WeakTip.Default.Write("WfmSave", MsgTipId.UnSupportedFormat);
- return false;
- }
- // return wf switch
- // {
- // WfmFormat.Binary =>
- // FilePrsnt.SaveWaveByBin(fs, cm.Pack),
- // WfmFormat.Text =>
- // FilePrsnt.SaveWaveByTxt(fs, cm.Pack, wfmtxtfotmat),
- // WfmFormat.Excel =>
- // FilePrsnt.SaveWaveByExcel(fs, cm.Pack),
- // WfmFormat.Matlab =>
- // FilePrsnt.SaveWaveByMatlab(fs, cm.Pack),
- // WfmFormat.CSV =>
- // FilePrsnt.SaveWaveByCSV(fs, cm.Pack),
- // WfmFormat.TSV =>
- // FilePrsnt.SaveWaveByTSV(fs, cm.Pack),
- // _ =>
- //#if DEBUG
- // throw new NotImplementedException(),
- //#else
- // //Logger.Error($"{nameof(WfmFormat)} '{nameof(wf)}' = {wf}"),
- // throw new NotImplementedException()
- //#endif
- // };
- }
- return false;
- }
- /// <summary>
- /// The MakeDefaultFileName.
- /// </summary>
- /// <param name="path">The path<see cref="String"/>.</param>
- /// <param name="ext">The ext<see cref="String"/>.</param>
- /// <returns>The <see cref="String"/>.</returns>
- public String MakeDefaultFileName(String path, String ext)
- {
- var result = new DirectoryInfo(path)
- .GetFiles($"*{ext}", SearchOption.TopDirectoryOnly)
- .Where(x => Regex.IsMatch(x.Name, $"^{DefaultPrefixName}[0-9]{"{3}"}{ext}$", RegexOptions.IgnoreCase));
- return DefaultPrefixName + String.Format("{0:D3}", result.Count());
- }
- }
- }
|