KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_sch_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/file.h>
26#include <wx/filename.h>
27
28#define ARG_TARGET "--target"
29#define ARG_FORCE "--force"
30#define ARG_DRY_RUN "--dry-run"
31#define ARG_LIBRARIES "--libraries"
32#define ARG_VARIANT "--variant"
33#define ARG_DROP_APPROXIMATIONS "--drop-approximations"
34#define ARG_REPORT "--report"
35
37 COMMAND( "downgrade" )
38{
40 m_argParser.add_description( UTF8STDSTR( _( "Export a copy of the schematic readable by an older KiCad version. "
41 "A new --output folder is required except for --dry-run." ) ) );
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(
68 _( "Copy this symbol-library folder into the output folder and downgrade the copies" ) ) );
69
70 m_argParser.add_argument( ARG_VARIANT )
71 .default_value( std::string() )
72 .help( UTF8STDSTR( _( "Flatten this variant into the export. Default is the base design." ) ) );
73}
74
76{
77 std::unique_ptr<JOB_SCH_DOWNGRADE> job = std::make_unique<JOB_SCH_DOWNGRADE>();
78
79 job->m_filename = m_argInput;
80 job->m_outputDir = m_argOutput;
81 job->m_target = wxString::FromUTF8( m_argParser.get<std::string>( ARG_TARGET ) );
82 job->m_force = m_argParser.get<bool>( ARG_FORCE );
83 job->m_dryRun = m_argParser.get<bool>( ARG_DRY_RUN );
84 job->m_dropInsteadOfApproximate = m_argParser.get<bool>( ARG_DROP_APPROXIMATIONS );
85 job->m_libraryDir = wxString::FromUTF8( m_argParser.get<std::string>( ARG_LIBRARIES ) );
86 job->m_variant = wxString::FromUTF8( m_argParser.get<std::string>( ARG_VARIANT ) );
87
88 if( !wxFile::Exists( job->m_filename ) )
89 {
90 wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
92 }
93
94 if( !job->m_dryRun && job->m_outputDir.IsEmpty() )
95 {
96 wxFprintf( stderr, _( "An output folder is required. Use --output.\n" ) );
98 }
99
100 if( !job->m_outputDir.IsEmpty() && !job->m_dryRun )
101 {
102 wxFileName inFn( job->m_filename );
103 wxFileName outDir( job->m_outputDir, wxEmptyString );
104 inFn.MakeAbsolute();
105 outDir.MakeAbsolute();
106 job->m_outputDir = outDir.GetPath();
107
108 wxFileName inDir( inFn.GetPath(), wxEmptyString );
109
110 if( outDir.SameAs( inDir ) )
111 {
112 wxFprintf( stderr, _( "The output folder must differ from the schematic folder.\n" ) );
114 }
115 }
116
117 wxString reportPath = wxString::FromUTF8( m_argParser.get<std::string>( ARG_REPORT ) );
118 wxString reportError;
119
120 if( !ValidateDowngradeReportPath( reportPath, reportError ) )
121 {
122 wxFprintf( stderr, wxS( "%s\n" ), reportError );
124 }
125
126 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, job.get() );
127
128 // Written after the run, so a refused export still explains itself
129 if( !WriteDowngradeReport( job->m_report, job->m_target, reportPath, reportError ) )
130 {
131 wxFprintf( stderr, wxS( "%s\n" ), reportError );
133 }
134
135 return exitCode;
136}
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_SCH
eeschema DSO
Definition kiway.h:355
#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 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.