FilePrsnt.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. // Copyright (c) UESTC. All Rights Reserved
  2. // <author>QC</author>
  3. // <date>2022/4/20</date>
  4. namespace Uestc.Auto6.Dso.Core
  5. {
  6. using NPOI.HSSF.UserModel;
  7. using NPOI.SS.UserModel;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Text.RegularExpressions;
  14. using Uestc.Auto6.Dso.ComModel;
  15. using Uestc.Auto6.Dso.Core.Tools;
  16. /// <summary>
  17. /// Defines the WfmFormat.
  18. /// </summary>
  19. public enum WfmFormat
  20. {
  21. [Alias("bin")]
  22. Binary,
  23. [Alias("txt")]
  24. Text,
  25. [Alias("mat")]
  26. Matlab,
  27. [Alias("xls")]
  28. Excel,
  29. [Alias("csv")]
  30. CSV,
  31. [Alias("tsv")]
  32. TSV,
  33. [Alias("wfm")]
  34. WFM,
  35. [Alias("h5")]
  36. HDF5,
  37. }
  38. /// <summary>
  39. /// Defines the PicFormat.
  40. /// </summary>
  41. public enum PicFormat
  42. {
  43. Bmp,
  44. Tiff,
  45. Gif,
  46. Png,
  47. Jpeg,
  48. }
  49. /// <summary>
  50. /// Defines the PicArea.
  51. /// </summary>
  52. public enum PicArea
  53. {
  54. FullScreen = 0,
  55. Window = 1,
  56. Application = 2
  57. }
  58. public enum PicColor
  59. {
  60. Standard = 0,
  61. BlackWhite = 1,
  62. Reverse = 2
  63. }
  64. public enum TxtFormat
  65. {
  66. ASCII,
  67. GB2312,
  68. UTF8,
  69. UTF32,
  70. Unicode,
  71. }
  72. /// <summary>
  73. /// Defines the <see cref="FilePrsnt" />.
  74. /// </summary>
  75. public class FilePrsnt : MulticastPrsnt<IFileView>, IFilePrsnt
  76. {
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="FilePrsnt"/> class.
  79. /// </summary>
  80. /// <param name="view">The view<see cref="IFileView?"/>.</param>
  81. /// <param name="mco">The mco<see cref="ModelCreateOptions"/>.</param>
  82. public FilePrsnt(IFileView? view, ModelCreateOptions mco = ModelCreateOptions.Dependant)
  83. {
  84. Model = mco switch
  85. {
  86. ModelCreateOptions.Dependant => DsoModel.Default.File,
  87. ModelCreateOptions.Standalone => new(),
  88. _ => throw new ArgumentException($"Argument '{nameof(mco)}' can not assign to '{nameof(ModelCreateOptions.InitializedByChild)}'."),
  89. };
  90. Model.PropertyChanged += OnPropertyChanged;
  91. if (view != null)
  92. {
  93. view.Presenter = this;
  94. TryAddView(view);
  95. }
  96. }
  97. /// <summary>
  98. /// Gets or sets the GetImageStreamHandler.
  99. /// </summary>
  100. public static Func<PicFormat, PicArea, PicColor, MemoryStream>? GetImageStreamHandler { get; set; }
  101. public static Func<String, String, ChannelId, Boolean>? SaveLabNoteBookHandler { get; set; }
  102. /// <summary>
  103. /// Gets the DefaultPrefixName.
  104. /// </summary>
  105. public String DefaultPrefixName => Model.DefaultPrefixName;
  106. /// <summary>
  107. /// Gets or sets the FileName.
  108. /// </summary>
  109. public String FileName { get => Model.FileName; set => Model.FileName = value; }
  110. /// <summary>
  111. /// Gets or sets the IfAppendDatetime.
  112. /// </summary>
  113. public Boolean IfAppendDatetime { get => Model.IfAppendDatetime; set => Model.IfAppendDatetime = value; }
  114. /// <summary>
  115. /// Gets or sets the IsDefaultSetting.
  116. /// </summary>
  117. public Boolean IsDefaultSetting { get => Model.IsDefaultSetting; set => Model.IsDefaultSetting = value; }
  118. /// <summary>
  119. /// Gets or sets the PicFormat.
  120. /// </summary>
  121. public PicFormat PicFormat { get => Model.PicFormat; set => Model.PicFormat = value; }
  122. /// <summary>
  123. /// Gets or sets the PicPath.
  124. /// </summary>
  125. public String PicPath { get => Model.PicPath; set => Model.PicPath = value; }
  126. /// <summary>
  127. /// Gets or sets the PicRegion.
  128. /// </summary>
  129. public PicArea PicRegion { get => Model.PicRegion; set => Model.PicRegion = value; }
  130. /// <summary>
  131. /// Gets or sets the SettingLoadFullPath.
  132. /// </summary>
  133. public String SettingLoadFullPath { get => Model.SettingLoadFullPath; set => Model.SettingLoadFullPath = value; }
  134. /// <summary>
  135. /// Gets or sets the SettingSavePath.
  136. /// </summary>
  137. public String SettingSavePath { get => Model.SettingSavePath; set => Model.SettingSavePath = value; }
  138. /// <summary>
  139. /// Gets or sets the WfmFormat.
  140. /// </summary>
  141. public WfmFormat WfmFormat { get => Model.WfmFormat; set => Model.WfmFormat = value; }
  142. /// <summary>
  143. /// Gets or sets the WfmPath.
  144. /// </summary>
  145. public String WfmPath { get => Model.WfmPath; set => Model.WfmPath = value; }
  146. /// <summary>
  147. /// Gets or sets the WfmSource.
  148. /// </summary>
  149. public ChannelId WfmSource { get => Model.WfmSource; set => Model.WfmSource = value; }
  150. /// <summary>
  151. /// Gets or sets the PicColor.
  152. /// </summary>
  153. public PicColor PicColor { get => Model.PicColor; set => Model.PicColor = value; }
  154. public TxtFormat WfmTxtFormat { get => Model.WfmTxtFormat; set => Model.WfmTxtFormat = value; }
  155. /// <summary>
  156. /// Gets the Model.
  157. /// </summary>
  158. private protected override FileModel Model { get; }
  159. /// <summary>
  160. /// The GetDateTimeString.
  161. /// </summary>
  162. /// <returns>The <see cref="String"/>.</returns>
  163. public static String GetDateTimeString() => "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
  164. /// <summary>
  165. /// The GetImageBase64String.
  166. /// </summary>
  167. /// <returns>The <see cref="String?"/>.</returns>
  168. public static String? GetImageBase64String()
  169. {
  170. var ms = GetImageStreamHandler?.Invoke(PicFormat.Jpeg, PicArea.Application, PicColor.Standard);
  171. if (ms is not null)
  172. {
  173. try
  174. {
  175. var b64string = Convert.ToBase64String(ms.ToArray());
  176. ms.Close();
  177. return b64string;
  178. }
  179. catch (Exception e)
  180. {
  181. Logger.Error("Failed to convert image to Base64 string: " + e.ToString());
  182. }
  183. }
  184. return null;
  185. }
  186. public static Encoding GetEncoding(TxtFormat format)
  187. {
  188. try
  189. {
  190. System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
  191. return format switch
  192. {
  193. TxtFormat.ASCII => Encoding.ASCII,
  194. TxtFormat.UTF8 => Encoding.UTF8,
  195. TxtFormat.UTF32 => Encoding.UTF32,
  196. TxtFormat.GB2312 => Encoding.GetEncoding("GB2312"),
  197. TxtFormat.Unicode => Encoding.Unicode,
  198. _ => Encoding.Default
  199. };
  200. }
  201. catch (Exception e)
  202. {
  203. Logger.Error(e.ToString());
  204. return Encoding.Default;
  205. }
  206. }
  207. /// <summary>
  208. /// The GetPicFileExtName.
  209. /// </summary>
  210. /// <param name="pf">The pf<see cref="PicFormat"/>.</param>
  211. /// <returns>The <see cref="String"/>.</returns>
  212. public static String GetPicFileExtName(PicFormat pf) => pf switch
  213. {
  214. PicFormat.Bmp => ".bmp",
  215. PicFormat.Gif => ".gif",
  216. PicFormat.Jpeg => ".jpeg",
  217. PicFormat.Png => ".png",
  218. PicFormat.Tiff => ".tiff",
  219. _ => ".dat"
  220. };
  221. /// <summary>
  222. /// The LoadDefSetting.
  223. /// </summary>
  224. /// <returns>The <see cref="Boolean"/>.</returns>
  225. public static Boolean LoadDefSetting() =>
  226. //Load Factory *.set
  227. LoadSetting(Constants.SET_DEF_PATH + "\\" + Constants.FACTORY_SET_NAME + ".set");
  228. /// <summary>
  229. /// The LoadFromText.
  230. /// </summary>
  231. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  232. /// <param name="reader">The reader<see cref="Action{StreamReader}"/>.</param>
  233. /// <returns>The <see cref="Boolean"/>.</returns>
  234. public static Boolean LoadFromText(String fullpath, Action<StreamReader> reader)
  235. {
  236. try
  237. {
  238. using var fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
  239. using StreamReader sr = new(fs, Encoding.UTF8);
  240. reader(sr);
  241. }
  242. catch (Exception e)
  243. {
  244. Logger.Error(e.ToString());
  245. #if DEBUG
  246. throw;
  247. #else
  248. return false;
  249. #endif
  250. }
  251. return true;
  252. }
  253. /// <summary>
  254. /// The LoadFromText.
  255. /// </summary>
  256. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  257. /// <param name="buffer">The buffer<see cref="List{Double}"/>.</param>
  258. /// <returns>The <see cref="Boolean"/>.</returns>
  259. public static Boolean LoadFromText(String fullpath, out List<Double> buffer)
  260. {
  261. buffer = new List<Double>();
  262. try
  263. {
  264. using var fs = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
  265. using StreamReader sr = new(fs, Encoding.UTF8);
  266. String? line;
  267. while ((line = sr.ReadLine()) != null)
  268. {
  269. buffer.Add(Double.Parse(line));
  270. }
  271. }
  272. catch (Exception e)
  273. {
  274. Logger.Error(e.ToString());
  275. #if DEBUG
  276. throw;
  277. #else
  278. return false;
  279. #endif
  280. }
  281. return true;
  282. }
  283. /// <summary>
  284. /// The LoadSetting.
  285. /// </summary>
  286. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  287. /// <returns>The <see cref="Boolean"/>.</returns>
  288. public static Boolean LoadSetting(String fullpath)
  289. {
  290. try
  291. {
  292. if (!File.Exists(fullpath))
  293. {
  294. WeakTip.Default.Write(nameof(LoadSetting), MsgTipId.ReadingFailed);
  295. return false;
  296. }
  297. using MemoryStream memorystream = new(File.ReadAllBytes(fullpath));
  298. var setting = BinaryConvert.Deserialize<SysSettings>(memorystream);
  299. if (setting is null)
  300. {
  301. return false;
  302. }
  303. setting.OnDeserialized();
  304. }
  305. catch (Exception e)
  306. {
  307. Logger.Error(e.ToString());
  308. #if DEBUG
  309. throw;
  310. #else
  311. return false;
  312. #endif
  313. }
  314. return true;
  315. }
  316. /// <summary>
  317. /// The LoadWaveform.
  318. /// </summary>
  319. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  320. /// <param name="id">The id<see cref="ChannelId"/>.</param>
  321. /// <param name="dso">The dso<see cref="DsoPrsnt"/>.</param>
  322. /// <returns>The <see cref="Boolean"/>.</returns>
  323. public static Boolean LoadWaveform(String fullpath, ChannelId id, DsoPrsnt dso)
  324. {
  325. ReferencePrsnt? rprsnt = null;
  326. if (ReferencePrsnt.TryRead(id, fullpath, ref rprsnt))
  327. {
  328. //Add a new reference channel presenter
  329. dso.AddChannel(id, rprsnt!);
  330. DsoPrsnt.FocusId = rprsnt!.Id;
  331. return true;
  332. }
  333. return false;
  334. }
  335. /// <summary>
  336. /// The SaveImage.
  337. /// </summary>
  338. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  339. /// <param name="pf">The pf<see cref="PicFormat"/>.</param>
  340. /// <param name="region">The region<see cref="PicArea"/>.</param>
  341. /// <returns>The <see cref="Boolean"/>.</returns>
  342. public static Boolean SaveImage(String fullpath, PicFormat pf, PicArea region, PicColor color = PicColor.Standard)
  343. {
  344. var path = Path.GetDirectoryName(fullpath);
  345. var file = Path.GetFileName(fullpath);
  346. if (String.IsNullOrEmpty(file))
  347. {
  348. return false;
  349. }
  350. if (path?.Length == 0)
  351. {
  352. path = Directory.GetCurrentDirectory();
  353. }
  354. return SaveImage(path!, file, pf, region, false, color);
  355. }
  356. /// <summary>
  357. /// The SaveImage.
  358. /// </summary>
  359. /// <param name="path">The path<see cref="String"/>.</param>
  360. /// <param name="name">The name<see cref="String"/>.</param>
  361. /// <param name="pf">The pf<see cref="PicFormat"/>.</param>
  362. /// <param name="region">The region<see cref="PicArea"/>.</param>
  363. /// <param name="postfix">The postfix<see cref="Boolean"/>.</param>
  364. /// <returns>The <see cref="Boolean"/>.</returns>
  365. public static Boolean SaveImage(String path, String name, PicFormat pf, PicArea region, Boolean postfix, PicColor color = PicColor.Standard)
  366. {
  367. if (!Directory.Exists(path))
  368. {
  369. Directory.CreateDirectory(path);
  370. if (!Directory.Exists(path))
  371. {
  372. Logger.Error($"Path does not exist: '{path}'");
  373. return false;
  374. }
  375. }
  376. String fullfilename = path + "\\" + name + (postfix ? GetDateTimeString() : "") + GetPicFileExtName(pf);
  377. if (File.Exists(fullfilename))
  378. {
  379. if (!StrongTip.Default.Show(MsgTipId.Warning, MsgTipId.FileExisted, MessageType.Warning))
  380. {
  381. return false;
  382. }
  383. File.Delete(fullfilename);
  384. }
  385. var ms = GetImageStreamHandler?.Invoke(pf, region, color);
  386. if (ms is not null)
  387. {
  388. try
  389. {
  390. using FileStream fs = new(fullfilename, FileMode.Create, FileAccess.Write);
  391. ms.WriteTo(fs);
  392. ms.Close();
  393. return true;
  394. }
  395. catch (Exception e)
  396. {
  397. Logger.Error("Failed to save full screen: " + e.ToString());
  398. }
  399. }
  400. return false;
  401. }
  402. public static Boolean? SaveLabNoteBook(String LabNotePath, String LabNoteName, ChannelId channelId) => SaveLabNoteBookHandler?.Invoke(LabNotePath, LabNoteName, channelId);
  403. /// <summary>
  404. /// The SaveSetting.
  405. /// </summary>
  406. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  407. /// <returns>The <see cref="Boolean"/>.</returns>
  408. public static Boolean SaveSetting(String fullpath)
  409. {
  410. var path = Path.GetDirectoryName(fullpath);
  411. var file = Path.GetFileName(fullpath);
  412. if (String.IsNullOrEmpty(file))
  413. {
  414. return false;
  415. }
  416. if (path?.Length == 0)
  417. {
  418. path = Directory.GetCurrentDirectory();
  419. }
  420. return SaveSetting(path!, file, false, true);
  421. }
  422. /// <summary>
  423. /// The SaveSetting.
  424. /// </summary>
  425. /// <param name="path">The path<see cref="String"/>.</param>
  426. /// <param name="name">The name<see cref="String"/>.</param>
  427. /// <param name="postfix">The postfix<see cref="Boolean"/>.</param>
  428. /// <param name="dumb">The dumb<see cref="Boolean"/>.</param>
  429. /// <returns>The <see cref="Boolean"/>.</returns>
  430. public static Boolean SaveSetting(String path, String name, Boolean postfix, Boolean dumb = false)
  431. {
  432. try
  433. {
  434. if (!Directory.Exists(path))
  435. {
  436. Directory.CreateDirectory(path);
  437. if (!Directory.Exists(path))
  438. {
  439. Logger.Error($"Path does not exist: '{path}'");
  440. return false;
  441. }
  442. }
  443. String fullfilename = path + "\\" + name + (postfix ? GetDateTimeString() : "") + ".set";
  444. if (File.Exists(fullfilename))
  445. {
  446. if (!dumb && !StrongTip.Default.Show(MsgTipId.Warning, MsgTipId.FileExisted, MessageType.Warning))
  447. {
  448. return false;
  449. }
  450. File.Delete(fullfilename);
  451. }
  452. SysSettings settings = new();
  453. settings.OnSerializing();
  454. using var fs = new FileStream(fullfilename, FileMode.Create, FileAccess.Write);
  455. BinaryConvert.Serialize(settings, fs);
  456. }
  457. catch (Exception e)
  458. {
  459. Logger.Error(e.ToString());
  460. #if DEBUG
  461. throw;
  462. #else
  463. return false;
  464. #endif
  465. }
  466. return true;
  467. }
  468. /// <summary>
  469. /// The SaveToText.
  470. /// </summary>
  471. /// <param name="path">The path<see cref="String"/>.</param>
  472. /// <param name="name">The name<see cref="String"/>.</param>
  473. /// <param name="writer">The writer<see cref="Action{StreamWriter}"/>.</param>
  474. /// <returns>The <see cref="Boolean"/>.</returns>
  475. public static Boolean SaveToText(String path, String name, Action<StreamWriter> writer)
  476. {
  477. try
  478. {
  479. String fullfilename = path + "\\" + name + ".txt";
  480. using var fs = new FileStream(fullfilename, FileMode.Create, FileAccess.Write);
  481. using StreamWriter sw = new(fs, Encoding.UTF8);
  482. writer(sw);
  483. }
  484. catch (Exception e)
  485. {
  486. Logger.Error(e.ToString());
  487. #if DEBUG
  488. throw;
  489. #else
  490. return false;
  491. #endif
  492. }
  493. return true;
  494. }
  495. /// <summary>
  496. /// The SaveToText.
  497. /// </summary>
  498. /// <param name="path">The path<see cref="String"/>.</param>
  499. /// <param name="name">The name<see cref="String"/>.</param>
  500. /// <param name="buffer">The buffer<see cref="IEnumerable{Double}"/>.</param>
  501. /// <returns>The <see cref="Boolean"/>.</returns>
  502. public static Boolean SaveToText(String path, String name, IEnumerable<Double> buffer)
  503. {
  504. try
  505. {
  506. String fullfilename = path + "\\" + name + ".txt";
  507. using var fs = new FileStream(fullfilename, FileMode.OpenOrCreate, FileAccess.Write);
  508. using StreamWriter sw = new(fs, Encoding.UTF8);
  509. foreach (var d in buffer)
  510. {
  511. sw.WriteLine(d);
  512. }
  513. }
  514. catch (Exception e)
  515. {
  516. Logger.Error(e.ToString());
  517. #if DEBUG
  518. throw;
  519. #else
  520. return false;
  521. #endif
  522. }
  523. return true;
  524. }
  525. /// <summary>
  526. /// The SaveWaveByBin.
  527. /// </summary>
  528. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  529. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  530. /// <returns>The <see cref="Boolean"/>.</returns>
  531. public static Boolean SaveWaveByBin(Stream stm, WfmPack pkg)
  532. {
  533. try
  534. {
  535. BinaryConvert.Serialize(pkg, stm);
  536. }
  537. catch (Exception e)
  538. {
  539. Logger.Error(e.ToString());
  540. #if DEBUG
  541. throw;
  542. #else
  543. return false;
  544. #endif
  545. }
  546. return true;
  547. }
  548. /// <summary>
  549. /// The SaveWaveByCSV.
  550. /// </summary>
  551. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  552. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  553. /// <returns>The <see cref="Boolean"/>.</returns>
  554. public static Boolean SaveWaveByCSV(Stream stm, WfmPack pkg) => SaveWaveByText(stm, pkg, (x, y) => x.ToString("E") + "," + String.Join(",", y.Select(o => o.ToString("G"))));
  555. /// <summary>
  556. /// The SaveWaveByExcel.
  557. /// </summary>
  558. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  559. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  560. /// <returns>The <see cref="Boolean"/>.</returns>
  561. public static Boolean SaveWaveByExcel(Stream stm, WfmPack pkg)
  562. {
  563. try
  564. {
  565. HSSFWorkbook workbook = new();
  566. ISheet sheet = workbook.CreateSheet("Sheet1");
  567. Int32 length = pkg.Buffer.GetLength(1);
  568. Int32 wfmcnt = pkg.Buffer.GetLength(0);
  569. Double sp = pkg.Properties.TmbScale.Value * Constants.VIS_XDIVS_NUM / length;
  570. Double pos0 = pkg.Properties.TmbPosition.Index;
  571. Double time;
  572. IRow head = sheet.CreateRow(0);
  573. head.CreateCell(0).SetCellValue($"Time({pkg.Properties.TmbUnit.Prefix.ToPfxString()}{pkg.Properties.TmbUnit.Name})");
  574. for (Int32 i = 0; i < wfmcnt; i++)
  575. {
  576. head.CreateCell(i + 1).SetCellValue($"Ampl({pkg.Properties.ChnlUnit.Prefix.ToPfxString()}{pkg.Properties.ChnlUnit.Name})");
  577. }
  578. for (Int32 j = 0; j < length; j++)
  579. {
  580. var row = (HSSFRow)sheet.CreateRow(j + 1);
  581. time = (j - pos0) * sp + pkg.Properties.TrigErrorTime;
  582. row.CreateCell(0).SetCellValue(time);
  583. for (Int32 i = 0; i < wfmcnt; i++)
  584. {
  585. row.CreateCell(i + 1).SetCellValue(pkg.Buffer[i, j]);
  586. //if (row.GetCell(i) == null)
  587. //{
  588. // row.CreateCell(i).SetCellValue(data);
  589. //}
  590. //else
  591. //{
  592. // row.GetCell(i).SetCellValue(data);
  593. //}
  594. }
  595. }
  596. workbook.Write(stm);
  597. }
  598. catch (Exception e)
  599. {
  600. Logger.Error(e.ToString());
  601. #if DEBUG
  602. throw;
  603. #else
  604. return false;
  605. #endif
  606. }
  607. return true;
  608. }
  609. /// <summary>
  610. /// The SaveWaveByMatlab.
  611. /// </summary>
  612. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  613. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  614. /// <returns>The <see cref="Boolean"/>.</returns>
  615. public static Boolean SaveWaveByMatlab(Stream stm, WfmPack pkg) => SaveWaveByText(stm, pkg, (x, y) => x.ToString("E") + " " + String.Join(" ", y.Select(o => o.ToString("G"))));
  616. /// <summary>
  617. /// The SaveWaveByText.
  618. /// </summary>
  619. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  620. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  621. /// <param name="cbfunc">The cbfunc<see cref="Func{Double, Double[], String}"/>.</param>
  622. /// <returns>The <see cref="Boolean"/>.</returns>
  623. public static Boolean SaveWaveByText(Stream stm, WfmPack pkg, Func<Double, Double[], String> cbfunc, TxtFormat wfmtxtformat = TxtFormat.UTF8)
  624. {
  625. try
  626. {
  627. using StreamWriter sw = new(stm, GetEncoding(wfmtxtformat));
  628. Int32 length = pkg.Buffer.GetLength(1);
  629. Int32 wfmcnt = pkg.Buffer.GetLength(0);
  630. Double sp = pkg.Properties.TmbScale.Value * Constants.VIS_XDIVS_NUM / length;
  631. Double pos0 = pkg.Properties.TmbPosition.Index;
  632. Double time;
  633. Double[] ampls = new Double[wfmcnt];
  634. var header = $"Ampl({pkg.Properties.ChnlUnit.Prefix.ToPfxString()}{pkg.Properties.ChnlUnit.Name})";
  635. for (Int32 i = 1; i < wfmcnt; i++)
  636. {
  637. header = header + ", " + header;
  638. }
  639. header = $"Time({pkg.Properties.TmbUnit.Prefix.ToPfxString()}{pkg.Properties.TmbUnit.Name}), " + header;
  640. sw.WriteLine(header);
  641. for (Int32 j = 0; j < length; j++)
  642. {
  643. time = (j - pos0) * sp + pkg.Properties.TrigErrorTime;
  644. for (Int32 i = 0; i < wfmcnt; i++)
  645. {
  646. ampls[i] = pkg.Buffer[i, j];
  647. }
  648. sw.WriteLine(cbfunc(time, ampls));
  649. }
  650. }
  651. catch (Exception e)
  652. {
  653. Logger.Error(e.ToString());
  654. #if DEBUG
  655. throw;
  656. #else
  657. return false;
  658. #endif
  659. }
  660. return true;
  661. }
  662. /// <summary>
  663. /// The SaveWaveByTSV.
  664. /// </summary>
  665. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  666. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  667. /// <returns>The <see cref="Boolean"/>.</returns>
  668. 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"))));
  669. /// <summary>
  670. /// The SaveWaveByTxt.
  671. /// </summary>
  672. /// <param name="stm">The stm<see cref="Stream"/>.</param>
  673. /// <param name="pkg">The pkg<see cref="WfmPack"/>.</param>
  674. /// <returns>The <see cref="Boolean"/>.</returns>
  675. 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);
  676. /// <summary>
  677. /// The SaveWaveform.
  678. /// </summary>
  679. /// <param name="fullpath">The fullpath<see cref="String"/>.</param>
  680. /// <param name="wf">The wf<see cref="WfmFormat"/>.</param>
  681. /// <param name="id">The id<see cref="ChannelId"/>.</param>
  682. /// <returns>The <see cref="Boolean"/>.</returns>
  683. public static Boolean SaveWaveform(String fullpath, WfmFormat wf, ChannelId id, TxtFormat wfmtxtfotmat = TxtFormat.UTF8)
  684. {
  685. var path = Path.GetDirectoryName(fullpath);
  686. var file = Path.GetFileName(fullpath);
  687. if (String.IsNullOrEmpty(file))
  688. {
  689. return false;
  690. }
  691. if (path?.Length == 0)
  692. {
  693. path = Directory.GetCurrentDirectory();
  694. }
  695. return SaveWaveform(path!, file, wf, id, false, wfmtxtfotmat);
  696. }
  697. /// <summary>
  698. /// The SaveWaveform.
  699. /// </summary>
  700. /// <param name="path">The path<see cref="String"/>.</param>
  701. /// <param name="name">The name<see cref="String"/>.</param>
  702. /// <param name="wf">The wf<see cref="WfmFormat"/>.</param>
  703. /// <param name="id">The id<see cref="ChannelId"/>.</param>
  704. /// <param name="postfix">The postfix<see cref="Boolean"/>.</param>
  705. /// <returns>The <see cref="Boolean"/>.</returns>
  706. public static Boolean SaveWaveform(String path, String name, WfmFormat wf, ChannelId id, Boolean postfix, TxtFormat wfmtxtfotmat = TxtFormat.UTF8)
  707. {
  708. if (DsoModel.Default.TryGetChannel(id, out var cm) && cm.Pack is not null)
  709. {
  710. //if (cm.Pack.Properties.Version != "U2.0")
  711. // return false;
  712. if (!Directory.Exists(path))
  713. {
  714. Directory.CreateDirectory(path);
  715. if (!Directory.Exists(path))
  716. {
  717. Logger.Error($"Path does not exist: '{path}'");
  718. return false;
  719. }
  720. }
  721. String fullfilename = path + "\\" + name + (postfix ? GetDateTimeString() : "") + "." + wf.GetAlias();
  722. if (File.Exists(fullfilename))
  723. {
  724. if (!StrongTip.Default.Show(MsgTipId.Warning, MsgTipId.FileExisted, MessageType.Warning))
  725. {
  726. return false;
  727. }
  728. File.Delete(fullfilename);
  729. }
  730. using var fs = new FileStream(fullfilename, FileMode.OpenOrCreate, FileAccess.Write);
  731. switch (wf)
  732. {
  733. case WfmFormat.Binary:
  734. return FilePrsnt.SaveWaveByBin(fs, cm.Pack);
  735. case WfmFormat.Text:
  736. return FilePrsnt.SaveWaveByTxt(fs, cm.Pack, wfmtxtfotmat);
  737. case WfmFormat.Matlab:
  738. return FilePrsnt.SaveWaveByMatlab(fs, cm.Pack);
  739. case WfmFormat.Excel:
  740. return FilePrsnt.SaveWaveByExcel(fs, cm.Pack);
  741. case WfmFormat.CSV:
  742. return FilePrsnt.SaveWaveByCSV(fs, cm.Pack);
  743. case WfmFormat.TSV:
  744. return FilePrsnt.SaveWaveByTSV(fs, cm.Pack);
  745. case WfmFormat.WFM:
  746. case WfmFormat.HDF5:
  747. default:
  748. WeakTip.Default.Write("WfmSave", MsgTipId.UnSupportedFormat);
  749. return false;
  750. }
  751. // return wf switch
  752. // {
  753. // WfmFormat.Binary =>
  754. // FilePrsnt.SaveWaveByBin(fs, cm.Pack),
  755. // WfmFormat.Text =>
  756. // FilePrsnt.SaveWaveByTxt(fs, cm.Pack, wfmtxtfotmat),
  757. // WfmFormat.Excel =>
  758. // FilePrsnt.SaveWaveByExcel(fs, cm.Pack),
  759. // WfmFormat.Matlab =>
  760. // FilePrsnt.SaveWaveByMatlab(fs, cm.Pack),
  761. // WfmFormat.CSV =>
  762. // FilePrsnt.SaveWaveByCSV(fs, cm.Pack),
  763. // WfmFormat.TSV =>
  764. // FilePrsnt.SaveWaveByTSV(fs, cm.Pack),
  765. // _ =>
  766. //#if DEBUG
  767. // throw new NotImplementedException(),
  768. //#else
  769. // //Logger.Error($"{nameof(WfmFormat)} '{nameof(wf)}' = {wf}"),
  770. // throw new NotImplementedException()
  771. //#endif
  772. // };
  773. }
  774. return false;
  775. }
  776. /// <summary>
  777. /// The MakeDefaultFileName.
  778. /// </summary>
  779. /// <param name="path">The path<see cref="String"/>.</param>
  780. /// <param name="ext">The ext<see cref="String"/>.</param>
  781. /// <returns>The <see cref="String"/>.</returns>
  782. public String MakeDefaultFileName(String path, String ext)
  783. {
  784. var result = new DirectoryInfo(path)
  785. .GetFiles($"*{ext}", SearchOption.TopDirectoryOnly)
  786. .Where(x => Regex.IsMatch(x.Name, $"^{DefaultPrefixName}[0-9]{"{3}"}{ext}$", RegexOptions.IgnoreCase));
  787. return DefaultPrefixName + String.Format("{0:D3}", result.Count());
  788. }
  789. }
  790. }