FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ORNLSlicer/src/managers/session_manager.cpp at develop · ORNLSlicer/ORNLSlicer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ORNLSlicer
/
ORNLSlicer
Public
Notifications
You must be signed in to change notification settings
Fork
20
Star
90
Code
Issues
18
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ORNLSlicer
/
src
/
managers
/
session_manager.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
843 lines (682 loc) · 31 KB
Breadcrumbs
ORNLSlicer
/
src
/
managers
/
session_manager.cpp
Copy path
File metadata and controls
843 lines (682 loc) · 31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#
include
"
managers/session_manager.h
"
#
include
<
QCoreApplication
>
#
include
<
QStandardPaths
>
#
include
<
cstdlib
>
#
include
<
string
>
#
include
<
qcontainerfwd.h
>
#
include
<
qdir.h
>
#
include
<
qfiledevice.h
>
#
include
<
qfileinfo.h
>
#
include
<
qhash.h
>
#
include
<
qlogging.h
>
#
include
<
qmatrix4x4.h
>
#
include
<
qobject.h
>
#
include
<
qsharedpointer.h
>
#
include
<
qtmetamacros.h
>
#
include
<
qtypes.h
>
#
include
"
configs/settings_base.h
"
#
include
"
gcode/gcode_meta.h
"
#
include
"
geometry/mesh/closed_mesh.h
"
#
include
"
geometry/mesh/mesh_base.h
"
#
include
"
geometry/mesh/mesh_factory.h
"
#
include
"
geometry/mesh/open_mesh.h
"
#
include
"
managers/preferences_manager.h
"
#
include
"
managers/settings/settings_manager.h
"
#
include
"
part/part.h
"
#
include
"
threading/mesh_loader.h
"
#
include
"
threading/session_loader.h
"
#
include
"
threading/slicers/cylindrical_slicer.h
"
#
include
"
threading/slicers/image_slicer.h
"
#
include
"
threading/slicers/planar_slicer.h
"
#
include
"
units/derivative_units.h
"
#
include
"
units/unit.h
"
#
include
"
utilities/constants.h
"
#
include
"
utilities/enums.h
"
#
include
"
utilities/qt_json_conversion.h
"
#
include
"
utilities/runtime_diagnostics.h
"
#
include
"
widgets/part_widget/model/part_meta_item.h
"
namespace
ORNL
{
namespace
{
constexpr
int
kMaxRecentFiles
=
10
;
bool
supportsCylindricalSlicing
(GcodeSyntax syntax) {
return
syntax == GcodeSyntax::
kArcSpecialties
;
}
QString
absoluteFilePath
(
const
QString& path) {
return
QFileInfo
(path).
absoluteFilePath
();
}
bool
hasSuffix
(
const
QString& path,
const
QStringList& suffixes) {
return
suffixes.
contains
(
QFileInfo
(path).
suffix
().
toLower
());
}
QStringList
readStringList
(
const
fifojson& j,
const
char
* key) {
QStringList values;
if
(!j.
contains
(key) || !j[key].
is_array
())
return
values;
for
(
const
fifojson& value : j[key]) {
if
(!value.
is_string
())
continue
;
values.
push_back
(
QString::fromStdString
(value.
get
<std::string>()));
}
return
values;
}
QStringList
sanitizedRecentFiles
(
const
QStringList& files,
const
QStringList& suffixes) {
QStringList result;
for
(
const
QString& file : files) {
const
QString path =
absoluteFilePath
(file);
if
(path.
isEmpty
() || !
hasSuffix
(path, suffixes) || result.
contains
(path))
continue
;
result.
push_back
(path);
if
(result.
size
() >=
kMaxRecentFiles
)
break
;
}
return
result;
}
void
addRecentFile
(QStringList& files, QString path) {
path =
absoluteFilePath
(path);
if
(path.
isEmpty
())
return
;
files.
removeAll
(path);
files.
prepend
(path);
while
(files.
size
() >
kMaxRecentFiles
) { files.
removeLast
(); }
}
fifojson
stringListToJson
(
const
QStringList& values) {
fifojson result =
fifojson::array
();
for
(
const
QString& value : values) { result.
push_back
(value); }
return
result;
}
}
//
namespace
QSharedPointer<SessionManager> SessionManager::m_singleton = QSharedPointer<SessionManager>();
QSharedPointer<SessionManager>
SessionManager::getInstance
() {
if
(m_singleton.
isNull
()) { m_singleton.
reset
(
new
SessionManager
()); }
return
m_singleton;
}
SessionManager::SessionManager
() : m_file(QString()), m_dirty_history(
false
), m_sensor_files_generated(
false
) {
//
Create static location for gcode output for session
QString appPathStr =
QStandardPaths::writableLocation
(QStandardPaths::AppDataLocation);
QDir
appPath
(appPathStr);
QString httpAppLocationStr =
QDir::temp
().
absolutePath
() +
"
/http_config
"
;
QDir
httpConfigPath
(httpAppLocationStr);
try
{
if
(!appPath.
exists
())
QDir
().
mkpath
(appPathStr);
if
(!httpConfigPath.
exists
())
QDir
().
mkpath
(httpAppLocationStr);
}
catch
(...) {
qWarning
() <<
"
Check your path, cannot create directory:
"
+ appPathStr; }
defaultGcodeFile = appPath.
filePath
(
"
gcode_output
"
);
loadHistory
();
//
clear any temp STLs created from http import
appPath.
setNameFilters
(
QStringList
() <<
"
*.stl
"
);
appPath.
setFilter
(QDir::Files);
for
(QString tempStls : appPath.
entryList
()) { appPath.
remove
(tempStls); }
}
SessionManager::~SessionManager
() {
//
Free the C allocated memory.
for
(model_data file : m_models) {
free
(file.
model
); }
saveHistory
();
}
void
SessionManager::loadHistory
() {
QDir path =
QStandardPaths::writableLocation
(QStandardPaths::AppDataLocation);
QFile
file
(path.
filePath
(
"
app.history
"
));
if
(file.
exists
()) {
file.
open
(QIODevice::ReadOnly);
QString history = file.
readAll
();
fifojson j =
json::parse
(history.
toStdString
());
QString defaultLocation =
QStandardPaths::writableLocation
(QStandardPaths::DesktopLocation);
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kPrinter
,
QString::fromStdString
(j.
value
(
"
printer_setting
"
,
"
LFAM_03in
"
)));
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kMaterial
,
QString::fromStdString
(j.
value
(
"
material_setting
"
,
"
LFAM_03in
"
)));
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kProfile
,
QString::fromStdString
(j.
value
(
"
profile_setting
"
,
"
LFAM_03in
"
)));
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kExperimental
,
QString::fromStdString
(j.
value
(
"
experimental_setting
"
,
"
LFAM_03in
"
)));
m_most_recent_model_location = j.
value
(
"
model_location
"
, defaultLocation);
m_most_recent_project_location = j.
value
(
"
project_location
"
, defaultLocation);
m_most_recent_gcode_location = j.
value
(
"
gcode_location
"
, defaultLocation);
m_most_recent_setting_folder_location = j.
value
(
"
setting_folder_location
"
, defaultLocation);
m_most_recent_layer_bar_setting_folder_location = j.
value
(
"
layer_bar_setting_folder_location
"
, defaultLocation);
m_most_recent_http_config = j.
value
(
"
http_config
"
,
QString
());
m_recent_project_files =
sanitizedRecentFiles
(
readStringList
(j,
"
recent_project_files
"
), {
"
s2p
"
});
m_recent_model_files =
sanitizedRecentFiles
(
readStringList
(j,
"
recent_model_files
"
), {
"
stl
"
,
"
3mf
"
,
"
obj
"
,
"
amf
"
,
"
step
"
,
"
stp
"
});
file.
close
();
}
else
{
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kPrinter
,
"
LFAM_03in
"
);
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kMaterial
,
"
LFAM_03in
"
);
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kProfile
,
"
LFAM_03in
"
);
m_most_recent_setting_history.
insert
(Constants::Settings::SettingTab::
kExperimental
,
"
LFAM_03in
"
);
m_most_recent_model_location = m_most_recent_project_location = m_most_recent_gcode_location =
m_most_recent_setting_folder_location = m_most_recent_layer_bar_setting_folder_location =
QStandardPaths::writableLocation
(QStandardPaths::DesktopLocation);
m_most_recent_http_config =
QString
();
m_recent_project_files.
clear
();
m_recent_model_files.
clear
();
m_dirty_history =
true
;
}
}
void
SessionManager::saveHistory
() {
if
(m_dirty_history) {
fifojson j;
j[
"
printer_setting
"
] = m_most_recent_setting_history[Constants::Settings::SettingTab::
kPrinter
];
j[
"
material_setting
"
] = m_most_recent_setting_history[Constants::Settings::SettingTab::
kMaterial
];
j[
"
profile_setting
"
] = m_most_recent_setting_history[Constants::Settings::SettingTab::
kProfile
];
j[
"
experimental_setting
"
] = m_most_recent_setting_history[Constants::Settings::SettingTab::
kExperimental
];
j[
"
model_location
"
] = m_most_recent_model_location;
j[
"
project_location
"
] = m_most_recent_project_location;
j[
"
gcode_location
"
] = m_most_recent_gcode_location;
j[
"
setting_folder_location
"
] = m_most_recent_setting_folder_location;
j[
"
layer_bar_setting_folder_location
"
] = m_most_recent_layer_bar_setting_folder_location;
j[
"
http_config
"
] = m_most_recent_http_config;
j[
"
recent_project_files
"
] =
stringListToJson
(m_recent_project_files);
j[
"
recent_model_files
"
] =
stringListToJson
(m_recent_model_files);
//
Causes segfault if QStandardPaths is referenced here?
//
QDir path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir path = m_save_dir;
QFile
file
(path.
filePath
(
"
app.history
"
));
file.
open
(QIODevice::WriteOnly);
file.
write
(j.
dump
(
4
).
c_str
());
file.
close
();
m_dirty_history =
false
;
}
}
QString
SessionManager::sessionFile
() {
return
m_file;
}
bool
SessionManager::loadModel
(QString filename,
bool
saveLocation, MeshType mt,
bool
synchRequired, QMatrix4x4 mtrx) {
QFileInfo
file_info
(filename);
if
(synchRequired) {
auto
meshes =
MeshLoader::LoadMeshes
(filename, mt, mtrx,
PreferencesManager::getInstance
()->
getImportUnit
());
for
(
auto
mesh_data : meshes) {
addPart
(mesh_data.
mesh
, filename, mt);
if
(!m_models.
contains
(file_info.
fileName
()))
m_models.
insert
(file_info.
fileName
(), {mesh_data.
raw_data
, mesh_data.
size
});
else
free
(mesh_data.
raw_data
);
}
}
else
{
MeshLoader* loader =
new
MeshLoader
(filename, mt, mtrx,
PreferencesManager::getInstance
()->
getImportUnit
());
connect
(loader, &MeshLoader::finished, loader, &MeshLoader::deleteLater);
connect
(loader, &MeshLoader::error,
this
, [
this
](QString msg) { emit
forwardStatusUpdate
(msg); });
connect
(loader, &MeshLoader::newMesh,
this
, [
this
, filename, file_info, mt](MeshLoader::MeshData mesh_data) {
addPart
(mesh_data.
mesh
, filename, mt);
if
(!m_models.
contains
(file_info.
fileName
()))
m_models.
insert
(file_info.
fileName
(), {mesh_data.
raw_data
, mesh_data.
size
});
else
free
(mesh_data.
raw_data
);
});
loader->
start
();
}
if
(saveLocation) {
setMostRecentModelLocation
(file_info.
absoluteFilePath
());
addRecentModelFile
(filename, mt);
}
return
true
;
}
void
SessionManager::addPart
(QSharedPointer<Part> new_part,
bool
notify) {
m_load_mutex.
lock
();
//
Try to find a name for this part.
QString name = new_part->
name
();
QString org_name = name;
uint count =
1
;
while
(m_parts.
contains
(name)) {
name = org_name +
"
_
"
+
QString::number
(count);
count++;
}
new_part->
setName
(name);
m_parts.
insert
(name, new_part);
if
(notify) emit
partAdded
(new_part);
m_load_mutex.
unlock
();
}
void
SessionManager::addPart
(QSharedPointer<MeshBase> new_mesh, QString filename, MeshType mt) {
m_load_mutex.
lock
();
QSharedPointer<Part> new_part = QSharedPointer<Part>::
create
(new_mesh, filename, mt);
//
Try to find a name for this part.
QString file_name;
if
(filename ==
"
"
)
//
filename is blank because the part is being loaded via project import
{
file_name = new_part->
name
();
QString org_name = file_name;
uint count =
1
;
while
(m_parts.
contains
(file_name)) {
file_name = org_name +
"
_
"
+
QString::number
(count);
count++;
}
}
else
//
filename exists because the part is being loaded via UI button
{
//
Find everything after the last /
QStringList full_name_parts = filename.
split
(
"
/
"
);
file_name = full_name_parts.
at
(full_name_parts.
size
() -
1
);
QString orig_file_name = file_name;
//
Add a number to duplicated file names
uint count =
1
;
while
(m_parts.
contains
(file_name)) {
//
Separate at the periods, then join everything before the last period
QStringList orig_file_name_parts = orig_file_name.
split
(
"
.
"
);
QString name;
for
(
int
i =
0
; i < orig_file_name_parts.
size
() -
1
; i++) { name += orig_file_name_parts[i] +
"
.
"
; }
name.
chop
(
1
);
//
Without this, the file name will always include an extra period
//
Add a number to signify a new instance of an existing file name
file_name = name +
"
_
"
+
QString::number
(count);
//
Re-add the file extension
file_name +=
"
.
"
+ orig_file_name_parts[orig_file_name_parts.
size
() -
1
];
count++;
}
}
new_part->
setName
(file_name);
m_parts.
insert
(file_name, new_part);
emit
partAdded
(new_part);
m_load_mutex.
unlock
();
}
void
SessionManager::reloadPart
(QSharedPointer<PartMetaItem> pm) {
QString filename = pm->
part
()->
sourceFilePath
();
if
(filename.
isNull
() || filename.
isEmpty
() || !(
QFileInfo
(filename).
exists
() &&
QFileInfo
(filename).
isFile
())) {
emit
forwardStatusUpdate
(
"
Part reload failed, file
\"
"
+ filename +
"
\"
, no such source file available
"
);
return
;
}
QFileInfo
file_info
(filename);
const
MeshType mesh_type = pm->
part
()->
getMeshType
();
setMostRecentModelLocation
(file_info.
absoluteFilePath
());
addRecentModelFile
(filename, mesh_type);
MeshLoader* loader =
new
MeshLoader
(filename, mesh_type,
QMatrix4x4
(),
PreferencesManager::getInstance
()->
getImportUnit
());
connect
(loader, &MeshLoader::finished, loader, &MeshLoader::deleteLater);
connect
(loader, &MeshLoader::error,
this
, [
this
](QString msg) { emit
forwardStatusUpdate
(msg); });
connect
(loader, &MeshLoader::newMesh,
this
, [
this
, file_info, pm](MeshLoader::MeshData mesh_data) {
pm->
part
()->
setRootMesh
(mesh_data.
mesh
);
m_models[file_info.
fileName
()] = {mesh_data.
raw_data
, mesh_data.
size
};
emit
partReloaded
(pm);
emit
forwardStatusUpdate
(
"
Reloaded part model, file
\"
"
+ file_info.
fileName
() +
"
\"
"
);
});
loader->
start
();
}
void
SessionManager::replacePart
(QSharedPointer<PartMetaItem> pm, QString filename) {
if
(filename.
isNull
() || filename.
isEmpty
() || !(
QFileInfo
(filename).
exists
() &&
QFileInfo
(filename).
isFile
())) {
emit
forwardStatusUpdate
(
"
Part reload failed, file
\"
"
+ filename +
"
\"
, no such source file available
"
);
return
;
}
QFileInfo
file_info
(filename);
const
MeshType mesh_type = pm->
part
()->
getMeshType
();
setMostRecentModelLocation
(file_info.
absoluteFilePath
());
addRecentModelFile
(filename, mesh_type);
MeshLoader* loader =
new
MeshLoader
(filename, mesh_type,
QMatrix4x4
(),
PreferencesManager::getInstance
()->
getImportUnit
());
connect
(loader, &MeshLoader::finished, loader, &MeshLoader::deleteLater);
connect
(loader, &MeshLoader::error,
this
, [
this
](QString msg) { emit
forwardStatusUpdate
(msg); });
connect
(loader, &MeshLoader::newMesh,
this
, [
this
, file_info, pm](MeshLoader::MeshData mesh_data) {
pm->
part
()->
setSourceFile
(file_info.
fileName
());
pm->
part
()->
setRootMesh
(mesh_data.
mesh
);
m_models[file_info.
fileName
()] = {mesh_data.
raw_data
, mesh_data.
size
};
emit
partReloaded
(pm);
emit
forwardStatusUpdate
(
"
Reloaded part model, file
\"
"
+ file_info.
fileName
() +
"
\"
"
);
});
loader->
start
();
}
void
SessionManager::addCopiedPart
(QSharedPointer<Part> new_part) {
//
Try to find a name for this part.
QString name = new_part->
name
();
QString org_name = name;
uint count =
1
;
while
(m_parts.
contains
(name)) {
name = org_name +
"
_
"
+
QString::number
(count);
count++;
}
new_part->
setName
(name);
m_parts.
insert
(name, new_part);
}
bool
SessionManager::removePart
(QSharedPointer<Part> part) {
if
(!m_parts.
contains
(part->
name
()))
return
false
;
//
Keep the part around for a second so we can emit a removed signal. That way, any object that wants to
//
perform some finals operations on the part can still do so.
QSharedPointer<Part> old_part = m_parts[part->
name
()];
m_parts.
remove
(old_part->
name
());
if
(m_models.
contains
(part->
rootMesh
()->
name
())) m_models.
remove
(part->
rootMesh
()->
name
());
for
(
auto
& model : part->
subMeshes
())
if
(m_models.
contains
(model->
name
())) m_models.
remove
(model->
name
());
emit
partRemoved
(old_part);
return
true
;
}
bool
SessionManager::removePart
(QString name) {
if
(!m_parts.
contains
(name))
return
false
;
//
Keep the part around for a second so we can emit a removed signal. That way, any object that wants to
//
perform some finals operations on the part can still do so.
QSharedPointer<Part> old_part = m_parts[name];
m_parts.
remove
(name);
emit
partRemoved
(old_part);
return
true
;
}
void
SessionManager::clearParts
() {
m_parts.
clear
();
emit
partsCleared
();
}
fifojson
SessionManager::partsJson
()
const
{
fifojson session_json =
fifojson::object
();
for
(QSharedPointer<Part> curr_part : m_parts) {
fifojson part_json =
fifojson::object
();
part_json[Constants::Settings::Session::
kFile
] = curr_part->
rootMesh
()->
path
().
split
(
"
/
"
).
back
();
part_json[Constants::Settings::Session::
kMeshType
] =
static_cast
<
int
>(curr_part->
rootMesh
()->
type
());
part_json[Constants::Settings::Session::
kGenType
] =
static_cast
<
int
>(curr_part->
rootMesh
()->
genType
());
part_json[Constants::Settings::Session::
kOrgDims
][
"
x
"
] = curr_part->
rootMesh
()->
originalDimensions
().
x
;
part_json[Constants::Settings::Session::
kOrgDims
][
"
y
"
] = curr_part->
rootMesh
()->
originalDimensions
().
y
;
part_json[Constants::Settings::Session::
kOrgDims
][
"
z
"
] = curr_part->
rootMesh
()->
originalDimensions
().
z
;
part_json[Constants::Settings::Session::
kTransforms
] = curr_part->
rootMesh
()->
transformations
();
session_json[Constants::Settings::Session::
kParts
][curr_part->
name
().
toStdString
()] = part_json;
}
return
session_json;
}
bool
SessionManager::loadPartsJson
(fifojson j) {
int
totalParts =
0
;
for
(
auto
it : j[Constants::Settings::Session::
kParts
].
items
()) { ++totalParts; }
emit
totalPartsInProject
(totalParts);
for
(
auto
it : j[Constants::Settings::Session::
kParts
].
items
()) {
//
Get mesh information
QString name =
QString::fromStdString
(it.
key
());
MeshType mesh_type = it.
value
()[Constants::Settings::Session::
kMeshType
];
MeshGeneratorType gen_type = it.
value
()[Constants::Settings::Session::
kGenType
];
Distance3D
org_dims
(it.
value
()[Constants::Settings::Session::
kOrgDims
][
"
x
"
],
it.
value
()[Constants::Settings::Session::
kOrgDims
][
"
y
"
],
it.
value
()[Constants::Settings::Session::
kOrgDims
][
"
z
"
]);
auto
mtrxesArray = it.
value
()[Constants::Settings::Session::
kTransforms
];
int
transformCount =
1
;
QVector<QMatrix4x4> mtrxes;
if
(mtrxesArray.
size
() ==
0
) {
QMatrix4x4 mtrx = it.
value
()[Constants::Settings::Session::
kTransform
];
mtrxes.
append
(mtrx);
}
else
{
transformCount = (
int
)mtrxesArray.
size
();
for
(
auto
i =
0
; i < transformCount; i++) {
QMatrix4x4 mx = mtrxesArray[i];
mtrxes.
append
(mx);
}
}
switch
(gen_type) {
case
kNone
:
//
Not generated, so load from file
{
QString filename =
QString::fromStdString
(it.
value
()[Constants::Settings::Session::
kFile
]);
if
(m_models.
contains
(filename))
//
Allready have this model data
{
auto
data = m_models.
value
(filename);
auto
meshes =
MeshLoader::LoadMeshes
(filename, mesh_type, mtrxes[
0
],
Distance
(mm), data.
model
, data.
size
);
for
(
auto
mesh_data : meshes) {
mesh_data.
mesh
->
setTransformations
(mtrxes);
mesh_data.
mesh
->
setName
(name);
addPart
(mesh_data.
mesh
);
}
}
break
;
}
case
kRectangularBox
: {
auto
mesh =
QSharedPointer<ClosedMesh>::
create
(
MeshFactory::CreateBoxMesh
(org_dims.
x
, org_dims.
y
, org_dims.
z
));
mesh->
setTransformations
(mtrxes);
mesh->
setType
(mesh_type);
mesh->
setName
(name);
CSM
->
addPart
(mesh);
break
;
}
case
kTriangularPyramid
: {
auto
mesh = QSharedPointer<ClosedMesh>::
create
(
MeshFactory::CreateTriaglePyramidMesh
(org_dims.
y
));
mesh->
setTransformations
(mtrxes);
mesh->
setType
(mesh_type);
mesh->
setName
(name);
CSM
->
addPart
(mesh);
break
;
}
case
kHexagonalPrism
: {
auto
mesh = QSharedPointer<ClosedMesh>::
create
(
MeshFactory::CreateHexagonalPrismMesh
(org_dims.
x
/
2.0
, org_dims.
z
));
mesh->
setTransformations
(mtrxes);
mesh->
setType
(mesh_type);
mesh->
setName
(name);
CSM
->
addPart
(mesh);
break
;
}
case
kCylinder
: {
auto
mesh = QSharedPointer<ClosedMesh>::
create
(
MeshFactory::CreateCylinderMesh
(org_dims.
y
, org_dims.
z
));
mesh->
setTransformations
(mtrxes);
mesh->
setType
(mesh_type);
mesh->
setName
(name);
CSM
->
addPart
(mesh);
break
;
}
case
kCone
: {
auto
mesh = QSharedPointer<ClosedMesh>::
create
(
MeshFactory::CreateConeMesh
(org_dims.
y
, org_dims.
z
));
mesh->
setTransformations
(mtrxes);
mesh->
setType
(mesh_type);
mesh->
setName
(name);
CSM
->
addPart
(mesh);
break
;
}
case
kOpenTopBox
:
case
kDefaultSettingRegion
: {
auto
mesh = QSharedPointer<OpenMesh>::
create
(
MeshFactory::CreateOpenTopBoxMesh
(org_dims.
x
, org_dims.
y
, org_dims.
z
));
mesh->
setTransformations
(mtrxes);
mesh->
setType
(mesh_type);
mesh->
setName
(name);
CSM
->
addPart
(mesh);
break
;
}
};
}
return
true
;
}
bool
SessionManager::isBuildMode
() {
for
(
auto
part : m_parts) {
if
(part->
getMeshType
() == MeshType::
kBuild
)
return
true
;
}
return
false
;
}
bool
SessionManager::doSlice
() {
const
GcodeSyntax syntax =
GSM
->
getGlobal
()->
setting
<GcodeSyntax>(
PRS
::MachineSetup::
kSyntax
);
const
SlicingMode type =
static_cast
<SlicingMode>(
GSM
->
getGlobal
()->
setting
<
int
>(
PS
::Slicing::
kSlicingMode
));
if
(type == SlicingMode::
kCylindrical
&& !
supportsCylindricalSlicing
(syntax)) {
const
QString message =
"
Cylindrical slicing requires Printer > Machine Setup > Syntax to be Arc Specialties.
"
;
qWarning
() << message;
emit
forwardStatusUpdate
(message);
return
false
;
}
//
check current syntax for file suffix that needs to be output
tempGcodeFile = defaultGcodeFile + GcodeMetaList::SyntaxToMetaHash[(
int
)syntax].
m_file_suffix
;
m_sensor_files_generated =
GSM
->
getGlobal
()->
setting
<
bool
>(
PS
::LaserScanner::
kLaserScanner
);
if
(m_ast.
isNull
())
this
->
changeSlicer
(type);
else
{
//
See if it has changed
if
(m_slicing_mode != type)
this
->
changeSlicer
(type);
else
m_ast->
setGcodeOutput
(tempGcodeFile);
}
//
Request new information about the parts to be sliced.
emit
requestTransformationUpdate
();
emit
startSlice
();
return
true
;
}
bool
SessionManager::sliceComplete
() {
Diagnostics::logLine
(
QString
(
"
SessionManager slice complete: %1
"
).
arg
(tempGcodeFile));
emit
forwardSliceComplete
(tempGcodeFile,
true
);
return
true
;
}
void
SessionManager::forwardDialogUpdate
(StatusUpdateStepType type,
int
completedPercentage) {
emit
updateDialog
(type, completedPercentage);
}
void
SessionManager::cancelSlice
() {
m_ast->
setCancel
();
}
void
SessionManager::setCopiedPart
(QSharedPointer<Part> part) {
m_copied_part = part;
}
void
SessionManager::pastePart
() {
QSharedPointer<Part> new_copy = QSharedPointer<Part>(
new
Part
(*m_copied_part));
//
reset transforms so that part matches it's original as loaded (graphical manipulations still apply)
new_copy->
setTransformation
(
QMatrix4x4
());
QString name = new_copy->
name
();
QString org_name = name = name.
left
(name.
lastIndexOf
(
'
_
'
));
uint count =
1
;
//
Try to find a name for this part.
while
(m_parts.
contains
(name)) {
name = org_name +
"
_
"
+
QString::number
(count);
count++;
}
new_copy->
rootMesh
()->
setName
(name);
m_load_mutex.
lock
();
m_parts.
insert
(name, new_copy);
emit
partAdded
(new_copy);
m_load_mutex.
unlock
();
}
qint64
SessionManager::getSliceTimeElapsed
() {
if
(m_ast.
isNull
()) {
return
0
; }
return
m_ast->
getTimeElapsed
();
}
bool
SessionManager::changeSlicer
(SlicingMode type) {
//
Disconnect the signals from the AST.
QObject::disconnect
(
this
, &SessionManager::startSlice,
nullptr
,
nullptr
);
//
Reset the AST with a new slicer.
switch
(type) {
case
SlicingMode::
kPlanar
:
m_ast.
reset
(
new
PlanarSlicer
(tempGcodeFile));
break
;
case
SlicingMode::
kImage
:
m_ast.
reset
(
new
ImageSlicer
(tempGcodeFile));
break
;
case
SlicingMode::
kCylindrical
:
m_ast.
reset
(
new
CylindricalSlicer
(tempGcodeFile));
break
;
default
:
qWarning
() <<
"
Unknown slicing mode requested. Falling back to Planar slicer.
"
;
m_ast.
reset
(
new
PlanarSlicer
(tempGcodeFile));
type = SlicingMode::
kPlanar
;
break
;
}
m_slicing_mode = type;
//
Reset part steps
for
(QSharedPointer<Part> part : m_parts) { part->
clearSteps
(); }
//
Reconnect the signal to the AST.
QObject::connect
(
this
, &SessionManager::startSlice, m_ast.
get
(), &AbstractSlicingThread::doSlice);
connect
(m_ast.
get
(), &AbstractSlicingThread::statusUpdate,
this
, &SessionManager::forwardDialogUpdate);
connect
(m_ast.
get
(), &AbstractSlicingThread::statusMessage,
this
, &SessionManager::forwardStatusUpdate);
connect
(m_ast.
get
(), &AbstractSlicingThread::sliceComplete,
this
, &SessionManager::sliceComplete);
return
true
;
}
SessionLoader*
SessionManager::saveSession
(QString path,
bool
shouldTrack,
bool
notifyOnSuccess) {
//
Request an update.
emit
requestTransformationUpdate
();
SessionLoader* loader =
new
SessionLoader
(path,
true
);
connect
(loader, &SessionLoader::finished, loader, &SessionLoader::deleteLater);
if
(notifyOnSuccess)
connect
(loader, &SessionLoader::saveSucceeded,
this
, [
this
, path]() { emit
sessionSaved
(path); });
loader->
start
();
m_file = path;
if
(shouldTrack) {
setMostRecentProjectLocation
(
QFileInfo
(path).
absolutePath
());
addRecentProjectFile
(path);
}
return
loader;
}
SessionLoader*
SessionManager::loadSession
(
bool
shouldDelete, QString path,
bool
promptForSettingsUpdate) {
//
Clear out old data if necessary.
if
(shouldDelete) {
for
(model_data file : m_models)
free
(file.
model
);
m_parts.
clear
();
emit
partsCleared
();
m_models.
clear
();
}
GSM
->
clearGlobal
();
//
m_should_shift = shouldShift;
SessionLoader* loader =
new
SessionLoader
(path,
false
);
QString filename =
QString::fromStdString
(Constants::Settings::Session::Files::
kGlobal
) +
"
in project file:
"
+ path +
"
"
;
fifojson settings = loader->
getSettingsFromZip
();
int
result =
GSM
->
checkVersion
(
filename, settings,
promptForSettingsUpdate ? SettingsVersionUpdateMode::
kGuiPrompt
: SettingsVersionUpdateMode::
kAutoUpdate
);
if
(result ==
1
) loader->
updateSettingsJson
(settings, promptForSettingsUpdate);
if
(result >=
0
) {
connect
(loader, &SessionLoader::finished, loader, &SessionLoader::deleteLater);
loader->
start
();
m_file = path;
return
loader;
}
delete
loader;
return
nullptr
;
}
QHash<QString, QString>
SessionManager::getMostRecentSettingHistory
() {
return
m_most_recent_setting_history;
}
void
SessionManager::setMostRecentSettingHistory
(QString key, QString name) {
m_most_recent_setting_history[key] = name;
m_dirty_history =
true
;
}
QString
SessionManager::getMostRecentModelLocation
() {
return
m_most_recent_model_location;
}
void
SessionManager::setMostRecentModelLocation
(QString path) {
m_most_recent_model_location = path;
m_dirty_history =
true
;
}
QString
SessionManager::getMostRecentProjectLocation
() {
return
m_most_recent_project_location;
}
void
SessionManager::setMostRecentProjectLocation
(QString path) {
m_most_recent_project_location = path;
m_dirty_history =
true
;
}
QString
SessionManager::getMostRecentGcodeLocation
() {
return
m_most_recent_gcode_location;
}
void
SessionManager::setMostRecentGcodeLocation
(QString path) {
m_most_recent_gcode_location = path;
m_dirty_history =
true
;
}
QString
SessionManager::getMostRecentSettingFolderLocation
() {
return
m_most_recent_setting_folder_location;
}
void
SessionManager::setMostRecentSettingFolderLocation
(QString path) {
m_most_recent_setting_folder_location = path;
m_dirty_history =
true
;
}
QString
SessionManager::getMostRecentLayerBarSettingFolderLocation
() {
return
m_most_recent_layer_bar_setting_folder_location;
}
void
SessionManager::setMostRecentLayerBarSettingFolderLocation
(QString path) {
m_most_recent_layer_bar_setting_folder_location = path;
m_dirty_history =
true
;
}
bool
SessionManager::sensorFilesGenerated
() {
return
m_sensor_files_generated;
}
QString
SessionManager::getMostRecentHTTPConfig
() {
return
m_most_recent_http_config;
}
void
SessionManager::setMostRecentHTTPConfig
(QString config) {
m_most_recent_http_config = config;
}
QStringList
SessionManager::getRecentProjectFiles
()
const
{
return
m_recent_project_files;
}
QStringList
SessionManager::getRecentModelFiles
()
const
{
return
m_recent_model_files;
}
void
SessionManager::addRecentProjectFile
(QString path) {
if
(!
hasSuffix
(path, {
"
s2p
"
}))
return
;
addRecentFile
(m_recent_project_files, path);
m_dirty_history =
true
;
}
void
SessionManager::addRecentModelFile
(QString path, MeshType mt) {
if
(mt != MeshType::
kBuild
|| !
hasSuffix
(path, {
"
stl
"
,
"
3mf
"
,
"
obj
"
,
"
amf
"
,
"
step
"
,
"
stp
"
}))
return
;
addRecentFile
(m_recent_model_files, path);
m_dirty_history =
true
;
}
void
SessionManager::removeRecentFile
(QString path) {
path =
absoluteFilePath
(path);
const
bool
removed_project = m_recent_project_files.
removeAll
(path) >
0
;
const
bool
removed_model = m_recent_model_files.
removeAll
(path) >
0
;
if
(removed_project || removed_model) m_dirty_history =
true
;
}
void
SessionManager::clearRecentFiles
() {
if
(m_recent_project_files.
isEmpty
() && m_recent_model_files.
isEmpty
())
return
;
m_recent_project_files.
clear
();
m_recent_model_files.
clear
();
m_dirty_history =
true
;
}
void
SessionManager::setDefaultGcodeDir
(QString dir) {
defaultGcodeFile = dir +
"
\\
gcode_output
"
;
}
}
//
namespace ORNL
Back
|
FazBrowse Home
|
New Git URL