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
|
/* Print bill of materials file is which can be used for checking stock
and purchasing needed materials. */
#include "config.h"
#include "conf_core.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <genvector/vts0.h>
#include "build_run.h"
#include "board.h"
#include "data.h"
#include <librnd/core/error.h>
#include <librnd/core/rnd_printf.h>
#include <librnd/core/plugins.h>
#include <librnd/core/compat_misc.h>
#include <librnd/core/safe_fs.h>
#include <librnd/core/hid.h>
#include <librnd/core/hid_nogui.h>
#include <librnd/core/hid_attrib.h>
#include "hid_cam.h"
#include <librnd/core/hid_init.h>
const char *bom_cookie = "bom HID";
static rnd_export_opt_t bom_options[] = {
/* %start-doc options "8 BOM Creation"
@ftable @code
@item --bomfile <string>
Name of the BOM output file.
@end ftable
%end-doc
*/
{"bomfile", "Name of the BOM output file",
RND_HATT_STRING, 0, 0, {0, 0, 0}, 0, 0},
#define HA_bomfile 0
{"cam", "CAM instruction",
RND_HATT_STRING, 0, 0, {0, 0, 0}, 0, 0},
#define HA_cam 1
};
#define NUM_OPTIONS (sizeof(bom_options)/sizeof(bom_options[0]))
static rnd_hid_attr_val_t bom_values[NUM_OPTIONS];
static const char *bom_filename;
typedef struct pcb_bom_list_s {
char *descr;
char *value;
int num;
vts0_t refdes;
struct pcb_bom_list_s *next;
} pcb_bom_list_t;
static rnd_export_opt_t *bom_get_export_options(rnd_hid_t *hid, int *n)
{
char **val = bom_options[HA_bomfile].value;
if ((PCB != NULL) && ((val == NULL) || (*val == NULL) || (**val == '\0')))
pcb_derive_default_filename(PCB->hidlib.filename, &bom_options[HA_bomfile], ".bom");
if (n)
*n = NUM_OPTIONS;
return bom_options;
}
char *pcb_bom_clean_str(const char *in)
{
char *out;
int i;
if ((out = malloc((strlen(in) + 1) * sizeof(char))) == NULL) {
fprintf(stderr, "Error: pcb_bom_clean_str() malloc() failed\n");
exit(1);
}
/* copy over in to out with some character conversions.
Go all the way to then end to get the terminating \0 */
for (i = 0; i <= strlen(in); i++) {
switch (in[i]) {
case '"':
out[i] = '\'';
break;
default:
out[i] = in[i];
}
}
return out;
}
static pcb_bom_list_t *bom_insert(char *refdes, char *descr, char *value, pcb_bom_list_t * bom)
{
pcb_bom_list_t *newlist, *cur, *prev = NULL;
if (bom == NULL) {
/* this is the first subcircuit so automatically create an entry */
if ((newlist = malloc(sizeof(pcb_bom_list_t))) == NULL) {
fprintf(stderr, "malloc() failed in bom_insert()\n");
exit(1);
}
newlist->next = NULL;
newlist->descr = rnd_strdup(descr);
newlist->value = rnd_strdup(value);
newlist->num = 1;
vts0_init(&newlist->refdes);
vts0_append(&newlist->refdes, rnd_strdup(refdes));
return newlist;
}
/* search and see if we already have used one of these components */
cur = bom;
while(cur != NULL) {
if ((RND_NSTRCMP(descr, cur->descr) == 0) && (RND_NSTRCMP(value, cur->value) == 0)) {
cur->num++;
vts0_append(&cur->refdes, rnd_strdup(refdes));
break;
}
prev = cur;
cur = cur->next;
}
if (cur == NULL) {
if ((newlist = malloc(sizeof(pcb_bom_list_t))) == NULL) {
fprintf(stderr, "malloc() failed in bom_insert()\n");
exit(1);
}
prev->next = newlist;
newlist->next = NULL;
newlist->descr = rnd_strdup(descr);
newlist->value = rnd_strdup(value);
newlist->num = 1;
vts0_init(&newlist->refdes);
vts0_append(&newlist->refdes, rnd_strdup(refdes));
}
return bom;
}
/* If fp is not NULL then print out the bill of materials contained in
bom. Either way, free all memory which has been allocated for bom. */
static void print_and_free(FILE * fp, pcb_bom_list_t *bom)
{
pcb_bom_list_t *lastb;
char *descr, *value;
long n;
while (bom != NULL) {
if (fp) {
descr = pcb_bom_clean_str(bom->descr);
value = pcb_bom_clean_str(bom->value);
fprintf(fp, "%d,\"%s\",\"%s\",", bom->num, descr, value);
free(descr);
free(value);
}
for(n = 0; n < bom->refdes.used; n++) {
char *refdes = bom->refdes.array[n];
if (fp)
fprintf(fp, "%s ", refdes);
free(refdes);
}
vts0_uninit(&bom->refdes);
if (fp)
fprintf(fp, "\n");
lastb = bom;
bom = bom->next;
free(lastb);
}
}
static int bom_print(void)
{
char utcTime[64];
FILE *fp;
pcb_bom_list_t *bom = NULL;
rnd_print_utc(utcTime, sizeof(utcTime), 0);
PCB_SUBC_LOOP(PCB->Data);
{
/* insert this component into the bill of materials list */
bom = bom_insert((char *) RND_UNKNOWN(subc->refdes),
(char *) RND_UNKNOWN(pcb_subc_name(subc, "bom::footprint")),
(char *) RND_UNKNOWN(pcb_attribute_get(&subc->Attributes, "value")),
bom);
}
PCB_END_LOOP;
fp = rnd_fopen_askovr(&PCB->hidlib, bom_filename, "w", NULL);
if (!fp) {
rnd_message(RND_MSG_ERROR, "Cannot open file %s for writing\n", bom_filename);
print_and_free(NULL, bom);
return 1;
}
fprintf(fp, "# $Id");
fprintf(fp, "$\n");
fprintf(fp, "# PcbBOM Version 1.0\n");
fprintf(fp, "# Date: %s\n", utcTime);
fprintf(fp, "# Author: %s\n", pcb_author());
fprintf(fp, "# Title: %s - PCB BOM\n", RND_UNKNOWN(PCB->hidlib.name));
fprintf(fp, "# Quantity, Description, Value, RefDes\n");
fprintf(fp, "# --------------------------------------------\n");
print_and_free(fp, bom);
fclose(fp);
return 0;
}
static void bom_do_export(rnd_hid_t *hid, rnd_hid_attr_val_t *options)
{
pcb_cam_t cam;
if (!options) {
bom_get_export_options(hid, 0);
options = bom_values;
}
bom_filename = options[HA_bomfile].str;
if (!bom_filename)
bom_filename = "pcb-out.bom";
pcb_cam_begin_nolayer(PCB, &cam, NULL, options[HA_cam].str, &bom_filename);
bom_print();
pcb_cam_end(&cam);
}
static int bom_usage(rnd_hid_t *hid, const char *topic)
{
fprintf(stderr, "\nbom exporter command line arguments:\n\n");
rnd_hid_usage(bom_options, sizeof(bom_options) / sizeof(bom_options[0]));
fprintf(stderr, "\nUsage: pcb-rnd [generic_options] -x bom [bom_options] foo.pcb\n\n");
return 0;
}
static int bom_parse_arguments(rnd_hid_t *hid, int *argc, char ***argv)
{
rnd_export_register_opts2(hid, bom_options, sizeof(bom_options) / sizeof(bom_options[0]), bom_cookie, 0);
return rnd_hid_parse_command_line(argc, argv);
}
rnd_hid_t bom_hid;
int pplg_check_ver_export_bom(int ver_needed) { return 0; }
void pplg_uninit_export_bom(void)
{
rnd_export_remove_opts_by_cookie(bom_cookie);
rnd_hid_remove_hid(&bom_hid);
}
int pplg_init_export_bom(void)
{
RND_API_CHK_VER;
memset(&bom_hid, 0, sizeof(rnd_hid_t));
rnd_hid_nogui_init(&bom_hid);
bom_hid.struct_size = sizeof(rnd_hid_t);
bom_hid.name = "bom";
bom_hid.description = "Exports a Bill of Materials";
bom_hid.exporter = 1;
bom_hid.get_export_options = bom_get_export_options;
bom_hid.do_export = bom_do_export;
bom_hid.parse_arguments = bom_parse_arguments;
bom_hid.argument_array = bom_values;
bom_hid.usage = bom_usage;
rnd_hid_register_hid(&bom_hid);
return 0;
}
|