-
Notifications
You must be signed in to change notification settings - Fork 2
/
slicer.c
1087 lines (931 loc) · 38.8 KB
/
slicer.c
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
#include "stdafx.h"
#include "LoftyCAD.h"
#include <stdio.h>
#include <ShlObj.h>
// Slic3r interface.
// Size of a section name.
#define SECT_NAME_SIZE 80
// Max number of key=value pairs in a section. Note that inheritances may make
// this requirement much larger than the final output as dups are culled later.
#define MAX_KEYVALS 512
// Max size of a section in characters, with null-separated key=value pairs.
#define MAX_SECT_SIZE 8192
// Max size of the section name buffer
#define MAX_SECT_NAME_SIZE 32767
// Max number of section in the vendor ini file
#define MAX_SECTIONS 1024
// Inheritable string and section structures.
typedef struct InhString
{
char* key; // Pointer to key=value in section string
char* value; // Pointer to value in section string
BOOL override; // TRUE is this key has been overridden
} InhString;
// Section structure.
typedef struct InhSection
{
char sect_name[SECT_NAME_SIZE]; // The original name of the section from the ini file.
int n_keyvals; // Number of key/value pairs.
double bed_xmin; // The bed size for a printer section
double bed_xmax;
double bed_ymin;
double bed_ymax;
InhString keyval[MAX_KEYVALS]; // Key/value pairs inside the section_string.
char section_string[MAX_SECT_SIZE]; // Null-separated raw string, returned from GetPrivateProfileSection.
} InhSection;
// Slicer and config fixed arrays
#define NUM_SLICER_LOCATIONS 10
#define NUM_CONFIG_LOCATIONS 4
// Location structure used for executables and ini files. Allows marking any location
// as PrusaSlicer, Slic3rPE, or Slic3r to discourage accidentally pointing a slicer at
// an incompatible set of config files.
typedef struct Location
{
char directory[64]; // Subdirectory under ProgramFiles or AppData\Roaming
char target[64]; // The target exe or ini file
SLICER type; // What kind of slicer it is
} Location;
// List of locations to look for Slic3r. If PrusaSlicer is installed, assume that takes priority
// over vanilla slic3r. The leading backslash allows cat'ing with the program files directory.
// Have (x86) versions here too.
// Directory names may have version stuff appended, so search using a wildcard.
Location slic3r_locations[NUM_SLICER_LOCATIONS] =
{
{"\\Prusa3D\\PrusaSlicer", "prusa-slicer-console.exe", SLIC_PRUSA },
{"\\Prusa3D\\Slic3rPE", "slic3r-console.exe", SLIC_SLIC3R},
{"\\Repetier-Host\\Slic3rPE", "prusa-slicer-console.exe", SLIC_PRUSA},
{"\\Repetier-Host\\Slic3r", "slic3r-console.exe", SLIC_SLIC3R},
{"\\Slic3r*", "slic3r-console.exe", SLIC_SLIC3R},
{" (x86)\\Prusa3D\\PrusaSlicer","prusa-slicer-console.exe", SLIC_PRUSA },
{" (x86)\\Prusa3D\\Slic3rPE", "slic3r-console.exe", SLIC_SLIC3R},
{" (x86)\\Repetier-Host\\Slic3rPE", "prusa-slicer-console.exe", SLIC_PRUSA},
{" (x86)\\Repetier-Host\\Slic3r", "slic3r-console.exe", SLIC_SLIC3R},
{" (x86)\\Slic3r*", "slic3r-console.exe", SLIC_SLIC3R}
};
// List of possible locations for config files under Application Data. No wildcards.
// Some Prusa installs (e.g. Repetier) use PrusaSlicer.ini under the old directory name.
Location config_locations[NUM_CONFIG_LOCATIONS] =
{
{"\\PrusaSlicer", "PrusaSlicer.ini", SLIC_PRUSA },
{"\\Slic3rPE", "PrusaSlicer.ini", SLIC_PRUSA },
{"\\Slic3rPE", "slic3r.ini", SLIC_SLIC3R},
{"\\Slic3r", "slic3r.ini", SLIC_SLIC3R}
};
// Command sets (a sprintf string that takes the bed centre as two doubles)
// Indexed by slicer type
char slicer_cmd[MAX_TYPES][80] =
{
" --print-center %d,%d ", // For vanilla slic3r and slic3rPE
" -g --center %d,%d " // For PrusaSlicer
};
// Arrays of exe and config names, and the index to the current one of each.
SlicerExe slicer_exe[MAX_SLICERS];
SlicerConfig slicer_config[MAX_SLICERS];
int slicer_index = 0;
int config_index = 0;
int num_slicers = 0;
int num_configs = 0;
// Currently selected printer model.
char current_model[64];
// Section names in PrusaResearch.ini
char sect_names[MAX_SECT_NAME_SIZE];
// Cache of inheritable sections already loaded.
int n_cache = 0;
InhSection* cache[MAX_SECTIONS];
// Vendor name
#define VENDOR "PrusaResearch"
// Vendor file (currently the only one known and supported)
#define VENDOR_INI_FILE "\\vendor\\PrusaResearch.ini"
// Preferences for printer connection
// What sort of connection
BOOL print_octo = FALSE; // TRUE for Octoprint, FALSE for serial
// Currently selected printer serial port (without colon: "COM10")
char printer_port[64] = "\0";
// If true, serial connections will send and check line numbers
BOOL print_serial_lineno = FALSE;
// The serial connection's baud rate. Zero if it has not been tested for yet
int print_serial_baud = 0;
// Octoprint server name (e.g. "octopi.local")
char octoprint_server[128] = "\0";
// Octoprint server API key
char octoprint_apikey[128] = "\0";
// If TRUE, user is prompted for a G-code filename. Otherwise, the slicer generates one.
BOOL explicit_gcode = TRUE;
// Load Slic3r executable and config directories from the reg. Return FALSE if we don't have an exe
// and leave the fields blank.
BOOL
load_slic3r_exe_and_config()
{
HKEY hkey;
char str[MAX_PATH];
char location[32];
int len, i;
BOOL rc = FALSE;
RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\LoftyCAD\\Slic3r", 0, NULL, 0, KEY_READ, NULL, &hkey, NULL);
num_slicers = 0;
num_configs = 0;
slicer_index = 0;
config_index = 0;
slicer_exe[0].exe[0] = '\0';
slicer_config[0].dir[0] = '\0';
for (i = 0; i < MAX_SLICERS; i++)
{
sprintf_s(location, 32, "Location%d", i);
len = MAX_PATH;
if (RegQueryValueEx(hkey, location, 0, NULL, str, &len) != ERROR_SUCCESS)
break;
rc = TRUE;
strcpy_s(slicer_exe[i].exe, MAX_PATH, str);
num_slicers++;
}
// Haven't got anything stored - try and find some.
if (!rc)
{
RegCloseKey(hkey);
return find_slic3r_exe_and_config();
}
// Look for configs and the indices
for (i = 0; i < MAX_SLICERS; i++)
{
sprintf_s(location, 32, "ConfigDir%d", i);
len = MAX_PATH;
if (RegQueryValueEx(hkey, location, 0, NULL, str, &len) != ERROR_SUCCESS)
break;
strcpy_s(slicer_config[i].dir, MAX_PATH, str);
sprintf_s(location, 32, "ConfigIni%d", i);
len = MAX_PATH;
if (RegQueryValueEx(hkey, location, 0, NULL, str, &len) != ERROR_SUCCESS)
break;
strcpy_s(slicer_config[i].ini, MAX_PATH, str);
sprintf_s(location, 32, "ConfigType%d", i);
len = 4;
if (RegQueryValueEx(hkey, location, 0, NULL, (LPBYTE)&slicer_config[i].type, &len) != ERROR_SUCCESS)
break;
num_configs++;
}
// Read the indices (they are DWORDs rather than strings)
len = 4;
RegQueryValueEx(hkey, "SlicerIndex", 0, NULL, (LPBYTE)&slicer_index, &len);
len = 4;
RegQueryValueEx(hkey, "ConfigIndex", 0, NULL, (LPBYTE)&config_index, &len);
// Printer connection
len = 4;
RegQueryValueEx(hkey, "PrintOcto", 0, NULL, (LPBYTE)&print_octo, &len);
len = 64;
RegQueryValueEx(hkey, "PrinterPort", 0, NULL, (LPBYTE)printer_port, &len);
len = 4;
RegQueryValueEx(hkey, "PrintSerialBaud", 0, NULL, (LPBYTE)&print_serial_baud, &len);
len = 128;
RegQueryValueEx(hkey, "OctoServer", 0, NULL, (LPBYTE)octoprint_server, &len);
len = 128;
RegQueryValueEx(hkey, "OctoAPIKey", 0, NULL, (LPBYTE)octoprint_apikey, &len);
RegCloseKey(hkey);
return TRUE;
}
// Save slicer stuff to registry.
void
save_slic3r_exe_and_config()
{
int i;
char location[32];
HKEY hkey;
RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\LoftyCAD\\Slic3r", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
for (i = 0; i < num_slicers; i++)
{
sprintf_s(location, 32, "Location%d", i);
RegSetValueEx(hkey, location, 0, REG_SZ, slicer_exe[i].exe, strlen(slicer_exe[i].exe) + 1);
}
for (; i < MAX_SLICERS; i++) // clear out any old ones beyond the num
{
sprintf_s(location, 32, "Location%d", i);
RegDeleteKeyValue(hkey, NULL, location);
}
for (i = 0; i < num_configs; i++)
{
sprintf_s(location, 32, "ConfigDir%d", i);
RegSetValueEx(hkey, location, 0, REG_SZ, slicer_config[i].dir, strlen(slicer_config[i].dir) + 1);
sprintf_s(location, 32, "ConfigIni%d", i);
RegSetValueEx(hkey, location, 0, REG_SZ, slicer_config[i].ini, strlen(slicer_config[i].ini) + 1);
sprintf_s(location, 32, "ConfigType%d", i);
RegSetValueEx(hkey, location, 0, REG_DWORD, (PBYTE)&slicer_config[i].type, 4);
}
for (; i < MAX_SLICERS; i++) // clear out any old ones beyond the num
{
sprintf_s(location, 32, "ConfigDir%d", i);
RegDeleteKeyValue(hkey, NULL, location);
sprintf_s(location, 32, "ConfigIni%d", i);
RegDeleteKeyValue(hkey, NULL, location);
sprintf_s(location, 32, "ConfigType%d", i);
RegDeleteKeyValue(hkey, NULL, location);
}
// Save the slicer index and config index.
RegSetValueEx(hkey, "SlicerIndex", 0, REG_DWORD, (LPBYTE)&slicer_index, 4);
RegSetValueEx(hkey, "ConfigIndex", 0, REG_DWORD, (LPBYTE)&config_index, 4);
// Printer connection
RegSetValueEx(hkey, "PrintOcto", 0, REG_DWORD, (LPBYTE)&print_octo, 4);
RegSetValueEx(hkey, "PrinterPort", 0, REG_SZ, (PBYTE)printer_port, strlen(printer_port) + 1);
RegSetValueEx(hkey, "PrintSerialBaud", 0, REG_DWORD, (LPBYTE)&print_serial_baud, 4);
RegSetValueEx(hkey, "OctoServer", 0, REG_SZ, (PBYTE)octoprint_server, strlen(octoprint_server) + 1);
RegSetValueEx(hkey, "OctoAPIKey", 0, REG_SZ, (PBYTE)octoprint_apikey, strlen(octoprint_apikey) + 1);
RegCloseKey(hkey);
// Clear the section cache, in case slicer has changed.
// TODO: Free the cached entries, or else re-use them (they are all the same size)
n_cache = 0;
}
// Save the printer configuration.
void
save_printer_config()
{
HKEY hkey;
RegCreateKeyEx(HKEY_CURRENT_USER, "Software\\LoftyCAD\\Slic3r", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
RegSetValueEx(hkey, "PrintOcto", 0, REG_DWORD, (LPBYTE)&print_octo, 4);
RegSetValueEx(hkey, "PrinterPort", 0, REG_SZ, (PBYTE)printer_port, strlen(printer_port) + 1);
RegSetValueEx(hkey, "PrintSerialBaud", 0, REG_DWORD, (LPBYTE)&print_serial_baud, 4);
RegSetValueEx(hkey, "OctoServer", 0, REG_SZ, (PBYTE)octoprint_server, strlen(octoprint_server) + 1);
RegSetValueEx(hkey, "OctoAPIKey", 0, REG_SZ, (PBYTE)octoprint_apikey, strlen(octoprint_apikey) + 1);
RegCloseKey(hkey);
}
// Find Slic3r executable and config directories in standard places (for Slic3r and PrusaSlicer).
// Return FALSE if an exe was not found.
// exe - could be anywhere, but look in list of locations under Program Files.
// config - look in <user>\AppData\Roaming\Slic3r[PE]
BOOL
find_slic3r_exe_and_config()
{
char progfiles[MAX_PATH];
char appdata[MAX_PATH];
char location[MAX_PATH];
char filename[MAX_PATH];
int i, len;
char* slosh;
HANDLE h, f;
WIN32_FIND_DATA find_data;
BOOL rc = FALSE;
num_slicers = 0;
num_configs = 0;
slicer_index = 0;
config_index = 0;
// Get the Program Files folder name.
SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, progfiles);
// Slic3r locations may be in Program Files, or Program Files (x86).
// For some reason (I am a 32-bit program maybe?) the above call may return
// the (x86) folder even if it's running on a 64-bit system. Strip this if present.
// In addition, the Slic3r directory name may have version stuff appended.
len = strlen(progfiles);
if (_strcmpi(&progfiles[len - 6], " (x86)") == 0)
progfiles[len - 6] = '\0';
for (i = 0; i < NUM_SLICER_LOCATIONS; i++)
{
strcpy_s(location, MAX_PATH, progfiles);
strcat_s(location, MAX_PATH, slic3r_locations[i].directory);
len = strlen(location);
h = FindFirstFile(location, &find_data);
if (h == INVALID_HANDLE_VALUE)
continue;
if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue;
while (1)
{
strcpy_s(filename, MAX_PATH, location);
// Replace the directory wildcard with its real name and look for slic3r-console.exe
slosh = strrchr(filename, '\\');
*(slosh + 1) = '\0';
strcat_s(filename, MAX_PATH, find_data.cFileName);
strcat_s(filename, MAX_PATH, "\\");
strcat_s(filename, MAX_PATH, slic3r_locations[i].target);
f = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (f != INVALID_HANDLE_VALUE)
{
// Found one - store it away filename and all.
strcpy_s(slicer_exe[num_slicers++].exe, MAX_PATH, filename);
rc = TRUE;
CloseHandle(f);
}
if (FindNextFile(h, &find_data) == 0)
break;
}
FindClose(h);
}
// Get the Application Data folder name for the current user.
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata);
for (i = 0; i < NUM_CONFIG_LOCATIONS; i++)
{
strcpy_s(location, MAX_PATH, appdata);
strcat_s(location, MAX_PATH, config_locations[i].directory);
// slic3r.ini must be present.
strcpy_s(filename, MAX_PATH, location);
strcat_s(filename, MAX_PATH, "\\");
strcat_s(filename, MAX_PATH, config_locations[i].target);
f = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (f != INVALID_HANDLE_VALUE)
{
// Found it. The directory and target ini file are stored separately.
slicer_config[num_configs].type = config_locations[i].type;
strcpy_s(slicer_config[num_configs].ini, MAX_PATH, config_locations[i].target);
strcpy_s(slicer_config[num_configs++].dir, MAX_PATH, location);
CloseHandle(f);
}
}
// Put the stuff we have found into the registry. Assume the selected index is 0 for now.
save_slic3r_exe_and_config();
return rc;
}
// Helper to find a key in an InhSection and return its value as a pointer.
char *
find_string_in_section(char* key, InhSection* s)
{
int i;
char* e;
char ckey[SECT_NAME_SIZE];
for (i = 0; i < s->n_keyvals; i++)
{
if (s->keyval[i].override)
continue;
e = strchr(s->keyval[i].key, '=');
if (e == NULL)
continue;
strncpy_s(ckey, SECT_NAME_SIZE, s->keyval[i].key, e - s->keyval[i].key);
if (strcmp(key, ckey) == 0)
return s->keyval[i].value;
}
return NULL;
}
// Helper to test keys match. The actual strings are key=value, but we test up to the '='.
// We are not allowed to poke '\0' in.
BOOL
keys_match(char* kv1, char* kv2)
{
char* e1 = strchr(kv1, '=');
char* e2 = strchr(kv2, '=');
if (e1 == NULL || e2 == NULL) // Check no '=' signs
return FALSE;
if (e1 - kv1 != e2 - kv2) // Check lengths match
return FALSE;
return strncmp(kv1, kv2, e1 - kv1) == 0;
}
// Pull apart the long string returned by GetPrivateProfileSection on "key:section"
// Returns pointers to NULL-separated key = value pairs, as well as pointers to the values (after '=')
// in an InhSection structure allocated here.
InhSection *
load_section(char *ini, char *key, char* sect)
{
char* p;
char* v;
int keyval_len, len;
int i, j;
int n = 0;
InhSection* s;
char section[SECT_NAME_SIZE];
#ifdef DEBUG_WRITE_INI_SECTION
char dbgini[MAX_PATH];
FILE* f;
#endif // DEBUG_WRITE_INI_SECTION
// Section is key:sect
strcpy_s(section, SECT_NAME_SIZE, key);
strcat_s(section, SECT_NAME_SIZE, ":");
strcat_s(section, SECT_NAME_SIZE, sect);
// See if the section has already been loaded.
for (i = 0; i < n_cache; i++)
{
if (strcmp(section, cache[i]->sect_name) == 0)
return cache[i];
}
#ifdef DEBUG_WRITE_INI_SECTION
// Get rid of * from sect
strcpy_s(dbgini, MAX_PATH, key);
strcat_s(dbgini, MAX_PATH, "_");
strcat_s(dbgini, MAX_PATH, sect);
for (p = dbgini; *p != '\0'; p++)
{
if (*p == '*')
*p = '_';
}
fopen_s(&f, dbgini, "wt");
#endif // DEBUG_WRITE_INI_SECTION
// No, make a new one and eventually put it in the cache.
s = calloc(sizeof(InhSection), 1);
strcpy_s(s->sect_name, 80, section);
len = GetPrivateProfileSection(section, s->section_string, MAX_SECT_SIZE, ini);
// section: key = value\0key = value\0 ... \0\0
for (p = s->section_string; *p != '\0'; p += keyval_len + 1)
{
keyval_len = strlen(p);
s->keyval[n].key = p;
v = strchr(p, '=');
if (v == NULL)
s->keyval[n].value = NULL; // no '=' found. Should never happen.
else
{
do { v++; } while (*v == ' '); // skip blanks
s->keyval[n].value = v;
}
// Follow inheritance links. This keyword always occurs first.
// It may take several sect names separated by ';'.
if (strncmp(s->keyval[n].key, "inherits", 8) == 0)
{
char* ctxt = NULL;
char* inh = strtok_s(s->keyval[n].value, ";", &ctxt);
while (inh != NULL)
{
InhSection* inhs = load_section(ini, key, inh);
// Copy the new section's key/value pairs into the current one,
// checking for overrides if there is more than one inherited section.
for (i = 0; i < inhs->n_keyvals; i++)
{
// Copy the pointers. They point to the string in the other InhSection,
// but that's OK, as we will never free it anyway (it will sit in the cache).
// This copy may be set to override but the original must be left alone.
s->keyval[n] = inhs->keyval[i];
// Check if it conflicts with any existing ones.
// Warning: potential n-squared loop!
for (j = 0; j < n; j++)
{
if (keys_match(s->keyval[j].key, s->keyval[n].key))
s->keyval[j].override = TRUE;
}
n++;
}
// Copy any bed size inherited from the section we just read
s->bed_xmin = inhs->bed_xmin;
s->bed_xmax = inhs->bed_xmax;
s->bed_ymin = inhs->bed_ymin;
s->bed_ymax = inhs->bed_ymax;
// See if there are any more sections to be inherited from.
inh = strtok_s(NULL, ";", &ctxt);
}
}
else
{
// Check if the key is overriding an earlier one.
// Warning: potential n-squared loop!
for (j = 0; j < n; j++)
{
if (keys_match(s->keyval[j].key, s->keyval[n].key))
s->keyval[j].override = TRUE;
}
// Extract bed min/max from the section if it exists.
if (strncmp(s->keyval[n].key, "bed_shape", 9) == 0)
{
char* nexttok = NULL;
char* tok;
char bed_str[256];
strcpy_s(bed_str, 256, s->keyval[n].value);
tok = strtok_s(bed_str, "x, \t\n", &nexttok); // 0x0
s->bed_xmin = (double)atof(tok);
tok = strtok_s(NULL, "x, \t\n", &nexttok);
s->bed_ymin = (double)atof(tok);
tok = strtok_s(NULL, ", \t\n", &nexttok); // 250x0 (skip)
tok = strtok_s(NULL, "x, \t\n", &nexttok); // 250x210
s->bed_xmax = (double)atof(tok);
tok = strtok_s(NULL, "x, \t\n", &nexttok);
s->bed_ymax = (double)atof(tok);
}
n++;
}
}
s->n_keyvals = n;
#ifdef DEBUG_WRITE_INI_SECTION
for (i = 0; i < s->n_keyvals; i++)
fprintf(f, "%s%s\n", s->keyval[i].override ? "## " : "", s->keyval[i].key);
fclose(f);
#endif // DEBUG_WRITE_INI_SECTION
if (n_cache < MAX_SECTIONS - 1)
cache[n_cache++] = s;
return s;
}
// Set the bed shape for a printer
void
set_bed_shape(char* printer)
{
InhSection* s;
char vendor[MAX_PATH];
strcpy_s(vendor, MAX_PATH, slicer_config[config_index].dir);
strcat_s(vendor, MAX_PATH, VENDOR_INI_FILE);
s = load_section(vendor, "printer", printer);
if (s->bed_xmax > s->bed_xmin)
{
bed_xmin = s->bed_xmin;
bed_xmax = s->bed_xmax;
bed_ymin = s->bed_ymin;
bed_ymax = s->bed_ymax;
}
}
// Load a string from a section and put its value into the corresponding dialog item.
BOOL
read_string_and_load_dlgitem(char* sect, char* string, int dlg_item, BOOL checkbox)
{
InhSection* s;
char* val;
char vendor[MAX_PATH];
strcpy_s(vendor, MAX_PATH, slicer_config[config_index].dir);
strcat_s(vendor, MAX_PATH, VENDOR_INI_FILE);
s = load_section(vendor, "print", sect);
val = find_string_in_section(string, s);
if (val == NULL)
return FALSE;
// Set up the value in the edit box
if (checkbox) // string is "0" or "1"
CheckDlgButton(hWndSlicer, dlg_item, string[0] == '1' ? BST_CHECKED : BST_UNCHECKED);
else
SendDlgItemMessage(hWndSlicer, dlg_item, WM_SETTEXT, 0, (LPARAM)val);
// Mark the edit box as having an unchanged value
SetWindowLong(GetDlgItem(hWndSlicer, dlg_item), GWL_USERDATA, 0);
return TRUE;
}
// Read Slic3r configuration and populate allowable printers, print settings and filament settings
// according to the given key (printer, print or filament). The settings are loaded into the
// given combo box item in hwndSlicer.
// If a [vendor] section exists in slic3r.ini (e.g. PrusaResearch.ini) go from those model(s)
// and establish the applicable settings. For vanilla Slic3r, just enumerate the user preset ini files.
// Sel_printer is written for printer model. (note: not the same as the displayed name)
void
read_slic3r_config(char* key, int dlg_item, char *sel_printer)
{
char dir[MAX_PATH], ini[MAX_PATH], vendor[MAX_PATH], filename[MAX_PATH];
InhSection* vs, *s;
char name[64];
char buf[256];
char* ctxt = NULL;
HANDLE h;
FILE* f;
WIN32_FIND_DATA find_data;
int len, i, n;
char* p, *v, *model;
char key_colon[64];
int klen;
char model_str[SECT_NAME_SIZE];
char bed_str[256];
char* e;
// Clear the combo
SendDlgItemMessage(hWndSlicer, dlg_item, CB_RESETCONTENT, 0, 0);
// Look for user preset(s) in complete files, of the form <ConfigDir>\<key>\<preset>.ini
strcpy_s(dir, MAX_PATH, slicer_config[config_index].dir);
strcat_s(dir, MAX_PATH, "\\");
strcat_s(dir, MAX_PATH, key);
strcpy_s(filename, MAX_PATH, dir);
strcat_s(filename, MAX_PATH, "\\*.ini");
h = FindFirstFile(filename, &find_data);
if (h != INVALID_HANDLE_VALUE)
{
while (1)
{
strcpy_s(filename, MAX_PATH, find_data.cFileName);
len = strlen(filename);
filename[len - 4] = '\0'; // strip ".ini"
SendDlgItemMessage(hWndSlicer, dlg_item, CB_ADDSTRING, 0, (LPARAM)filename);
// Create a InhSection struct for this section so it can be loaded later
s = calloc(sizeof(InhSection), 1);
// Section name is key:sect
strcpy_s(s->sect_name, SECT_NAME_SIZE, key);
strcat_s(s->sect_name, SECT_NAME_SIZE, ":");
strcat_s(s->sect_name, SECT_NAME_SIZE, filename);
// Open file and read the key=value pairs
strcpy_s(filename, MAX_PATH, dir);
strcat_s(filename, MAX_PATH, "\\");
strcat_s(filename, MAX_PATH, find_data.cFileName);
fopen_s(&f, filename, "rt");
if (f != NULL)
{
// Pack all the key=value pairs into a long string
p = s->section_string;
n = 0;
while (1)
{
if (fgets(p, 2048, f) == NULL) // these can be very long
break;
len = strlen(p);
s->keyval[n].key = p;
v = strchr(p, '=');
if (v == NULL)
s->keyval[n].value = NULL; // no '=' found. Should never happen.
else
{
do { v++; } while (*v == ' '); // skip blanks
s->keyval[n].value = v;
}
// Extract bed min/max from the section if it exists.
if (strncmp(s->keyval[n].key, "bed_shape", 9) == 0)
{
char* nexttok = NULL;
char* tok;
strcpy_s(bed_str, 256, s->keyval[n].value);
tok = strtok_s(bed_str, "x, \t\n", &nexttok); // 0x0
s->bed_xmin = (double)atof(tok);
tok = strtok_s(NULL, "x, \t\n", &nexttok);
s->bed_ymin = (double)atof(tok);
tok = strtok_s(NULL, ", \t\n", &nexttok); // 250x0 (skip)
tok = strtok_s(NULL, "x, \t\n", &nexttok); // 250x210
s->bed_xmax = (double)atof(tok);
tok = strtok_s(NULL, "x, \t\n", &nexttok);
s->bed_ymax = (double)atof(tok);
}
n++;
p += len;
*(p - 1) = '\0'; // overwrite the '\n' left by fgets
}
fclose(f);
s->n_keyvals = n;
// Cache the section
if (n_cache < MAX_SECTIONS - 1)
cache[n_cache++] = s;
}
// Go back for more files
if (!FindNextFile(h, &find_data))
break;
}
FindClose(h);
}
// Find slic3r.ini (or PrusaSlicer.ini, or whatever it is)
strcpy_s(dir, MAX_PATH, slicer_config[config_index].dir);
strcpy_s(ini, MAX_PATH, dir);
strcat_s(ini, MAX_PATH, "\\");
strcat_s(ini, MAX_PATH, slicer_config[config_index].ini);
// Find possible vendor file
strcpy_s(vendor, MAX_PATH, slicer_config[config_index].dir);
strcat_s(vendor, MAX_PATH, VENDOR_INI_FILE);
// Look for printer, print or filament presets. Printer must be called first.
if (strcmp(key, "printer") == 0)
{
// Read slic3r.ini and get the slicer name: # generated by XXXX on ....
fopen_s(&f, ini, "rt");
if (f == NULL)
return; // no slic3r.ini, can't do much more
fgets(buf, 256, f);
if (buf[0] == '#') // must be a comment
{
char* by = strstr(buf, " by ");
char* on = strstr(buf, " on ");
if (by != NULL && on != NULL)
{
*on = '\0';
SendDlgItemMessage(hWndSlicer, IDC_STATIC_SLICER, WM_SETTEXT, 0, (LPARAM)(by + 4));
}
fclose(f);
}
// Look for [vendor:PrusaResearch] in slic3r.ini to find all the installed printer models
vs = load_section(ini, "vendor", VENDOR);
if (vs->n_keyvals != 0)
{
// Get the names of all the sections so we can later filter them out by key (print or filament)
GetPrivateProfileSectionNames(sect_names, MAX_SECT_NAME_SIZE, vendor);
}
else
{
// There's no vendor file, so clear out the list
sect_names[0] = '\0';
}
// Filter out the section names by key (printer:xxxx)
strcpy_s(key_colon, 64, key);
strcat_s(key_colon, 64, ":");
klen = strlen(key_colon);
for (p = sect_names; *p != '\0'; p += len + 1)
{
len = strlen(p);
// key:name --> name, and skip the ones with '*' at front
if (strncmp(key_colon, p, klen) == 0 && *(p + klen) != '*')
{
// Load the section so we can get the printer_model string (it may be buried
// in an inherited section)
s = load_section(vendor, key, p + klen);
// Check printer_model against vs
model = find_string_in_section("printer_model", s);
for (i = 0; i < vs->n_keyvals; i++)
{
// We just want the model:xxxx key (not its value).
strcpy_s(model_str, SECT_NAME_SIZE, vs->keyval[i].key);
if ((e = strchr(model_str, '=')) != NULL)
*e = '\0';
if (strcmp(&model_str[6], model) == 0) // skip "model:"
{
// Add the name to the list
SendDlgItemMessage(hWndSlicer, dlg_item, CB_ADDSTRING, 0, (LPARAM)(p + klen));
break;
}
}
}
}
// Select the preset given in slic3r.ini (the last one worked on in the Slic3r GUI)
GetPrivateProfileString("presets", key, "", name, 64, ini);
i = SendDlgItemMessage(hWndSlicer, dlg_item, CB_FINDSTRINGEXACT, -1, (LPARAM)name);
sel_printer[0] = '\0';
if (i != CB_ERR)
{
SendDlgItemMessage(hWndSlicer, dlg_item, CB_SETCURSEL, i, 0);
// Return the selected printer name
strcpy_s(sel_printer, 64, name);
// Set the bed shape
set_bed_shape(name);
}
}
else // print or filament.
{
// Build a PRINTER_MODEL_XXX string for the selected printer
// model will be NULL if there is no vendor file.
s = load_section(vendor, "printer", sel_printer);
model = find_string_in_section("printer_model", s);
strcpy_s(model_str, SECT_NAME_SIZE, "PRINTER_MODEL_");
if (model != NULL)
strcat_s(model_str, SECT_NAME_SIZE, model);
// Filter out the section names by key (print:xxxx or filament:xxxx)
strcpy_s(key_colon, 64, key);
strcat_s(key_colon, 64, ":");
klen = strlen(key_colon);
for (p = sect_names; *p != '\0'; p += len + 1)
{
len = strlen(p);
// key:name --> name, and skip the ones with '*' at front
if (strncmp(key_colon, p, klen) == 0 && *(p + klen) != '*')
{
char compat[1024];
// Check compatibility only for print (not filament).
// Don't load the section yet (too many unused sections will be loaded).
// Instead, just find the compat string which is assumed to be at top level
// and do a quick and dirty substring match for the PRINTER_MODEL_XXX string.
if (strcmp(key, "print") == 0 && model != NULL)
{
GetPrivateProfileString(p, "compatible_printers_condition", "", compat, 1024, vendor);
if (strstr(compat, model_str) == NULL)
continue;
}
// Add the name to the list
SendDlgItemMessage(hWndSlicer, dlg_item, CB_ADDSTRING, 0, (LPARAM)(p + klen));
}
}
// Select the preset given in slic3r.ini (the last one worked on in the Slic3r GUI)
GetPrivateProfileString("presets", key, "", name, 64, ini);
i = SendDlgItemMessage(hWndSlicer, dlg_item, CB_FINDSTRINGEXACT, -1, (LPARAM)name);
if (i != CB_ERR)
SendDlgItemMessage(hWndSlicer, dlg_item, CB_SETCURSEL, i, 0);
}
}
// Build one of a set of ini files to load to Slic3r command line.
// Given a preset key (printer, print or filament) as found in [presets] in slic3r.ini :
// - find any user-created settings under the config directory and return those
// - if not found, find the section in the [vendor] ini file (if any), following chains of
// inheritance and overriding individual settings as needed.
// Write to the given ini file to be given to the slic3r command.
// FALSE is returned if nothing was found.
BOOL
get_slic3r_config_section(char* key, char* preset, char *inifile)
{
InhSection* s;
FILE* ini;
char vendor[MAX_PATH];
int i;
// Find possible vendor file and load the section.
// (User preset sections will always be in the cache, and will ignore the filename)
strcpy_s(vendor, MAX_PATH, slicer_config[config_index].dir);
strcat_s(vendor, MAX_PATH, VENDOR_INI_FILE);
s = load_section(vendor, key, preset);
if (s == NULL || s->n_keyvals == 0)
return FALSE;
fopen_s(&ini, inifile, "wt");
if (ini == NULL)
return FALSE;
fprintf_s(ini, "# %s:%s\n", key, preset);
for (i = 0; i < s->n_keyvals; i++)
{
// Clean out comments and overridden settings
if (!isalpha(s->keyval[i].key[0]))
continue;
if (s->keyval[i].override)
continue;
fprintf_s(ini, "%s\n", s->keyval[i].key);
}
fclose(ini);
return TRUE;
}
#define BUFSIZE 4096
#define PATH_STR "Using an 8.3 path"
#define OUTPUT_FILE_STR "Exporting G-code to "
// Run the slicer and capture its output. (Courtesy of MS process-read-from-pipe example)
BOOL
run_slicer(char* slicer_exe, char* cmd_line, char* dir, char* gcode_filename)
{
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;
SECURITY_ATTRIBUTES saAttr;
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
BOOL bSuccess = FALSE;
DWORD dwRead;
CHAR chBuf[BUFSIZE];
char line[256], out_filename[MAX_PATH];
char* bufptr, * bufend, *endline;
BOOL have_output = FALSE;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create a pipe for the child process's STDOUT.
if (!CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0))
return FALSE;
// Ensure the read handle to the pipe for STDOUT is not inherited.
if (!SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
return FALSE;
// Set up members of the PROCESS_INFORMATION structure.
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));
// Set up members of the STARTUPINFO structure.
// This structure specifies the STDIN and STDOUT handles for redirection.
// We only need stdout/err.
// Keep console window hidden (progress and status will show instead)
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.hStdError = g_hChildStd_OUT_Wr;
siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
siStartInfo.hStdInput = NULL;
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
// Create the child process. Log stuff.
Log(slicer_exe);
Log("\r\n");
Log(cmd_line);
Log("\r\n");
bSuccess = CreateProcess
(
slicer_exe, // application name
cmd_line, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
dir, // current directory