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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
|
/*
* LibrePCB - Professional EDA for everyone!
* Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
* https://librepcb.org/
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*******************************************************************************
* Includes
******************************************************************************/
#include "commandlineinterface.h"
#include <librepcb/core/3d/occmodel.h>
#include <librepcb/core/application.h>
#include <librepcb/core/attribute/attributesubstitutor.h>
#include <librepcb/core/debug.h>
#include <librepcb/core/export/bom.h>
#include <librepcb/core/export/bomcsvwriter.h>
#include <librepcb/core/export/graphicsexport.h>
#include <librepcb/core/export/pickplacecsvwriter.h>
#include <librepcb/core/fileio/csvfile.h>
#include <librepcb/core/fileio/fileutils.h>
#include <librepcb/core/fileio/transactionalfilesystem.h>
#include <librepcb/core/library/cat/componentcategory.h>
#include <librepcb/core/library/cat/packagecategory.h>
#include <librepcb/core/library/cmp/component.h>
#include <librepcb/core/library/dev/device.h>
#include <librepcb/core/library/library.h>
#include <librepcb/core/library/pkg/package.h>
#include <librepcb/core/library/sym/symbol.h>
#include <librepcb/core/project/board/board.h>
#include <librepcb/core/project/board/boardd356netlistexport.h>
#include <librepcb/core/project/board/boardfabricationoutputsettings.h>
#include <librepcb/core/project/board/boardgerberexport.h>
#include <librepcb/core/project/board/boardpickplacegenerator.h>
#include <librepcb/core/project/board/boardplanefragmentsbuilder.h>
#include <librepcb/core/project/board/drc/boarddesignrulecheck.h>
#include <librepcb/core/project/bomgenerator.h>
#include <librepcb/core/project/circuit/assemblyvariant.h>
#include <librepcb/core/project/circuit/circuit.h>
#include <librepcb/core/project/erc/electricalrulecheck.h>
#include <librepcb/core/project/outputjobrunner.h>
#include <librepcb/core/project/project.h>
#include <librepcb/core/project/projectattributelookup.h>
#include <librepcb/core/project/projectloader.h>
#include <librepcb/core/project/schematic/schematicpainter.h>
#include <librepcb/core/utils/toolbox.h>
#include <QtCore>
#include <algorithm>
/*******************************************************************************
* Namespace
******************************************************************************/
namespace librepcb {
namespace cli {
/*******************************************************************************
* Constructors / Destructor
******************************************************************************/
CommandLineInterface::CommandLineInterface() noexcept {
}
/*******************************************************************************
* General Methods
******************************************************************************/
int CommandLineInterface::execute(const QStringList& args) noexcept {
QStringList positionalArgNames;
QMap<QString, QPair<QString, QString>> commands = {
{"open-project",
{tr("Open a project to execute project-related tasks."),
"open-project [command_options]"}}, // no tr()!
{"open-library",
{tr("Open a library to execute library-related tasks."),
"open-library [command_options]"}}, // no tr()!
{"open-step",
{tr("Open a STEP model to execute STEP-related tasks outside of a "
"library."),
"open-step [command_options]"}}, // no tr()!
};
// Add global options
QCommandLineParser parser;
parser.setApplicationDescription(tr("LibrePCB Command Line Interface"));
// Don't use the built-in addHelpOption() since it also adds the "--help-all"
// option which we don't need, and the OS-dependent option "-?".
const QCommandLineOption helpOption({"h", "help"}, tr("Print this message."));
parser.addOption(helpOption);
const QCommandLineOption versionOption({"V", "version"},
tr("Displays version information."));
parser.addOption(versionOption);
QCommandLineOption verboseOption({"v", "verbose"}, tr("Verbose output."));
parser.addOption(verboseOption);
parser.addPositionalArgument("command",
tr("The command to execute (see list below)."));
positionalArgNames.append("command");
// Define options for "open-project"
QCommandLineOption ercOption(
"erc",
tr("Run the electrical rule check, print all non-approved "
"warnings/errors and "
"report failure (exit code = 1) if there are non-approved messages."));
QCommandLineOption drcOption(
"drc",
tr("Run the design rule check, print all non-approved warnings/errors "
"and report failure (exit code = 1) if there are non-approved "
"messages."));
QCommandLineOption drcSettingsOption(
"drc-settings",
tr("Override DRC settings by providing a *.lp file containing custom "
"settings. If not set, the settings from the boards will be used "
"instead."),
tr("file"));
QCommandLineOption runSpecificJobOption(
"run-job",
tr("Run a particular output job. Can be given multiple times to run "
"multiple jobs."),
tr("name"));
QCommandLineOption runAllJobsOption("run-jobs",
tr("Run all existing output jobs."));
QCommandLineOption customJobsOption(
"jobs",
tr("Override output jobs with a *.lp file containing custom jobs. If not "
"set, the jobs from the project will be used instead."),
tr("file"));
QCommandLineOption customOutDirOption(
"outdir",
tr("Override the output base directory of jobs. If not set, the "
"standard output directory from the project is used."),
tr("path"));
QCommandLineOption exportSchematicsOption(
"export-schematics",
tr("Export schematics to given file(s). Existing files will be "
"overwritten. Supported file extensions: %1")
.arg(GraphicsExport::getSupportedExtensions().join(", ")),
tr("file"));
QCommandLineOption exportBomOption(
"export-bom",
tr("Export generic BOM to given file(s). Existing files will be "
"overwritten. Supported file extensions: %1")
.arg("csv"),
tr("file"));
QCommandLineOption exportBoardBomOption(
"export-board-bom",
tr("Export board-specific BOM to given file(s). Existing files "
"will be overwritten. Supported file extensions: %1")
.arg("csv"),
tr("file"));
QCommandLineOption bomAttributesOption(
"bom-attributes",
tr("Comma-separated list of additional attributes to be exported "
"to the BOM. Example: \"%1\"")
.arg("SUPPLIER, SKU"),
tr("attributes"));
QCommandLineOption exportPcbFabricationDataOption(
"export-pcb-fabrication-data",
tr("Export PCB fabrication data (Gerber/Excellon) according the "
"fabrication "
"output settings of boards. Existing files will be overwritten."));
QCommandLineOption pcbFabricationSettingsOption(
"pcb-fabrication-settings",
tr("Override PCB fabrication output settings by providing a *.lp file "
"containing custom settings. If not set, the settings from the boards "
"will be used instead."),
tr("file"));
QCommandLineOption exportPnpTopOption(
"export-pnp-top",
tr("Export pick&place file for automated assembly of the top board side. "
"Existing files will be overwritten. Supported file extensions: %1")
.arg("csv, gbr"),
tr("file"));
QCommandLineOption exportPnpBottomOption(
"export-pnp-bottom",
tr("Export pick&place file for automated assembly of the bottom board "
"side. Existing files will be overwritten. Supported file extensions: "
"%1")
.arg("csv, gbr"),
tr("file"));
QCommandLineOption exportNetlistOption(
"export-netlist",
tr("Export netlist file for automated PCB testing. Existing files will "
"be overwritten. Supported file extensions: %1")
.arg("d356"),
tr("file"));
QCommandLineOption boardOption("board",
tr("The name of the board(s) to export. Can "
"be given multiple times. If not set, "
"all boards are exported."),
tr("name"));
QCommandLineOption boardIndexOption("board-index",
tr("Same as '%1', but allows to specify "
"boards by index instead of by name.")
.arg("--board"),
tr("index"));
QCommandLineOption removeOtherBoardsOption(
"remove-other-boards",
tr("Remove all boards not specified with '%1' from the project before "
"executing all the other actions. If '%1' is not passed, all boards "
"will be removed. Pass '%2' to save the modified project to disk.")
.arg("--board[-index]")
.arg("--save"));
QCommandLineOption assemblyVariantOption(
"variant",
tr("The name of the assembly variant(s) to export. Can be given multiple "
"times. If not set, all assembly variants are exported."),
tr("name"));
QCommandLineOption assemblyVariantIndexOption(
"variant-index",
tr("Same as '%1', but allows to specify assembly variants by index "
"instead of by name.")
.arg("--variant"),
tr("index"));
QCommandLineOption setDefaultAssemblyVariantOption(
"set-default-variant",
tr("Move the specified assembly variant to the top before executing all "
"the other actions. Pass '%1' to save the modified project to disk.")
.arg("--save"),
tr("name"));
QCommandLineOption saveOption(
"save",
tr("Save project before closing it (useful to upgrade file format)."));
QCommandLineOption prjStrictOption(
"strict",
tr("Fail if the project files are not strictly canonical, i.e. "
"there would be changes when saving the project. Note that "
"this option is not available for *.lppz files."));
// Define options for "open-library"
QCommandLineOption libAllOption(
"all",
tr("Perform the selected action(s) on all elements contained in "
"the opened library."));
QCommandLineOption libCheckOption(
"check",
tr("Run the library element check, print all non-approved messages and "
"report failure (exit code = 1) if there are non-approved messages."));
QCommandLineOption libMinifyStepOption(
"minify-step",
tr("Minify the STEP models of all packages. Only works in conjunction "
"with '--all'. Pass '--save' to write the minified files to disk."));
QCommandLineOption libSaveOption(
"save",
tr("Save library (and contained elements if '--all' is given) "
"before closing them (useful to upgrade file format)."));
QCommandLineOption libStrictOption(
"strict",
tr("Fail if the opened files are not strictly canonical, i.e. "
"there would be changes when saving the library elements."));
// Define options for "open-step"
QCommandLineOption stepMinifyOption(
"minify",
tr("Minify the STEP model before validating it. Use in conjunction with "
"'%1' to save the output of the operation.")
.arg("--save-to"));
QCommandLineOption stepTesselateOption(
"tesselate",
tr("Tesselate the loaded STEP model to check if LibrePCB is able to "
"render it. Reports failure (exit code = 1) if no content is "
"detected."));
QCommandLineOption stepSaveToOption(
"save-to",
tr("Write the (modified) STEP file to this output location (may be equal "
"to the opened file path). Only makes sense in conjunction with '%1'.")
.arg("--minify"),
tr("file"));
// Build help text.
const QString executable = args.value(0);
QString helpText = parser.helpText() % "\n" % tr("Commands:") % "\n";
for (auto it = commands.constBegin(); it != commands.constEnd(); ++it) {
helpText += " " % it.key().leftJustified(15) % it.value().first % "\n";
}
helpText += "\n" % tr("List command-specific options:") % "\n " %
executable % " <command> --help";
QString usageHelpText = helpText.split("\n").value(0);
const QString helpCommandTextPrefix = tr("Help:") % " ";
QString helpCommandText = helpCommandTextPrefix % executable % " --help";
// First parse to get the supplied command (ignoring errors because the parser
// does not yet know the command-dependent options).
parser.parse(args);
// Add command-dependent options
const QString command = parser.positionalArguments().value(0);
parser.clearPositionalArguments();
if (command == "open-project") {
parser.addPositionalArgument(command, commands[command].first,
commands[command].second);
parser.addPositionalArgument("project",
tr("Path to project file (*.lpp[z])."));
positionalArgNames.append("project");
parser.addOption(ercOption);
parser.addOption(drcOption);
parser.addOption(drcSettingsOption);
parser.addOption(runSpecificJobOption);
parser.addOption(runAllJobsOption);
parser.addOption(customJobsOption);
parser.addOption(customOutDirOption);
parser.addOption(exportSchematicsOption);
parser.addOption(exportBomOption);
parser.addOption(exportBoardBomOption);
parser.addOption(bomAttributesOption);
parser.addOption(exportPcbFabricationDataOption);
parser.addOption(pcbFabricationSettingsOption);
parser.addOption(exportPnpTopOption);
parser.addOption(exportPnpBottomOption);
parser.addOption(exportNetlistOption);
parser.addOption(boardOption);
parser.addOption(boardIndexOption);
parser.addOption(removeOtherBoardsOption);
parser.addOption(assemblyVariantOption);
parser.addOption(assemblyVariantIndexOption);
parser.addOption(setDefaultAssemblyVariantOption);
parser.addOption(saveOption);
parser.addOption(prjStrictOption);
} else if (command == "open-library") {
parser.addPositionalArgument(command, commands[command].first,
commands[command].second);
parser.addPositionalArgument("library",
tr("Path to library directory (*.lplib)."));
positionalArgNames.append("library");
parser.addOption(libAllOption);
parser.addOption(libCheckOption);
parser.addOption(libMinifyStepOption);
parser.addOption(libSaveOption);
parser.addOption(libStrictOption);
} else if (command == "open-step") {
parser.addPositionalArgument(command, commands[command].first,
commands[command].second);
parser.addPositionalArgument(
"file", tr("Path to the STEP file (%1).").arg("*.step"));
positionalArgNames.append("file");
parser.addOption(stepMinifyOption);
parser.addOption(stepTesselateOption);
parser.addOption(stepSaveToOption);
} else if (!command.isEmpty()) {
printErr(tr("Unknown command '%1'.").arg(command));
printErr(usageHelpText);
printErr(helpCommandText);
return 1;
}
// If a command is given, make the help texts command-specific now.
if (!command.isEmpty()) {
helpText = parser.helpText().trimmed(); // Remove the list of commands.
usageHelpText = helpText.split("\n").value(0);
helpCommandText =
helpCommandTextPrefix % executable % " " % command % " --help";
}
// Parse the actual command line arguments given by the user
if (!parser.parse(args)) {
printErr(parser.errorText());
printErr(usageHelpText);
printErr(helpCommandText);
return 1;
}
// --verbose
if (parser.isSet(verboseOption)) {
Debug::instance()->setDebugLevelStderr(Debug::DebugLevel_t::All);
OccModel::setVerboseOutput(true);
}
// --help (also shown if no arguments supplied)
if (parser.isSet(helpOption) || (args.count() <= 1)) {
print(helpText);
return 0;
}
// --version
if (parser.isSet(versionOption)) {
// Note: Do not translate this output as it probably looks ugly and this
// way it is deterministic even if LANG/LC_ALL is not explicitly set.
print(QString("LibrePCB CLI Version %1").arg(Application::getVersion()));
print(QString("File Format %1")
.arg(Application::getFileFormatVersion().toStr()) %
" " %
(Application::isFileFormatStable() ? "(stable)" : "(unstable)"));
print(QString("Git Revision %1").arg(Application::getGitRevision()));
print(QString("Qt Version %1 (compiled against %2)")
.arg(qVersion(), QT_VERSION_STR));
print("OpenCascade " % OccModel::getOccVersionString());
print(QString("Built at %1")
.arg(QLocale().toString(Application::getBuildDate(),
QLocale::ShortFormat)));
return 0;
}
// Check number of passed positional command arguments.
const QStringList positionalArgs = parser.positionalArguments();
if (positionalArgs.count() < positionalArgNames.count()) {
const QStringList names = positionalArgNames.mid(positionalArgs.count());
printErr(tr("Missing arguments:") % " " % names.join(" "));
printErr(usageHelpText);
printErr(helpCommandText);
return 1;
} else if (positionalArgs.count() > positionalArgNames.count()) {
const QStringList args = positionalArgs.mid(positionalArgNames.count());
printErr(tr("Unknown arguments:") % " " % args.join(" "));
printErr(usageHelpText);
printErr(helpCommandText);
return 1;
}
// Execute command
bool cmdSuccess = false;
if (command == "open-project") {
cmdSuccess = openProject(
positionalArgs.value(1), // project filepath
parser.isSet(ercOption), // run ERC
parser.isSet(drcOption), // run DRC
parser.value(drcSettingsOption), // DRC settings
parser.values(runSpecificJobOption), // run specific output jobs
parser.isSet(runAllJobsOption), // run all output jobs
parser.value(customJobsOption).trimmed(), // custom jobs file path
parser.value(customOutDirOption).trimmed(), // custom jobs outdir
parser.values(exportSchematicsOption), // export schematics
parser.values(exportBomOption), // export generic BOM
parser.values(exportBoardBomOption), // export board BOM
parser.value(bomAttributesOption), // BOM attributes
parser.isSet(exportPcbFabricationDataOption), // export PCB fab. data
parser.value(pcbFabricationSettingsOption), // PCB fab. settings
parser.values(exportPnpTopOption), // export PnP top
parser.values(exportPnpBottomOption), // export PnP bottom
parser.values(exportNetlistOption), // export netlist
parser.values(boardOption), // board names
parser.values(boardIndexOption), // board indices
parser.isSet(removeOtherBoardsOption), // remove other boards
parser.values(assemblyVariantOption), // assembly variant names
parser.values(assemblyVariantIndexOption), // assembly variant indices
parser.value(setDefaultAssemblyVariantOption), // set default AV
parser.isSet(saveOption), // save project
parser.isSet(prjStrictOption) // strict mode
);
} else if (command == "open-library") {
cmdSuccess = openLibrary(positionalArgs.value(1), // library directory
parser.isSet(libAllOption), // all elements
parser.isSet(libCheckOption), // run check
parser.isSet(libMinifyStepOption), // minify STEP
parser.isSet(libSaveOption), // save
parser.isSet(libStrictOption) // strict mode
);
} else if (command == "open-step") {
cmdSuccess = openStep(positionalArgs.value(1), // STEP file path
parser.isSet(stepMinifyOption), // minify
parser.isSet(stepTesselateOption), // tesselate
parser.value(stepSaveToOption) // save to
);
} else {
printErr("Internal failure."); // No tr() because this cannot occur.
}
if (cmdSuccess) {
print(tr("SUCCESS"));
return 0;
} else {
print(tr("Finished with errors!"));
return 1;
}
}
/*******************************************************************************
* Private Methods
******************************************************************************/
bool CommandLineInterface::openProject(
const QString& projectFile, bool runErc, bool runDrc,
const QString& drcSettingsPath, const QStringList& runJobs, bool runAllJobs,
const QString& customJobsPath, const QString& customOutDir,
const QStringList& exportSchematicsFiles, const QStringList& exportBomFiles,
const QStringList& exportBoardBomFiles, const QString& bomAttributes,
bool exportPcbFabricationData, const QString& pcbFabricationSettingsPath,
const QStringList& exportPnpTopFiles,
const QStringList& exportPnpBottomFiles,
const QStringList& exportNetlistFiles, const QStringList& boardNames,
const QStringList& boardIndices, bool removeOtherBoards,
const QStringList& avNames, const QStringList& avIndices,
const QString& setDefaultAv, bool save, bool strict) const noexcept {
try {
bool success = true;
QMap<FilePath, int> writtenFilesCounter;
QMap<FilePath, int> writtenOutputJobFilesCounter;
// Open project
FilePath projectFp(QFileInfo(projectFile).absoluteFilePath());
print(tr("Open project '%1'...").arg(prettyPath(projectFp, projectFile)));
std::shared_ptr<TransactionalFileSystem> projectFs;
QString projectFileName;
if (projectFp.getSuffix() == "lppz") {
projectFs = TransactionalFileSystem::openRO(projectFp.getParentDir());
projectFs->removeDirRecursively(); // 1) get a clean initial state
projectFs->loadFromZip(projectFp); // 2) load files from ZIP
foreach (const QString& fn, projectFs->getFiles()) {
if (fn.endsWith(".lpp")) {
projectFileName = fn;
}
}
} else {
projectFs = TransactionalFileSystem::open(projectFp.getParentDir(), save);
projectFileName = projectFp.getFilename();
}
ProjectLoader loader;
std::unique_ptr<Project> project =
loader.open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(projectFs)),
projectFileName); // can throw
if (auto messages = loader.getUpgradeMessages()) {
print(tr("Attention: Project has been upgraded to a newer file format!"));
std::sort(messages->begin(), messages->end(),
[](const FileFormatMigration::Message& a,
const FileFormatMigration::Message& b) {
if (a.severity > b.severity) return true;
if (a.message < b.message) return true;
return false;
});
foreach (const auto& msg, *messages) {
const QString multiplier = msg.affectedItems > 0
? QString(" (%1x)").arg(msg.affectedItems)
: "";
print(QString(" - %1%2: %3")
.arg(msg.getSeverityStrTr())
.arg(multiplier)
.arg(msg.message));
}
}
// Set the default assembly variant.
if (!setDefaultAv.isEmpty()) {
print(tr("Set default assembly variant to '%1'...").arg(setDefaultAv));
if (project->getCircuit().getAssemblyVariants().contains(setDefaultAv)) {
project->getCircuit().getAssemblyVariants().insert(
0, project->getCircuit().getAssemblyVariants().take(setDefaultAv));
} else {
printErr(tr("ERROR: No assembly variant with the name '%1' found.")
.arg(setDefaultAv));
success = false;
}
}
// Parse list of assembly variants.
QVector<std::shared_ptr<AssemblyVariant>> assemblyVariants;
foreach (const QString& avName, avNames) {
if (auto av = project->getCircuit().getAssemblyVariants().find(avName)) {
if (!assemblyVariants.contains(av)) {
assemblyVariants.append(av);
}
} else {
printErr(tr("ERROR: No assembly variant with the name '%1' found.")
.arg(avName));
success = false;
}
}
foreach (const QString& avIndex, avIndices) {
bool ok = false;
const int index = avIndex.trimmed().toInt(&ok);
auto av = project->getCircuit().getAssemblyVariants().value(index);
if (ok && av) {
if (!assemblyVariants.contains(av)) {
assemblyVariants.append(av);
}
} else {
printErr(
tr("ERROR: Assembly variant index '%1' is invalid.").arg(avIndex));
success = false;
}
}
// If no assembly variants are specified, export all variants.
if (avNames.isEmpty() && avIndices.isEmpty()) {
for (auto it = project->getCircuit().getAssemblyVariants().begin();
it != project->getCircuit().getAssemblyVariants().end(); ++it) {
assemblyVariants.append(it.ptr());
}
}
// Parse list of boards.
QList<Board*> boards;
foreach (const QString& boardName, boardNames) {
if (Board* board = project->getBoardByName(boardName)) {
if (!boards.contains(board)) {
boards.append(board);
}
} else {
printErr(
tr("ERROR: No board with the name '%1' found.").arg(boardName));
success = false;
}
}
foreach (const QString& boardIndex, boardIndices) {
bool ok = false;
const int index = boardIndex.trimmed().toInt(&ok);
Board* board = project->getBoardByIndex(index);
if (ok && board) {
if (!boards.contains(board)) {
boards.append(board);
}
} else {
printErr(tr("ERROR: Board index '%1' is invalid.").arg(boardIndex));
success = false;
}
}
// Remove other boards (note: do this at the very beginning to make all
// the other commands, e.g. the ERC, working without the removed boards).
if (removeOtherBoards) {
print(tr("Remove other boards..."));
foreach (Board* board, project->getBoards()) {
if (!boards.contains(board)) {
print(QString(" - '%1'").arg(*board->getName()));
project->removeBoard(*board);
}
}
}
// If no boards are specified, export all boards.
if (boardNames.isEmpty() && boardIndices.isEmpty()) {
boards = project->getBoards();
}
// Build planes, if needed.
if (runDrc || exportPcbFabricationData || (!runJobs.isEmpty()) ||
runAllJobs) {
foreach (Board* board, boards) {
qInfo().nospace().noquote() << "Rebuilding all planes of board '"
<< *board->getName() << "'...";
BoardPlaneFragmentsBuilder builder;
builder.runAndApply(*board); // can throw
}
} else {
qInfo() << "No need to rebuild planes, thus skipped.";
}
// Check for non-canonical files (strict mode)
if (strict) {
print(tr("Check for non-canonical files..."));
if (projectFp.getSuffix() == "lppz") {
printErr(" " %
tr("ERROR: The option '--strict' is not available for "
"*.lppz files!"));
success = false;
} else {
project->save(); // can throw
QStringList paths = projectFs->checkForModifications(); // can throw
// ignore user config files
paths = paths.filter(QRegularExpression("^((?!\\.user\\.lp).)*$"));
// sort file paths to increases readability of console output
std::sort(paths.begin(), paths.end());
foreach (const QString& path, paths) {
printErr(
QString(" - Non-canonical file: '%1'")
.arg(prettyPath(projectFs->getAbsPath(path), projectFile)));
}
if (paths.count() > 0) {
success = false;
}
}
}
// ERC
if (runErc) {
print(tr("Run ERC..."));
ElectricalRuleCheck erc(*project);
int approvedMsgCount = 0;
const RuleCheckMessageList messages = erc.runChecks();
const QStringList nonApproved = prepareRuleCheckMessages(
messages, project->getErcMessageApprovals(), approvedMsgCount);
print(" " % tr("Approved messages: %1").arg(approvedMsgCount));
print(" " % tr("Non-approved messages: %1").arg(nonApproved.count()));
foreach (const QString& msg, nonApproved) {
printErr(" - " % msg);
success = false;
}
}
// DRC
if (runDrc) {
print(tr("Run DRC..."));
tl::optional<BoardDesignRuleCheckSettings> customSettings;
QList<Board*> boardsToCheck = boards;
if (!drcSettingsPath.isEmpty()) {
try {
qDebug() << "Load custom DRC settings:" << drcSettingsPath;
const FilePath fp(QFileInfo(drcSettingsPath).absoluteFilePath());
const std::unique_ptr<const SExpression> root =
SExpression::parse(FileUtils::readFile(fp), fp);
customSettings = BoardDesignRuleCheckSettings(*root); // can throw
} catch (const Exception& e) {
printErr(
tr("ERROR: Failed to load custom settings: %1").arg(e.getMsg()));
success = false;
boardsToCheck.clear(); // avoid exporting any boards
}
}
foreach (Board* board, boardsToCheck) {
print(" " % tr("Board '%1':").arg(*board->getName()));
BoardDesignRuleCheck drc;
drc.start(*board,
customSettings ? *customSettings : board->getDrcSettings(),
false);
const BoardDesignRuleCheck::Result result = drc.waitForFinished();
for (const QString& msg : result.errors) {
printErr("FATAL ERROR: " % msg);
success = false;
}
int approvedMsgCount = 0;
const QStringList nonApproved = prepareRuleCheckMessages(
result.messages, board->getDrcMessageApprovals(), approvedMsgCount);
print(" " % tr("Approved messages: %1").arg(approvedMsgCount));
print(" " %
tr("Non-approved messages: %1").arg(nonApproved.count()));
foreach (const QString& msg, nonApproved) {
printErr(" - " % msg);
success = false;
}
}
}
// Run output jobs.
if ((!runJobs.isEmpty()) || runAllJobs) {
// Determine jobs.
tl::optional<OutputJobList> allJobs;
if (!customJobsPath.isEmpty()) {
try {
qDebug() << "Load custom output jobs:" << customJobsPath;
const FilePath fp(QFileInfo(customJobsPath).absoluteFilePath());
const std::unique_ptr<const SExpression> root =
SExpression::parse(FileUtils::readFile(fp), fp);
allJobs = deserialize<OutputJobList>(*root); // can throw
} catch (const Exception& e) {
printErr(tr("ERROR: Failed to load custom output jobs: %1")
.arg(e.getMsg()));
success = false;
}
} else {
allJobs = project->getOutputJobs();
}
if (allJobs) {
QVector<std::shared_ptr<OutputJob>> jobs;
if (runAllJobs) {
jobs = allJobs->values();
} else {
foreach (const QString& name, runJobs) {
if (auto job = allJobs->find(name)) {
jobs.append(job);
} else {
printErr(tr("ERROR: No output job with the name '%1' found.")
.arg(name));
success = false;
}
}
}
try {
OutputJobRunner runner(*project);
QObject::connect(
&runner, &OutputJobRunner::jobStarted,
[](std::shared_ptr<const OutputJob> job) {
print(tr("Run output job '%1'...").arg(*job->getName()));
});
QObject::connect(
&runner, &OutputJobRunner::aboutToWriteFile,
[&projectFile,
&writtenOutputJobFilesCounter](const FilePath& fp) {
print(QString(" => '%1'").arg(prettyPath(fp, projectFile)));
writtenOutputJobFilesCounter[fp]++;
});
if (!customOutDir.isEmpty()) {
runner.setOutputDirectory(
QDir::isRelativePath(customOutDir)
? FilePath(QDir::currentPath()).getPathTo(customOutDir)
: FilePath(customOutDir));
}
qDebug() << "Using output base directory:"
<< runner.getOutputDirectory().toNative();
runner.run(jobs); // can throw
} catch (const Exception& e) {
printErr(tr("ERROR:") % " " % e.getMsg());
success = false;
}
}
}
// Export schematics
foreach (const QString& destStr, exportSchematicsFiles) {
print(tr("Export schematics to '%1'...").arg(destStr));
QString destPathStr = AttributeSubstitutor::substitute(
destStr, ProjectAttributeLookup(*project, nullptr),
[&](const QString& str) {
return FilePath::cleanFileName(
str, FilePath::ReplaceSpaces | FilePath::KeepCase);
});
FilePath destPath(QFileInfo(destPathStr).absoluteFilePath());
GraphicsExport graphicsExport;
graphicsExport.setDocumentName(*project->getName());
std::shared_ptr<GraphicsExportSettings> settings =
std::make_shared<GraphicsExportSettings>();
GraphicsExport::Pages pages;
foreach (const Schematic* schematic, project->getSchematics()) {
pages.append(std::make_pair(
std::make_shared<SchematicPainter>(*schematic), settings));
}
graphicsExport.startExport(pages, destPath);
const GraphicsExport::Result result = graphicsExport.waitForFinished();
foreach (const FilePath& writtenFile, result.writtenFiles) {
print(QString(" => '%1'").arg(prettyPath(writtenFile, destPathStr)));
writtenFilesCounter[writtenFile]++;
}
if (!result.errorMsg.isEmpty()) {
printErr(" " % tr("ERROR") % ": " % result.errorMsg);
success = false;
}
}
// Export BOM
if (exportBomFiles.count() + exportBoardBomFiles.count() > 0) {
QList<QPair<QString, bool>> jobs; // <OutputPath, BoardSpecific>
foreach (const QString& fp, exportBomFiles) {
jobs.append(qMakePair(fp, false));
}
foreach (const QString& fp, exportBoardBomFiles) {
jobs.append(qMakePair(fp, true));
}
QStringList attributes;
if (bomAttributes.isEmpty()) {
attributes = project->getCustomBomAttributes();
} else {
foreach (const QString str, bomAttributes.simplified().split(',')) {
if (!str.trimmed().isEmpty()) {
attributes.append(str.trimmed());
}
}
}
foreach (const auto& job, jobs) {
QList<Board*> boardsToExport;
const QString& destStr = job.first;
bool boardSpecific = job.second;
if (boardSpecific) {
print(tr("Export board-specific BOM to '%1'...").arg(destStr));
boardsToExport = boards;
} else {
print(tr("Export generic BOM to '%1'...").arg(destStr));
boardsToExport = {nullptr};
}
foreach (const Board* board, boardsToExport) {
foreach (std::shared_ptr<AssemblyVariant> av, assemblyVariants) {
QString destPathStr = AttributeSubstitutor::substitute(
destStr,
board ? ProjectAttributeLookup(*board, av)
: ProjectAttributeLookup(*project, av),
[&](const QString& str) {
return FilePath::cleanFileName(
str, FilePath::ReplaceSpaces | FilePath::KeepCase);
});
FilePath fp(QFileInfo(destPathStr).absoluteFilePath());
BomGenerator gen(*project);
gen.setAdditionalAttributes(attributes);
std::shared_ptr<Bom> bom = gen.generate(board, av->getUuid());
if (board) {
print(QString(" - '%1' => '%2'")
.arg(*board->getName(), prettyPath(fp, destPathStr)));
} else {
print(QString(" => '%1'").arg(prettyPath(fp, destPathStr)));
}
QString suffix = destStr.split('.').last().toLower();
if (suffix == "csv") {
BomCsvWriter writer(*bom);
std::shared_ptr<CsvFile> csv = writer.generateCsv(); // can throw
csv->saveToFile(fp); // can throw
writtenFilesCounter[fp]++;
} else {
printErr(" " % tr("ERROR: Unknown extension '%1'.").arg(suffix));
success = false;
}
}
}
}
}
// Export PCB fabrication data
if (exportPcbFabricationData) {
print(tr("Export PCB fabrication data..."));
tl::optional<BoardFabricationOutputSettings> customSettings;
QList<Board*> boardsToExport = boards;
if (!pcbFabricationSettingsPath.isEmpty()) {
try {
qDebug() << "Load custom fabrication output settings:"
<< pcbFabricationSettingsPath;
const FilePath fp(
QFileInfo(pcbFabricationSettingsPath).absoluteFilePath());
const std::unique_ptr<const SExpression> root =
SExpression::parse(FileUtils::readFile(fp), fp);
customSettings = BoardFabricationOutputSettings(*root); // can throw
} catch (const Exception& e) {
printErr(
tr("ERROR: Failed to load custom settings: %1").arg(e.getMsg()));
success = false;
boardsToExport.clear(); // avoid exporting any boards
}
}
foreach (const Board* board, boardsToExport) {
print(" " % tr("Board '%1':").arg(*board->getName()));
BoardGerberExport grbExport(*board);
grbExport.exportPcbLayers(
customSettings
? *customSettings
: board->getFabricationOutputSettings()); // can throw
foreach (const FilePath& fp, grbExport.getWrittenFiles()) {
print(QString(" => '%1'").arg(prettyPath(fp, projectFile)));
writtenFilesCounter[fp]++;
}
}
}
// Export pick&place files
if ((exportPnpTopFiles.count() + exportPnpBottomFiles.count()) > 0) {
struct Job {
QString boardSideStr;
PickPlaceCsvWriter::BoardSide boardSideCsv;
BoardGerberExport::BoardSide boardSideGbr;
QString destStr;
};
QVector<Job> jobs;
foreach (const QString& fp, exportPnpTopFiles) {
jobs.append(Job{tr("top"), PickPlaceCsvWriter::BoardSide::Top,
BoardGerberExport::BoardSide::Top, fp});
}
foreach (const QString& fp, exportPnpBottomFiles) {
jobs.append(Job{tr("bottom"), PickPlaceCsvWriter::BoardSide::Bottom,
BoardGerberExport::BoardSide::Bottom, fp});
}
foreach (const auto& job, jobs) {
print(tr("Export %1 assembly data to '%2'...")
.arg(job.boardSideStr)
.arg(job.destStr));
foreach (const Board* board, boards) {
foreach (std::shared_ptr<AssemblyVariant> av, assemblyVariants) {
const QString destPathStr = AttributeSubstitutor::substitute(
job.destStr, ProjectAttributeLookup(*board, av),
[&](const QString& str) {
return FilePath::cleanFileName(
str, FilePath::ReplaceSpaces | FilePath::KeepCase);
});
const FilePath fp(QFileInfo(destPathStr).absoluteFilePath());
print(QString(" - '%1' => '%2'")
.arg(*board->getName(), prettyPath(fp, destPathStr)));
const QString suffix = job.destStr.split('.').last().toLower();
if (suffix == "csv") {
BoardPickPlaceGenerator gen(*board, av->getUuid());
std::shared_ptr<PickPlaceData> data = gen.generate();
PickPlaceCsvWriter writer(*data);
writer.setIncludeMetadataComment(true);
writer.setBoardSide(job.boardSideCsv);
std::shared_ptr<CsvFile> csv = writer.generateCsv(); // can throw
csv->saveToFile(fp); // can throw
writtenFilesCounter[fp]++;
} else if (suffix == "gbr") {
BoardGerberExport gen(*board);
gen.exportComponentLayer(job.boardSideGbr, av->getUuid(),
fp); // can throw
writtenFilesCounter[fp]++;
} else {
printErr(" " % tr("ERROR: Unknown extension '%1'.").arg(suffix));
success = false;
}
}
}
}
}
// Export netlist files
foreach (const QString& destStr, exportNetlistFiles) {
print(tr("Export netlist to '%1'...").arg(destStr));
foreach (const Board* board, boards) {
QString destPathStr = AttributeSubstitutor::substitute(
destStr, ProjectAttributeLookup(*board, nullptr),
[&](const QString& str) {
return FilePath::cleanFileName(
str, FilePath::ReplaceSpaces | FilePath::KeepCase);
});
const FilePath fp(QFileInfo(destPathStr).absoluteFilePath());
print(QString(" - '%1' => '%2'")
.arg(*board->getName(), prettyPath(fp, destPathStr)));
const QString suffix = destStr.split('.').last().toLower();
if (suffix == "d356") {
BoardD356NetlistExport exp(*board);
FileUtils::writeFile(fp, exp.generate()); // can throw
writtenFilesCounter[fp]++;
} else {
printErr(" " % tr("ERROR: Unknown extension '%1'.").arg(suffix));
success = false;
}
}
}
// Save project
if (save) {
print(tr("Save project..."));
if (failIfFileFormatUnstable()) {
success = false;
} else {
project->save(); // can throw
if (projectFp.getSuffix() == "lppz") {
projectFs->exportToZip(projectFp); // can throw
} else {
projectFs->save(); // can throw
}
}
}
// Fail if some files were written multiple times
bool filesOverwritten = false;
for (auto it = writtenFilesCounter.begin(); it != writtenFilesCounter.end();
++it) {
const int totalWritten =
it.value() + writtenOutputJobFilesCounter.value(it.key(), 0);
if (totalWritten > 1) {
filesOverwritten = true;
printErr(tr("ERROR: The file '%1' was written multiple times!")
.arg(prettyPath(it.key(), projectFile)));
}
}
if (filesOverwritten) {
printErr(tr("NOTE: To avoid writing files multiple times, make "
"sure to pass unique filepaths to all export "
"functions. For board output files, you could either "
"add the placeholder '%1' to the path or specify the "
"boards to export with the '%2' argument.")
.arg("{{BOARD}}", "--board"));
success = false;
}
return success;
} catch (const Exception& e) {
printErr(tr("ERROR: %1").arg(e.getMsg()));
return false;
}
}
bool CommandLineInterface::openLibrary(const QString& libDir, bool all,
bool runCheck, bool minifyStepFiles,
bool save, bool strict) const noexcept {
try {
bool success = true;
// Open library
FilePath libFp(QFileInfo(libDir).absoluteFilePath());
print(tr("Open library '%1'...").arg(prettyPath(libFp, libDir)));
std::shared_ptr<TransactionalFileSystem> libFs =
TransactionalFileSystem::open(libFp, save); // can throw
std::unique_ptr<Library> lib =
Library::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(libFs))); // can throw
processLibraryElement(libDir, *libFs, *lib, runCheck, minifyStepFiles, save,
strict,
success); // can throw
// Open all component categories
if (all) {
QStringList elements = lib->searchForElements<ComponentCategory>();
elements.sort(); // For deterministic console output.
print(tr("Process %1 component categories...").arg(elements.count()));
foreach (const QString& dir, elements) {
FilePath fp = libFp.getPathTo(dir);
qInfo().noquote() << tr("Open '%1'...").arg(prettyPath(fp, libDir));
std::shared_ptr<TransactionalFileSystem> fs =
TransactionalFileSystem::open(fp, save); // can throw
std::unique_ptr<ComponentCategory> element =
ComponentCategory::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(fs))); // can throw
processLibraryElement(libDir, *fs, *element, runCheck, minifyStepFiles,
save, strict,
success); // can throw
}
}
// Open all package categories
if (all) {
QStringList elements = lib->searchForElements<PackageCategory>();
elements.sort(); // For deterministic console output.
print(tr("Process %1 package categories...").arg(elements.count()));
foreach (const QString& dir, elements) {
FilePath fp = libFp.getPathTo(dir);
qInfo().noquote() << tr("Open '%1'...").arg(prettyPath(fp, libDir));
std::shared_ptr<TransactionalFileSystem> fs =
TransactionalFileSystem::open(fp, save); // can throw
std::unique_ptr<PackageCategory> element =
PackageCategory::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(fs))); // can throw
processLibraryElement(libDir, *fs, *element, runCheck, minifyStepFiles,
save, strict,
success); // can throw
}
}
// Open all symbols
if (all) {
QStringList elements = lib->searchForElements<Symbol>();
elements.sort(); // For deterministic console output.
print(tr("Process %1 symbols...").arg(elements.count()));
foreach (const QString& dir, elements) {
FilePath fp = libFp.getPathTo(dir);
qInfo().noquote() << tr("Open '%1'...").arg(prettyPath(fp, libDir));
std::shared_ptr<TransactionalFileSystem> fs =
TransactionalFileSystem::open(fp, save); // can throw
std::unique_ptr<Symbol> element =
Symbol::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(fs))); // can throw
processLibraryElement(libDir, *fs, *element, runCheck, minifyStepFiles,
save, strict,
success); // can throw
}
}
// Open all packages
if (all) {
QStringList elements = lib->searchForElements<Package>();
elements.sort(); // For deterministic console output.
print(tr("Process %1 packages...").arg(elements.count()));
foreach (const QString& dir, elements) {
FilePath fp = libFp.getPathTo(dir);
qInfo().noquote() << tr("Open '%1'...").arg(prettyPath(fp, libDir));
std::shared_ptr<TransactionalFileSystem> fs =
TransactionalFileSystem::open(fp, save); // can throw
std::unique_ptr<Package> element =
Package::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(fs))); // can throw
processLibraryElement(libDir, *fs, *element, runCheck, minifyStepFiles,
save, strict,
success); // can throw
}
}
// Open all components
if (all) {
QStringList elements = lib->searchForElements<Component>();
elements.sort(); // For deterministic console output.
print(tr("Process %1 components...").arg(elements.count()));
foreach (const QString& dir, elements) {
FilePath fp = libFp.getPathTo(dir);
qInfo().noquote() << tr("Open '%1'...").arg(prettyPath(fp, libDir));
std::shared_ptr<TransactionalFileSystem> fs =
TransactionalFileSystem::open(fp, save); // can throw
std::unique_ptr<Component> element =
Component::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(fs))); // can throw
processLibraryElement(libDir, *fs, *element, runCheck, minifyStepFiles,
save, strict,
success); // can throw
}
}
// Open all devices
if (all) {
QStringList elements = lib->searchForElements<Device>();
elements.sort(); // For deterministic console output.
print(tr("Process %1 devices...").arg(elements.count()));
foreach (const QString& dir, elements) {
FilePath fp = libFp.getPathTo(dir);
qInfo().noquote() << tr("Open '%1'...").arg(prettyPath(fp, libDir));
std::shared_ptr<TransactionalFileSystem> fs =
TransactionalFileSystem::open(fp, save); // can throw
std::unique_ptr<Device> element =
Device::open(std::unique_ptr<TransactionalDirectory>(
new TransactionalDirectory(fs))); // can throw
processLibraryElement(libDir, *fs, *element, runCheck, minifyStepFiles,
save, strict,
success); // can throw
}
}
return success;
} catch (const Exception& e) {
printErr(tr("ERROR: %1").arg(e.getMsg()));
return false;
}
}
void CommandLineInterface::processLibraryElement(
const QString& libDir, TransactionalFileSystem& fs,
LibraryBaseElement& element, bool runCheck, bool minifyStepFiles, bool save,
bool strict, bool& success) const {
// Helper function to print an error header to console only once, if
// there is at least one error.
bool errorHeaderPrinted = false;
auto printErrorHeaderOnce = [&errorHeaderPrinted, &element]() {
if (!errorHeaderPrinted) {
printErr(QString(" - %1 (%2):")
.arg(*element.getNames().getDefaultValue(),
element.getUuid().toStr()));
errorHeaderPrinted = true;
}
};
// Save element to transactional file system, if needed
if (strict || save) {
element.save(); // can throw
}
// Minify STEP files, if needed.
if (minifyStepFiles && dynamic_cast<Package*>(&element)) {
foreach (const QString& file, fs.getFiles()) {
if (file.endsWith(".step")) {
const QString fp = prettyPath(fs.getAbsPath(file), libDir);
qInfo().noquote() << tr("Minify STEP model '%1'...").arg(fp);
try {
const QByteArray content = fs.read(file); // can throw
const QByteArray minified =
OccModel::minifyStep(content); // can throw
if (minified != content) {
print(tr(" - Minified '%1' from %2 to %3 bytes")
.arg(fp)
.arg(content.size())
.arg(minified.size()));
OccModel::loadStep(minified); // throws if STEP is invalid
fs.write(file, minified);
}
} catch (const Exception& e) {
printErrorHeaderOnce();
printErr(QString(" - Failed to minify STEP model '%1': %2")
.arg(fp, e.getMsg()));
success = false;
}
}
}
}
// Check for non-canonical files (strict mode)
if (strict) {
qInfo().noquote() << tr("Check '%1' for non-canonical files...")
.arg(prettyPath(fs.getPath(), libDir));
QStringList paths = fs.checkForModifications(); // can throw
if (!paths.isEmpty()) {
// sort file paths to increases readability of console output
std::sort(paths.begin(), paths.end());
printErrorHeaderOnce();
foreach (const QString& path, paths) {
printErr(QString(" - Non-canonical file: '%1'")
.arg(prettyPath(fs.getAbsPath(path), libDir)));
}
success = false;
}
}
// Run library element check, if needed.
if (runCheck) {
qInfo().noquote() << tr("Check '%1' for non-approved messages...")
.arg(prettyPath(fs.getPath(), libDir));
int approvedMsgCount = 0;
const RuleCheckMessageList messages = element.runChecks();
const QStringList nonApproved = prepareRuleCheckMessages(
messages, element.getMessageApprovals(), approvedMsgCount);
qInfo().noquote() << " " %
tr("Approved messages: %1").arg(approvedMsgCount);
qInfo().noquote() << " " %
tr("Non-approved messages: %1").arg(nonApproved.count());
foreach (const QString& msg, nonApproved) {
printErrorHeaderOnce();
printErr(" - " % msg);
success = false;
}
}
// Save element to file system, if needed
if (save) {
qInfo().noquote()
<< tr("Save '%1'...").arg(prettyPath(fs.getPath(), libDir));
if (failIfFileFormatUnstable()) {
success = false;
} else {
fs.save(); // can throw
}
}
// Do not propagate changes in the transactional file system to the
// following checks
fs.discardChanges();
}
bool CommandLineInterface::openStep(const QString& filePath, bool minify,
bool tesselate,
const QString& saveTo) const noexcept {
try {
// Note: Not using tr() for this command as it is basically intended for
// developers, not end users.
bool success = true;
// Open file.
const FilePath stepFp(QFileInfo(filePath).absoluteFilePath());
print(QString("Open STEP file '%1'...").arg(prettyPath(stepFp, filePath)));
QByteArray stepContent = FileUtils::readFile(stepFp); // can throw
// Minify before validation.
if (minify) {
print("Perform minify...");
const QByteArray minified =
OccModel::minifyStep(stepContent); // can throw
if (minified != stepContent) {
const qreal percent = 100 * (minified.size() - stepContent.size()) /
qreal(stepContent.size());
QLocale locale = QLocale::c();
locale.setNumberOptions(QLocale::DefaultNumberOptions);
print(QString(" - Minified from %1 bytes to %2 bytes (%3%)")
.arg(locale.toString(stepContent.size()))
.arg(locale.toString(minified.size()))
.arg(percent, 0, 'f', 0));
if (minified.size() > stepContent.size()) {
printErr(" - ERROR: The output is larger than the input!");
success = false;
}
stepContent = minified;
} else {
print(" - File is already minified");
}
}
// Write to output *before* validating it, otherwise it won't be possible
// to inspect the invalid result of the minify operation.
if (!saveTo.isEmpty()) {
const FilePath outFp(QFileInfo(saveTo).absoluteFilePath());
print(QString("Save to '%1'...").arg(prettyPath(outFp, saveTo)));
FileUtils::writeFile(outFp, stepContent); // can throw
}
// Validate.
print("Load model...");
std::unique_ptr<OccModel> model =
OccModel::loadStep(stepContent); // throws if STEP is invalid
// Tesselate.
if (tesselate) {
print("Tesselate model...");
const QMap<OccModel::Color, QVector<QVector3D>> vertices =
model->tesselate(); // can throw
int vertexCount = 0;
foreach (const auto& v, vertices) {
vertexCount += v.count();
}
print(QString(" - Built %1 vertices with %2 different colors")
.arg(vertexCount)
.arg(vertices.count()));
if (vertexCount == 0) {
printErr(" - ERROR: No content found in model!");
success = false;
}
}
return success;
} catch (const Exception& e) {
printErr(tr("ERROR: %1").arg(e.getMsg()));
return false;
}
}
QStringList CommandLineInterface::prepareRuleCheckMessages(
RuleCheckMessageList messages, const QSet<SExpression>& approvals,
int& approvedMsgCount) noexcept {
// Sort messages to increases readability of console output.
Toolbox::sortNumeric(
messages,
[](const QCollator& cmp,
const std::shared_ptr<const RuleCheckMessage>& lhs,
const std::shared_ptr<const RuleCheckMessage>& rhs) {
if (lhs->getSeverity() != rhs->getSeverity()) {
return lhs->getSeverity() > rhs->getSeverity();
} else {
return cmp(lhs->getMessage(), rhs->getMessage());
}
},
Qt::CaseInsensitive, false);
approvedMsgCount = 0;
QStringList printedMessages;
foreach (const auto& msg, messages) {
if (approvals.contains(msg->getApproval())) {
++approvedMsgCount;
} else {
printedMessages.append(QString("[%1] %2").arg(
msg->getSeverityTr().toUpper(), msg->getMessage()));
}
}
return printedMessages;
}
QString CommandLineInterface::prettyPath(const FilePath& path,
const QString& style) noexcept {
if (QFileInfo(style).isAbsolute()) {
// absolute path
return path.toNative();
} else if (path == FilePath(QDir::currentPath())) {
// name of current directory
return path.getFilename();
} else {
// relative path
return path.toRelativeNative(FilePath(QDir::currentPath()));
}
}
bool CommandLineInterface::failIfFileFormatUnstable() noexcept {
if ((!Application::isFileFormatStable()) &&
(qgetenv("LIBREPCB_DISABLE_UNSTABLE_WARNING") != "1")) {
printErr(
tr("This application version is UNSTABLE! Option '%1' is disabled to "
"avoid breaking projects or libraries. Please use a stable "
"release instead.")
.arg("--save"));
return true;
} else {
qInfo() << "Application version is unstable, but warning is disabled with "
"environment variable LIBREPCB_DISABLE_UNSTABLE_WARNING.";
return false;
}
}
void CommandLineInterface::print(const QString& str) noexcept {
QTextStream s(stdout);
s << str << '\n';
}
void CommandLineInterface::printErr(const QString& str) noexcept {
QTextStream s(stderr);
s << str << '\n';
}
/*******************************************************************************
* End of File
******************************************************************************/
} // namespace cli
} // namespace librepcb
|