forked from Scott-Cooper/Drawbot_image_to_gcode_v2
/
BotDrawing.pde
133 lines (115 loc) · 3.97 KB
/
BotDrawing.pde
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
///////////////////////////////////////////////////////////////////////////////////////////////////////
// A class to describe all the line segments
class botDrawing {
private int line_count = 0;
botLine[] lines = new botLine[10000000];
String gcode_comment = "";
void botDrawing() {
}
void render_last () {
lines[line_count].render_with_copic();
}
void render_all () {
for (int i=1; i<line_count; i++) {
lines[i].render_with_copic();
}
}
void render_some (int line_count) {
for (int i=1; i<line_count; i++) {
lines[i].render_with_copic();
}
}
void render_one_pen (int line_count, int pen) {
color c = color(255, 0, 0);
for (int i=1; i<line_count; i++) {
//for (int i=line_count; i>1; i--) {
if (lines[i].pen_number == pen) {
lines[i].render_with_copic();
}
}
}
void render_to_pdf (int line_count) {
String pdfname = "gcode\\gcode_" + basefile_selected + ".pdf";
PGraphics pdf = createGraphics(img.width, img.height, PDF, pdfname);
pdf.beginDraw();
pdf.background(255, 255, 255);
for(int i=line_count; i>0; i--) {
if(lines[i].pen_down) {
color c = copic.get_original_color(copic_sets[current_copic_set][lines[i].pen_number]);
pdf.stroke(c, 255);
pdf.line(lines[i].x1, lines[i].y1, lines[i].x2, lines[i].y2);
}
}
pdf.dispose();
pdf.endDraw();
println("PDF created: " + pdfname);
}
void render_each_pen_to_pdf (int line_count) {
for (int p=0; p<=pen_count-1; p++) {
String pdfname = "gcode\\gcode_" + basefile_selected + "_pen" + p + "_" + copic_sets[current_copic_set][p] + ".pdf";
PGraphics pdf = createGraphics(img.width, img.height, PDF, pdfname);
pdf.beginDraw();
pdf.background(255, 255, 255);
for (int i=line_count; i>0; i--) {
if (lines[i].pen_down & lines[i].pen_number == p) {
color c = copic.get_original_color(copic_sets[current_copic_set][lines[i].pen_number]);
pdf.stroke(c, 255);
pdf.line(lines[i].x1, lines[i].y1, lines[i].x2, lines[i].y2);
}
}
pdf.dispose();
pdf.endDraw();
println("PDF created: " + pdfname);
}
}
void set_pen_continuation_flags () {
float prev_x = 123456.0;
float prev_y = 654321.0;
boolean prev_pen_down = false;
int prev_pen_number = 123456;
for (int i=1; i<line_count; i++) {
if (prev_x != lines[i].x1 || prev_y != lines[i].y1 || prev_pen_down != lines[i].pen_down || prev_pen_number != lines[i].pen_number) {
lines[i].pen_continuation = false;
} else {
lines[i].pen_continuation = true;
}
prev_x = lines[i].x2;
prev_y = lines[i].y2;
prev_pen_down = lines[i].pen_down;
prev_pen_number = lines[i].pen_number;
}
println("set_pen_continuation_flags");
}
void addline(int pen_number_, boolean pen_down_, float x1_, float y1_, float x2_, float y2_) {
line_count++;
lines[line_count] = new botLine (pen_down_, pen_number_, x1_, y1_, x2_, y2_);
}
public int get_line_count() {
return line_count;
}
public void evenly_distribute_pen_changes (int line_count, int total_pens) {
println("evenly_distribute_pen_changes");
for (int i=1; i<=line_count; i++) {
int cidx = (int)map(i - 1, 0, line_count, 1, total_pens);
lines[i].pen_number = cidx;
//println (i + " " + lines[i].pen_number);
}
}
public void distribute_pen_changes_according_to_percentages (int line_count, int total_pens) {
int p = 0;
float p_total = 0;
for (int i=1; i<=line_count; i++) {
if (i > pen_distribution[p] + p_total) {
p_total = p_total + pen_distribution[p];
p++;
}
if (p > total_pens - 1) {
// Hacky fix for off by one error
println("ERROR: distribute_pen_changes_according_to_percentages, p: ", p);
p = total_pens - 1;
}
lines[i].pen_number = p;
//println (i + " " + lines[i].pen_number);
}
}
}