Repository navigation
Expand file tree
/
Copy pathgui_export.cpp
More file actions
706 lines (650 loc) · 29.3 KB
/
Copy pathgui_export.cpp
File metadata and controls
706 lines (650 loc) · 29.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
// GUI file export: samples, spectra, PNG, and save dialogs.
#include "gui_export.hpp"
#include "gui_frf.hpp"
#include "gui_analysis_source.hpp"
#include "gui_export_metadata.hpp"
#include "gui_documents.hpp"
#include "gui_status.hpp"
#include "gui_state.hpp"
#include "gui_theme.hpp"
#include "gui_text.hpp"
#include "gui_layout.hpp"
#include "gui_ids.hpp"
#include "gui_processing.hpp"
#include "gui_render.hpp"
#include "gui_spectrum.hpp"
#include "gui_dialogs.hpp"
#include "gui_source_export.hpp"
#include "gui_recording_processing.hpp"
#include "disk_spectrum.hpp"
#include "gui_resources.hpp"
namespace gui {
// ---- PNG export (GDI+) ---------------------------------------------------
namespace {
constexpr wchar_t kPngCredit[] = L"Generated by AMSignal";
void draw_png_credit(HDC dc, int width, int height) {
const HFONT font = g.axis_font ? g.axis_font :
(g.ui_font ? g.ui_font : reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT)));
HGDIOBJ previous_font = SelectObject(dc, font);
SetBkMode(dc, TRANSPARENT);
SetTextColor(dc, g_theme->text_secondary);
SetTextAlign(dc, TA_RIGHT | TA_BASELINE);
TextOutW(dc, width - 20, height - 6, kPngCredit, lstrlenW(kPngCredit));
SelectObject(dc, previous_font);
}
} // namespace
int png_encoder_clsid(CLSID* clsid) {
UINT num = 0, size = 0;
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0) return -1;
auto* info = reinterpret_cast<Gdiplus::ImageCodecInfo*>(malloc(size));
if (!info) return -1;
Gdiplus::GetImageEncoders(num, size, info);
int found = -1;
for (UINT i = 0; i < num; ++i)
if (wcscmp(info[i].MimeType, L"image/png") == 0) { *clsid = info[i].Clsid; found = static_cast<int>(i); break; }
free(info);
return found;
}
bool save_png(const std::wstring& path) {
if (g.mode == AnalysisMode::FRF && (!g.frf.result.ok || g.frf.pending)) return false;
RECT pr = plot_rect();
int W = (pr.right - pr.left) + 90;
const int top_margin = 14 + kAxisNameTop;
const int bottom_margin = kAxisBottom + 18; // leave room for the PNG credit
int H = (pr.bottom - pr.top) + top_margin + bottom_margin;
if (W < 400) W = 1000;
if (H < 240) H = 600;
HDC screen = GetDC(g.main);
HDC mem = CreateCompatibleDC(screen);
HBITMAP bmp = CreateCompatibleBitmap(screen, W, H);
HGDIOBJ obmp = SelectObject(mem, bmp);
RECT all = {0, 0, W, H};
HBRUSH bg = CreateSolidBrush(g_theme->bg_plot);
FillRect(mem, &all, bg);
DeleteObject(bg);
SelectObject(mem, reinterpret_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT)));
SetBkMode(mem, TRANSPARENT);
RECT inner = {70, top_margin, W - 20, H - bottom_margin};
draw_chart(mem, inner);
draw_png_credit(mem, W, H);
SelectObject(mem, obmp);
bool ok = false;
{
Gdiplus::Bitmap gb(bmp, nullptr);
CLSID clsid;
if (png_encoder_clsid(&clsid) >= 0)
ok = (gb.Save(path.c_str(), &clsid, nullptr) == Gdiplus::Ok);
}
DeleteObject(bmp);
DeleteDC(mem);
ReleaseDC(g.main, screen);
// draw_chart updated the mapping cache for the off-screen rect; restore it.
InvalidateRect(g.main, nullptr, FALSE);
return ok;
}
double export_channel_sample(std::size_t channel_index, std::size_t row_index, bool apply_processing) {
if (channel_index >= g.ds.channel_count()) return std::numeric_limits<double>::quiet_NaN();
const auto& column = g.ds.channels[channel_index];
if (row_index >= column.size()) return std::numeric_limits<double>::quiet_NaN();
if (!apply_processing) return column[row_index];
return rendered_channel_sample(channel_index, row_index);
}
std::vector<std::size_t> export_channel_indices(bool include_hidden_channels) {
std::vector<std::size_t> cols;
const std::size_t count = g.ds.channel_count();
if (include_hidden_channels) {
cols.reserve(count);
for (std::size_t c = 0; c < count; ++c) cols.push_back(c);
return cols;
}
for (std::size_t c = 0; c < count; ++c) {
if (c < g.visible.size() && g.visible[c]) cols.push_back(c);
}
return cols;
}
std::vector<int> export_spectrum_channel_indices(const lvm::Spectrum& spec) {
std::vector<int> indices;
indices.reserve(spec.names.size());
for (std::size_t c : spec.source_channels) {
indices.push_back(static_cast<int>(c));
}
return indices;
}
bool build_export_spectrum(const ExportOptions& opts, lvm::Spectrum& out_spec,
std::vector<int>& out_channel_indices, bool& out_all_channels) {
if (!has_data() || g.mode != AnalysisMode::FFT) return false;
if(g.overview.render_enabled) {
double a=0,b=0;bool selected=false;if(!current_fft_source_window(a,b,selected))return false;
lvm::RecordingReadRequest read;read.range=lvm::AnalysisRange::selected(a,b);read.channels=export_channel_indices(opts.include_hidden_channels);
std::vector<std::string> names;for(auto c:read.channels)names.push_back(g.ds.names[c]);
out_spec=lvm::compute_recording_spectrum(processed_recording_source(opts.apply_processing_to_data),read,names,current_analysis_resources());
for(auto& c:out_spec.source_channels)c=read.channels.at(c);
out_channel_indices=export_spectrum_channel_indices(out_spec);out_all_channels=true;return out_spec.ok;
}
out_all_channels = false;
const bool have_current_spectrum = ensure_current_spectrum();
if (opts.apply_processing_to_data && !opts.include_hidden_channels && have_current_spectrum) {
out_spec = g.spec;
out_channel_indices = g.spec_channel_indices;
return out_spec.ok;
}
double source_start = 0.0;
double source_end = 0.0;
bool from_selection = false;
if (!last_fft_source_window(source_start, source_end, from_selection) &&
!current_fft_source_window(source_start, source_end, from_selection)) {
return false;
}
std::vector<std::size_t> all_channels;
all_channels.reserve(g.ds.channel_count());
for (std::size_t i = 0; i < g.ds.channel_count(); ++i) {
if (opts.include_hidden_channels || (i < g.visible.size() && g.visible[i])) all_channels.push_back(i);
}
lvm::Dataset view;
if (all_channels.empty()) return false;
build_time_window_dataset(g.ds, source_start, source_end, view, &all_channels, opts.apply_processing_to_data);
out_spec = lvm::compute_spectrum(view, 0);
for (auto& c : out_spec.source_channels) c = all_channels[c];
if (!out_spec.ok) return false;
out_channel_indices = export_spectrum_channel_indices(out_spec);
out_all_channels = true;
return true;
}
bool export_range_bounds_for_mode(ExportRangeMode range, double& start, double& end, bool& actual_selected) {
if (g.mode == AnalysisMode::FRF) return false;
if (!has_data()) return false;
actual_selected = false;
if ((g.mode == AnalysisMode::FFT)) {
if (!ensure_current_spectrum() && !g.overview.render_enabled) return false;
actual_selected = (range == ExportRangeMode::Selected);
if (range == ExportRangeMode::Whole) {
start = 0;
end = g.spec.nyquist;
} else {
start = g.freq_start;
end = g.freq_end;
}
if (!std::isfinite(start) || !std::isfinite(end)) return false;
if (end < start) std::swap(start, end);
return end > start;
}
switch (range) {
case ExportRangeMode::Selected:
if (has_fft_window()) {
start = g.fft_window_start;
end = g.fft_window_end;
clamp_time_window(start, end);
actual_selected = true;
return end > start;
}
start = g.win_start;
end = g.win_end;
clamp_time_window(start, end);
return end > start;
case ExportRangeMode::Visible:
start = g.win_start;
end = g.win_end;
clamp_time_window(start, end);
return end > start;
case ExportRangeMode::Whole:
start = g.overview.render_enabled ? g.recording_source->bounds().start : g.ds.time.empty() ? 0.0 : g.ds.time.front();
end = g.overview.render_enabled ? g.recording_source->bounds().end : g.ds.time.empty() ? 0.0 : g.ds.time.back();
return end > start;
}
return false;
}
bool write_tabular_export(std::ofstream& out, const ExportOptions& opts) {
if (g.mode == AnalysisMode::FRF) return opts.format == ExportFileFormat::Csv && write_frf_csv(out);
if(g.overview.render_enabled)return prepare_source_export(opts)(out,nullptr);
if (!has_data()) return false;
const bool csv = opts.format == ExportFileFormat::Csv;
const char sep = csv ? ',' : '\t';
const char* line_end = csv ? "\n" : "\r\n";
double export_start = 0.0;
double export_end = 0.0;
bool actual_selected_range = false;
if ((g.mode == AnalysisMode::FFT)) {
lvm::Spectrum export_spec;
std::vector<int> spec_channel_indices;
bool export_all_channels = false;
if (!build_export_spectrum(opts, export_spec, spec_channel_indices, export_all_channels)) return false;
if (opts.selected_range == ExportRangeMode::Whole) {
export_start = export_spec.freqs.empty() ? 0.0 : export_spec.freqs.front();
export_end = export_spec.freqs.empty() ? 0.0 : export_spec.freqs.back();
} else {
export_start = g.freq_start;
export_end = g.freq_end;
actual_selected_range = (opts.selected_range == ExportRangeMode::Selected);
}
if (!std::isfinite(export_start) || !std::isfinite(export_end)) return false;
if (export_end < export_start) std::swap(export_start, export_end);
if (export_end <= export_start) return false;
std::vector<std::size_t> cols;
std::vector<std::size_t> source_cols;
for (std::size_t j = 0; j < export_spec.amp.size(); ++j) {
const int ci = (j < spec_channel_indices.size()) ? spec_channel_indices[j] : -1;
const bool visible_channel =
ci >= 0 && static_cast<std::size_t>(ci) < g.visible.size() &&
g.visible[static_cast<std::size_t>(ci)];
if (export_all_channels || visible_channel) {
const std::size_t label_index = (ci >= 0) ? static_cast<std::size_t>(ci) : j;
source_cols.push_back(label_index);
cols.push_back(j);
}
}
if (cols.empty()) return false;
write_export_metadata(out, opts, csv, export_start, export_end, actual_selected_range, source_cols);
if (opts.include_metadata) {
write_export_comment(out, L"[fft_sampling]", line_end);
write_export_key_value(out, L"source_start", to_w(numfmt(export_spec.source_start)), line_end);
write_export_key_value(out, L"source_end", to_w(numfmt(export_spec.source_end)), line_end);
write_export_key_value(out, L"sample_dt", to_w(numfmt(export_spec.sample_dt)), line_end);
write_export_key_value(out, L"sample_count", std::to_wstring(export_spec.n), line_end);
write_export_key_value(out, L"gaps_ignored", export_spec.gaps_ignored ? L"1" : L"0", line_end);
write_export_key_value(out, L"resampled", export_spec.resampled ? L"1" : L"0", line_end);
write_export_comment(out, L"", line_end);
}
out << "Frequency";
for (std::size_t c : source_cols) {
const std::string name = to_utf8(export_channel_label_text(c, opts.include_channel_names));
out << sep << (csv ? lvm::csv_field(name) : lvm::tsv_header_field(name));
}
out << line_end;
std::size_t begin = 0;
std::size_t end = export_spec.freqs.size();
if (opts.selected_range != ExportRangeMode::Whole) {
double start = export_start;
double finish = export_end;
if (start > finish) std::swap(start, finish);
const auto bounds = export_range_bounds(export_spec.freqs, start, finish);
begin = bounds.first;
end = bounds.second;
}
for (std::size_t k = begin; k < end; ++k) {
out << numfmt(export_spec.freqs[k]);
for (std::size_t j : cols) {
if (j < export_spec.amp.size() && k < export_spec.amp[j].size()) {
out << sep << numfmt(export_spec.amp[j][k]);
} else {
out << sep;
}
}
out << line_end;
}
return true;
}
if (opts.selected_range != ExportRangeMode::Whole) {
// Selected range falls back to the visible view when no explicit window exists.
if (!export_range_bounds_for_mode(opts.selected_range, export_start, export_end, actual_selected_range)) return false;
} else {
export_start = g.ds.time.empty() ? 0.0 : g.ds.time.front();
export_end = g.ds.time.empty() ? 0.0 : g.ds.time.back();
}
const std::vector<std::size_t> cols = export_channel_indices(opts.include_hidden_channels);
if (cols.empty()) return false;
write_export_metadata(out, opts, csv, export_start, export_end, actual_selected_range, cols);
out << "Time";
for (std::size_t c : cols) {
const std::string name = to_utf8(export_channel_label_text(c, opts.include_channel_names));
out << sep << (csv ? lvm::csv_field(name) : lvm::tsv_header_field(name));
}
out << line_end;
std::size_t begin = 0;
std::size_t end = g.ds.rows();
if (opts.selected_range != ExportRangeMode::Whole && export_end > export_start) {
const auto bounds = export_range_bounds(g.ds.time, export_start, export_end);
begin = bounds.first;
end = bounds.second;
}
if (begin >= end) return false;
for (std::size_t r = begin; r < end; ++r) {
out << numfmt(g.ds.time[r]);
for (std::size_t c : cols) {
out << sep << numfmt(export_channel_sample(c, r, opts.apply_processing_to_data));
}
out << line_end;
}
return true;
}
bool save_tabular_export(const std::wstring& path, const ExportOptions& opts) {
if(g.overview.render_enabled && g.mode!=AnalysisMode::FRF)return save_source_export(path,opts);
return lvm::atomic_write_file(std::filesystem::path(path), [&](std::ofstream& out) { return write_tabular_export(out, opts); }, &g.last_error);
}
bool write_lvm_export(std::ofstream& out, const ExportOptions& opts) {
if(g.overview.render_enabled)return prepare_source_export(opts)(out,nullptr);
const bool project = opts.save_mode == ExportSaveMode::Project;
if (!has_data() || (!project && g.mode != AnalysisMode::Time)) return false;
double export_start = 0.0;
double export_end = 0.0;
bool actual_selected_range = false;
if (project) {
export_start = g.ds.time.empty() ? 0.0 : g.ds.time.front();
export_end = g.ds.time.empty() ? 0.0 : g.ds.time.back();
} else if (!export_range_bounds_for_mode(opts.selected_range, export_start, export_end, actual_selected_range)) {
return false;
}
const auto bounds = export_range_bounds(g.ds.time, export_start, export_end);
std::size_t begin = bounds.first;
std::size_t end = bounds.second;
if (begin >= end) return false;
const std::vector<std::size_t> cols = export_channel_indices(opts.include_hidden_channels);
if (cols.empty()) return false;
const char* line_end = "\r\n";
SYSTEMTIME stamp{};
GetLocalTime(&stamp);
const std::wstring date_text = lvm_current_date_text(stamp);
const std::wstring time_text = lvm_current_time_text(stamp);
const std::wstring y_unit = g.axis_y_label.empty() ? L"Value" : g.axis_y_label;
const double delta_x = lvm_export_nominal_delta_x();
const std::wstring delta_x_text = format_edit_number(delta_x);
auto repeat_header = [&](const char* key, const std::wstring& value) {
out << key;
const std::string encoded = to_utf8(value);
for (std::size_t i = 0; i < cols.size(); ++i) {
out << '\t' << encoded;
}
out << line_end;
};
auto clean_label = [](std::string text) {
for (char& ch : text) {
if (ch == '\t' || ch == '\r' || ch == '\n') ch = ' ';
}
return text;
};
write_export_metadata(out, opts, false, export_start, export_end, actual_selected_range, cols);
out << "LabVIEW Measurement" << line_end;
out << "Writer_Version\t0.92" << line_end;
out << "Reader_Version\t1" << line_end;
out << "Separator\tTab" << line_end;
out << "Multi_Headings\tYes" << line_end;
out << "X_Columns\tMulti" << line_end;
out << "Time_Pref\tAbsolute" << line_end;
out << "Operator\tAMSignal" << line_end;
out << "Date\t" << to_utf8(date_text) << line_end;
out << "Time\t" << to_utf8(time_text) << line_end;
out << "***End_of_Header***" << line_end << line_end;
out << "Channels\t" << cols.size() << line_end;
repeat_header("Samples", std::to_wstring(end - begin));
repeat_header("Date", date_text);
repeat_header("Time", time_text);
repeat_header("Y_Unit_Label", y_unit);
repeat_header("X_Dimension", L"Time");
repeat_header("X0", L"0");
repeat_header("Delta_X", delta_x_text);
out << "***End_of_Header***" << line_end;
std::string labels = "X_Value";
for (std::size_t i = 0; i < cols.size(); ++i) {
labels.push_back('\t');
labels += clean_label(to_utf8(export_channel_label_text(cols[i], opts.include_channel_names)));
if (i + 1 < cols.size()) {
labels.push_back('\t');
labels += "X_Value";
}
}
labels += "\tComment";
out << labels << line_end;
for (std::size_t r = begin; r < end; ++r) {
std::string row;
for (std::size_t c : cols) {
if (!row.empty()) row.push_back('\t');
row += numfmt(g.ds.time[r]);
row.push_back('\t');
row += numfmt(export_channel_sample(c, r, opts.apply_processing_to_data));
}
row.push_back('\t');
out << row << line_end;
}
return true;
}
bool save_lvm_export(const std::wstring& path, const ExportOptions& opts) {
if(g.overview.render_enabled)return save_source_export(path,opts);
return lvm::atomic_write_file(std::filesystem::path(path), [&](std::ofstream& out) { return write_lvm_export(out, opts); }, &g.last_error);
}
bool save_dialog(std::wstring& out_path, const wchar_t* filter, const wchar_t* defext,
const std::wstring& defname) {
wchar_t file[2048] = L"";
lstrcpynW(file, defname.c_str(), 2048);
OPENFILENAMEW ofn = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = g.main;
ofn.lpstrFilter = filter;
ofn.lpstrFile = file;
ofn.nMaxFile = 2048;
ofn.lpstrDefExt = defext;
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
if (!GetSaveFileNameW(&ofn)) return false;
out_path = file;
return true;
}
std::wstring file_stem() {
std::wstring stem = g.file_name;
std::size_t dot = stem.find_last_of(L'.');
if (dot != std::wstring::npos) stem = stem.substr(0, dot);
return stem;
}
void save_png_dialog() {
if (g.mode == AnalysisMode::FRF && (!g.frf.result.ok || g.frf.pending)) {
show_styled_info_prompt(g.main, L"FRF", frf_status_text().c_str(), false);
return;
}
if (!has_data()) { show_styled_info_prompt(g.main, g_str->msg_nodata, g_str->msg_openfirst, false); return; }
std::wstring def = file_stem() + (g.mode == AnalysisMode::FRF ? L"_frf.png" :
(g.mode == AnalysisMode::FFT ? L"_spectrum.png" : L"_plot.png"));
std::wstring path;
if (!save_dialog(path, g_str->filter_png, L"png", def)) return;
if (save_png(path)) {
const wchar_t* b = wcsrchr(path.c_str(), L'\\');
status_msg(std::wstring(g_str->msg_saved_png) + (b ? b + 1 : path.c_str()));
} else {
MessageBoxW(g.main, g_str->msg_savepng_err, g_str->msg_error_title, MB_ICONERROR);
}
}
const wchar_t* export_file_extension(ExportFileFormat format) {
switch (format) {
case ExportFileFormat::Txt: return L"txt";
case ExportFileFormat::Csv: return L"csv";
case ExportFileFormat::Lvm: return L"lvm";
}
return L"txt";
}
const wchar_t* export_file_filter(ExportFileFormat format) {
const bool en = (g_str == &kEn);
switch (format) {
case ExportFileFormat::Txt:
return en ? L"TXT file\0*.txt\0All files\0*.*\0"
: L"TXT файлы\0*.txt\0Все файлы\0*.*\0";
case ExportFileFormat::Csv:
return en ? L"CSV file\0*.csv\0All files\0*.*\0"
: L"CSV файлы\0*.csv\0Все файлы\0*.*\0";
case ExportFileFormat::Lvm:
return en ? L"LVM file\0*.lvm\0All files\0*.*\0"
: L"LVM файлы\0*.lvm\0Все файлы\0*.*\0";
}
return en ? L"TXT file\0*.txt\0All files\0*.*\0"
: L"TXT файлы\0*.txt\0Все файлы\0*.*\0";
}
const wchar_t* export_file_name(ExportFileFormat format) {
switch (format) {
case ExportFileFormat::Txt: return L"TXT";
case ExportFileFormat::Csv: return L"CSV";
case ExportFileFormat::Lvm: return L"LVM";
}
return L"TXT";
}
bool save_export_file(const std::wstring& path, const ExportOptions& opts) {
if (opts.save_mode == ExportSaveMode::Project) return save_project_file(path);
if (g.mode == AnalysisMode::FRF) return opts.format == ExportFileFormat::Csv && save_frf_csv(path);
switch (opts.format) {
case ExportFileFormat::Txt:
case ExportFileFormat::Csv:
return save_tabular_export(path, opts);
case ExportFileFormat::Lvm:
return save_lvm_export(path, opts);
}
return false;
}
bool save_project_file(const std::wstring& path) {
ExportOptions project;
project.save_mode = ExportSaveMode::Project;
project.format = ExportFileFormat::Lvm;
project.selected_range = ExportRangeMode::Whole;
project.apply_processing_to_data = false;
project.include_channel_names = true;
project.include_hidden_channels = true;
project.include_points = true;
project.include_markers = true;
project.include_guides = true;
project.include_formulas = true;
project.include_filter_settings = true;
project.include_graph_settings = true;
project.include_metadata = true;
return save_lvm_export(path, project);
}
bool save_current_project() {
if (!has_data()) {
show_styled_info_prompt(g.main, g_str->msg_nodata, g_str->msg_openfirst, false);
return false;
}
if (g.project_path.empty()) {
const wchar_t* filter = (g_str == &kEn)
? L"AMSignal project\0*.AMSig\0All files\0*.*\0"
: L"Проект AMSignal\0*.AMSig\0Все файлы\0*.*\0";
std::wstring path;
if (!save_dialog(path, filter, L"AMSig", file_stem() + L".AMSig")) return false;
if(g.overview.render_enabled){ExportOptions opts;opts.save_mode=ExportSaveMode::Project;return start_source_export(path,opts);}
if (save_project_file(path)) {
g.project_path = path;
mark_active_document_saved();
status_msg((g_str == &kEn ? L"Project saved: " : L"Проект сохранён: ") + path);
return true;
} else {
MessageBoxW(g.main, g_str == &kEn ? L"Failed to save project." : L"Не удалось сохранить проект.",
g_str->msg_error_title, MB_ICONERROR);
}
return false;
}
if(g.overview.render_enabled){ExportOptions opts;opts.save_mode=ExportSaveMode::Project;return start_source_export(g.project_path,opts);}
if (save_project_file(g.project_path)) {
mark_active_document_saved();
status_msg((g_str == &kEn ? L"Project saved: " : L"Проект сохранён: ") + g.project_path);
return true;
} else {
MessageBoxW(g.main, g_str == &kEn ? L"Failed to save project." : L"Не удалось сохранить проект.",
g_str->msg_error_title, MB_ICONERROR);
}
return false;
}
void save_as_dialog() {
if (!has_data()) { show_styled_info_prompt(g.main, g_str->msg_nodata, g_str->msg_openfirst, false); return; }
ExportOptions opts;
if (!prompt_export_options(opts)) return;
if (opts.save_mode != ExportSaveMode::Project && g.mode == AnalysisMode::FRF && (!g.frf.result.ok || g.frf.pending)) {
show_styled_info_prompt(g.main, L"FRF", frf_status_text().c_str(), false);
return;
}
if (opts.save_mode != ExportSaveMode::Project && opts.format == ExportFileFormat::Lvm && (g.mode == AnalysisMode::FFT)) {
MessageBoxW(g.main,
g_str == &kEn ? L"LVM export is available only in Time mode."
: L"Экспорт LVM доступен только в режиме времени.",
g_str->msg_error_title, MB_ICONINFORMATION);
return;
}
const bool project = opts.save_mode == ExportSaveMode::Project;
const wchar_t* ext = project ? L"AMSig" : export_file_extension(opts.format);
std::wstring def = project ? file_stem() + L".AMSig" :
export_default_name(file_stem(), opts.selected_range, L".", (g.mode == AnalysisMode::FFT)) + ext;
std::wstring path;
const wchar_t* project_filter = (g_str == &kEn)
? L"AMSignal project\0*.AMSig\0All files\0*.*\0"
: L"Проект AMSignal\0*.AMSig\0Все файлы\0*.*\0";
if (!save_dialog(path, project ? project_filter : export_file_filter(opts.format), ext, def)) return;
if(g.overview.render_enabled && (project || g.mode!=AnalysisMode::FRF)) {
if(!start_source_export(path,opts))MessageBoxW(g.main,to_w(g.last_error).c_str(),g_str->msg_error_title,MB_ICONERROR);
return;
}
if (save_export_file(path, opts)) {
if (project) {
g.project_path = path;
mark_active_document_saved();
}
const wchar_t* b = wcsrchr(path.c_str(), L'\\');
status_msg((project ? (g_str == &kEn ? L"Project saved: " : L"Проект сохранён: ") :
export_status_prefix(export_file_name(opts.format), opts.selected_range, g_str == &kEn)) + (b ? b + 1 : path.c_str()));
} else {
MessageBoxW(g.main,
g_str == &kEn ? L"Failed to export file." : L"Не удалось сохранить файл.",
g_str->msg_error_title, MB_ICONERROR);
}
}
std::wstring lvm_current_date_text(const SYSTEMTIME& st) {
wchar_t buf[32]{};
swprintf(buf, 32, L"%04u/%02u/%02u",
static_cast<unsigned int>(st.wYear),
static_cast<unsigned int>(st.wMonth),
static_cast<unsigned int>(st.wDay));
return buf;
}
std::wstring lvm_current_time_text(const SYSTEMTIME& st) {
wchar_t buf[32]{};
swprintf(buf, 32, L"%02u:%02u:%02u,%03u",
static_cast<unsigned int>(st.wHour),
static_cast<unsigned int>(st.wMinute),
static_cast<unsigned int>(st.wSecond),
static_cast<unsigned int>(st.wMilliseconds));
return buf;
}
double lvm_export_nominal_delta_x() {
if (g.cached_global_gap_step_ready && g.cached_global_gap_step > 0.0) return g.cached_global_gap_step;
if (g.approx_dt > 0.0) return g.approx_dt;
if (g.ds.time.size() > 1) {
const double diff = g.ds.time[1] - g.ds.time[0];
if (std::isfinite(diff) && diff > 0.0) return diff;
}
return 1e-6;
}
bool write_frf_csv(std::ofstream& out) {
const auto& common = g.frf.result.common();
if (!g.frf.result.ok || g.frf.pending) return false;
const double low = std::pow(10.0, g.frf.log_start), high = std::pow(10.0, g.frf.log_end);
bool have_bins = false;
for (double f : common.frequencies) if (f > 0 && f >= low*(1-1e-12) && f <= high*(1+1e-12)) { have_bins = true; break; }
if (!have_bins) return false;
write_frf_metadata(out);
const bool multi=g.frf.outputs.size()>1;
if (multi) out << "response_index,response_name,";
out << "frequency_hz,dynamic_coefficient,h_real,h_imag,valid,coherence,coherence_valid,reference_amplitude,reference_valid\n";
for (std::size_t response=0;response<g.frf.result.responses.size();++response) {
const auto& r=g.frf.result.responses[response];
for (std::size_t k = 1; k < common.frequencies.size(); ++k) {
const double f = common.frequencies[k];
if (f < low*(1-1e-12) || f > high*(1+1e-12)) continue;
if (multi) {
out << g.frf.outputs[response] << ",\"";
const auto name=to_utf8(g.frf.output_names[response]);
for (char c:name) { if (c=='"') out << '"'; out << c; }
out << "\",";
}
out << numfmt(f) << ',';
if (k<r.valid.size() && r.valid[k]) {
const double kd = lvm::frf_dynamic_coefficient(r, k);
out << numfmt(kd) << ','
<< numfmt(r.transfer[k].real()) << ',' << numfmt(r.transfer[k].imag()) << ",1";
} else {
out << ",,,0";
}
if (k<r.coherence_valid.size() && r.coherence_valid[k]) out << ',' << numfmt(r.coherence[k]) << ",1";
else out << ",,0";
if (k<common.reference_amplitude_valid.size() && common.reference_amplitude_valid[k])
out << ',' << numfmt(common.reference_amplitude[k]) << ",1\n";
else out << ",,0\n";
}
}
return out.good();
}
bool save_frf_csv(const std::wstring& path) {
return lvm::atomic_write_file(std::filesystem::path(path), [&](std::ofstream& out) {
return write_frf_csv(out);
}, &g.last_error);
}
} // namespace gui