-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextFile.pas
More file actions
376 lines (342 loc) · 16.6 KB
/
TextFile.pas
File metadata and controls
376 lines (342 loc) · 16.6 KB
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
uses ShellApi;
Const
ArraySize = 1024;
ParamStringSeperator = '|'
Var
TextLine : string;
FileNameInput : string;
FileNameOutput : string;
FileNameBom : string;
PartNumber : string;
PartDesignator : string;
TestText : string;
NewString : string;
BomString : AnsiString; // max. 2GB
TextSegment : array [0..11] of string; // create Array for component definition
FileInput : Variant;
StartConvert : boolean;
TextLineLength : integer;
SemicolonPosition: integer;
CommaPosition : integer;
SegmentLength : integer;
ErrorCode : integer;
StartParameters : Array[0..5] of string;
BOMFilePath : string;
ODBTopCompFilePath : string;
ODBBotCompFilePath : string;
Procedure ProcessParameters(Paramstring : string);
Var
Position : integer;
TempString : string;
Begin
// Target Path
Position := AnsiPos(ParamStringSeperator, ParamString);
TempString := AnsiMidStr(ParamString, 14, Position - 21);//14);
StartParameters[1] := TempString;
ParamString := AnsiRightStr(ParamString, length(ParamString) - Position);
// Variant Name
Position := AnsiPos(ParamStringSeperator, ParamString);
TempString := AnsiMidStr(ParamString, 16, Position - 16);
StartParameters[2] := TempString;
ParamString := AnsiRightStr(ParamString, length(ParamString) - Position);
Position := AnsiPos(ParamStringSeperator, ParamString);
TempString := AnsiMidStr(ParamString, 14, Position - 14);
StartParameters[3] := TempString;
ParamString := AnsiRightStr(ParamString, length(ParamString) - Position);
//Open Output Files
Position := AnsiPos(ParamStringSeperator, ParamString);
TempString := AnsiMidStr(ParamString, 13, Position - 13);
StartParameters[4] := TempString;
ParamString := AnsiRightStr(ParamString, length(ParamString) - Position);
// Add Output Files to project
Position := AnsiPos('=', ParamString);
TempString := AnsiMidStr(ParamString, Position+1, length(ParamString)-Position);
StartParameters[5] := TempString;
End;
Procedure GetBOMData;
var
BomFile : TextFile;
i : integer;
InputString : variant;
Begin
// Open Bom File
AssignFile(BomFile, BOMFilePath);
Reset(BomFile); // open for read
// clear Variables
BomString := ''; // clear String
// Read Bom File
i := 0;
while not (Eof(BomFile)) do
begin
ReadLN(BomFile, InputString);
if (InputString <> NULL) then
begin
InputString := AnsiMidStr(InputString,2,length(InputString)-2); // EE Rothhaas 2020.02.06
InputString := InputString + ','; // EE Rothhaas 2020.02.06
BomString := BomString + InputString; // Create String with all designators
i := i + 1;
end
end;
CloseFile(BomFile);
End;
Procedure ProcessODBTop;
Var
InputFile : TextFile;
OutputFile ; TextFile;
FileNameInput : string;
FileNameOutput : string;
InputString : string;
i : integer;
Begin
// Duplicate original ODB file
try // don't know if file exists
Begin
CopyFile(ODBTopCompFilePath,ODBTopCompFilePath+'.org', False);
// Open Input Text File
FileNameInput := ODBTopCompFilePath + '.org';
AssignFile(InputFile, FileNameInput);
Reset(InputFile);
// Open Output Text File
FileNameOutput := ODBTopCompFilePath + '.txt';
AssignFile(OutputFile, FileNameOutput);
Rewrite(OutputFile);
// Start writing new odb file header
WriteLN(OutputFile, '#');
WriteLN(OutputFile, '#Component attribute names');
WriteLN(OutputFile, '#');
WriteLN(OutputFile, '@0 .no_pop');
WriteLN(OutputFile, '@1 .comp_mount_type');
WriteLN(OutputFile, '@2 .comp_height');
// Read InputFile Content
//Text := '';
StartConvert := False;
// Skip header
repeat
ReadLN(InputFile, InputString);
if (InputString = NULL) then // Search for first empty line
// Component definitions start here
begin
WriteLN(OutputFile, '');
StartConvert := True;
end;
until (StartConvert = True);
// Process Components
while not Eof(InputFile) do
begin
ReadLN(InputFile, FileInput);
if (FileInput = NULL) then // Search for empty lines
begin
//Text := Text + #13 + #10;
end
else begin
// check if the line is a component definition
TextLine := FileInput;
if (AnsiStartsStr('CMP', TextLine)) then
begin
// if yes ...
for i:=1 to 8 do
begin
TextLineLength := length(TextLine); // get text length
SegmentLength := AnsiPos(' ', TextLine); // Search for first space
TextSegment[i] := AnsiLeftStr(TextLine, SegmentLength-1); // copy Segment here
TextLine := AnsiRightStr(TextLine, TextLineLength - SegmentLength); // shorten text
end;
TextLine := AnsiRightStr(TextLine, length(TextLine)-1);
CommaPosition := AnsiPos(',', TextLine);
TextSegment[10] := AnsiLeftStr(TextLine, CommaPosition-1);
TextSegment[10] := AnsiRightStr(TextSegment[10], 1);
TextSegment[11] := AnsiRightStr(TextLine, length(TextLine)-CommaPosition);
TestText := TextSegment[11];
i := length(TestText);
TextSegment[11] := AnsiRightStr(TextSegment[11], i-2);
//PartDesignator := TextSegment[7];
PartDesignator := TextSegment[7] + ','; // EE Rothhaas 2020.02.06
PartNumber := TextSegment[8];
if (AnsiContainsText(BomString, PartDesignator)) then
begin
TextSegment[9]:='';
end
else begin
TextSegment[9]:='0,';
end;
NewString := TextSegment[1] + ' ' + TextSegment[2] + ' ' + TextSegment[3] + ' ' + TextSegment[4] + ' ' + TextSegment[5] + ' ' + TextSegment[6] + ' ' + TextSegment[7] + ' ' + TextSegment[8] + ' ;' + TextSegment[9] + '1=' + TextSegment[10] + ',2=' + TextSegment[11];
WriteLN(OutputFile, NewString);
//TextLineLength := length(TextLine);
//SemicolonPosition := AnsiPos(';', TextLine);
//PartDesignator
//PartNumber := AnsiMidStr(TextLine, SemicolonPosition-12, 11);
//WriteLN(MyOutputFile, AnsiLeftStr(TextLine, SemicolonPosition));
//WriteLN(MyOutputFile, 'Do something here' + ' ' + TextLine);
end
else begin
// if not write text without modification
WriteLN(OutputFile, TextLine);
//Text := Text + TextLine;
//Text := Text + #13 + #10;
end;
end;
end;
// Close both files when done
CloseFile(InputFile);
CloseFile(OutputFile);
deletefile(ODBTopCompFilePath); // Delete original file
CopyFile(ODBTopCompFilePath+'.txt',ODBTopCompFilePath, False); //replace it with the modified copy
deletefile(ODBTopCompFilePath+'.txt'); // Delete temorary copy
deletefile(ODBTopCompFilePath+'.org'); // Delete temorary copy
End;
except
//Begin
Showmessage('Datei Top existiert nicht');
CloseFile(InputFile);
CloseFile(OutputFile);
End;
End;
Procedure ProcessODBBottom;
Var
InputFile : TextFile;
OutputFile ; TextFile;
FileNameInput : string;
FileNameOutput : string;
InputString : string;
i : integer;
Begin
// Duplicate original ODB file
try // don't know if file exists
Begin
CopyFile(ODBBotCompFilePath,ODBBotCompFilePath+'.org', False);
// Open Input Text File
FileNameInput := ODBBotCompFilePath + '.org';
AssignFile(InputFile, FileNameInput);
Reset(InputFile);
// Open Output Text File
FileNameOutput := ODBBotCompFilePath + '.txt';
AssignFile(OutputFile, FileNameOutput);
Rewrite(OutputFile);
// Start writing new odb file header
WriteLN(OutputFile, '#');
WriteLN(OutputFile, '#Component attribute names');
WriteLN(OutputFile, '#');
WriteLN(OutputFile, '@0 .no_pop');
WriteLN(OutputFile, '@1 .comp_mount_type');
WriteLN(OutputFile, '@2 .comp_height');
// Read InputFile Content
//Text := '';
StartConvert := False;
// Skip header
repeat
ReadLN(InputFile, InputString);
if (InputString = NULL) then // Search for first empty line
// Component definitions start here
begin
WriteLN(OutputFile, '');
StartConvert := True;
end;
until (StartConvert = True);
// Process Components
while not Eof(InputFile) do
begin
ReadLN(InputFile, FileInput);
if (FileInput = NULL) then // Search for empty lines
begin
//Text := Text + #13 + #10;
end
else begin
// check if the line is a component definition
TextLine := FileInput;
if (AnsiStartsStr('CMP', TextLine)) then
begin
// if yes ...
for i:=1 to 8 do
begin
TextLineLength := length(TextLine); // get text length
SegmentLength := AnsiPos(' ', TextLine); // Search for first space
TextSegment[i] := AnsiLeftStr(TextLine, SegmentLength-1); // copy Segment here
TextLine := AnsiRightStr(TextLine, TextLineLength - SegmentLength); // shorten text
end;
TextLine := AnsiRightStr(TextLine, length(TextLine)-1);
CommaPosition := AnsiPos(',', TextLine);
TextSegment[10] := AnsiLeftStr(TextLine, CommaPosition-1);
TextSegment[10] := AnsiRightStr(TextSegment[10], 1);
TextSegment[11] := AnsiRightStr(TextLine, length(TextLine)-CommaPosition);
TestText := TextSegment[11];
i := length(TestText);
TextSegment[11] := AnsiRightStr(TextSegment[11], i-2);
PartDesignator := TextSegment[7];
PartNumber := TextSegment[8];
if (AnsiContainsText(BomString, PartDesignator)) then
begin
TextSegment[9]:='';
end
else begin
TextSegment[9]:='0,';
end;
NewString := TextSegment[1] + ' ' + TextSegment[2] + ' ' + TextSegment[3] + ' ' + TextSegment[4] + ' ' + TextSegment[5] + ' ' + TextSegment[6] + ' ' + TextSegment[7] + ' ' + TextSegment[8] + ' ;' + TextSegment[9] + '1=' + TextSegment[10] + ',2=' + TextSegment[11];
WriteLN(OutputFile, NewString);
end
else begin
// if not write text without modification
WriteLN(OutputFile, TextLine);
//Text := Text + TextLine;
//Text := Text + #13 + #10;
end;
end;
end;
// Close both files when done
CloseFile(InputFile);
CloseFile(OutputFile);
deletefile(ODBBotCompFilePath); // Delete original file
CopyFile(ODBBotCompFilePath+'.txt',ODBBotCompFilePath, False); //replace it with the modified copy
deletefile(ODBBotCompFilePath+'.txt'); // Delete temorary copy
deletefile(ODBBotCompFilePath+'.org'); // Delete temorary copy
End;
except
//Begin
Showmessage('Datei Bot existiert nicht');
CloseFile(InputFile);
CloseFile(OutputFile);
End;
End;
Procedure AddBomToODB;
Begin
GetBomData;
// Check for Top-Layer components
If (FileExists(ODBTopCompFilePath) = True) Then
ProcessODBTop;
// Check for Bottom Layer components
If (FileExists(ODBBotCompFilePath) = True) Then
ProcessODBBottom;
End;
Procedure PredictOutputFileNames(Parameters : string);
Begin
End;
Procedure Generate(Parameters : string);
Var
FileInfo : string;
EBADirPath : string;
Begin
ProcessParameters(Parameters);
BOMFilePath := StartParameters[1] + 'BOM\' + StartParameters[2] + '.txt';
EBADirPath := StartParameters[1] + 'ODB\odb\steps\pcb1'; // Check for Embedded Board Array
if (DirectoryExists(EBADirPath) = True) Then
Begin // Has Embedded Board Array
ODBTopCompFilePath := StartParameters[1] + 'ODB\odb\steps\pcb1\layers\comp_+_top\components';
ODBBotCompFilePath := StartParameters[1] + 'ODB\odb\steps\pcb1\layers\comp_+_bot\components';
End
Else Begin // Is single PCB
ODBTopCompFilePath := StartParameters[1] + 'ODB\odb\steps\pcb\layers\comp_+_top\components';
ODBBotCompFilePath := StartParameters[1] + 'ODB\odb\steps\pcb\layers\comp_+_bot\components';
End;
AddBomToODB;
FileInfo := StartParameters[1] + 'ODB\' + StartParameters[2] + '.tgz'; // Path to .tgz File
deletefile(FileInfo); // Delete tgz file
FileInfo := StartParameters[1] + 'ODB\' + StartParameters[2] + '.zip'; // Path to .zip File
deletefile(FileInfo); // Delete zip file
FileInfo := StartParameters[1] + '\ODB' + StartParameters[2] + '.rep'; // Path to Report File
deletefile(FileInfo); // Delete Report file
FileInfo := '"C:\Program Files\7-Zip\7z.exe" a' + ' "' + StartParameters[1] + 'ODB\' + StartParameters[2] + '.tar"' + ' "' + StartParameters[1] + 'ODB\odb"';
ErrorCode := RunApplication(FileInfo);
sleep(3000); // wait some time
FileInfo := '"C:\Program Files\7-Zip\7z.exe" a' + ' "' + StartParameters[1] + 'ODB\' + StartParameters[2] + '.tgz"' + ' "' + StartParameters[1] + 'ODB\' + StartParameters[2] + '.tar"';
ErrorCode := RunApplication(FileInfo);
End;
End.