KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_sch_erc.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 (C) 2022 Mark Roszko <[email protected]>
5 * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "command_sch_erc.h"
22#include <cli/exit_codes.h>
23#include "jobs/job_sch_erc.h"
24#include <kiface_base.h>
25#include <layer_ids.h>
26#include <string_utils.h>
27#include <wx/crt.h>
28
29#include <macros.h>
30#include <wx/tokenzr.h>
31
32#define ARG_FORMAT "--format"
33#define ARG_UNITS "--units"
34#define ARG_SEVERITY_ALL "--severity-all"
35#define ARG_SEVERITY_ERROR "--severity-error"
36#define ARG_SEVERITY_WARNING "--severity-warning"
37#define ARG_SEVERITY_EXCLUSIONS "--severity-exclusions"
38#define ARG_EXIT_CODE_VIOLATIONS "--exit-code-violations"
39
41{
42 addCommonArgs( true, true, false, false );
44
45 m_argParser.add_description( UTF8STDSTR( _( "Runs the Electrical Rules Check (ERC) on the "
46 "schematic and creates a report" ) ) );
47
48 m_argParser.add_argument( ARG_FORMAT )
49 .default_value( std::string( "report" ) )
50 .help( UTF8STDSTR( _( "Output file format, options: json, report" ) ) );
51
52 m_argParser.add_argument( ARG_UNITS )
53 .default_value( std::string( "mm" ) )
54 .help( UTF8STDSTR(
55 _( "Report units; valid options: in, mm, mils" ) ) );
56
57 m_argParser.add_argument( ARG_SEVERITY_ALL )
58 .help( UTF8STDSTR( _( "Report all ERC violations, this is equivalent to including "
59 "all the other severity arguments" ) ) )
60 .flag();
61
62 m_argParser.add_argument( ARG_SEVERITY_ERROR )
63 .help( UTF8STDSTR( _( "Report all ERC error level violations, this can be combined "
64 "with the other severity arguments" ) ) )
65 .flag();
66
67 m_argParser.add_argument( ARG_SEVERITY_WARNING )
68 .help( UTF8STDSTR( _( "Report all ERC warning level violations, this can be combined "
69 "with the other severity arguments" ) ) )
70 .implicit_value( true )
71 .default_value( false );
72
74 .help( UTF8STDSTR( _( "Report all excluded ERC violations, this can be combined "
75 "with the other severity arguments" ) ) )
76 .flag();
77
79 .help( UTF8STDSTR( _( "Return a nonzero exit code if ERC violations exist" ) ) )
80 .flag();
81}
82
83
85{
86 std::unique_ptr<JOB_SCH_ERC> ercJob( new JOB_SCH_ERC() );
87
88 ercJob->m_outputFile = m_argOutput;
89 ercJob->m_filename = m_argInput;
90 ercJob->m_exitCodeViolations = m_argParser.get<bool>( ARG_EXIT_CODE_VIOLATIONS );
91 ercJob->SetVarOverrides( m_argDefineVars );
92
93 int severity = 0;
94 if( m_argParser.get<bool>( ARG_SEVERITY_ALL ) )
95 {
97 }
98
99 if( m_argParser.get<bool>( ARG_SEVERITY_ERROR ) )
100 {
101 severity |= RPT_SEVERITY_ERROR;
102 }
103
104 if( m_argParser.get<bool>( ARG_SEVERITY_WARNING ) )
105 {
106 severity |= RPT_SEVERITY_WARNING;
107 }
108
109 if( m_argParser.get<bool>( ARG_SEVERITY_EXCLUSIONS ) )
110 {
111 severity |= RPT_SEVERITY_EXCLUSION;
112 }
113
114 if( severity ) // override the default only if something we configured
115 ercJob->m_severity = severity;
116
117 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_UNITS ).c_str() );
118
119 if( units == wxS( "mm" ) )
120 {
121 ercJob->m_units = JOB_SCH_ERC::UNITS::MILLIMETERS;
122 }
123 else if( units == wxS( "in" ) )
124 {
125 ercJob->m_units = JOB_SCH_ERC::UNITS::INCHES;
126 }
127 else if( units == wxS( "mils" ) )
128 {
129 ercJob->m_units = JOB_SCH_ERC::UNITS::MILS;
130 }
131 else if( !units.IsEmpty() )
132 {
133 wxFprintf( stderr, _( "Invalid units specified\n" ) );
135 }
136
137 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
138
139 if( format == "report" )
140 {
141 ercJob->m_format = JOB_SCH_ERC::OUTPUT_FORMAT::REPORT;
142 }
143 else if( format == "json" )
144 {
145 ercJob->m_format = JOB_SCH_ERC::OUTPUT_FORMAT::JSON;
146 }
147 else
148 {
149 wxFprintf( stderr, _( "Invalid report format\n" ) );
151 }
152
153 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, ercJob.get() );
154
155 return exitCode;
156}
argparse::ArgumentParser m_argParser
Definition: command.h:100
void addDefineArg()
Set up the drawing sheet arg used by many of the export commands.
Definition: command.cpp:169
void addCommonArgs(bool aInput, bool aOutput, bool aInputIsDir, bool aOutputIsDir)
Set up the most common of args used across cli.
Definition: command.cpp:115
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:284
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob)
Definition: kiway.cpp:709
@ FACE_SCH
eeschema DSO
Definition: kiway.h:291
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_SEVERITY_EXCLUSIONS
#define ARG_SEVERITY_ERROR
#define ARG_UNITS
#define ARG_SEVERITY_WARNING
#define ARG_EXIT_CODE_VIOLATIONS
#define ARG_SEVERITY_ALL
#define ARG_FORMAT
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
Definition: exit_codes.h:31
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
wxString From_UTF8(const char *cstring)