-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbom-ex.ulp
More file actions
3917 lines (3293 loc) · 113 KB
/
bom-ex.ulp
File metadata and controls
3917 lines (3293 loc) · 113 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
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
//////////////////////////////////////////////////////////////////////////////
//
// BOM-EX ENHANCED BILL OF MATERIALS
//
// Copyright (C) 2010-2014, Robert E. Starr
//
// Get the latest version at http://www.bobstarr.net
//
// Additional enhancements by Malcolm Miranda and Markus Baertschi
//
// REVISION HISTORY:
//
// 1.07 RES 04/01/10 Initial Release
//
// 1.08 RES 04/07/10 Added auto-load database support from global attribute.
// If global attr 'DATABASE' exists we use the value
// as the file pathname and load the database at startup.
//
// 1.09 RES 04/14/10 Minor user interface changes to save space.
//
// 1.10 RES 04/14/10 Added support for DNP attribute. Part ID names will
// have (DNP) appended if the DNP attribute is set for
// on the part.
//
// 1.11 RES 04/16/10 Added logic from part num manager to allow importing,
// editing and updating of part numbers from within
// BOM viewer. In list by name view part numbers are
// editable and in list by value mode database parts may
// be edited.
//
// 1.12 RES 04/19/10 Added logic to prevent DNP parts from appearing
// in the order export files.
//
// 1.13 RES 04/21/10 Fixed bug causing multi-gate parts on different sheets
// to appear multiple times in the bom list views.
//
// 1.14 RES 04/22/10 Changed database column heading names and fixed bug
// when exporting order files and a blank partname was
// was encountered.
//
// 1.15 RES 04/23/10 Fixed problem with BOM-ENTRY part-id missing.
// Added logic to include addtional devices that
// have no package but need to be included in the BOM
// for ordering purposes (e.g. jumper blocks, etc).
//
// 1.16 RES 05/03/10 Added new dlgSelectionChanged() logic to part no
// pick list dialog. Part number is updated when the
// user clicks a different item. Double clicking accepts
// the current item selected and closes the dialog.
//
// 1.17 RES 08/30/10 Added build qty options for digikey & generic order
// export files. Fixed problem with generic CSV export
// where DNP parts were being omitted.
//
// 1.18 RES 09/04/10 Added options to edit build qty's for order export.
//
// 1.19 RES 10/13/10 Fixed quantity calculations for parts with DNP
// attributes for part order exports.
//
// 1.20 RES 11/15/10 Added logic to automatically use the parts id's for
// the customer reference if the Customer Ref column is
// not provided in the Digikey export function.
//
// 1.21 RES 11/29/10 Modified SaveBOM to append ListByPartID or ListByPartValue
// to the filename depending on the current list view mode.
//
// 1.22 RES 02/13/11 Added export part number only option in export dialog.
//
// 1.23 MM 02/25/11 Set part# enhancment allows setting groups of part
// numbers in list by value mode!
//
// 1.24 RES 02/26/11 Added database editing functionality and ability to
// enter a new part VALUE also when assigning part numbers.
//
// 1.25 RES 02/26/11 Added export part numbers options to specify all part
// numbers or part numbers assigned but not found in the
// database. Changed sort orders for exporting so data
// appears in a more logical order.
//
// 1.26 RES 02/28/11 Restored original sort calls to fix bugs introduced
// by inadvertant changes.
//
// 1.27 RES 03/08/11 Added QTY attribute processing logic to allow
// BOM_ENTRY objects to specify an optional quantity.
//
// 1.28 RES 03/25/11 Changed Save BOM to use BOM configuration directory
// rather than default schematic directory.
//
// 1.29 RES 04/29/11 Fixed DNP attribute check in CollectData(). Values other
// than "T" were still being treated as DNP enabled.
//
// 1.30 RES 06/05/11 Changed generice CSV export to enclose part designators
// string in quotes with commas between part names for
// generic order export file. This allows Excel to import
// properly with commas between names. Also added part
// value column to generic order export.
//
// 1.31 RES 07/27/11 Removed three quantity options. Export order options
// now prompt the user for the number of units to build.
//
// 1.32 RES 09/16/11 Added ability to change package along with part number
// when editing a part number.
//
// 1.33 RES 10/17/11 Added option to override the part description with
// DESC attribute. Mainly for use by BOM-ENTRY entries.
//
// 1.34 RES 10/30/11 Added buttons to allow clearing part numbers.
//
// 1.35 RES 11/17/11 Added optional PN part number name support. User
// may specify as PARTNO, PARTNUM or PN as attribute name.
//
// 1.36 RES 01/27/12 Added new Mouser CSV order upload format.
//
// 1.37 RES 02/08/12 Added variable to allow changing name of PARTNO
// attribute to 'PN' for compatibility with the naming
// convention used in Eagle v6.0 libraries.
//
// Added logic to only update part numbers that were
// changed for each particular part. This improves
// performance significnatly by reducing the size of
// the script in cases where only a few part numbers
// are being updated. A checkbox was also added to apply
// partnumber attributes globally if desired.
//
// 1.38 RES 02/16/12 Fixed bug with clear all part# attributes and when
// clearing partno attributes for a single part.
//
// 1.39 RES 02/24/12 Mosly minor UI changes.
//
// 1.40 RES 03/03/12 Changed default order upload file suffixes.
//
// 1.41 RES 03/16/12 Modified the Export and order file to compare part
// value and part number order first. If the part number
// is not supplied then it compares value and the part
// package. This was done to avoid duplicate line items
// in the BOM order export files where the same part might
// be used with slightly difference packages (eg a cap
// or other part packages with and without silkscreen
// perimeter outlines).
//
// 1.42 RES 03/22/12 Fixed bugs from 1.41 changes.
//
// 1.43 RES 03/22/12 Added tabbed text and CSV file save BOM formats.
//
// 1.44 RES 04/09/12 Added part-id auto xref generation for database.
// Changed export generic BOM output format.
//
// 1.45 RES 04/13/12 Added config file save/restore settings.
//
// 1.46 RES 05/17/12 Added command line option to set default window size.
//
// 1.47 RES 07/19/12 Integrated changes from Markus to trim quotes from
// input database records fields.
//
// 1.48 MB 07/19/12 Added filtering for pick part dialog.
//
// 1.49 RES 08/05/12 Remove database create/edit features. Databases files
// should be edited externally with a spreadsheet.
//
// 1.50 RES 02/22/13 Added package check scanning feature.
//
// 1.51 RES 06/21/13 Added option to override and include DNP parts in
// the part order files.
//
// 1.52 RES 07/06/13 Fixed case sensitivity for attribute names when
// collecting the known part attribute names at startup.
//
// 1.53 RES 07/22/13 Added package check scanning feature.
//
// 1.54 RES 09/11/13 Enhanced value checker function to tokenize the value
// value string delimited by a space and search for each
// token in the part description text.
//
// 1.55 RES 09/24/13 Modified Digikey order file generation to allow sorting
// only by the mfg part number and ignore the PartVal
// to avoid duplicate line items when folks use the part
// value field as a generic catch all.
//
// 1.56 RES 10/10/13 Added support for assembly variants. Note that a
// DNP attributes overrides the new assembly variant
// place option. Thus, DNP attribute now act in a global
// fashion to exclude parts.
//
// 1.57 RES 10/24/13 Minor enhancements.
//
// 1.58 RES 11/26/13 Corrected list view sort problems when regnerating view
// list during part edits and such.
//
// 1.59 RES 11/30/13 Improved part value checker tool warnings.
//
// 1.60 RES 03/10/14 Changed HTML report to center text cells left/top.
//
// 1.61 RES 06/17/14 Added sort by part number option on other part order
// export options. Initially this was only on the Digikey
// option.
//
// 1.62 RES 08/05/14 Added logic to reckognize MFPN for part numbers.
//
// 1.63 RES 08/11/14 Added option to show parts with EXCLUDE attribute.
//
// 1.64 RES 12/02/14 Extended support for Eagle v7.
//
// 1.65 RES 12/02/14 Fix for parts with sheet 0.
//
// 1.66 RES 5/7/15 Fix for setting module part number ID references.
//
// 1.67 RES 12/10/15 Updated DigiKey export column headings record.
//
// 1.68 RES 12/10/15 Added reload database button.
//
// 1.69 RES 02/08/18 Removed automatically added XREF column field.
// Added sorted output for BOM save files
//
//////////////////////////////////////////////////////////////////////////////
//
// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESSED OR IMPLIED. IF YOU DON'T LIKE IT, DON'T USE IT!
//
//////////////////////////////////////////////////////////////////////////////
#require 6.5100
#usage "en: <b>Export a Bill Of Material</b> - v1.69\n"
"<p>"
"Generates a project's <i>Bill Of Material</i>."
"<p>"
"A database with additional information like order codes, manufacturers "
"or prices can be created and managed."
"<p>"
"This version uses ATRRIBUTE[PARTNO] for the database key. Thus, all parts "
"in a schematic should have an attribute of 'PARTNO' with the mfg part number. "
"Additionally the PARTNO attribute is used as the lookup key in the part database."
"<p>Get the latest version at: http://www.bobstarr.net<p>"
"<author>Author: Bob Starr (rtzaudio@comcast.net)</author>",
"de: <b>Stückliste exportieren</b>\n"
"<p>"
"Erzeugt die <i>Stückliste</i> (Bill Of Material) eines Projekts."
"<p>"
"Eine Datenbank mit zusätzlichen Informationen wie Bestellnummern, Herstellern "
"oder Preisen kann angelegt und verwaltet werden."
"<p>Get the latest version at: http://www.bobstarr.net<p>"
"<author>Author: Bob Starr (rtzaudio@comcast.net)</author>"
string HelpTextEN =
"<b>HOW TO GENERATE A BILL OF MATERIALS</b>\n"
"<p>\n"
"<b>LIST TYPE</b>\n"
"<p>\n"
"The <i>Bill Of Materials</i> can be generated either as a list\n"
"of parts (where every part is listed on a line of its own),\n"
"or as a list of values, where all parts with the same value are grouped\n"
"together in one line. Use the <b>List by</b> combo box to view the list\n"
"by 'Parts' or 'Values'.\n"
"<p>\n"
"<b>Saving the Report</b>\n"
"<p>\n"
"The <b>Format</b> combo box sets the save report output to pure ASCII\n"
"<b>Text</b> or <b>HTML</b> format. Click on the <b><u>S</u>ave BOM</b>\n"
"button to save the report to disk. Click on the <b>Preview</b> button\n"
"to see a preview of the report data that will be saved to disk.\n"
"<p>\n"
"<b>PART DATABASE</b>\n"
"<p>\n"
"You can pull in additional information about the used parts by loading\n"
"a database file with the <b><u>L</u>oad</b> button.\n"
"<p>\n"
"A database file must consist of lines of text, each of which contains\n"
"one record consisting of CSV (<u>C</u>omma <u>S</u>eparated <u>V</u>alues)\n"
"or txt (<u>T</u>ab <u>S</u>eparated <u>V</u>alues) data.\n"
"The very first line must contain a \"header\", which defines a unique name for\n"
"each column, and the first column of every following line must contain\n"
"a unique (non-empty) key for this record.\n"
"<p>\n"
"An example for a valid database file would be:\n"
"<pre>\n"
" Mfg Part Num Mfg Name VID Vendor Part Num Description\n"
" MKS2C031001A00KSSD WIMA ME 505-MKS2.1/63/10 Polyester Film Capacitor 63V .1uF 10%\n"
" C315C220K2G5TA Kemet ME 80-C315C220K2G Multilayer Ceramic Capacitor (MLCC) - 200volts 22pF 10% C0G\n"
"</pre>\n"
"Note that the columns are separated by a <b>tab</b> character (you may also\n"
"use a semicolon (';') to separate the columns, but then you will have to make sure\n"
"none of the data items contains a semicolon).\n"
"<p>\n"
"<b>ATTRIBUTES FOR BOM CONTROL</b>\n"
"<p>\n"
"The keys for looking up records in the database are built from the\n"
"part <b>ATTRIBUTE[PARTNO]</b> field. Each part should have an attribute of <b>PARTNO</b> defined \n"
"that is used to lookup in the database against the <b>Key</b> field described above.\n"
"<p>\n"
"The following part ATTRIBUTES are reckognized by the BOM processor:\n"
"<pre>\n"
" Attribute Value Description\n"
" ---------------------------------------------------------------------------------\n"
" PARTNO [<i>MfgPartNum</i>] Specifies mfg part number key in the database file.\n"
" BOM [EXCLUDE] Excludes part from the BOM listing.\n"
" DNP [T/F] Do not place identifier (T=true or F=false)\n"
"</pre>\n"
"<p>\n"
"Addtionally the following special attribtues are reckognized and concatenated to the\n"
"parts VALUE field during bom generation to list additional information for a part.\n"
"In some cases a part with the same value and package will differ for tolerance, temp\n"
"codes, etc. These additional attributes are concatenated to the part value field during\n"
"BOM generation so the part is given a unique line item entry. NOTE this behavior can be\n"
"overridden with the BOM[NOVALMOD] attribute described above. If the [NOVALMOD] flag is\n"
"also specified, the original value field will be used as is for value key comparisions\n"
"and the attribute modifiers below will not appear appended to the part value in the BOM"
"\n"
"<pre>\n"
" Attribute Description\n"
" --------------------------------------------------------\n"
" TOL tolerance (1%, 5%, etc)\n"
" TC temp coefficient (caps, NPO, X7R, etc)\n"
" VOLT voltage rating (caps, etc)\n"
" RATE rating (watts, etc)\n"
" COLOR color (for leds, etc)\n"
" LABEL label (for buttons, switches, etc)\n"
" TYPE type field\n"
" SIZE package or other size code (0805, etc)\n"
" LOAD load field\n"
" PLACE place option text\n"
" OPT generic option text\n"
" DESC part description override text\n"
"</pre>\n"
"<p>\n"
"<b>EXPORTING AN ORDER BOM</b>\n"
"<p>\n"
"You can export a BOM in Digi-Key BOM upload order format in CSV or TAB format.\n"
"However, this feature requires that part database be in the following format\n"
"with the exact column field names as shown below (less ticks):\n"
"<p>\n"
"<pre>\n"
"Col Field Name Description\n"
"-------------------------------------------------------------------\n"
" 0 'Mfg Part Num' PARTNUM key for schematic\n"
" 1 'Mfg Name' Mfg name of the part\n"
" 2 'VID' Vendor ID (eg 'DK'=Digikey)\n"
" 3 'Vendor Part Num' Specifies the vendor part number\n"
" 4 'Description' Part description (OPTIONAL)\n"
" 5 'Pkg Code' Package code 0603, 0504, etc (OPTIONAL)\n"
"</pre>\n"
"<p>\n"
"Note that the 'Qty' fields are optional and will be calculated\n"
"automatically if not specified when exporting order files.\n"
"<p>\n"
;
//////////////////////////////////////////////////////////////////////////////
//
string strVersion = "1.69";
int nPartCount;
int nBOMCount;
string strBOMCount;
numeric string Lines[];
int PartSheet[];
int PartDNP[];
int PartQty[];
numeric string PartNum[], PartName[], PartVal[], PartAttr[],
PartDev[], PartPkg[], PartDesc[];
numeric string PartNumPrev[];
numeric string PartValSave[];
numeric string PartDevChange[];
numeric string PartModule[];
numeric string PartModulepartName[];
int Selected = -1;
int SortCol = 0;
enum { ltParts, ltValues }; // List Types
enum { ofText, ofTextTabbed, ofTextCSV, ofHTML }; // Output Formats
int ListType = ltParts;
int OutputFormat = ofText;
int ApplyAllAttrs = 0;
int ShowExcludedParts = 0;
string DefaultFileName;
int DatabaseRecCount;
string Database[];
string DatabaseFields[];
char DatabaseSeparator = '\t';
string DatabaseFilename;
int PartNumModified = 0;
int PartNumPicked = 0;
char ValueSeparator = ':';
string ConfigFilePath;
string ConfigFileName = "bom-ex.cfg";
string nullKey = "*";
string dnpSuffix = "(DNP)";
// This sets the default minimum window size. Make it smaller if you
// have a small monitor. Or, you can override this with a command line
// option.
int WindowMinWidth = 1200;
// This defines the attribute name that will be written to parts
// in the schematic/board. Note any old part number attribute names are
// preserved and the new attributes will may also be assigned to the
// name below.
string strPartNumAttrName = "PARTNO";
int BldQty1 = 1; // specifies the qty of units to build
// Build Variant Support
string CurrentVariant = "";
string Variants[] = { "" };
int cntVD = 0;
int VDsel = 0;
//////////////////////////////////////////////////////////////////////////////
// First, process any command line options
/*
void debug (string s)
{
output("debug.out","at")
{
printf("%s",s);
};
}
*/
void unknown(string m)
{
if (dlgMessageBox("unknown parameter\n" + m, "OK", "Cancel" ) != 0) exit(-1);
return;
}
if (argc > 1)
{
int n = 1;
string para;
do {
para = strupr(argv[n]);
if (para[0] == 'W' ) { para[0] = ' '; WindowMinWidth = strtod(para); }
else if (para[0] == 'B' ) { para[0] = ' '; BldQty1 = strtod(para); }
else if (para[0] == 'C' ) { ConfigFilePath = strsub(para, 1); }
else unknown(para);
n++;
} while (argv[n]);
}
//////////////////////////////////////////////////////////////////////////////
// Some Helper Functions
int isquote(char c)
{
return (c == '"') ? 1 : 0;
}
// Trim leading and trailing white space characters
string TrimWhiteSpace(string s)
{
while (s && isspace(s[0]))
s = strsub(s, 1);
while (s && isspace(s[strlen(s) - 1]))
s = strsub(s, 0, strlen(s) - 1);
return s;
}
// Trim leading and trailing quote characters from string
string TrimQuotes(string s)
{
while (s && isquote(s[0]))
s = strsub(s, 1);
while (s && isquote(s[strlen(s) - 1]))
s = strsub(s, 0, strlen(s) - 1);
return s;
}
// Trim DNP suffix from a string
string TrimDNPSuffix(string s)
{
// we have to strip off any (DNP) suffix from the part name
int lensuffix = strlen(dnpSuffix);
int lenstr = strlen(s);
if (lenstr > lensuffix)
{
if (strstr(s, dnpSuffix) > 0)
s = strsub(s, 0, lenstr - lensuffix);
}
return s;
}
int PartSearch(string partname)
{
for (int i=0; i < nPartCount; i++)
{
if (PartName[i] == partname)
return i;
}
return -1;
}
//////////////////////////////////////////////////////////////////////////////
// Here we're checking for a specific set of known attributes that
// we will allow to display in the Attributes column of the BOM. Any other
// part attributes we will ignore for now.
int IsKnownAttribute(string attrname)
{
string names = "TOL:TEMP:TC:VOLT:VOLTAGE:RATE:RATING:COLOR:LABEL:TYPE:SIZE:LOAD:PLACE:FREQ:OPT";
string a[];
int n = strsplit(a, names, ':');
int i;
for (i=0; i < n; i++)
{
if (attrname == a[i])
return 1;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// Here we're checking for a specific device names that have no associated
// package that we want added to the BOM listing also. We don't add any
// devices without packages to listing except those listed here.
int IsKnownDevice(string devname)
{
string names = "BOM-ENTRY:BOM-PART:JUMPER:JUMP-A:JUMP-B:JUMP-Q";
string a[];
int n = strsplit(a, names, ':');
int i;
for (i=0; i < n; i++)
{
if (devname == a[i])
return 1;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// Collect all part data for the BOM and process and handle any attribute
// modifiers found for the part.
void AddPartData(UL_PART P, UL_INSTANCE I)
{
int dnp = 0;
int exclude = 0;
if ((P.device.package) || IsKnownDevice(P.device.name))
{
// PART NUMBER CANNOT BE EMPTY FOR LIST BY VALUE SORT!
string partnum = nullKey;
string val = P.value;
string attrs = "";
string desc = "";
int qty = 0;
if (!P.populate)
dnp = 1;
P.attributes(A)
{
// Get the attribute name and value in upper case
string aname = strupr(A.name);
string avalue = strupr(A.value);
if ((aname == "PARTNUM") || (aname == "PARTNO") || (aname == "PN") || (aname == "MPN") || (aname == "MFPN"))
{
//if (strPartNumAttrName != aname)
// strPartNumAttrName = aname;
// trim any leading/trailing white space from part number
partnum = TrimWhiteSpace(avalue);
// set to nullkey if empty PARTNO attribute
if (partnum == "")
partnum = nullKey;
}
else if (aname == "VALUE")
{
// Attribute VALUE overrides native value
if (strlen(avalue))
val = avalue;
}
else if (aname == "BOM")
{
if (!ShowExcludedParts)
{
// Exclude part from BOM?
if (avalue == "EXCLUDE")
exclude = 1;
}
}
else if (aname == "DNP")
{
// Exclude part from BOM?
if (avalue == "T")
dnp = 1;
}
else if (aname == "QTY")
{
// Exclude part from BOM?
qty = strtol(A.value);
}
else if (aname == "DESC")
{
// Part description override (mainly for BOM-ENTRY devices)
desc = TrimWhiteSpace(A.value);
}
else
{
if (IsKnownAttribute(aname))
{
if (strlen(attrs))
attrs += "/";
attrs += A.value;
}
}
}
if (!exclude)
{
// We have to check to make sure the part has not already been added
// since it's possible a multi-gate part can appear on multiple sheets.
//
// In this case we only need to update the attributes on the first
// instance encountered for the attributes update process.
if (!lookup(PartName, P.name, 0))
{
/* Ignore parts if sheet 0, this indicates uninvoked gates */
if (I.sheet == 0)
{
string msg;
sprintf(msg, "Warning: Part '%s' may have uninvoked gates!", P.name);
dlgMessageBox(msg, "OK");
}
else
{
PartSheet[nPartCount] = I.sheet;
PartName[nPartCount] = P.name;
PartVal[nPartCount] = val;
PartValSave[nPartCount] = val;
PartAttr[nPartCount] = attrs;
PartNum[nPartCount] = partnum;
PartNumPrev[nPartCount] = partnum;
PartDev[nPartCount] = P.device.name;
PartPkg[nPartCount] = (P.device.package) ? P.device.package.name : "*";
PartDesc[nPartCount] = (desc != "") ? desc : P.device.headline;
PartDNP[nPartCount] = dnp;
PartQty[nPartCount] = qty;
PartModule[nPartCount] = (P.module) ? P.module.name : "";
PartModulepartName[nPartCount] = (P.module) ? P.modulepart.name : "";
if (!dnp)
++nBOMCount;
nPartCount++;
}
}
}
}
}
void CollectPartData(string var)
{
// We must zero out all the part names each time this function is called,
// otherwise the names still exist after each call and the lookup() call
// that checks for duplicates causes problems since it thinks everything
// is a duplicate on subsequent calls to this function.
int i;
for(i=0; i < nPartCount; i++)
PartName[i]="";
nPartCount = nBOMCount = 0;
schematic(S)
{
S.allparts(P)
{
// Module instances
if (P.modulepart) {
P.modulepart.instances(I)
AddPartData(P, I);
}
else
{
// Sheet instances
P.instances(I)
AddPartData(P, I);
}
}
}
//string msg;
//sprintf(msg, "Variant='%s' Count=%d BOMCount=%d", var, nPartCount, nBOMCount);
//dlgMessageBox(msg, "OK");
sprintf(strBOMCount, " Total Parts:%d", nBOMCount);
}
//////////////////////////////////////////////////////////////////////////////
//
string DatabaseHeader(void)
{
string s;
if (Database[0])
{
string a[];
int n = strsplit(a, Database[0], DatabaseSeparator);
int i;
for (i = 1; i < n; i++)
{
s += "\t" + a[i];
DatabaseFields[i - 1] = a[i];
}
DatabaseFields[i - 1] = "";
}
return s;
}
//////////////////////////////////////////////////////////////////////////////
//
string GetKey(int i)
{
return PartNum[i];
}
string DatabaseLookup(string key, int f)
{
string s;
s = lookup(Database, key, DatabaseFields[f], DatabaseSeparator);
if (!s || (s == DatabaseFields[f]))
s = nullKey;
return s;
}
// Functions to implement bubble sort
void SwapLines(int x, int y)
{
string temp = Lines[x];
Lines[x] = Lines[y];
Lines[y] = temp;
}
void SortLines(int sortcol, int numlines)
{
int i, j;
int cnt = numlines;
int col = sortcol - 1;
for (i=0; i < cnt-1; i++)
{
// Last i elements are already in place
for (j=1; j < cnt-i-1; j++)
{
numeric string a1[];
numeric string a2[];
strsplit(a1, Lines[j], '\t');
strsplit(a2, Lines[j+1], '\t');
if (a1[col] > a2[col])
SwapLines(j, j+1);
}
}
}
//////////////////////////////////////////////////////////////////////////////
// Generate parts list in part name order
void GeneratePartList(int nSortCol)
{
numeric string line;
int NumLines = 0;
//XXX column sequence?
Lines[NumLines++] = "Part\tValue\tAttributes\tPackage\tDescription\tPart Num" + DatabaseHeader();
for (int i=0; i < nPartCount; i++)
{
// append (DNP) suffix on part name if needed
string partname;
partname = PartName[i];
if (PartDNP[i])
partname += dnpSuffix;
Lines[NumLines] = partname + "\t" + PartVal[i] + "\t" +
PartAttr[i] + "\t" + PartPkg[i] + "\t" + PartDesc[i] + "\t" + PartNum[i];
if (Database[0])
{
string key = GetKey(i);
for (int f = 0; DatabaseFields[f]; f++)
Lines[NumLines] += "\t" + DatabaseLookup(key, f);
Lines[NumLines] += "\t" + key; // hidden field!
}
NumLines++;
}
Lines[NumLines] = "";
SortLines(nSortCol, NumLines);
}
//////////////////////////////////////////////////////////////////////////////
// Generate parts list in part value order
void GenerateValueList(int nSortCol)
{
int NumLines = 0;
int Index[];
//XXX column sequence?
Lines[NumLines++] = "Qty\tValue\tPart Num\tPackage\tParts" + DatabaseHeader();
sort(nPartCount, Index, PartVal, PartNum, PartDev, PartName);
for (int n1 = 0, n2 = 0; ++n2 <= nPartCount;)
{
int i1 = Index[n1];
if (n2 < nPartCount)
{
int i2 = Index[n2];
// check for same value but different partnum
if ((PartVal[i1] == PartVal[i2]) && (PartDev[i1] == PartDev[i2]))
{
if (strlen(PartNum[i1]) && strlen(PartNum[i2]))
{
if (PartNum[i1] == PartNum[i2])
continue;
}
else
{
continue;
}
}
}
string Quantity;
// Calculate the qty of parts needed to build one unit less any DNP parts.
int uqty = n2 - n1;
int nn1 = n1;
int nn2 = n2;
int ii1 = i1;
for (;;)
{
// append (DNP) suffix on part name if needed
if (PartDNP[ii1] && uqty)
--uqty;
if (++nn1 < nn2)
ii1 = Index[nn1];
else
break;
}
if (PartQty[i1])
uqty = PartQty[i1];
sprintf(Quantity, "%d", uqty);
Lines[NumLines] = Quantity + "\t" + PartVal[i1] + "\t" + PartNum[i1] + "\t" + PartPkg[i1] + "\t";
for (;;)
{
// append (DNP) suffix on part name if needed
string partname;
partname = PartName[i1];
if (PartDNP[i1])
partname += dnpSuffix;
Lines[NumLines] += partname;
if (++n1 < n2)
{
i1 = Index[n1];
Lines[NumLines] += ", ";
}
else
break;
}
if (Database[0])
{
string key = GetKey(i1);
for (int f = 0; DatabaseFields[f]; f++)
Lines[NumLines] += "\t" + DatabaseLookup(key, f);
Lines[NumLines] += "\t" + key; // hidden field!
}
NumLines++;
}
Lines[NumLines] = "";
SortLines(nSortCol, NumLines);
}
//////////////////////////////////////////////////////////////////////////////
//
void GenerateList(void)
{
switch (ListType)
{
case ltParts:
GeneratePartList(1);
//SortCol = 1;
break;
case ltValues:
GenerateValueList(5);
//SortCol = 5;
break;
}
}
//////////////////////////////////////////////////////////////////////////////
//
string MakeListHeader(void)
{
string s;
schematic(SCH) sprintf(s, "BOM-EX v%s exported from %s at %s", strVersion, SCH.name, t2string(time()));
return s;
}
string MakeListText(void)
{
int l, Width[];
for (l = 0; Lines[l]; l++)
{
string a[];
for (int n = strsplit(a, Lines[l], '\t'); n--;)
Width[n] = max(Width[n], strlen(a[n]));
}
string List;
//List = MakeListHeader() + "\n\n";
int numHeaders;
for (l = 0; Lines[l]; l++)
{
string line, a[];
int n = strsplit(a, Lines[l], '\t');
if (l == 0)
numHeaders = n;
else
n = numHeaders; // for the hidden key!
for (int i = 0; i < n; i++)
{
string s;
sprintf(s, "%s%-*s", line ? " " : "", Width[i], a[i]);
line += s;
}
List += line + "\n";
}
return List;
}
string MakeListTextTabbed(void)
{
int l;
int numHeaders;
string List;
for (l=0; Lines[l]; l++)
{
string line, a[];
int n = strsplit(a, Lines[l], '\t');
if (l == 0)
numHeaders = n;
else
n = numHeaders; // for the hidden key!
for (int i = 0; i < n; i++)