-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pas
419 lines (390 loc) · 12.2 KB
/
main.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
417
418
unit main;
{
* Author A.Kouznetsov
* Rev 1.01 dated 4/6/2015
Redistribution and use in source and binary forms, with or without modification, are permitted.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.}
{$mode objfpc}{$H+}
// ###########################################################################
interface
// ###########################################################################
uses
Classes, SysUtils, Windows, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
StdCtrls, Grids, ExtCtrls, ComCtrls, IniFiles, sch_to_bom, inventory_unit;
type
{ TForm1 }
TForm1 = class(TForm)
GroupBox1: TGroupBox;
MainMenu1: TMainMenu;
FileMenuItem: TMenuItem;
Pop1CopyTo: TMenuItem;
Pop1CopyFrom: TMenuItem;
Pop1Find: TMenuItem;
MiAbout: TMenuItem;
MiNewBom: TMenuItem;
MiAddSch: TMenuItem;
OpenDialog1: TOpenDialog;
PageControl1: TPageControl;
PageControl2: TPageControl;
Pop1: TPopupMenu;
Pop2: TPopupMenu;
SaveDialog1: TSaveDialog;
MiSaveBom: TMenuItem;
MenuItem2: TMenuItem;
MiExit: TMenuItem;
Splitter1: TSplitter;
Splitter2: TSplitter;
StatusBar: TStatusBar;
SG1: TStringGrid;
SG2: TStringGrid;
SG3: TStringGrid;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheetSep: TTabSheet;
TabSheetGr: TTabSheet;
TreeView1: TTreeView;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure MiAboutClick(Sender: TObject);
procedure MiExitClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure MiNewBomClick(Sender: TObject);
procedure MiAddSchClick(Sender: TObject);
procedure MiSaveBomClick(Sender: TObject);
procedure Pop1FindClick(Sender: TObject);
procedure SG2GetCellHint(Sender: TObject; ACol, ARow: Integer;
var HintText: String);
procedure SG2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure TreeView1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
SchBOM : TKicadSchBOM;
Stock : TKicadStock;
BomFileName : string;
const
revision = '1.02';
// ###########################################################################
implementation
// ###########################################################################
{$R *.lfm}
// ==================================================
// Remove file ext
// ==================================================
function file_name_no_ext(fn : string) : string;
var i : integer;
c : char;
begin
result := fn;
for i := Length(fn) downto 2 do begin
c := fn[i];
if c = '.' then begin
result := Copy(fn,1,i-1);
break;
end;
end;
end;
{ TForm1 }
// ==================================================
// Create
// ==================================================
procedure TForm1.FormCreate(Sender: TObject);
var Inif : TIniFile;
s, ss, sn, si, inv_path, fn : string;
i, j, k, x, n : integer;
sec: TStringList;
inv : boolean;
ttn : TTreeNode;
begin
SchBOM := TKicadSchBOM.Create;
Stock := TKicadStock.Create ;
Inif := TIniFile.Create('KiCadBOM.ini');
// --------------------------
// Restore window
// --------------------------
i := Inif.ReadInteger('Window','Width',0);
if i > 400 then
Form1.Width := i;
i := Inif.ReadInteger('Window','Height',0);
if i > 200 then
Form1.Height := i;
i := Inif.ReadInteger('Window','Left',0);
Form1.Left := i;
i := Inif.ReadInteger('Window','Top',0);
Form1.Top := i;
PageControl1.Width := Inif.ReadInteger('Window','Split',200);
// --------------------------
// Exclude components with certain RefDes
// --------------------------
for i:=1 to 1000 do begin
si := 'Ref'+IntToStr(i);
s := AnsiUpperCase(Inif.ReadString('Excluded Ref',si,''));
if s = '' then
break
else
SchBOM.FExcludedRef.Add(s);
end;
// --------------------------
// Exclude library prefixes from footprints
// --------------------------
for i:=1 to 1000 do begin
si := 'Prefix'+IntToStr(i);
s := AnsiUpperCase(Inif.ReadString('Excluded Footprint Prefix',si,''));
if s = '' then
break
else
SchBOM.FExcludedFootPrefix.Add(s);
end;
// --------------------------
// Read inventory (stock) path
// --------------------------
inv := false;
si := 'Path';
inv_path := AnsiUpperCase(Inif.ReadString('Inventory',si,''));
Inif.Free;
// --------------------------
// Read inventory
// --------------------------
if inv_path <> '' then begin
k := 0;
sec := TStringList.Create;
Inif := TIniFile.Create(inv_path+'\Inventory.ini');
Inif.ReadSections(sec);
for j:=0 to sec.Count-1 do begin
sn := sec.Strings[j];
if j = 0 then
TreeView1.Items.Add(nil,sn)
else
TreeView1.Items.Add(TreeView1.Items[k],sn);
k := TreeView1.Items.Count-1;
for i:=1 to 1000 do begin
// ---------------------
// get file name
// ---------------------
si := 'File'+IntToStr(i);
s := Inif.ReadString(sn,si,'');
if s = '' then
break // finish section
else begin
// ---------------------
// get name for TreeView
// ---------------------
fn := inv_path + '\' + s;
if FileExists(fn) then begin
// ---------------------
// get Ref name
// ---------------------
si := 'Ref'+IntToStr(i);
ss := Trim(Inif.ReadString(sn,si,'?'));
Stock.RefNames.Add(ss);
// ---------------------
// get name
// ---------------------
si := 'Name'+IntToStr(i);
ss := Inif.ReadString(sn,si,'');
if ss = '' then
ss := file_name_no_ext(s); // use file name
Stock.FileNames.Add(fn);
x := Stock.FileNames.Count;
ttn := TreeView1.Items.AddChildObject(TreeView1.Items[k],ss, Pointer(x));
// ---------------------
// load first to SG3
// ---------------------
if not inv then begin
inv := true;
ttn.Selected := true;
n := Integer(ttn.Data);
if n > 0 then begin
Stock.ToStringGrid(SG3, n-1);
end;
end;
end;
end;
end;
end;
sec.Free;
Inif.Free;
end;
end;
// ==================================================
// Destroy
// ==================================================
procedure TForm1.FormDestroy(Sender: TObject);
begin
SchBOM.Free;
Stock.Free;
end;
// ==================================================
// Form resize
// ==================================================
procedure TForm1.FormResize(Sender: TObject);
var
x, y, w, aw, i: integer;
MaxWidth: integer;
SG : TStringGrid;
begin
for i := 1 to 3 do begin
case i of
1 : SG := SG1;
2 : SG := SG2;
3 : SG := SG3;
end;
aw := 0;
with SG do begin
for x := 0 to ColCount-2 do begin
MaxWidth := Canvas.TextWidth(Columns[x].Title.Caption)+10;
if MaxWidth < 50 then
MaxWidth := 50;
for y := 0 to RowCount-1 do
begin
w := Canvas.TextWidth(Cells[x,y]);
if w > MaxWidth then
MaxWidth := w;
end;
ColWidths[x] := MaxWidth + 10;
aw := aw + ColWidths[x];
end;
ColWidths[ColCount-1] := ClientWidth - aw;
end;
end;
end;
// ==================================================
// New BOM
// ==================================================
procedure TForm1.MiNewBomClick(Sender: TObject);
begin
SchBOM.Reset;
SchBOM.ToStringGrid(SG1);
SG1.RowCount := 50;
SchBOM.FPartsList.ToStringGrid(SG2);
SG2.RowCount := 50;
MiAddSchClick(Sender);
BomFileName := ExtractFileName(OpenDialog1.FileName);
BomFileName := file_name_no_ext(BomFileName);
StatusBar.Panels[2].Text := BomFileName;
FormResize(Sender);
end;
// ==================================================
// Add SCH file
// ==================================================
procedure TForm1.MiAddSchClick(Sender: TObject);
begin
if OpenDialog1.execute then begin
SchBOM.ReadFile(OpenDialog1.FileName, true);
SchBOM.ToStringGrid(SG1);
SchBOM.FPartsList.ToStringGrid(SG2);
FormResize(Sender);
end;
StatusBar.Panels[0].Text:=IntToStr(SchBOM.Count-1) + ' items';
StatusBar.Panels[1].Text:=IntToStr(SchBOM.FPartsList.Count-1) + ' groups';
end;
// ==================================================
// Save BOM
// ==================================================
procedure TForm1.MiSaveBomClick(Sender: TObject);
begin
if PageControl1.TabIndex = 0 then begin;
SaveDialog1.Title := 'Save BOM';
SaveDialog1.FileName := BomFileName+'_BOM.csv';
if SaveDialog1.execute then begin
SchBOM.SaveCSV(SaveDialog1.FileName, BomFileName, SG1);
end;
end else begin
SaveDialog1.Title := 'Save Grouped BOM';
SaveDialog1.FileName := BomFileName+'_PL.csv';
if SaveDialog1.execute then begin
SchBOM.SaveCSV(SaveDialog1.FileName, BomFileName, SG2);
end;
end;
FormResize(Sender);
end;
// ==================================================
// Find selected in inventory
// ==================================================
procedure TForm1.Pop1FindClick(Sender: TObject);
begin
end;
// ==================================================
// SG2 cell hint
// ==================================================
procedure TForm1.SG2GetCellHint(Sender: TObject; ACol, ARow: Integer;
var HintText: String);
begin
if ACol = 0 then
SG2.Hint:=HintText;
end;
// ==================================================
// SG2 mouse move
// ==================================================
procedure TForm1.SG2MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer
);
var
Row, Col: Integer;
s : string;
begin
Row := 1;
Col := 1;
SG2.MouseToCell(X, Y, Col, Row);
if Col = 0 then begin
s := SchBOM.FPartsList.GetRef(Row);
SG2GetCellHint(Sender, Col, Row, s);
end else
Application.CancelHint;
end;
// ==================================================
// Click on inventory group
// ==================================================
procedure TForm1.TreeView1Click(Sender: TObject);
var ttn : TTreeNode;
i : integer;
begin
ttn := TreeView1.Selected;
if ttn = nil then
exit;
if not ttn.HasChildren then begin
i := Integer(ttn.Data);
if i > 0 then begin
Stock.ToStringGrid(SG3, i-1);
end;
end;
end;
// ==================================================
// Exit
// ==================================================
procedure TForm1.MiExitClick(Sender: TObject);
begin
SchBOM.Free;
Application.Terminate;
end;
// ==================================================
// About
// ==================================================
procedure TForm1.MiAboutClick(Sender: TObject);
begin
ShowMessage('KiCAD BOM Generator rev '+revision+char(13)+'This program is free software');
end;
// ==================================================
// Close
// ==================================================
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var Inif : TIniFile;
begin
Inif := TIniFile.Create('KiCadBOM.ini');
Inif.WriteInteger('Window','Width',Form1.Width);
Inif.WriteInteger('Window','Height',Form1.Height);
Inif.WriteInteger('Window','Left',Form1.Left);
Inif.WriteInteger('Window','Top',Form1.Top);
Inif.WriteInteger('Window','Split',Splitter2.Left);
Inif.Free;
end;
end.