-
Notifications
You must be signed in to change notification settings - Fork 469
/
solvespace.cpp
1056 lines (919 loc) · 38.4 KB
/
solvespace.cpp
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
// Entry point in to the program, our registry-stored settings and top-level
// housekeeping when we open, save, and create new files.
//
// Copyright 2008-2013 Jonathan Westhues.
//-----------------------------------------------------------------------------
#include "solvespace.h"
#include "config.h"
SolveSpaceUI SolveSpace::SS = {};
Sketch SolveSpace::SK = {};
void SolveSpaceUI::Init() {
#if !defined(HEADLESS)
// Check that the resource system works.
dbp("%s", LoadString("banner.txt").data());
#endif
Platform::SettingsRef settings = Platform::GetSettings();
SS.tangentArcRadius = 10.0;
// Then, load the registry settings.
// Default list of colors for the model material
modelColor[0] = settings->ThawColor("ModelColor_0", RGBi(150, 150, 150));
modelColor[1] = settings->ThawColor("ModelColor_1", RGBi(100, 100, 100));
modelColor[2] = settings->ThawColor("ModelColor_2", RGBi( 30, 30, 30));
modelColor[3] = settings->ThawColor("ModelColor_3", RGBi(150, 0, 0));
modelColor[4] = settings->ThawColor("ModelColor_4", RGBi( 0, 100, 0));
modelColor[5] = settings->ThawColor("ModelColor_5", RGBi( 0, 80, 80));
modelColor[6] = settings->ThawColor("ModelColor_6", RGBi( 0, 0, 130));
modelColor[7] = settings->ThawColor("ModelColor_7", RGBi( 80, 0, 80));
// Light intensities
lightIntensity[0] = settings->ThawFloat("LightIntensity_0", 1.0);
lightIntensity[1] = settings->ThawFloat("LightIntensity_1", 0.5);
ambientIntensity = 0.3; // no setting for that yet
// Light positions
lightDir[0].x = settings->ThawFloat("LightDir_0_Right", -1.0);
lightDir[0].y = settings->ThawFloat("LightDir_0_Up", 1.0);
lightDir[0].z = settings->ThawFloat("LightDir_0_Forward", 0.0);
lightDir[1].x = settings->ThawFloat("LightDir_1_Right", 1.0);
lightDir[1].y = settings->ThawFloat("LightDir_1_Up", 0.0);
lightDir[1].z = settings->ThawFloat("LightDir_1_Forward", 0.0);
exportMode = false;
// Chord tolerance
chordTol = settings->ThawFloat("ChordTolerancePct", 0.5);
// Max pwl segments to generate
maxSegments = settings->ThawInt("MaxSegments", 10);
// Chord tolerance
exportChordTol = settings->ThawFloat("ExportChordTolerance", 0.1);
// Max pwl segments to generate
exportMaxSegments = settings->ThawInt("ExportMaxSegments", 64);
// View units
viewUnits = (Unit)settings->ThawInt("ViewUnits", (uint32_t)Unit::MM);
// Number of digits after the decimal point
afterDecimalMm = settings->ThawInt("AfterDecimalMm", 2);
afterDecimalInch = settings->ThawInt("AfterDecimalInch", 3);
afterDecimalDegree = settings->ThawInt("AfterDecimalDegree", 2);
// Camera tangent (determines perspective)
cameraTangent = settings->ThawFloat("CameraTangent", 0.3f/1e3);
// Grid spacing
gridSpacing = settings->ThawFloat("GridSpacing", 5.0);
// Export scale factor
exportScale = settings->ThawFloat("ExportScale", 1.0);
// Export offset (cutter radius comp)
exportOffset = settings->ThawFloat("ExportOffset", 0.0);
// Rewrite exported colors close to white into black (assuming white bg)
fixExportColors = settings->ThawBool("FixExportColors", true);
// Draw back faces of triangles (when mesh is leaky/self-intersecting)
drawBackFaces = settings->ThawBool("DrawBackFaces", true);
// Use turntable mouse navigation
turntableNav = settings->ThawBool("TurntableNav", false);
// Check that contours are closed and not self-intersecting
checkClosedContour = settings->ThawBool("CheckClosedContour", true);
// Enable automatic constrains for lines
automaticLineConstraints = settings->ThawBool("AutomaticLineConstraints", true);
// Draw closed polygons areas
showContourAreas = settings->ThawBool("ShowContourAreas", false);
// Export shaded triangles in a 2d view
exportShadedTriangles = settings->ThawBool("ExportShadedTriangles", true);
// Export pwl curves (instead of exact) always
exportPwlCurves = settings->ThawBool("ExportPwlCurves", false);
// Background color on-screen
backgroundColor = settings->ThawColor("BackgroundColor", RGBi(0, 0, 0));
// Whether export canvas size is fixed or derived from bbox
exportCanvasSizeAuto = settings->ThawBool("ExportCanvasSizeAuto", true);
// Margins for automatic canvas size
exportMargin.left = settings->ThawFloat("ExportMargin_Left", 5.0);
exportMargin.right = settings->ThawFloat("ExportMargin_Right", 5.0);
exportMargin.bottom = settings->ThawFloat("ExportMargin_Bottom", 5.0);
exportMargin.top = settings->ThawFloat("ExportMargin_Top", 5.0);
// Dimensions for fixed canvas size
exportCanvas.width = settings->ThawFloat("ExportCanvas_Width", 100.0);
exportCanvas.height = settings->ThawFloat("ExportCanvas_Height", 100.0);
exportCanvas.dx = settings->ThawFloat("ExportCanvas_Dx", 5.0);
exportCanvas.dy = settings->ThawFloat("ExportCanvas_Dy", 5.0);
// Extra parameters when exporting G code
gCode.depth = settings->ThawFloat("GCode_Depth", 10.0);
gCode.passes = settings->ThawInt("GCode_Passes", 1);
gCode.feed = settings->ThawFloat("GCode_Feed", 10.0);
gCode.plungeFeed = settings->ThawFloat("GCode_PlungeFeed", 10.0);
// Show toolbar in the graphics window
showToolbar = settings->ThawBool("ShowToolbar", true);
// Recent files menus
for(size_t i = 0; i < MAX_RECENT; i++) {
std::string rawPath = settings->ThawString("RecentFile_" + std::to_string(i), "");
if(rawPath.empty()) continue;
recentFiles.push_back(Platform::Path::From(rawPath));
}
// Autosave timer
autosaveInterval = settings->ThawInt("AutosaveInterval", 5);
// Locale
std::string locale = settings->ThawString("Locale", "");
if(!locale.empty()) {
SetLocale(locale);
}
generateAllTimer = Platform::CreateTimer();
generateAllTimer->onTimeout = std::bind(&SolveSpaceUI::GenerateAll, &SS, Generate::DIRTY,
/*andFindFree=*/false, /*genForBBox=*/false);
showTWTimer = Platform::CreateTimer();
showTWTimer->onTimeout = std::bind(&TextWindow::Show, &TW);
autosaveTimer = Platform::CreateTimer();
autosaveTimer->onTimeout = std::bind(&SolveSpaceUI::Autosave, &SS);
// The default styles (colors, line widths, etc.) are also stored in the
// configuration file, but we will automatically load those as we need
// them.
ScheduleAutosave();
NewFile();
AfterNewFile();
if(TW.window && GW.window) {
TW.window->ThawPosition(settings, "TextWindow");
GW.window->ThawPosition(settings, "GraphicsWindow");
TW.window->SetVisible(true);
GW.window->SetVisible(true);
GW.window->Focus();
// Do this once the window is created.
Request3DConnexionEventsForWindow(GW.window);
}
}
bool SolveSpaceUI::LoadAutosaveFor(const Platform::Path &filename) {
Platform::Path autosaveFile = filename.WithExtension(AUTOSAVE_EXT);
FILE *f = OpenFile(autosaveFile, "rb");
if(!f)
return false;
fclose(f);
Platform::MessageDialogRef dialog = CreateMessageDialog(GW.window);
using Platform::MessageDialog;
dialog->SetType(MessageDialog::Type::QUESTION);
dialog->SetTitle(C_("title", "Autosave Available"));
dialog->SetMessage(C_("dialog", "An autosave file is available for this sketch."));
dialog->SetDescription(C_("dialog", "Do you want to load the autosave file instead?"));
dialog->AddButton(C_("button", "&Load autosave"), MessageDialog::Response::YES,
/*isDefault=*/true);
dialog->AddButton(C_("button", "Do&n't Load"), MessageDialog::Response::NO);
// FIXME(async): asyncify this call
if(dialog->RunModal() == MessageDialog::Response::YES) {
unsaved = true;
return LoadFromFile(autosaveFile, /*canCancel=*/true);
}
return false;
}
bool SolveSpaceUI::Load(const Platform::Path &filename) {
bool autosaveLoaded = LoadAutosaveFor(filename);
bool fileLoaded = autosaveLoaded || LoadFromFile(filename, /*canCancel=*/true);
if(fileLoaded) {
saveFile = filename;
AddToRecentList(filename);
} else {
saveFile.Clear();
NewFile();
}
AfterNewFile();
unsaved = autosaveLoaded;
return fileLoaded;
}
void SolveSpaceUI::Exit() {
Platform::SettingsRef settings = Platform::GetSettings();
GW.window->FreezePosition(settings, "GraphicsWindow");
TW.window->FreezePosition(settings, "TextWindow");
// Recent files
for(size_t i = 0; i < MAX_RECENT; i++) {
std::string rawPath;
if(recentFiles.size() > i) {
rawPath = recentFiles[i].raw;
}
settings->FreezeString("RecentFile_" + std::to_string(i), rawPath);
}
// Model colors
for(size_t i = 0; i < MODEL_COLORS; i++)
settings->FreezeColor("ModelColor_" + std::to_string(i), modelColor[i]);
// Light intensities
settings->FreezeFloat("LightIntensity_0", (float)lightIntensity[0]);
settings->FreezeFloat("LightIntensity_1", (float)lightIntensity[1]);
// Light directions
settings->FreezeFloat("LightDir_0_Right", (float)lightDir[0].x);
settings->FreezeFloat("LightDir_0_Up", (float)lightDir[0].y);
settings->FreezeFloat("LightDir_0_Forward", (float)lightDir[0].z);
settings->FreezeFloat("LightDir_1_Right", (float)lightDir[1].x);
settings->FreezeFloat("LightDir_1_Up", (float)lightDir[1].y);
settings->FreezeFloat("LightDir_1_Forward", (float)lightDir[1].z);
// Chord tolerance
settings->FreezeFloat("ChordTolerancePct", (float)chordTol);
// Max pwl segments to generate
settings->FreezeInt("MaxSegments", (uint32_t)maxSegments);
// Export Chord tolerance
settings->FreezeFloat("ExportChordTolerance", (float)exportChordTol);
// Export Max pwl segments to generate
settings->FreezeInt("ExportMaxSegments", (uint32_t)exportMaxSegments);
// View units
settings->FreezeInt("ViewUnits", (uint32_t)viewUnits);
// Number of digits after the decimal point
settings->FreezeInt("AfterDecimalMm", (uint32_t)afterDecimalMm);
settings->FreezeInt("AfterDecimalInch", (uint32_t)afterDecimalInch);
settings->FreezeInt("AfterDecimalDegree", (uint32_t)afterDecimalDegree);
// Camera tangent (determines perspective)
settings->FreezeFloat("CameraTangent", (float)cameraTangent);
// Grid spacing
settings->FreezeFloat("GridSpacing", gridSpacing);
// Export scale
settings->FreezeFloat("ExportScale", exportScale);
// Export offset (cutter radius comp)
settings->FreezeFloat("ExportOffset", exportOffset);
// Rewrite exported colors close to white into black (assuming white bg)
settings->FreezeBool("FixExportColors", fixExportColors);
// Draw back faces of triangles (when mesh is leaky/self-intersecting)
settings->FreezeBool("DrawBackFaces", drawBackFaces);
// Draw closed polygons areas
settings->FreezeBool("ShowContourAreas", showContourAreas);
// Check that contours are closed and not self-intersecting
settings->FreezeBool("CheckClosedContour", checkClosedContour);
// Use turntable mouse navigation
settings->FreezeBool("TurntableNav", turntableNav);
// Enable automatic constrains for lines
settings->FreezeBool("AutomaticLineConstraints", automaticLineConstraints);
// Export shaded triangles in a 2d view
settings->FreezeBool("ExportShadedTriangles", exportShadedTriangles);
// Export pwl curves (instead of exact) always
settings->FreezeBool("ExportPwlCurves", exportPwlCurves);
// Background color on-screen
settings->FreezeColor("BackgroundColor", backgroundColor);
// Whether export canvas size is fixed or derived from bbox
settings->FreezeBool("ExportCanvasSizeAuto", exportCanvasSizeAuto);
// Margins for automatic canvas size
settings->FreezeFloat("ExportMargin_Left", exportMargin.left);
settings->FreezeFloat("ExportMargin_Right", exportMargin.right);
settings->FreezeFloat("ExportMargin_Bottom", exportMargin.bottom);
settings->FreezeFloat("ExportMargin_Top", exportMargin.top);
// Dimensions for fixed canvas size
settings->FreezeFloat("ExportCanvas_Width", exportCanvas.width);
settings->FreezeFloat("ExportCanvas_Height", exportCanvas.height);
settings->FreezeFloat("ExportCanvas_Dx", exportCanvas.dx);
settings->FreezeFloat("ExportCanvas_Dy", exportCanvas.dy);
// Extra parameters when exporting G code
settings->FreezeFloat("GCode_Depth", gCode.depth);
settings->FreezeInt("GCode_Passes", gCode.passes);
settings->FreezeFloat("GCode_Feed", gCode.feed);
settings->FreezeFloat("GCode_PlungeFeed", gCode.plungeFeed);
// Show toolbar in the graphics window
settings->FreezeBool("ShowToolbar", showToolbar);
// Autosave timer
settings->FreezeInt("AutosaveInterval", autosaveInterval);
// And the default styles, colors and line widths and such.
Style::FreezeDefaultStyles(settings);
Platform::ExitGui();
}
void SolveSpaceUI::ScheduleGenerateAll() {
generateAllTimer->RunAfterProcessingEvents();
}
void SolveSpaceUI::ScheduleShowTW() {
showTWTimer->RunAfterProcessingEvents();
}
void SolveSpaceUI::ScheduleAutosave() {
autosaveTimer->RunAfter(autosaveInterval * 60 * 1000);
}
double SolveSpaceUI::MmPerUnit() {
switch(viewUnits) {
case Unit::INCHES: return 25.4;
case Unit::METERS: return 1000.0;
case Unit::MM: return 1.0;
}
return 1.0;
}
const char *SolveSpaceUI::UnitName() {
switch(viewUnits) {
case Unit::INCHES: return "inch";
case Unit::METERS: return "m";
case Unit::MM: return "mm";
}
return "";
}
std::string SolveSpaceUI::MmToString(double v) {
switch(viewUnits) {
case Unit::INCHES: return ssprintf("%.*f", afterDecimalInch, v / 25.4);
case Unit::METERS: return ssprintf("%.*f", afterDecimalMm, v / 1000.0);
case Unit::MM: return ssprintf("%.*f", afterDecimalMm, v);
}
return "";
}
std::string SolveSpaceUI::DegreeToString(double v) {
if(fabs(v - floor(v)) > 1e-10) {
return ssprintf("%.*f", afterDecimalDegree, v);
} else {
return ssprintf("%.0f", v);
}
}
double SolveSpaceUI::ExprToMm(Expr *e) {
return (e->Eval()) * MmPerUnit();
}
double SolveSpaceUI::StringToMm(const std::string &str) {
return std::stod(str) * MmPerUnit();
}
double SolveSpaceUI::ChordTolMm() {
if(exportMode) return ExportChordTolMm();
return chordTolCalculated;
}
double SolveSpaceUI::ExportChordTolMm() {
return exportChordTol / exportScale;
}
int SolveSpaceUI::GetMaxSegments() {
if(exportMode) return exportMaxSegments;
return maxSegments;
}
int SolveSpaceUI::UnitDigitsAfterDecimal() {
return (viewUnits == Unit::INCHES) ? afterDecimalInch : afterDecimalMm;
}
void SolveSpaceUI::SetUnitDigitsAfterDecimal(int v) {
if(viewUnits == Unit::INCHES) {
afterDecimalInch = v;
} else {
afterDecimalMm = v;
}
}
double SolveSpaceUI::CameraTangent() {
if(!usePerspectiveProj) {
return 0;
} else {
return cameraTangent;
}
}
void SolveSpaceUI::AfterNewFile() {
// Clear out the traced point, which is no longer valid
traced.point = Entity::NO_ENTITY;
traced.path.l.Clear();
// and the naked edges
nakedEdges.Clear();
// Quit export mode
justExportedInfo.draw = false;
centerOfMass.draw = false;
exportMode = false;
// GenerateAll() expects the view to be valid, because it uses that to
// fill in default values for extrusion depths etc. (which won't matter
// here, but just don't let it work on garbage)
SS.GW.offset = Vector::From(0, 0, 0);
SS.GW.projRight = Vector::From(1, 0, 0);
SS.GW.projUp = Vector::From(0, 1, 0);
GenerateAll(Generate::ALL);
GW.Init();
TW.Init();
unsaved = false;
GW.ZoomToFit();
// Create all the default styles; they'll get created on the fly anyways,
// but can't hurt to do it now.
Style::CreateAllDefaultStyles();
UpdateWindowTitles();
}
void SolveSpaceUI::AddToRecentList(const Platform::Path &filename) {
auto it = std::find_if(recentFiles.begin(), recentFiles.end(),
[&](const Platform::Path &p) { return p.Equals(filename); });
if(it != recentFiles.end()) {
recentFiles.erase(it);
}
if(recentFiles.size() > MAX_RECENT) {
recentFiles.erase(recentFiles.begin() + MAX_RECENT);
}
recentFiles.insert(recentFiles.begin(), filename);
GW.PopulateRecentFiles();
}
bool SolveSpaceUI::GetFilenameAndSave(bool saveAs) {
Platform::SettingsRef settings = Platform::GetSettings();
Platform::Path newSaveFile = saveFile;
if(saveAs || saveFile.IsEmpty()) {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(GW.window);
dialog->AddFilter(C_("file-type", "SolveSpace models"), { "slvs" });
dialog->ThawChoices(settings, "Sketch");
if(!newSaveFile.IsEmpty()) {
dialog->SetFilename(newSaveFile);
}
if(dialog->RunModal()) {
dialog->FreezeChoices(settings, "Sketch");
newSaveFile = dialog->GetFilename();
} else {
return false;
}
}
if(SaveToFile(newSaveFile)) {
AddToRecentList(newSaveFile);
RemoveAutosave();
saveFile = newSaveFile;
unsaved = false;
return true;
} else {
return false;
}
}
void SolveSpaceUI::Autosave()
{
ScheduleAutosave();
if(!saveFile.IsEmpty() && unsaved) {
SaveToFile(saveFile.WithExtension(AUTOSAVE_EXT));
}
}
void SolveSpaceUI::RemoveAutosave()
{
Platform::Path autosaveFile = saveFile.WithExtension(AUTOSAVE_EXT);
RemoveFile(autosaveFile);
}
bool SolveSpaceUI::OkayToStartNewFile() {
if(!unsaved) return true;
Platform::MessageDialogRef dialog = CreateMessageDialog(GW.window);
using Platform::MessageDialog;
dialog->SetType(MessageDialog::Type::QUESTION);
dialog->SetTitle(C_("title", "Modified File"));
if(!SolveSpace::SS.saveFile.IsEmpty()) {
dialog->SetMessage(ssprintf(C_("dialog", "Do you want to save the changes you made to "
"the sketch “%s”?"), saveFile.raw.c_str()));
} else {
dialog->SetMessage(C_("dialog", "Do you want to save the changes you made to "
"the new sketch?"));
}
dialog->SetDescription(C_("dialog", "Your changes will be lost if you don't save them."));
dialog->AddButton(C_("button", "&Save"), MessageDialog::Response::YES,
/*isDefault=*/true);
dialog->AddButton(C_("button", "Do&n't Save"), MessageDialog::Response::NO);
dialog->AddButton(C_("button", "&Cancel"), MessageDialog::Response::CANCEL);
// FIXME(async): asyncify this call
switch(dialog->RunModal()) {
case MessageDialog::Response::YES:
return GetFilenameAndSave(/*saveAs=*/false);
case MessageDialog::Response::NO:
RemoveAutosave();
return true;
default:
return false;
}
}
void SolveSpaceUI::UpdateWindowTitles() {
if(!GW.window || !TW.window) return;
if(saveFile.IsEmpty()) {
GW.window->SetTitle(C_("title", "(new sketch)"));
} else {
if(!GW.window->SetTitleForFilename(saveFile)) {
GW.window->SetTitle(saveFile.raw);
}
}
TW.window->SetTitle(C_("title", "Property Browser"));
}
void SolveSpaceUI::MenuFile(Command id) {
Platform::SettingsRef settings = Platform::GetSettings();
switch(id) {
case Command::NEW:
if(!SS.OkayToStartNewFile()) break;
SS.saveFile.Clear();
SS.NewFile();
SS.AfterNewFile();
break;
case Command::OPEN: {
if(!SS.OkayToStartNewFile()) break;
Platform::FileDialogRef dialog = Platform::CreateOpenFileDialog(SS.GW.window);
dialog->AddFilters(Platform::SolveSpaceModelFileFilters);
dialog->ThawChoices(settings, "Sketch");
if(dialog->RunModal()) {
dialog->FreezeChoices(settings, "Sketch");
SS.Load(dialog->GetFilename());
}
break;
}
case Command::SAVE:
SS.GetFilenameAndSave(/*saveAs=*/false);
break;
case Command::SAVE_AS:
SS.GetFilenameAndSave(/*saveAs=*/true);
break;
case Command::EXPORT_IMAGE: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::RasterFileFilters);
dialog->ThawChoices(settings, "ExportImage");
if(dialog->RunModal()) {
dialog->FreezeChoices(settings, "ExportImage");
SS.ExportAsPngTo(dialog->GetFilename());
}
break;
}
case Command::EXPORT_VIEW: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::VectorFileFilters);
dialog->ThawChoices(settings, "ExportView");
if(!dialog->RunModal()) break;
dialog->FreezeChoices(settings, "ExportView");
// If the user is exporting something where it would be
// inappropriate to include the constraints, then warn.
if(SS.GW.showConstraints &&
(dialog->GetFilename().HasExtension("txt") ||
fabs(SS.exportOffset) > LENGTH_EPS))
{
Message(_("Constraints are currently shown, and will be exported "
"in the toolpath. This is probably not what you want; "
"hide them by clicking the link at the top of the "
"text window."));
}
SS.ExportViewOrWireframeTo(dialog->GetFilename(), /*exportWireframe=*/false);
break;
}
case Command::EXPORT_WIREFRAME: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::Vector3dFileFilters);
dialog->ThawChoices(settings, "ExportWireframe");
if(!dialog->RunModal()) break;
dialog->FreezeChoices(settings, "ExportWireframe");
SS.ExportViewOrWireframeTo(dialog->GetFilename(), /*exportWireframe*/true);
break;
}
case Command::EXPORT_SECTION: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::VectorFileFilters);
dialog->ThawChoices(settings, "ExportSection");
if(!dialog->RunModal()) break;
dialog->FreezeChoices(settings, "ExportSection");
SS.ExportSectionTo(dialog->GetFilename());
break;
}
case Command::EXPORT_MESH: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::MeshFileFilters);
dialog->ThawChoices(settings, "ExportMesh");
if(!dialog->RunModal()) break;
dialog->FreezeChoices(settings, "ExportMesh");
SS.ExportMeshTo(dialog->GetFilename());
break;
}
case Command::EXPORT_SURFACES: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::SurfaceFileFilters);
dialog->ThawChoices(settings, "ExportSurfaces");
if(!dialog->RunModal()) break;
dialog->FreezeChoices(settings, "ExportSurfaces");
StepFileWriter sfw = {};
sfw.ExportSurfacesTo(dialog->GetFilename());
break;
}
case Command::IMPORT: {
Platform::FileDialogRef dialog = Platform::CreateOpenFileDialog(SS.GW.window);
dialog->AddFilters(Platform::ImportFileFilters);
dialog->ThawChoices(settings, "Import");
if(!dialog->RunModal()) break;
dialog->FreezeChoices(settings, "Import");
Platform::Path importFile = dialog->GetFilename();
if(importFile.HasExtension("dxf")) {
ImportDxf(importFile);
} else if(importFile.HasExtension("dwg")) {
ImportDwg(importFile);
} else {
Error(_("Can't identify file type from file extension of "
"filename '%s'; try .dxf or .dwg."), importFile.raw.c_str());
break;
}
SS.GenerateAll(SolveSpaceUI::Generate::UNTIL_ACTIVE);
SS.ScheduleShowTW();
break;
}
case Command::EXIT:
if(!SS.OkayToStartNewFile()) break;
SS.Exit();
break;
default: ssassert(false, "Unexpected menu ID");
}
SS.UpdateWindowTitles();
}
void SolveSpaceUI::MenuAnalyze(Command id) {
Platform::SettingsRef settings = Platform::GetSettings();
SS.GW.GroupSelection();
auto const &gs = SS.GW.gs;
switch(id) {
case Command::STEP_DIM:
if(gs.constraints == 1 && gs.n == 0) {
Constraint *c = SK.GetConstraint(gs.constraint[0]);
if(c->HasLabel() && !c->reference) {
SS.TW.stepDim.finish = c->valA;
SS.TW.stepDim.steps = 10;
SS.TW.stepDim.isDistance =
(c->type != Constraint::Type::ANGLE) &&
(c->type != Constraint::Type::LENGTH_RATIO) &&
(c->type != Constraint::Type::LENGTH_DIFFERENCE);
SS.TW.shown.constraint = c->h;
SS.TW.shown.screen = TextWindow::Screen::STEP_DIMENSION;
// The step params are specified in the text window,
// so force that to be shown.
SS.GW.ForceTextWindowShown();
SS.ScheduleShowTW();
SS.GW.ClearSelection();
} else {
Error(_("Constraint must have a label, and must not be "
"a reference dimension."));
}
} else {
Error(_("Bad selection for step dimension; select a constraint."));
}
break;
case Command::NAKED_EDGES: {
ShowNakedEdges(/*reportOnlyWhenNotOkay=*/false);
break;
}
case Command::INTERFERENCE: {
SS.nakedEdges.Clear();
SMesh *m = &(SK.GetGroup(SS.GW.activeGroup)->displayMesh);
SKdNode *root = SKdNode::From(m);
bool inters, leaks;
root->MakeCertainEdgesInto(&(SS.nakedEdges),
EdgeKind::SELF_INTER, /*coplanarIsInter=*/false, &inters, &leaks);
SS.GW.Invalidate();
if(inters) {
Error("%d edges interfere with other triangles, bad.",
SS.nakedEdges.l.n);
} else {
Message(_("The assembly does not interfere, good."));
}
break;
}
case Command::CENTER_OF_MASS: {
SS.UpdateCenterOfMass();
SS.centerOfMass.draw = true;
SS.GW.Invalidate();
break;
}
case Command::VOLUME: {
SMesh *m = &(SK.GetGroup(SS.GW.activeGroup)->displayMesh);
double vol = 0;
int i;
for(i = 0; i < m->l.n; i++) {
STriangle tr = m->l.elem[i];
// Translate to place vertex A at (x, y, 0)
Vector trans = Vector::From(tr.a.x, tr.a.y, 0);
tr.a = (tr.a).Minus(trans);
tr.b = (tr.b).Minus(trans);
tr.c = (tr.c).Minus(trans);
// Rotate to place vertex B on the y-axis. Depending on
// whether the triangle is CW or CCW, C is either to the
// right or to the left of the y-axis. This handles the
// sign of our normal.
Vector u = Vector::From(-tr.b.y, tr.b.x, 0);
u = u.WithMagnitude(1);
Vector v = Vector::From(tr.b.x, tr.b.y, 0);
v = v.WithMagnitude(1);
Vector n = Vector::From(0, 0, 1);
tr.a = (tr.a).DotInToCsys(u, v, n);
tr.b = (tr.b).DotInToCsys(u, v, n);
tr.c = (tr.c).DotInToCsys(u, v, n);
n = tr.Normal().WithMagnitude(1);
// Triangles on edge don't contribute
if(fabs(n.z) < LENGTH_EPS) continue;
// The plane has equation p dot n = a dot n
double d = (tr.a).Dot(n);
// nx*x + ny*y + nz*z = d
// nz*z = d - nx*x - ny*y
double A = -n.x/n.z, B = -n.y/n.z, C = d/n.z;
double mac = tr.c.y/tr.c.x, mbc = (tr.c.y - tr.b.y)/tr.c.x;
double xc = tr.c.x, yb = tr.b.y;
// I asked Maple for
// int(int(A*x + B*y +C, y=mac*x..(mbc*x + yb)), x=0..xc);
double integral =
(1.0/3)*(
A*(mbc-mac)+
(1.0/2)*B*(mbc*mbc-mac*mac)
)*(xc*xc*xc)+
(1.0/2)*(A*yb+B*yb*mbc+C*(mbc-mac))*xc*xc+
C*yb*xc+
(1.0/2)*B*yb*yb*xc;
vol += integral;
}
std::string msg = ssprintf(_("The volume of the solid model is:\n\n"
" %.3f %s^3"),
vol / pow(SS.MmPerUnit(), 3),
SS.UnitName());
if(SS.viewUnits == Unit::MM) {
msg += ssprintf("\n %.2f mL", vol/(10*10*10));
}
msg += _("\n\nCurved surfaces have been approximated as triangles.\n"
"This introduces error, typically of around 1%.");
Message("%s", msg.c_str());
break;
}
case Command::AREA: {
Group *g = SK.GetGroup(SS.GW.activeGroup);
if(g->polyError.how != PolyError::GOOD) {
Error(_("This group does not contain a correctly-formed "
"2d closed area. It is open, not coplanar, or self-"
"intersecting."));
break;
}
SEdgeList sel = {};
g->polyLoops.MakeEdgesInto(&sel);
SPolygon sp = {};
sel.AssemblePolygon(&sp, NULL, /*keepDir=*/true);
sp.normal = sp.ComputeNormal();
sp.FixContourDirections();
double area = sp.SignedArea();
double scale = SS.MmPerUnit();
Message(_("The area of the region sketched in this group is:\n\n"
" %.3f %s^2\n\n"
"Curves have been approximated as piecewise linear.\n"
"This introduces error, typically of around 1%%."),
area / (scale*scale),
SS.UnitName());
sel.Clear();
sp.Clear();
break;
}
case Command::PERIMETER: {
if(gs.n > 0 && gs.n == gs.entities) {
double perimeter = 0.0;
for(int i = 0; i < gs.entities; i++) {
Entity *e = SK.entity.FindById(gs.entity[i]);
SEdgeList *el = e->GetOrGenerateEdges();
for(const SEdge &e : el->l) {
perimeter += e.b.Minus(e.a).Magnitude();
}
}
double scale = SS.MmPerUnit();
Message(_("The total length of the selected entities is:\n\n"
" %.3f %s\n\n"
"Curves have been approximated as piecewise linear.\n"
"This introduces error, typically of around 1%%."),
perimeter / scale,
SS.UnitName());
} else {
Error(_("Bad selection for perimeter; select line segments, arcs, and curves."));
}
break;
}
case Command::SHOW_DOF:
// This works like a normal solve, except that it calculates
// which variables are free/bound at the same time.
SS.GenerateAll(SolveSpaceUI::Generate::ALL, /*andFindFree=*/true);
break;
case Command::TRACE_PT:
if(gs.points == 1 && gs.n == 1) {
SS.traced.point = gs.point[0];
SS.GW.ClearSelection();
} else {
Error(_("Bad selection for trace; select a single point."));
}
break;
case Command::STOP_TRACING: {
Platform::FileDialogRef dialog = Platform::CreateSaveFileDialog(SS.GW.window);
dialog->AddFilters(Platform::CsvFileFilters);
dialog->ThawChoices(settings, "Trace");
if(dialog->RunModal()) {
dialog->FreezeChoices(settings, "Trace");
FILE *f = OpenFile(dialog->GetFilename(), "wb");
if(f) {
int i;
SContour *sc = &(SS.traced.path);
for(i = 0; i < sc->l.n; i++) {
Vector p = sc->l.elem[i].p;
double s = SS.exportScale;
fprintf(f, "%.10f, %.10f, %.10f\r\n",
p.x/s, p.y/s, p.z/s);
}
fclose(f);
} else {
Error(_("Couldn't write to '%s'"), dialog->GetFilename().raw.c_str());
}
}
// Clear the trace, and stop tracing
SS.traced.point = Entity::NO_ENTITY;
SS.traced.path.l.Clear();
SS.GW.Invalidate();
break;
}
default: ssassert(false, "Unexpected menu ID");
}
}
void SolveSpaceUI::ShowNakedEdges(bool reportOnlyWhenNotOkay) {
SS.nakedEdges.Clear();
Group *g = SK.GetGroup(SS.GW.activeGroup);
SMesh *m = &(g->displayMesh);
SKdNode *root = SKdNode::From(m);
bool inters, leaks;
root->MakeCertainEdgesInto(&(SS.nakedEdges),
EdgeKind::NAKED_OR_SELF_INTER, /*coplanarIsInter=*/true, &inters, &leaks);
if(reportOnlyWhenNotOkay && !inters && !leaks && SS.nakedEdges.l.n == 0) {
return;
}
SS.GW.Invalidate();
const char *intersMsg = inters ?
_("The mesh is self-intersecting (NOT okay, invalid).") :
_("The mesh is not self-intersecting (okay, valid).");
const char *leaksMsg = leaks ?
_("The mesh has naked edges (NOT okay, invalid).") :
_("The mesh is watertight (okay, valid).");
std::string cntMsg = ssprintf(
_("\n\nThe model contains %d triangles, from %d surfaces."),
g->displayMesh.l.n, g->runningShell.surface.n);
if(SS.nakedEdges.l.n == 0) {
Message(_("%s\n\n%s\n\nZero problematic edges, good.%s"),
intersMsg, leaksMsg, cntMsg.c_str());
} else {
Error(_("%s\n\n%s\n\n%d problematic edges, bad.%s"),
intersMsg, leaksMsg, SS.nakedEdges.l.n, cntMsg.c_str());
}
}
void SolveSpaceUI::MenuHelp(Command id) {
switch(id) {
case Command::WEBSITE:
Platform::OpenInBrowser("http://solvespace.com/helpmenu");
break;
case Command::ABOUT:
Message(_(
"This is SolveSpace version %s.\n"
"\n"
"For more information, see http://solvespace.com/\n"
"\n"
"SolveSpace is free software: you are free to modify\n"
"and/or redistribute it under the terms of the GNU\n"
"General Public License (GPL) version 3 or later.\n"
"\n"
"There is NO WARRANTY, to the extent permitted by\n"
"law. For details, visit http://gnu.org/licenses/\n"
"\n"
"© 2008-%d Jonathan Westhues and other authors.\n"),
PACKAGE_VERSION, 2019);
break;
default: ssassert(false, "Unexpected menu ID");
}
}
void SolveSpaceUI::Clear() {
sys.Clear();
for(int i = 0; i < MAX_UNDO; i++) {
if(i < undo.cnt) undo.d[i].Clear();
if(i < redo.cnt) redo.d[i].Clear();
}
TW.window = NULL;
GW.openRecentMenu = NULL;
GW.linkRecentMenu = NULL;
GW.showGridMenuItem = NULL;
GW.perspectiveProjMenuItem = NULL;
GW.showToolbarMenuItem = NULL;
GW.showTextWndMenuItem = NULL;
GW.fullScreenMenuItem = NULL;
GW.unitsMmMenuItem = NULL;
GW.unitsMetersMenuItem = NULL;
GW.unitsInchesMenuItem = NULL;
GW.inWorkplaneMenuItem = NULL;
GW.in3dMenuItem = NULL;
GW.undoMenuItem = NULL;
GW.redoMenuItem = NULL;
GW.window = NULL;
}
void Sketch::Clear() {
group.Clear();
groupOrder.Clear();
constraint.Clear();
request.Clear();
style.Clear();
entity.Clear();
param.Clear();
}
BBox Sketch::CalculateEntityBBox(bool includingInvisible) {
BBox box = {};
bool first = true;
auto includePoint = [&](const Vector &point) {
if(first) {
box.minp = point;
box.maxp = point;
first = false;
} else {
box.Include(point);