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
|
/*
* COPYRIGHT
*
* pcb-rnd, interactive printed circuit board design
*
* auto routing with external router process: dialog box
* pcb-rnd Copyright (C) 2020 Tibor 'Igor2' Palinkas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact:
* Project page: http://repo.hu/projects/pcb-rnd
* lead developer: http://repo.hu/projects/pcb-rnd/contact.html
* mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
*/
#include <librnd/core/safe_fs.h>
#include <librnd/hid/hid_dad.h>
typedef struct {
RND_DAD_DECL_NOINIT(dlg)
int active; /* already open - allow only one instance */
vts0_t tabs;
int wtabs, wprog[3];
} ar_ctx_t;
static ar_ctx_t ar_ctx;
/* copy values from the dialog box to the memory representation (router_apis) */
static void dlg2mem(void)
{
int an, mn;
if (!ar_ctx.active)
return;
for(an = 0; an < router_apis.used; an++) {
router_api_t *a = router_apis.array[an];
for(mn = 0; mn < a->num_methods; mn++) {
router_method_t *m = &a->methods[mn];
int *wid;
rnd_hid_attr_val_t *val;
rnd_export_opt_t *cfg;
for(cfg = m->confkeys, wid = m->w, val = m->val; cfg->name != NULL; cfg++, wid++, val++)
*val = ar_ctx.dlg[*wid].val;
}
}
}
static void save_conf(FILE *f)
{
int an, mn;
dlg2mem();
fprintf(f, "ha:autorouter-settings-v1 {\n");
fprintf(f, " ha:confkeys {\n");
for(an = 0; an < router_apis.used; an++) {
router_api_t *a = router_apis.array[an];
fprintf(f, " ha:%s {\n", a->router->name);
for(mn = 0; mn < a->num_methods; mn++) {
router_method_t *m = &a->methods[mn];
int *wid;
rnd_hid_attr_val_t *val;
rnd_export_opt_t *cfg;
fprintf(f, " ha:%s {\n", m->name);
for(cfg = m->confkeys, wid = m->w, val = m->val; cfg->name != NULL; cfg++, wid++, val++) {
switch(cfg->type) {
case RND_HATT_BOOL:
case RND_HATT_INTEGER:
fprintf(f, " %s=%ld\n", cfg->name, val->lng);
break;
case RND_HATT_REAL:
fprintf(f, " %s=%f\n", cfg->name, val->dbl);
break;
case RND_HATT_COORD:
rnd_fprintf(f, " %s=%$mH", cfg->name, val->crd);
fprintf(f, "\n");
break;
case RND_HATT_STRING:
fprintf(f, " %s={%s}\n", cfg->name, val->str);
break;
default: break;
}
}
fprintf(f, " }\n");
}
fprintf(f, " }\n");
}
fprintf(f, " }\n");
fprintf(f, "}\n");
}
static void load_conf(FILE *f, const char *fn)
{
char *errmsg = NULL;
int an, mn;
lht_doc_t *doc = lht_dom_load(fn, &errmsg);
lht_node_t *croot, *nrouter, *nmethod, *n;
if (doc == NULL) {
rnd_message(RND_MSG_ERROR, "Failed to parse autorouter settings: %s\n", errmsg);
free(errmsg);
return;
}
free(errmsg);
if ((doc->root == NULL) || (doc->root->type != LHT_HASH) || (strcmp(doc->root->name, "autorouter-settings-v1") != 0)) {
rnd_message(RND_MSG_ERROR, "Failed to load autorouter settings from %s: invalid root node (expected ha:autorouter-settings-v1)\n", fn);
return;
}
croot = lht_dom_hash_get(doc->root, "confkeys");
if ((croot == NULL) || (croot->type != LHT_HASH)) {
rnd_message(RND_MSG_ERROR, "Failed to load autorouter settings from %s: no confkeys under\n", fn);
return;
}
for(an = 0; an < router_apis.used; an++) {
router_api_t *a = router_apis.array[an];
nrouter = lht_dom_hash_get(croot, a->router->name);
if ((nrouter == NULL) || (nrouter->type != LHT_HASH))
continue;
for(mn = 0; mn < a->num_methods; mn++) {
router_method_t *m = &a->methods[mn];
int *wid;
rnd_hid_attr_val_t *val;
rnd_export_opt_t *cfg;
nmethod = lht_dom_hash_get(nrouter, m->name);
if ((nmethod == NULL) || (nmethod->type != LHT_HASH))
continue;
for(cfg = m->confkeys, wid = m->w, val = m->val; cfg->name != NULL; cfg++, wid++, val++) {
char *end;
long l;
double d;
rnd_bool succ;
n = lht_dom_hash_get(nmethod, cfg->name);
if ((n == NULL) || (n->type != LHT_TEXT))
continue;
switch(cfg->type) {
case RND_HATT_BOOL:
case RND_HATT_INTEGER:
l = strtol(n->data.text.value, &end, 10);
if (*end == '\0')
val->lng = l;
break;
case RND_HATT_REAL:
d = strtod(n->data.text.value, &end);
if (*end == '\0')
val->dbl = d;
break;
case RND_HATT_COORD:
d = rnd_get_value(n->data.text.value, NULL, NULL, &succ);
if (succ)
val->crd = d;
break;
case RND_HATT_STRING:
free((char *)val->str);
val->str = n->data.text.value;
n->data.text.value = NULL; /* took over ownership */
break;
default: break;
}
rnd_gui->attr_dlg_set_value(ar_ctx.dlg_hid_ctx, *wid, val);
}
}
}
lht_dom_uninit(doc);
}
static void save_conf_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr)
{
FILE *f;
char *fname;
rnd_design_t *hl = rnd_gui->get_dad_design(hid_ctx);
fname = rnd_hid_fileselect(rnd_gui, "Save autoroute settings to...",
"Pick a file for saving autoroute settings to.\n",
"autoroute.cfg.lht", ".lht", NULL, "ar_extern", 0, NULL);
if (fname == NULL)
return;
f = rnd_fopen(hl, fname, "w");
if (f == NULL) {
rnd_message(RND_MSG_ERROR, "Failed to open '%s' for write\n", fname);
return;
}
save_conf(f);
fclose(f);
free(fname);
}
static void load_conf_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr)
{
FILE *f;
char *fname;
rnd_design_t *hl = rnd_gui->get_dad_design(hid_ctx);
fname = rnd_hid_fileselect(rnd_gui, "Load autoroute settings from...",
"Pick a file for loading autoroute settings from.\n",
"autoroute.cfg.lht", ".lht", NULL, "ar_extern", 0, NULL);
if (fname == NULL)
return;
f = rnd_fopen(hl, fname, "r");
if (f == NULL) {
rnd_message(RND_MSG_ERROR, "Failed to open '%s' for read\n", fname);
return;
}
load_conf(f, fname);
fclose(f);
free(fname);
}
static int val_eq(rnd_hid_attr_type_t type, const rnd_hid_attr_val_t *v1, const rnd_hid_attr_val_t *v2)
{
switch(type) {
case RND_HATT_BOOL:
case RND_HATT_INTEGER: return v1->lng == v2->lng;
case RND_HATT_REAL: return v1->dbl == v2->dbl;
case RND_HATT_COORD: return v1->crd == v2->crd;
case RND_HATT_STRING: return strcmp(v1->str, v2->str) == 0;
default: rnd_message(RND_MSG_ERROR, "external router: internal error: unhandled val type - report this bug!\n");
}
return 0; /* better stay with the default */
}
static void route_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr)
{
int argc, an, mn, cnt = 0, target = ar_ctx.dlg[ar_ctx.wtabs].val.lng;
rnd_export_opt_t *cfg;
router_method_t *m;
rnd_hid_attr_val_t *val;
router_api_t *a;
fgw_arg_t *argv;
/* count with cnt until reaching target; mn is then the method */
for(an = 0; an < router_apis.used; an++) {
a = router_apis.array[an];
for(mn = 0; mn < a->num_methods; mn++)
if (cnt++ == target)
goto out;
}
rnd_message(RND_MSG_ERROR, "external router: can't find the router for this tab (%d/%d; internal error)\n", cnt, target);
out:;
if (a->router->route == NULL) {
rnd_message(RND_MSG_ERROR, "external router: internal error: this router does not implement the ->route call\n");
return;
}
dlg2mem();
m = &a->methods[mn];
argc = 0;
argv = calloc(sizeof(fgw_arg_t), m->len);
for(cfg = m->confkeys, val = m->val; cfg->name != NULL; cfg++, val++) {
char *s = NULL;
if (val_eq(cfg->type, val, &cfg->default_val))
continue;
switch(cfg->type) {
case RND_HATT_BOOL:
case RND_HATT_INTEGER:
s = rnd_strdup_printf("%s=%d", cfg->name, val->lng);
break;
case RND_HATT_REAL:
s = rnd_strdup_printf("%s=%f", cfg->name, val->dbl);
break;
case RND_HATT_COORD:
s = rnd_strdup_printf("%s=%.06mm", cfg->name, val->crd);
break;
case RND_HATT_STRING:
s = rnd_concat(cfg->name, "=", val->str, NULL);
break;
default: break;
}
argv[argc].type = FGW_STR; argv[argc].val.str = s;
argc++;
}
a->router->route(PCB, ERSC_BOARD, m->name, argc, argv);
rnd_gui->invalidate_all(rnd_gui);
fgw_argv_free(&rnd_fgw, argc, argv);
free(argv);
}
static void reroute_cb(void *hid_ctx, void *caller_data, rnd_hid_attribute_t *attr)
{
rnd_actionva(&PCB->hidlib, "RipUp", "All", NULL);
route_cb(hid_ctx, caller_data, attr);
}
static void ar_close_cb(void *caller_data, rnd_hid_attr_ev_t ev)
{
ar_ctx_t *ctx = caller_data;
dlg2mem();
RND_DAD_FREE(ctx->dlg);
vts0_uninit(&ctx->tabs);
memset(ctx, 0, sizeof(ar_ctx_t)); /* reset all states to the initial - includes ctx->active = 0; */
}
int pcb_ar_extern_progress(double p_total, double p1, double p2)
{
rnd_hid_attr_val_t hv;
if (!ar_ctx.active)
return 0;
hv.dbl = p_total;
rnd_gui->attr_dlg_set_value(ar_ctx.dlg_hid_ctx, ar_ctx.wprog[0], &hv);
hv.dbl = p1;
rnd_gui->attr_dlg_set_value(ar_ctx.dlg_hid_ctx, ar_ctx.wprog[1], &hv);
hv.dbl = p2;
rnd_gui->attr_dlg_set_value(ar_ctx.dlg_hid_ctx, ar_ctx.wprog[2], &hv);
rnd_hid_iterate(rnd_gui);
return 0;
}
static void extroute_gui(pcb_board_t *pcb)
{
rnd_hid_dad_buttons_t clbtn[] = {{"Close", 0}, {NULL, 0}};
long an, mn;
if (ar_ctx.active)
return; /* do not open another */
extroute_query_conf(pcb);
if (router_apis.used < 1) {
rnd_message(RND_MSG_ERROR, "No external autorouter installed. Please install one, e.g.\nfrom http://www.repo.hu/projects/route-rnd/\n");
return;
}
for(an = 0; an < router_apis.used; an++) {
router_api_t *a = router_apis.array[an];
for(mn = 0; mn < a->num_methods; mn++)
vts0_append(&ar_ctx.tabs, a->methods[mn].name);
}
RND_DAD_BEGIN_VBOX(ar_ctx.dlg);
RND_DAD_BEGIN_TABBED(ar_ctx.dlg, ar_ctx.tabs.array);
RND_DAD_COMPFLAG(ar_ctx.dlg, RND_HATF_LEFT_TAB|RND_HATF_EXPFILL);
ar_ctx.wtabs = RND_DAD_CURRENT(ar_ctx.dlg);
for(an = 0; an < router_apis.used; an++) {
router_api_t *a = router_apis.array[an];
for(mn = 0; mn < a->num_methods; mn++) {
router_method_t *m = &a->methods[mn];
char *title;
int *wid;
rnd_hid_attr_val_t *val;
rnd_export_opt_t *cfg;
RND_DAD_BEGIN_VBOX(ar_ctx.dlg);
if (m->len > 10)
RND_DAD_COMPFLAG(ar_ctx.dlg, RND_HATF_SCROLL);
title = rnd_concat(m->router->name, " / ", m->name, NULL);
RND_DAD_LABEL(ar_ctx.dlg, title);
RND_DAD_LABEL(ar_ctx.dlg, m->desc);
free(title);
for(cfg = m->confkeys, wid = m->w, val = m->val; cfg->name != NULL; cfg++, wid++, val++) {
RND_DAD_BEGIN_HBOX(ar_ctx.dlg);
switch(cfg->type) {
case RND_HATT_BOOL:
RND_DAD_BOOL(ar_ctx.dlg);
RND_DAD_DEFAULT_NUM(ar_ctx.dlg, val->lng);
break;
case RND_HATT_INTEGER:
RND_DAD_INTEGER(ar_ctx.dlg);
RND_DAD_MINMAX(ar_ctx.dlg, cfg->min_val, cfg->max_val);
RND_DAD_DEFAULT_NUM(ar_ctx.dlg, val->lng);
break;
case RND_HATT_REAL:
RND_DAD_REAL(ar_ctx.dlg);
RND_DAD_MINMAX(ar_ctx.dlg, cfg->min_val, cfg->max_val);
RND_DAD_DEFAULT_NUM(ar_ctx.dlg, val->dbl);
break;
case RND_HATT_COORD:
RND_DAD_COORD(ar_ctx.dlg);
RND_DAD_MINMAX(ar_ctx.dlg, cfg->min_val, cfg->max_val);
RND_DAD_DEFAULT_NUM(ar_ctx.dlg, val->crd);
break;
case RND_HATT_STRING:
RND_DAD_STRING(ar_ctx.dlg);
RND_DAD_DEFAULT_PTR(ar_ctx.dlg, val->str);
break;
default:
break;
}
*wid = RND_DAD_CURRENT(ar_ctx.dlg);
RND_DAD_LABEL(ar_ctx.dlg, cfg->name);
RND_DAD_END(ar_ctx.dlg);
}
RND_DAD_END(ar_ctx.dlg);
}
}
RND_DAD_END(ar_ctx.dlg);
RND_DAD_BEGIN_HBOX(ar_ctx.dlg);
RND_DAD_BUTTON(ar_ctx.dlg, "Route");
RND_DAD_CHANGE_CB(ar_ctx.dlg, route_cb);
RND_DAD_BUTTON(ar_ctx.dlg, "Re-route");
RND_DAD_CHANGE_CB(ar_ctx.dlg, reroute_cb);
RND_DAD_BUTTON(ar_ctx.dlg, "Save");
RND_DAD_CHANGE_CB(ar_ctx.dlg, save_conf_cb);
RND_DAD_BUTTON(ar_ctx.dlg, "Load");
RND_DAD_CHANGE_CB(ar_ctx.dlg, load_conf_cb);
RND_DAD_BEGIN_HBOX(ar_ctx.dlg);
RND_DAD_COMPFLAG(ar_ctx.dlg, RND_HATF_EXPFILL);
RND_DAD_BEGIN_VBOX(ar_ctx.dlg);
RND_DAD_COMPFLAG(ar_ctx.dlg, RND_HATF_EXPFILL);
RND_DAD_PROGRESS(ar_ctx.dlg);
ar_ctx.wprog[0] = RND_DAD_CURRENT(ar_ctx.dlg);
RND_DAD_BEGIN_HBOX(ar_ctx.dlg);
RND_DAD_PROGRESS(ar_ctx.dlg);
ar_ctx.wprog[1] = RND_DAD_CURRENT(ar_ctx.dlg);
RND_DAD_PROGRESS(ar_ctx.dlg);
ar_ctx.wprog[2] = RND_DAD_CURRENT(ar_ctx.dlg);
RND_DAD_END(ar_ctx.dlg);
RND_DAD_END(ar_ctx.dlg);
RND_DAD_END(ar_ctx.dlg);
RND_DAD_BUTTON_CLOSES(ar_ctx.dlg, clbtn);
RND_DAD_END(ar_ctx.dlg);
RND_DAD_END(ar_ctx.dlg);
/* set up the context */
ar_ctx.active = 1;
RND_DAD_NEW("external_autorouter", ar_ctx.dlg, "External autorouter", &ar_ctx, rnd_false, ar_close_cb);
}
|