KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_pcb_downgrade.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
22#include "cli/exit_codes.h"
23#include <downgrade_copy.h>
24#include <downgrade_copy.h>
25#include <wx/crt.h>
26#include <wx/file.h>
27#include <wx/filename.h>
28
29#define ARG_TARGET "--target"
30#define ARG_FORCE "--force"
31#define ARG_DRY_RUN "--dry-run"
32#define ARG_LIBRARIES "--libraries"
33#define ARG_VARIANT "--variant"
34#define ARG_DROP_APPROXIMATIONS "--drop-approximations"
35#define ARG_REPORT "--report"
36
38 COMMAND( "downgrade" )
39{
41 m_argParser.add_description( UTF8STDSTR( _( "Export a copy of the board readable by an older KiCad version" ) ) );
42
43 m_argParser.add_argument( ARG_TARGET )
44 .default_value( std::string() )
45 .help( UTF8STDSTR(
46 _( "Target version name or id, e.g. \"KiCad 10.0\" or \"10.0\". Defaults to the newest." ) ) );
47
48 m_argParser.add_argument( ARG_FORCE )
49 .help( UTF8STDSTR( _( "Export even if some features must be dropped or approximated" ) ) )
50 .flag();
51
52 m_argParser.add_argument( ARG_DRY_RUN )
53 .help( UTF8STDSTR( _( "Print the compatibility report without writing anything" ) ) )
54 .flag();
55
57 .help( UTF8STDSTR(
58 _( "Omit features instead of approximating them; may remove drawing or electrical data" ) ) )
59 .flag();
60
61 m_argParser.add_argument( ARG_REPORT )
62 .default_value( std::string() )
63 .help( UTF8STDSTR( _( "Also write the compatibility report as text to this file" ) ) );
64
65 m_argParser.add_argument( ARG_LIBRARIES )
66 .default_value( std::string() )
67 .help( UTF8STDSTR( _( "Copy this library folder beside the output board and downgrade its footprints" ) ) );
68
69 m_argParser.add_argument( ARG_VARIANT )
70 .default_value( std::string() )
71 .help( UTF8STDSTR( _( "Flatten this variant into the export. Default is the base design." ) ) );
72}
73
75{
76 std::unique_ptr<JOB_PCB_DOWNGRADE> job = std::make_unique<JOB_PCB_DOWNGRADE>();
77
78 job->m_filename = m_argInput;
79 job->m_outputFile = m_argOutput;
80 job->m_target = wxString::FromUTF8( m_argParser.get<std::string>( ARG_TARGET ) );
81 job->m_force = m_argParser.get<bool>( ARG_FORCE );
82 job->m_dryRun = m_argParser.get<bool>( ARG_DRY_RUN );
83 job->m_dropInsteadOfApproximate = m_argParser.get<bool>( ARG_DROP_APPROXIMATIONS );
84 job->m_libraryDir = wxString::FromUTF8( m_argParser.get<std::string>( ARG_LIBRARIES ) );
85 job->m_variant = wxString::FromUTF8( m_argParser.get<std::string>( ARG_VARIANT ) );
86
87 if( !wxFile::Exists( job->m_filename ) )
88 {
89 wxFprintf( stderr, _( "Board file does not exist or is not accessible\n" ) );
91 }
92
93 if( !job->m_dryRun )
94 {
95 if( job->m_outputFile.IsEmpty() )
96 {
97 wxFprintf( stderr, _( "An output file is required. Use --output.\n" ) );
99 }
100
101 wxString error;
102
103 if( !ValidateFreshDowngradeOutput( job->m_outputFile, error ) )
104 {
105 wxFprintf( stderr, wxT( "%s\n" ), error );
107 }
108 }
109
110 wxString reportPath = wxString::FromUTF8( m_argParser.get<std::string>( ARG_REPORT ) );
111 wxString reportError;
112
113 if( !ValidateDowngradeReportPath( reportPath, reportError ) )
114 {
115 wxFprintf( stderr, wxS( "%s\n" ), reportError );
117 }
118
119 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, job.get() );
120
121 // Written after the run, so a refused export still explains itself
122 if( !WriteDowngradeReport( job->m_report, job->m_target, reportPath, reportError ) )
123 {
124 wxFprintf( stderr, wxS( "%s\n" ), reportError );
126 }
127
128 return exitCode;
129}
argparse::ArgumentParser m_argParser
Definition command.h:113
COMMAND(const std::string &aName)
Define a new COMMAND instance.
Definition command.cpp:30
void addCommonArgs(bool aInput, bool aOutput, IO_TYPE aInputType, IO_TYPE aOutputType)
Set up the most common of args used across cli.
Definition command.cpp:135
wxString m_argOutput
Value of the output arg if configured.
Definition command.h:143
wxString m_argInput
Value of the common input arg if configured.
Definition command.h:138
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Definition kiway.cpp:760
@ FACE_PCB
pcbnew DSO
Definition kiway.h:356
#define ARG_VARIANT
Definition command.h:38
#define UTF8STDSTR(s)
Definition command.h:27
#define ARG_DRY_RUN
#define ARG_TARGET
#define ARG_FORCE
#define ARG_DROP_APPROXIMATIONS
#define ARG_REPORT
#define ARG_LIBRARIES
bool ValidateDowngradeReportPath(const wxString &aPath, wxString &aError)
A report destination that is a design or library file is a mistyped path, not a report.
bool ValidateFreshDowngradeOutput(const wxString &aOutput, wxString &aError)
bool WriteDowngradeReport(const COMPATIBILITY_REPORT &aReport, const wxString &aTarget, const wxString &aPath, wxString &aError)
Write the readable compatibility report, the same text the export dialog shows.
#define _(s)
static const int ERR_ARGS
Definition exit_codes.h:31
static const int ERR_INVALID_INPUT_FILE
Definition exit_codes.h:33
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.