-
Notifications
You must be signed in to change notification settings - Fork 22
/
hpgl_import.pas
418 lines (388 loc) · 12.9 KB
/
hpgl_import.pas
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
// #############################################################################
// HPGL Import
// #############################################################################
function get_hpgl_cmd(var my_pos: Integer; var my_line: string): THPGL_cmd;
begin
if my_line[my_pos] = ';' then begin
result:= cmd_exit;
exit;
end;
if my_pos >= length(my_line) then begin
result:= cmd_exit;
exit;
end;
if my_line[my_pos] in ['0'..'9', '-', '+'] then begin
result:= cmd_number;
exit;
end;
if pos('PA',my_line) = my_pos then begin
result:= cmd_pa;
my_pos:= my_pos+2;
exit;
end;
if pos('PU',my_line) = my_pos then begin
result:= cmd_pu;
my_pos:= my_pos+2;
exit;
end;
if pos('PD',my_line) = my_pos then begin
result:= cmd_pd;
my_pos:= my_pos+2;
exit;
end;
if pos('SP',my_line) = my_pos then begin
result:= cmd_sp;
my_pos:= my_pos+2;
exit;
end;
if pos('IN',my_line) = my_pos then begin
result:= cmd_in;
my_pos:= my_pos+2;
exit;
end;
if pos('LT',my_line) = my_pos then begin
result:= cmd_none;
my_pos:= my_pos+2;
exit;
end;
result:= cmd_exit; // alle anderen Befehle ignorieren
end;
procedure hpgl_import_line(my_line: String; fileID, penOverride: Integer);
// Actions: none, lift, seek, drill, mill
var
my_x, my_y: Integer;
my_pos: Integer;
my_cmd: THPGL_cmd;
my_valid: boolean;
my_pen: Integer;
xx, yy: Double;
dummy_str: String;
begin
my_pos:= 1;
repeat
my_cmd:= get_hpgl_cmd(my_pos, my_line);
if my_cmd = cmd_exit then
exit;
case my_cmd of
cmd_pa:
begin
my_valid:= ParseLine(my_pos, my_line, xx, dummy_str) = p_number;
my_valid:= my_valid and (ParseLine(my_pos, my_line, yy, dummy_str) = p_number);
if my_valid then begin
LastPoint.x:= round(xx);
LastPoint.y:= round(yy);
end;
if LastAction >= mill then begin // war unten
append_point(fileID, CurrentBlockID, LastPoint);
blockArrays[fileID, CurrentBlockID].pen:= CurrentPen;
end;
end;
cmd_pu:
begin
my_valid:= ParseLine(my_pos, my_line, xx, dummy_str) = p_number;
my_valid:= my_valid and (ParseLine(my_pos, my_line, yy, dummy_str) = p_number);
if my_valid then begin
LastPoint.x:= round(xx);
LastPoint.y:= round(yy);
end;
LastAction:= seek;
end;
cmd_pd:
begin
if LastAction <= seek then begin
// war oben, letzte Koordinaten sind erster Punkt im Block
CurrentBlockID:= new_block(fileID);
append_point(fileID, CurrentBlockID, LastPoint);
blockArrays[fileID, CurrentBlockID].pen:= CurrentPen;
end;
my_valid:= ParseLine(my_pos, my_line, xx, dummy_str) = p_number;
my_valid:= my_valid and (ParseLine(my_pos, my_line, yy, dummy_str) = p_number);
if my_valid then begin
LastPoint.x:= round(xx);
LastPoint.y:= round(yy);
end;
append_point(fileID, CurrentBlockID, LastPoint);
LastAction:= mill;
end;
cmd_in:
begin
ParseLine(my_pos, my_line, xx, dummy_str);
ParseLine(my_pos, my_line, xx, dummy_str);
LastAction:= lift;
end;
cmd_sp:
begin
my_valid:= ParseLine(my_pos, my_line, xx, dummy_str) = p_number;
my_pen:= round(xx);
if penOverride >= 0 then
my_pen:= penoverride;
if my_valid and (my_pen < 10) then begin
CurrentPen:= my_pen;
job.pens[my_pen].used:= true;
job.pens[my_pen].enable:= true;
end else
CurrentPen:= 0;
LastAction:= lift;
end;
end;
until false;
end;
// #############################################################################
procedure hpgl_fileload(my_name:String; fileID, penOverride: Integer);
// Liest File in FileBuffer und liefert Länge zurück
var
my_ReadFile: TextFile;
i: Integer;
my_line: String;
my_char: char;
my_sl: TStringList;
begin
if not FileExists(my_name) then begin
FileParamArray[fileID].valid := false;
exit;
end;
FileParamArray[fileID].bounds.min.x := high(Integer);
FileParamArray[fileID].bounds.min.y := high(Integer);
FileParamArray[fileID].bounds.max.x := low(Integer);
FileParamArray[fileID].bounds.max.y := low(Integer);
my_sl:= TStringList.Create;
my_line:='';
FileMode := fmOpenRead;
AssignFile(my_ReadFile, my_name);
Reset(my_ReadFile);
while not Eof(my_ReadFile) do begin
Read(my_ReadFile,my_char);
if my_char >= #32 then
my_line:= my_line + my_char;
if my_char= ';' then begin
my_sl.Add(my_line);
my_line:='';
end;
end;
CloseFile(my_ReadFile);
CurrentPen:= 0;
LastAction:= lift;
LastPoint.x:= 0;
LastPoint.y:= 0;
for i:= 0 to my_sl.count-1 do begin
hpgl_import_line(my_sl[i], fileID, penOverride);
end;
my_sl.free;
FileParamArray[fileID].valid := true;
file_rotate_mirror(fileID, true); // auto close path
block_scale_file(fileID);
end;
// #############################################################################
// SVG Import
// #############################################################################
procedure svg_fileload(my_name:String; fileID, penOverride: Integer);
// SEHR simpler svg-Parser nur für pcb2gcode-SVGs!
// nutzt
// function ParseLine(var position: Integer; var linetoparse: string;
// var value: Double; var letters: String): T_parseReturnType;
// SVG-Zeilen können SEHR lang sein, StringList hat deshalb wenig Vorteile
var
my_ReadFile: TextFile;
i, n, my_pos: Integer;
my_line, my_str, my_cmd_str: String;
my_cmd, my_dummy: char;
my_val: Double;
next_is_first_point: boolean;
my_x, my_y: integer; // gebraucht werden HPGL-Koordinaten!
my_firstpoint: TIntpoint; // gebraucht werden HPGL-Koordinaten!
begin
if not FileExists(my_name) then begin
FileParamArray[fileID].valid := false;
exit;
end;
if penOverride < 1 then
penOverride:= 9;
job.pens[penOverride].used:= true;
FileParamArray[fileID].bounds.min.x := high(Integer);
FileParamArray[fileID].bounds.min.y := high(Integer);
FileParamArray[fileID].bounds.max.x := low(Integer);
FileParamArray[fileID].bounds.max.y := low(Integer);
my_line:= '';
my_cmd_str:= '';
FileMode := fmOpenRead;
AssignFile(my_ReadFile, my_name);
Reset(my_ReadFile);
while not Eof(my_ReadFile) do begin
Readln(my_ReadFile, my_line);
if pos('<path', my_line) > 0 then
break;
end;
next_is_first_point:= true;
while not Eof(my_ReadFile) do begin
my_pos:= pos(' d="', my_line) + 3;
if my_pos > 3 then
repeat
if ParseLine(my_pos, my_line, my_val, my_cmd_str) = p_letters then begin
my_cmd:= my_cmd_str[1]; // ist immer nur ein Buchstabe
case my_cmd of
'M':
begin
// c_hpgl_scale * Punkt US-amer.?
if ParseLine(my_pos, my_line, my_val, my_cmd_str) = p_number then
LastPoint.x:= round(my_val * c_hpgl_scale * 0.352777);
if ParseLine(my_pos, my_line, my_val, my_cmd_str) = p_number then
LastPoint.y:= round(my_val * c_hpgl_scale * 0.352777);
if next_is_first_point or (LastAction = lift) then begin
my_firstpoint.x:= LastPoint.x + 0;
my_firstpoint.y:= LastPoint.y + 0;
end;
next_is_first_point:= false;
LastAction:= lift;
end;
'L':
begin
if LastAction = lift then begin
CurrentBlockID:= new_block(fileID);
blockArrays[fileID, CurrentBlockID].pen:= penOverride;
append_point(fileID, CurrentBlockID, LastPoint);
end;
if ParseLine(my_pos, my_line, my_val, my_cmd_str) = p_number then
LastPoint.x:= round(my_val * c_hpgl_scale * 0.352777);
if ParseLine(my_pos, my_line, my_val, my_cmd_str) = p_number then
LastPoint.y:= round(my_val * c_hpgl_scale * 0.352777);
LastAction:= mill;
next_is_first_point:= false;
append_point(fileID, CurrentBlockID, LastPoint);
end;
'Z':
begin
next_is_first_point:= true;
LastAction:= lift;
append_point(fileID, CurrentBlockID, my_firstpoint);
end;
end;
end;
inc(my_pos);
until my_pos >= length(my_line) - 3;
Readln(my_ReadFile, my_line);
end;
CloseFile(my_ReadFile);
CurrentPen:= 0;
LastAction:= lift;
LastPoint.x:= 0;
LastPoint.y:= 0;
blockArrays[fileID, CurrentBlockID].closed:= false;
FileParamArray[fileID].valid := true;
file_rotate_mirror(fileID, false); // kein Auto-close!
block_scale_file(fileID);
end;
// #############################################################################
// GCode 2D Import, berücksichtigt kein Z bzw. nur über/unter Null
// #############################################################################
procedure gcode_import_line(my_line: String; fileID, penOverride: Integer);
// Simpler Gcode-Parser für 2D-Daten. Ignoriert Z-Tiefe, sondern entscheidet
// anhandh G0/G1, ob verfahren oder gefräst wird.
// Einfaches Format, zeilenorientiert.
// nutzt
// function ParseLine(var position: Integer; var linetoparse: string;
// var value: Double; var letters: String): T_parseReturnType;
// Actions: none, lift, seek, drill, mill
var
my_cmd: char;
my_pos, my_len: Integer;
my_valid: boolean;
my_pen: Integer;
my_dval: Double;
my_cmd_str, my_dummy_str: String;
got_new_xy: Boolean;
my_action: tAction;
begin
if penOverride < 1 then
penOverride:= 9;
job.pens[penOverride].used:= true;
FileParamArray[fileID].bounds.min.x := high(Integer);
FileParamArray[fileID].bounds.min.y := high(Integer);
FileParamArray[fileID].bounds.max.x := low(Integer);
FileParamArray[fileID].bounds.max.y := low(Integer);
my_pos:= 1;
my_len:= length(my_line) - 1;
if my_pos > my_len then // Leerzeile
exit;
got_new_xy:= false;
repeat
if (my_line[my_pos] = '(') or (my_line[my_pos] = '/') then // Kommentar
break;
if ParseLine(my_pos, my_line, my_dval, my_cmd_str) = p_letters then begin
my_cmd:= my_cmd_str[1];
case my_cmd of
'G':
begin
if ParseLine(my_pos, my_line, my_dval, my_cmd_str) = p_number then begin
if my_dval = 0 then
my_action:= seek
else if my_dval = 1 then
my_action:= mill
else
break; // alles andere in dieser Zeile ignorieren
end;
end;
'X':
if ParseLine(my_pos, my_line, my_dval, my_cmd_str) = p_number then begin
LastPoint.x:= round(my_dval * c_hpgl_scale);
got_new_xy:= true;
end;
'Y':
if ParseLine(my_pos, my_line, my_dval, my_cmd_str) = p_number then begin
LastPoint.y:= round(my_dval * c_hpgl_scale);
got_new_xy:= true;
end;
'Z':
ParseLine(my_pos, my_line, my_dval, my_cmd_str); // war eigentlich schon durch G0/G1 klar
'M':
begin // Modale Maschinenbefehle abbrechen, irrelevant
my_action:= none;
break;
end;
'F', 'S':
ParseLine(my_pos, my_line, my_dval, my_cmd_str); // dummy
end;
end;
inc(my_pos);
until my_pos > my_len;
if (LastAction = none) or ((my_action >= mill) and (LastAction < mill)) then begin
CurrentBlockID:= new_block(fileID);
blockArrays[fileID, CurrentBlockID].pen:= penOverride;
blockArrays[fileID, CurrentBlockID].closed:= true;
end;
if got_new_xy and (my_action >= mill) then begin
append_point(fileID, CurrentBlockID, LastPoint);
end;
LastAction:= my_action;
end;
// #############################################################################
procedure gcode_fileload(my_name:String; fileID, penOverride: Integer);
// Liest File in FileBuffer und liefert Länge zurück
var
my_ReadFile: TextFile;
i: Integer;
my_line: String;
my_char: char;
my_sl: TStringList;
begin
if not FileExists(my_name) then begin
FileParamArray[fileID].valid := false;
exit;
end;
FileParamArray[fileID].bounds.min.x := high(Integer);
FileParamArray[fileID].bounds.min.y := high(Integer);
FileParamArray[fileID].bounds.max.x := low(Integer);
FileParamArray[fileID].bounds.max.y := low(Integer);
my_sl:= TStringList.Create;
my_sl.loadfromfile(my_name);
CurrentPen:= 0;
LastAction:= none;
LastPoint.x:= 0;
LastPoint.y:= 0;
for i:= 0 to my_sl.count-1 do begin
gcode_import_line(my_sl[i], fileID, penOverride);
end;
my_sl.free;
FileParamArray[fileID].valid := true;
file_rotate_mirror(fileID, true); // auto close path
block_scale_file(fileID);
end;