KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_fp_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 <wx/crt.h>
25#include <wx/filename.h>
26
27#define ARG_TARGET "--target"
28#define ARG_FORCE "--force"
29#define ARG_DRY_RUN "--dry-run"
30#define ARG_DROP_APPROXIMATIONS "--drop-approximations"
31#define ARG_REPORT "--report"
32
33
35 COMMAND( "downgrade" )
36{
38 m_argParser.add_description( UTF8STDSTR( _( "Export a copy of a footprint library readable by an older KiCad "
39 "version. A new --output is required except for --dry-run." ) ) );
40
41 m_argParser.add_argument( ARG_TARGET )
42 .default_value( std::string() )
43 .help( UTF8STDSTR(
44 _( "Target version name or id, e.g. \"KiCad 10.0\" or \"10.0\". Defaults to the newest." ) ) );
45
46 m_argParser.add_argument( ARG_FORCE )
47 .help( UTF8STDSTR( _( "Export even if some features must be dropped or approximated" ) ) )
48 .flag();
49
50 m_argParser.add_argument( ARG_DRY_RUN )
51 .help( UTF8STDSTR( _( "Print the compatibility report without writing anything" ) ) )
52 .flag();
53
55 .help( UTF8STDSTR(
56 _( "Omit features instead of approximating them; may remove drawing or electrical data" ) ) )
57 .flag();
58
59 m_argParser.add_argument( ARG_REPORT )
60 .default_value( std::string() )
61 .help( UTF8STDSTR( _( "Also write the compatibility report as text to this file" ) ) );
62}
63
64
66{
67 std::unique_ptr<JOB_FP_DOWNGRADE> job = std::make_unique<JOB_FP_DOWNGRADE>();
68
69 job->m_libraryPath = m_argInput;
70 job->m_outputLibraryPath = m_argOutput;
71 job->m_target = wxString::FromUTF8( m_argParser.get<std::string>( ARG_TARGET ) );
72 job->m_force = m_argParser.get<bool>( ARG_FORCE );
73 job->m_dryRun = m_argParser.get<bool>( ARG_DRY_RUN );
74 job->m_dropInsteadOfApproximate = m_argParser.get<bool>( ARG_DROP_APPROXIMATIONS );
75
76 if( !wxFileName::Exists( job->m_libraryPath ) )
77 {
78 wxFprintf( stderr, _( "The footprint library does not exist or is not accessible\n" ) );
80 }
81
82 if( !job->m_dryRun )
83 {
84 if( job->m_outputLibraryPath.IsEmpty() )
85 {
86 wxFprintf( stderr, _( "An output library is required. Use --output.\n" ) );
88 }
89
90 wxFileName input( job->m_libraryPath );
91 wxFileName output( job->m_outputLibraryPath );
92 input.MakeAbsolute();
93 output.MakeAbsolute();
94 job->m_outputLibraryPath = output.GetFullPath();
95
96 if( output.SameAs( input ) )
97 {
98 wxFprintf( stderr, _( "The output must differ from the footprint library.\n" ) );
100 }
101 }
102
103 wxString reportPath = wxString::FromUTF8( m_argParser.get<std::string>( ARG_REPORT ) );
104 wxString reportError;
105
106 if( !ValidateDowngradeReportPath( reportPath, reportError ) )
107 {
108 wxFprintf( stderr, wxS( "%s\n" ), reportError );
110 }
111
112 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, job.get() );
113
114 // Written after the run, so a refused export still explains itself
115 if( !WriteDowngradeReport( job->m_report, job->m_target, reportPath, reportError ) )
116 {
117 wxFprintf( stderr, wxS( "%s\n" ), reportError );
119 }
120
121 return exitCode;
122}
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:765
@ FACE_PCB
pcbnew DSO
Definition kiway.h:356
#define UTF8STDSTR(s)
Definition command.h:27
#define ARG_DRY_RUN
#define ARG_TARGET
#define ARG_FORCE
#define ARG_DROP_APPROXIMATIONS
#define ARG_REPORT
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 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.