From 87d5ae5580d3485720a5a912c13b6f9d2bc54e00 Mon Sep 17 00:00:00 2001 From: Shane Ringrose Date: Thu, 11 Jun 2026 11:45:35 +1200 Subject: [PATCH] CLI export: add wiring list and wire-number list (CSV) Extends the headless command-line export with two CSV outputs: qelectrotech --export-cables wiring list qelectrotech --export-wires wire numbers - --export-cables reuses WiringListExport (one row per conductor). - --export-wires reuses ConductorNumExport::wiresNum() (distinct wire numbers). WiringListExport::toCsv() mixed CSV generation with the file dialog and writing. Extracted the generation into a new const method toCsvString() that returns the CSV; toCsv() now calls it and writes the result. This makes the wiring list usable headlessly with no behavioural change to the GUI export. Addresses part of the CLI export requests (#162, #309): @pkess specifically asked to "export all connections as a list". --- sources/cli_export.cpp | 34 +++++++++++++++++++++++ sources/cli_export.h | 10 +++++-- sources/wiringlistexport.cpp | 53 ++++++++++++++++++++++-------------- sources/wiringlistexport.h | 5 ++++ 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/sources/cli_export.cpp b/sources/cli_export.cpp index cbb5d31e8..c3985375b 100644 --- a/sources/cli_export.cpp +++ b/sources/cli_export.cpp @@ -18,10 +18,13 @@ #include "cli_export.h" #include "bordertitleblock.h" +#include "conductornumexport.h" #include "diagram.h" #include "qetproject.h" +#include "wiringlistexport.h" #include +#include #include #include #include @@ -40,6 +43,8 @@ const QHash &exportFlags() {"--export-pdf", "pdf"}, {"--export-png", "png"}, {"--export-svg", "svg"}, + {"--export-cables", "cables"}, + {"--export-wires", "wires"}, }; return flags; } @@ -159,6 +164,33 @@ int exportImages(QETProject &project, const QString &format, return 0; } +int exportCsv(QETProject &project, const QString &format, const QString &output) +{ + QString csv; + if (format == "cables") { + WiringListExport wle(&project, nullptr); + csv = wle.toCsvString(); + } else { // wires + ConductorNumExport cne(&project, nullptr); + csv = cne.wiresNum(); + } + if (csv.isEmpty()) { + err << "Nothing to export (empty list).\n"; + return 1; + } + + QFile file(output); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + err << "Cannot open '" << output << "' for writing.\n"; + return 1; + } + QTextStream fout(&file); + fout << csv; + file.close(); + out << "Exported " << format << " list -> " << output << "\n"; + return 0; +} + } // anonymous namespace namespace CLIExport { @@ -205,6 +237,8 @@ int run(const QStringList &args) const QString format = exportFlags().value(flag); if (format == "pdf") return exportPdf(project, output); + if (format == "cables" || format == "wires") + return exportCsv(project, format, output); return exportImages(project, format, output); } diff --git a/sources/cli_export.h b/sources/cli_export.h index 7037838ac..9ac699307 100644 --- a/sources/cli_export.h +++ b/sources/cli_export.h @@ -42,12 +42,16 @@ namespace CLIExport { @return process exit code (0 on success). Usage: - qelectrotech --export-pdf - qelectrotech --export-png - qelectrotech --export-svg + qelectrotech --export-pdf + qelectrotech --export-png + qelectrotech --export-svg + qelectrotech --export-cables + qelectrotech --export-wires PDF: one multi-page document (one diagram per page). PNG/SVG: one file per diagram, named /_.<ext>. + cables: wiring list (one row per conductor) as CSV. + wires: list of distinct wire numbers as CSV. */ int run(const QStringList &args); diff --git a/sources/wiringlistexport.cpp b/sources/wiringlistexport.cpp index 77b8d0d74..e688a1460 100644 --- a/sources/wiringlistexport.cpp +++ b/sources/wiringlistexport.cpp @@ -151,13 +151,39 @@ void WiringListExport::toCsv() { if (!m_project) return; - QDomDocument doc = m_project->toXml(); - - if (doc.isNull()) { + const QString csv = toCsvString(); + if (csv.isEmpty()) { QMessageBox::warning(m_parent, tr("Erreur"), tr("Impossible de lire la structure en mémoire du projet.")); return; } + QFileDialog dialog(m_parent); + dialog.setAcceptMode(QFileDialog::AcceptSave); + dialog.setWindowTitle(tr("Exporter le plan de câblage")); + dialog.setDefaultSuffix("csv"); + dialog.setNameFilter(tr("Fichiers CSV (*.csv)")); + + if (dialog.exec() != QDialog::Accepted) return; + QString fileName = dialog.selectedFiles().first(); + + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + QMessageBox::warning(m_parent, tr("Erreur"), tr("Impossible d'ouvrir le fichier pour l'écriture.")); + return; + } + QTextStream out(&file); + out << csv; + file.close(); + QMessageBox::information(m_parent, tr("Export réussi"), tr("Le plan de câblage a été exporté avec succès !")); +} + +QString WiringListExport::toCsvString() const +{ + if (!m_project) return QString(); + + QDomDocument doc = m_project->toXml(); + if (doc.isNull()) return QString(); + QSet<QString> conductorDefinitionTypes; QDomElement rootElem = doc.documentElement(); QDomElement collection = rootElem.firstChildElement("collection"); @@ -197,21 +223,6 @@ void WiringListExport::toCsv() } } - QFileDialog dialog(m_parent); - dialog.setAcceptMode(QFileDialog::AcceptSave); - dialog.setWindowTitle(tr("Exporter le plan de câblage")); - dialog.setDefaultSuffix("csv"); - dialog.setNameFilter(tr("Fichiers CSV (*.csv)")); - - if (dialog.exec() != QDialog::Accepted) return; - QString fileName = dialog.selectedFiles().first(); - - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { - QMessageBox::warning(m_parent, tr("Erreur"), tr("Impossible d'ouvrir le fichier pour l'écriture.")); - return; - } - QMap<QString, ElementInfo> elementsInfo = collectElementsInfo(doc.documentElement()); QList<ConductorData> conductors = collectConductors(doc.documentElement()); @@ -353,7 +364,8 @@ void WiringListExport::toCsv() return a.terminalname2 < b.terminalname2; }); - QTextStream out(&file); + QString csv; + QTextStream out(&csv); out << tr("Page", "Wiring list CSV header") << ";" << tr("Composant 1", "Wiring list CSV header") << ";" << tr("Borne 1", "Wiring list CSV header") << ";" @@ -376,6 +388,5 @@ void WiringListExport::toCsv() << c.function << "\n"; } - file.close(); - QMessageBox::information(m_parent, tr("Export réussi"), tr("Le plan de câblage a été exporté avec succès !")); + return csv; } diff --git a/sources/wiringlistexport.h b/sources/wiringlistexport.h index b2766f394..b61779267 100644 --- a/sources/wiringlistexport.h +++ b/sources/wiringlistexport.h @@ -45,6 +45,11 @@ class WiringListExport : public QObject public: explicit WiringListExport(QETProject *project, QWidget *parent = nullptr); void toCsv(); + /** + Build the wiring-list CSV and return it as a string (no GUI). + Used by toCsv() and by the headless command-line export. + */ + QString toCsvString() const; private: QETProject *m_project;