-
Notifications
You must be signed in to change notification settings - Fork 13
/
pcb-gcode.ulp
1746 lines (1540 loc) · 44.7 KB
/
pcb-gcode.ulp
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
//
// Generate g-code for milling PC boards.
//
// Copyright 2004-2014 by John Johnson Software, LLC.
// See readme.html for copyright information.
//
// See pcb-defaults.h, gcode-defaults.h and pcb-machine.h for options.
//
#include "source/pcb-gcode.h"
#include "source/pcb-gcode-stack.h"
#include "settings/pcb-defaults.h"
#include "settings/pcb-machine.h"
#include "settings/gcode-defaults.h"
#include "settings/pcb-gcode-options.h"
#include "settings/user-gcode.h"
#include "source/math.h"
#include "source/drill.h"
#include "source/library.h"
#include "source/pcb-file-utils.h"
#include "source/filename_subs.h"
#usage "<center><b>pcb-gcode™ Gcode Generation Utility</b><br>"
" Version 3.6.6<p>"
"Copyright© 2003 - 2022 by John Johnson Software, LLC<br>"
"All Rights Reserved</center>"
"<p>"
"Join the Groups pcb-gcode group "
"<a href=https://groups.io/g/pcbgcode>https://groups.io/g/pcbgcode</a>"
"<br>or contact the author at <a href=mailto:[email protected]>[email protected]</a>"
"<br>"
"<hr>"
"This program generates g-code for 'mechanically etching' PC boards. "
"Using a CNC router or milling machine, you can make PC boards without using etching chemicals.<p />"
"It will create files for the outlines of tracks, drilling holes, and milling cutouts in the board."
"<br>See the <a href=file://readme.html>readme.html</a> file for more info.<p>"
"There are <em>many</em> options in the setup program.<br />"
"To setup which files are generated, and how, use the following command:<br>"
"<code>run pcb-gcode --setup</code><p>"
"<p/>"
"If you want to produce a set of files without changing options, you can run pcb-gcode directly."
"Usage is as follows:<br>"
"<code>run pcb-gcode [option] [file]</code><br>"
"Where:\n"
"<table>"
"<tr><td><em>Option</em></td><td><em>Function</em></td></tr>"
"<tr><td><code>--help</code></td><td>Show this help screen</td></tr>"
"<tr><td><code>--setup</code></td><td>Run the setup / configuration program</td></tr>"
"<tr><td><code>file</code></td><td>Is the optional root filename.\n</td></tr>"
"<tr><td></td><td>If not given, the board name is used as the root filename.</td></tr>"
"</table>"
"</td></tr>"
"</table>"
"<hr>"
g_tool_size = TOOL_SIZE;
// Filename to output to.
string m_file_name = "";
// Which side we're currently working on.
g_side = TOP;
// Tool currently being used.
int m_current_tool = 0;
// Amount to isolate traces.
real m_isolate = 0.0;
// Used to pass the pass info to the next pass of the program.
int m_pass_num;
// The layer being worked on.
int m_layer;
// Used to create command strings.
string g_cmd;
string g_cmd_temp;
////////// mlmSolutions added 29 Jan 14
real m_v_x[], m_v_y[], m_v_rad[];
real m_v_xc[], m_v_yc[]; // JJ
real m_tool_x, m_tool_y, m_vx1, m_vy1, m_vx2, m_vy2, m_rad;
int m_v_index[], m_v_prev[], m_v_next[], m_v_step[], m_v_cont[], m_v_curve[], m_c_start[], m_c_end[];
int m_contours, m_cw, m_v_n=1, m_fwd, m_dist, m_dist_min, m_dist_min_index;
int m_wire_start, m_z_up, m_step, m_i, m_j, m_cont, m_border_contour;
//////////
//
// Save the maximum and minimum coordinates.
//
// Params:
// x, y Coordinates to be tested, and possibly saved as min or max values.
// Return:
// none
//
real m_max_x;
real m_max_y;
real m_min_x;
real m_min_y;
void save_extents(real x, real y)
{
m_max_x = max(m_max_x, x);
m_max_y = max(m_max_y, y);
m_min_x = min(m_min_x, x);
m_min_y = min(m_min_y, y);
}
//
// Open the platform-specific previewer.
//
// Params: none
// Return: none
//
void preview()
{
if (SHOW_PREVIEW) {
switch(get_os()) {
case OS_MACOSX:
system(g_path + "/viewer/application.macosx/viewer.app/Contents/MacOS/JavaApplicationStub");
break;
case OS_LINUX:
system(g_path + "/source/viewer.linux.sh");
break;
case OS_WINDOWS:
system(g_path + "/viewer/application.windows/viewer.exe");
dlgMessageBox("Close this window when you have finished with the preview");
break;
default:
Fatal("Oops!", "Can't figure out which OS you have.");
}
}
}
//
// Determine the next phase, and set g_phase accordingly.
//
// Params: none
// Return: none
//
void next_phase(void)
{
if (g_phase == PH_TOP_OUT_GEN && GENERATE_TOP_OUTLINES != YES) {
g_phase += 2;
return;
}
if (g_phase == PH_BOTTOM_OUT_GEN && GENERATE_BOTTOM_OUTLINES != YES) {
g_phase += 2;
return;
}
if (g_phase == PH_TOP_OUT_WRITE || g_phase == PH_BOTTOM_OUT_WRITE) {
if (m_isolate < ISO_MAX && !SINGLE_PASS) {
m_isolate += ISO_STEP;
m_pass_num++;
g_phase--;
}
else {
m_isolate = ISO_MIN;
m_pass_num = 0;
g_phase++;
preview();
}
}
else {
g_phase++;
}
}
//////////////////////////////////////////////////
//
// Hole drilling routines.
//
//////////////////////////////////////////////////
//
// Add a drill hole entry (size, x, y) to the stack.
//
// Params:
// Size Drill size.
// req_size Desired drill hole size. (May be subbed.)
// x x coordinate.
// y y coordinate.
// Returns:
// none
// Changes:
// the stack
//
void add_hole(int req_size, int x, int y)
{
string tempstr;
int drill_size;
drill_size = get_drill_for(req_size);
sprintf(tempstr, "%06d\t%06d\t%06d", drill_size, x, y);
stack_push(tempstr);
}
//
// Return the distance between two points.
//
// Params:
// x1, y1, x2, y2 coordinates for two points.
// Return:
// the distance between the points.
//
real distance(real x1, real y1, real x2, real y2) {
return (sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
}
//
// Sorts a stack of drills in g_stack by distance from last hole.
//
// Params: none
// Return: none
// Changes:
// the stack
//
void pythagorean_sort() {
real drill_size1;
real drill_x1;
real drill_y1;
real drill_size2;
real drill_x2;
real drill_y2;
real drill_size3;
real drill_x3;
real drill_y3;
int i;
int j;
string drill_args1[];
string drill_args2[];
string drill_args3[];
string tmp;
real before_distance;
real after_distance;
// Sort by size, x, then y
stack_sort();
for (i = 0; i <= stack_count(); i++) {
strsplit(drill_args1, stack_elem(i), '\t');
drill_size1 = internals_to_user(my_strtol(drill_args1[DRILL_SIZE]));
drill_x1 = scale_x(my_strtol(drill_args1[DRILL_X]));
drill_y1 = scale_y(my_strtol(drill_args1[DRILL_Y]));
j = i + 2;
while (j <= stack_count()) {
strsplit(drill_args2, stack_elem(i+1), '\t');
drill_size2 = internals_to_user(my_strtol(drill_args2[DRILL_SIZE]));
drill_x2 = scale_x(my_strtol(drill_args2[DRILL_X]));
drill_y2 = scale_y(my_strtol(drill_args2[DRILL_Y]));
strsplit(drill_args3, stack_elem(j), '\t');
drill_size3 = internals_to_user(my_strtol(drill_args3[DRILL_SIZE]));
drill_x3 = scale_x(my_strtol(drill_args3[DRILL_X]));
drill_y3 = scale_y(my_strtol(drill_args3[DRILL_Y]));
if (drill_size2 != drill_size1 || drill_size3 != drill_size1)
break;
if (distance(drill_x1, drill_y1, drill_x3, drill_y3) < distance(drill_x1, drill_y1, drill_x2, drill_y2)) {
tmp = stack_elem(j);
stack_put(j, stack_elem(i+1));
stack_put(i+1, tmp);
j = i + 1;
}
j++;
}
}
}
/*
* Create a drill file for the desired side.
*
* Params:
* which_side Side to produce the file for.
* Returns:
* none
* Changes:
* m_current_tool
* g_side
*/
void drill(int which_side)
{
int num_lines;
int i;
real last_size;
string drill_args[];
real drill_size_mm;
real drill_size_inch;
real drill_size;
real drill_x;
real drill_y;
int drill_tool_num;
m_current_tool = 0;
g_side = which_side;
//
// Build a stack with all the holes.
//
board(B) {
B.holes(H) add_hole(H.drill, H.x, H.y);
B.signals(S) S.vias(V) add_hole(V.drill, V.x, V.y);
B.elements(E) {
E.package.contacts(C) {
if (C.pad) {
add_hole(C.pad.drill, C.pad.x, C.pad.y);
}
}
E.package.holes(H) add_hole(H.drill, H.x, H.y);
}
// B.elements
// Sorts the drills by size, then distance from previous hole.
pythagorean_sort();
//
// Create the drill g-code file.
//
last_size = -1;
output(get_filename(), FILEMODE_WRITE_TEXT) {
output_file_preamble();
//
// Create a tool table at the beginning of the file.
//
out(TOOL_CHANGE_TABLE_HEADER);
for (i=stack_fwd_iter(); i != END_OF_STACK; i=stack_fwd_next()) {
if (i==END_OF_STACK) break;
strsplit(drill_args, stack_elem(i), '\t');
drill_size_inch = u2inch(my_strtol(drill_args[DRILL_SIZE]));
drill_size_mm = u2mm(my_strtol(drill_args[DRILL_SIZE]));
drill_size = my_strtol(drill_args[DRILL_SIZE]);
if (drill_size_mm == 0.0) continue;
if (drill_size != last_size) {
++m_current_tool;
drill_tool_num = get_tool_num_for(my_strtol(drill_args[DRILL_SIZE]), m_current_tool);
out(TOOL_CHANGE_TABLE_FORMAT(drill_tool_num,
drill_size_mm, drill_size_inch,
g_mins[drill_tool_num], g_maxs[drill_tool_num],
g_drill_sub_cnt[drill_tool_num]));
}
last_size = drill_size;
}
// for
begin_gcode(SPINDLE_DRILL_RPM);
//
// Generate drill code.
//
m_current_tool = 0;
for (i=stack_fwd_iter(); i != END_OF_STACK; i=stack_fwd_next()) {
strsplit(drill_args, stack_elem(i), '\t');
drill_size = internals_to_user(my_strtol(drill_args[DRILL_SIZE]));
drill_x = scale_x(my_strtol(drill_args[DRILL_X]));
drill_y = scale_y(my_strtol(drill_args[DRILL_Y]));
save_extents(drill_x, drill_y);
if (drill_size == 0.0) continue;
//
// Check to see if the drill size has changed,
// if so, output a tool change and optional zero code.
//
if (drill_size == last_size) {
output_drill_hole(drill_x, drill_y, DRILL_DEPTH);
}
else {
m_current_tool++;
// Tool change routine
output_tool_change_begin();
out(SPINDLE_OFF);
rz(TOOL_CHANGE_POS_Z);
rxy(TOOL_CHANGE_POS_X, TOOL_CHANGE_POS_Y);
out(fir(TOOL_CHANGE, get_tool_num_for(my_strtol(drill_args[DRILL_SIZE]), m_current_tool), drill_size));
output_tool_changed();
// Output zero steps and a pause
if (DO_TOOL_CHANGE_WITH_ZERO_STEP == YES) {
output_tool_zero_begin();
fzr(0.000, FEED_RATE_DRILL_Z);
out(OPERATOR_PAUSE + EOL);
output_tool_zero_end();
}
rz(DEFAULT_Z_UP);
out(fr(SPINDLE_ON, SPINDLE_ON_TIME));
output_tool_change_end();
output_drill_first_hole(drill_x, drill_y, DRILL_DEPTH);
last_size = drill_size;
}
// if drill_size != last_size
}
// for
output_file_postamble();
// End of file
// change back to tool 1
out(fi(TOOL_CODE + EOL, 1));
end_gcode();
}
// output
}
// Board
}
// drill
// holds line segments generated for the previewer
string m_lines;
//
// Format the coordinates of two points to be used by the previewer.
//
// Params:
// x1, y1, x2, y2 Two points.
// Return:
// Coordinates formatted for the previewer.
//
string LINE_SEP = "\n";
string COORD_SEP = ",";
string COORD_FMT = "%8.5f";
string coords(real x1, real y1, real x2, real y2)
{
return frrrr(COORD_FMT + COORD_SEP + COORD_FMT + COORD_SEP +
COORD_FMT + COORD_SEP + COORD_FMT + LINE_SEP, x1, y1, x2, y2);
}
//
// Generates gcode for spot drilling and previewing holes.
//
// Params:
// drill_size Size of the hole to drill.
// x, y Location of the hole.
// Return: none
// Changes:
// m_first_spot_drill
// m_lines
// g_side
//
int m_first_spot_drill = YES;
void spot_drill_hole(int drill_size, int x, int y)
{
real drill_x;
real drill_y;
real offset;
drill_x = scale_x(x);
drill_y = scale_y(y);
save_extents(drill_x, drill_y);
if (SPOT_DRILL == YES) {
if (m_first_spot_drill) {
output_drill_first_hole(drill_x, drill_y, SPOT_DRILL_DEPTH);
m_first_spot_drill = NO;
}
else {
output_drill_hole(drill_x, drill_y, SPOT_DRILL_DEPTH);
}
}
offset = internals_to_user(get_drill_for(drill_size)) / 2;
//
// Generate X's for drill preview if drills are turned on for this side.
//
if ((g_side == TOP && GENERATE_TOP_DRILL == YES) ||
(g_side == BOTTOM && GENERATE_BOTTOM_DRILL == YES)) {
m_lines += coords(drill_x - offset, drill_y - offset, drill_x + offset, drill_y + offset);
m_lines += coords(drill_x - offset, drill_y + offset, drill_x + offset, drill_y - offset);
}
}
//
// Enumerates all holes and generates spot drills for them.
//
// Params:
// B The board.
// Return: none
// Changes:
// m_lines
//
void spot_drill(UL_BOARD B)
{
m_lines += "# spot drills\n";
B.holes(H) spot_drill_hole(H.drill, H.x, H.y);
B.signals(S) S.vias(V) spot_drill_hole(V.drill, V.x, V.y);
B.elements(E) {
E.package.contacts(C) {
if (C.pad) {
spot_drill_hole(C.pad.drill, C.pad.x, C.pad.y);
}
}
E.package.holes(H) spot_drill_hole(H.drill, H.x, H.y);
}
// B.elements
}
//
// "Draw" on the output device, i.e. the gcode file.
//
// Params:
// x1,y1 Start of the line.
// x2,y2 End of the line.
// state The state of the line being drawn, start, continue, etc.
// z_down_or_radius Z depth or radius of arc
// fr_xy XY feed rate
// fr_z Z feed rate
// Returns:
// none
// Changes:
// m_lines
// m_arc_begin_x
// m_arc_begin_y
// m_arc_radius
//
real m_arc_begin_x;
real m_arc_begin_y;
real m_arc_radius;
real m_arc_center_x;
real m_arc_center_y;
int pair_count = 0;
int MAX_COORDS_PER_LINE = 4;
void device_draw(int x1, int y1, int x2, int y2, int state, real z_down_or_radius, real fr_xy, real fr_z)
{
real rx1, ry1, rx2, ry2;
string str;
real i_coord;
real j_coord;
rx1 = scale_x(x1);
ry1 = scale_y(y1);
rx2 = scale_x(x2);
ry2 = scale_y(y2);
save_extents(rx1, ry1);
save_extents(rx2, ry2);
// Output g-code based on the current state.
switch(state) {
// Start of a new line.
case ST_START_LINE:
user_track_begin(rx1, ry1, rx2, ry2);
m_lines += coords(rx1, ry1, rx2, ry2);
rz(DEFAULT_Z_UP);
rxy(rx1, ry1);
fzr(z_down_or_radius, fr_z);
fxyr(rx2, ry2, fr_xy);
pair_count = 0;
break;
// A fill line.
case ST_FILL:
Fatal("Programmer Error", "The Fill functions are no longer supported.");
break;
// Continue a line.
// End a line.
case ST_CONTINUE_LINE:
user_track_continue(rx1, ry1, rx2, ry2);
m_lines += coords(rx1, ry1, rx2, ry2);
if (COMPACT_GCODE == YES) {
if (pair_count == 0) {
fxy(rx2, ry2);
}
else {
xy(rx2, ry2);
}
pair_count++;
}
else {
fxy(rx2, ry2);
}
break;
case ST_END_LINE:
user_track_end(rx1, ry1, rx2, ry2);
m_lines += coords(rx1, ry1, rx2, ry2);
if (COMPACT_GCODE == YES) {
xy(rx2, ry2);
pair_count = 0;
}
else {
fxy(rx2, ry2);
}
break;
// Drill a hole.
// todo is this valid?
case ST_DRILL:
printf("cause an error if used %d");
// printf("drill (%f, %f)\n", rx2, ry2);
break;
// Create an arc.
case ST_ARC_BEGIN:
user_arc_begin(rx1, ry1, rx2, ry2);
m_arc_begin_x = rx1;
m_arc_begin_y = ry1;
m_arc_radius = internals_to_user(z_down_or_radius);
break;
// Finish an arc.
case ST_ARC_END:
user_arc_end(rx1, ry1, rx2, ry2);
real cx = rx2;
real cy = ry2;
real end_x = rx1;
real end_y = ry1;
rz(DEFAULT_Z_UP);
rxy(end_x, end_y);
fzr(z_down_or_radius, FEED_RATE_MILL_Z);
if (g_side == TOP || MIRROR_BOTTOM == YES) {
out(frrrr(ARC_CLOCK, m_arc_begin_x, m_arc_begin_y, m_arc_radius, fr_xy));
}
else {
out(frrrr(ARC_CCLOCK, m_arc_begin_x, m_arc_begin_y, m_arc_radius, fr_xy));
}
break;
////////// mlmSolutions 5 Feb 14
case ST_ARC_CENTER:
sprintf(str, "ARC CENTER: rx1=%f, ry1=%f, rx2=%f, ry2=%f", rx1, ry1, rx2, ry2);
comm(str);
m_arc_center_x = rx1;
m_arc_center_y = ry1;
break;
case ST_ARC:
sprintf(str, "ARC: rx1=%f, ry1=%f, rx2=%f, ry2=%f, cx=%f, cy=%f", rx1, ry1, rx2, ry2, m_arc_center_x, m_arc_center_y);
comm(str);
i_coord = m_arc_center_x - rx1;
j_coord = m_arc_center_y - ry1;
if (m_z_up){
rz(DEFAULT_Z_UP);
rxy(rx1, ry1);
fzr(z_down_or_radius, fr_z);
}
if (((g_side == TOP) && m_cw) || ((g_side != TOP) && !m_cw)) str = ARC_CLOCK; else str = ARC_CCLOCK;
out(frrrr(str, rx2, ry2, internals_to_user(m_rad), fr_xy));
break;
//////////
}
// switch(state)
}
// device_draw
//
// Generate polygons for outline or fill.
// This creates a series of commands for Eagle and puts them
// in g_cmd to be passed when we exit from this phase of pcb-gcode.
//
// Params:
// which_side The side to operate on.
// Return:
// none
// Changes:
// m_layer
// g_cmd
//
void generate_outlines(int which_side)
{
string cmd_temp = "";
g_cmd = "";
if (which_side == TOP)
m_layer = TOP_LAYER;
else
m_layer = BOTTOM_LAYER;
board(B) {
real f = BORDER_SIZE;
real x1 = internals_to_user(B.area.x1) - f;
real y1 = internals_to_user(B.area.y1) - f;
real x2 = internals_to_user(B.area.x2) + f;
real y2 = internals_to_user(B.area.y2) + f;
B.signals(S) {
if (S.name == OUTLINES_SIGNAL_NAME) {
sprintf(cmd_temp, "delete (%f %f) (%f %f);\n",
x1, y1, x2, y2);
g_cmd = g_cmd + cmd_temp;
}
}
sprintf(cmd_temp, "grid %s;\n"
"change isolate %f;\n"
"change rank 6;\n"
"change pour solid;\n"
"change width %f;\n"
"change orphans on;\n"
"layer %d;\n"
"polygon %s %f (%f %f) (%f %f) (%f %f) (%f %f) (%f %f);\n"
"ratsnest;\n"
,
get_unit_of_measure(),
m_isolate,
g_tool_size,
m_layer,
OUTLINES_SIGNAL_NAME, g_tool_size,
x1, y1, x2, y1, x2, y2, x1, y2, x1, y1
);
g_cmd = g_cmd + cmd_temp;
return;
}
// board(B)
}
// generate_outlines
//
// Output info and lines to a file for the previewer.
//
// Params:
// path Path and filename.
// mode File open mode. May be write or append.
// layer MILL, TEXT, etc.
// Returns:
// none
// Changes:
// none
//
void out_lines(string path, string mode, int layer)
{
output(path, mode) {
if (m_pass_num == 0) {
printf("# board=%s\n", elided_path(get_filename(), 30));
if (layer == MILL) {
printf("# depth=%f\n", MILLING_DEPTH);
}
else if (layer == TEXT) {
printf("# depth=%f\n", TEXT_DEPTH);
}
else {
printf("# tool size=%f\n", g_tool_size);
}
}
printf("# pass=%d\n", m_pass_num + 1);
printf("# preview window width=%d height=%d\n", PREVIEW_WINDOW_WIDTH, PREVIEW_WINDOW_HEIGHT);
printf(m_lines);
}
}
//
// Output preview data to the platform specific files.
//
// Params:
// mode File open mode. May be write or append.
// Returns:
// none
// Changes:
// none
//
void output_preview_data(string mode, int layer)
{
out_lines(g_path + "/viewer/data/optimize_me.txt", mode, layer);
out_lines(g_path + "/viewer/applet/data/optimize_me.txt", mode, layer);
out_lines(g_path + "/viewer/application.macosx/data/optimize_me.txt", mode, layer);
out_lines(g_path + "/viewer/application.linux/data/optimize_me.txt", mode, layer);
out_lines(g_path + "/viewer/application.windows/data/optimize_me.txt", mode, layer);
}
//
// Write text from the Milling layer to a text engraving file.
//
// Params:
// none
// Return:
// none
// Changes:
// g_tool_size
// m_lines
//
void output_text_code()
{
int state;
int old_x, old_y;
board(B) {
output(get_filename(), FILEMODE_WRITE_TEXT) {
user_file_opened(get_filename(), FILEMODE_WRITE_TEXT);
output_file_preamble();
// Initialize the device (i.e. the output file).
begin_gcode(SPINDLE_TEXT_RPM);
// Process all the wires.
B.texts(T) T.wires(W) {
if ((T.mirror && g_side == BOTTOM) || (! T.mirror && g_side == TOP)) {
// If this is a text layer, check if we should continue
// a line, or start a new one.
if (W.layer == TEXT_LAYER) {
if (W.x1 == old_x && W.y1 == old_y) {
state = ST_CONTINUE_LINE;
}
else {
state = ST_START_LINE;
}
// if this is an arc, it is treated specially.
if (W.arc) {
device_draw( W.arc.x1, W.arc.y1, W.arc.x2, W.arc.y2, ST_ARC_BEGIN, W.arc.radius, FEED_RATE_TEXT_XY, FEED_RATE_TEXT_Z);
device_draw( W.arc.x2, W.arc.y2, W.arc.xc, W.arc.yc, ST_ARC_END, TEXT_DEPTH, FEED_RATE_TEXT_XY, FEED_RATE_TEXT_Z);
save_extents(W.x1, W.y1);
save_extents(W.x2, W.y2);
state = ST_START_LINE;
}
else {
// Draw the line (i.e. output the gcode for the line).
device_draw(W.x1, W.y1, W.x2, W.y2, state, TEXT_DEPTH, FEED_RATE_TEXT_XY, FEED_RATE_TEXT_Z);
old_x = W.x2;
old_y = W.y2;
save_extents(W.x1, W.y1);
save_extents(W.x2, W.y2);
}
// if (W.arc)
}
// if (W.layer == TEXT_LAYER)
}
// if (top and bottom check)
}
// T.wires
output_file_postamble();
end_gcode();
user_file_closing();
}
// output
user_file_closed(get_filename(), FILEMODE_WRITE_TEXT);
}
// board
g_tool_size = abs(TEXT_DEPTH);
output_preview_data(FILEMODE_WRITE_TEXT, TEXT);
preview();
m_lines = "";
}
// output_text_code
////////// mlmSolutions added 21 Jan 14
//
// Checks if two vertices match. If yes, return true else return false.
//
// Params:
// X1, Y1, X2, Y2
// Return:
// true or false
// Changes:
// nothing
//
int match(real X1, real Y1, real X2, real Y2)
{
if ((X1 == X2) && (Y1 == Y2)) return true; else return false;
}
//
// Removes a vertex from the linked list.
//
// Params:
// id - the id of the vertex to be removed
// Return:
// nothing
// Changes:
// pointers of previous and next entry
//
void vertex_remove(int id)
{
m_v_next[m_v_prev[id]] = m_v_next[id];
m_v_prev[m_v_next[id]] = m_v_prev[id];
}
//////////
//
// Write layer data to mill files.
//
// Params:
// none
// Return:
// none
// Changes:
// g_tool_size
// m_lines
//
void output_mill_code()
{
int state;
int old_x, old_y;
int first_run = true;
string str;
board(B) {
output(get_filename(), FILEMODE_WRITE_TEXT) {
user_file_opened(get_filename(), FILEMODE_WRITE_TEXT);
output_file_preamble();
// Initialize the device (i.e. the output file).
begin_gcode(SPINDLE_MILL_RPM);
m_v_n =1;
// Process all the wires.
B.wires(W) W.pieces(P) {
// If this is a mill layer, check if we should continue
// a line, or start a new one.
if (P.layer == MILL_LAYER) {
if (P.x1 == old_x && P.y1 == old_y && !first_run) {
state = ST_CONTINUE_LINE;
}
else {
state = ST_START_LINE;
first_run = false;
}
// if this is an arc, it is treated specially.
if (P.arc) {
sprintf(str, "# arc xc=%f yc=%f sang=%f eang=%f radius=%f\n",
scale_x(P.arc.xc), scale_y(P.arc.yc), P.arc.angle1, P.arc.angle2, scale_x(P.arc.radius));
m_lines += str;
sprintf(str, "# debug W.x1=%8.5f W.y1=%8.5f W.x2=%8.5f W.y2=%8.5f W.curve=%f\n",
scale_x(W.x1), scale_y(W.y1), scale_x(W.x2), scale_y(W.y2), W.curve);
m_lines += str;
sprintf(str, "# debug P.arc.x1=%8.5f P.arc.y1=%8.5f P.arc.x2=%8.5f P.arc.y2=%8.5f\n",
scale_x(P.arc.x1), scale_y(P.arc.y1), scale_x(P.arc.x2), scale_y(P.arc.y2));
m_lines += str;
m_lines += "# debug\n";
// device_draw( P.arc.x1, P.arc.y1, P.arc.x2, P.arc.y2, ST_ARC_BEGIN, P.arc.radius, FEED_RATE_MILL_XY, FEED_RATE_MILL_Z);
// device_draw( P.arc.x2, P.arc.y2, P.arc.xc, P.arc.yc, ST_ARC_END, MILLING_DEPTH, FEED_RATE_MILL_XY, FEED_RATE_MILL_Z);
////////// mlmSolutions added 5 Feb 14
// As we cannot assume that "pieces" come from "wires" in any particular order or that they are contiguous
// we will have to save the start and end vertices.
// save arc data
m_v_x[m_v_n] = P.arc.x1;
m_v_y[m_v_n] = P.arc.y1;
m_v_xc[m_v_n] = P.arc.xc;
m_v_yc[m_v_n] = P.arc.yc;
m_v_rad[m_v_n] = P.arc.radius;
m_v_curve[m_v_n] = W.curve;
m_v_prev[m_v_n] = m_v_n - 1;
m_v_next[m_v_n] = 0;
m_v_next[m_v_n - 1] = m_v_n;
m_v_step[m_v_n] = 0;
++m_v_n;
m_v_x[m_v_n] = P.arc.x2;
m_v_y[m_v_n] = P.arc.y2;
m_v_xc[m_v_n] = P.arc.xc;
m_v_yc[m_v_n] = P.arc.yc;
m_v_rad[m_v_n] = P.arc.radius;
m_v_curve[m_v_n] = W.curve;
m_v_prev[m_v_n] = m_v_n - 1;
m_v_next[m_v_n] = 0;
m_v_next[m_v_n - 1] = m_v_n;
m_v_step[m_v_n] = 0;
++m_v_n;
//////////
save_extents(P.x1, P.y1);
save_extents(P.x2, P.y2);
state = ST_START_LINE;
}
else {