KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_pcb_drc.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_pcb_drc.h"
22#include <cli/exit_codes.h>
23#include "jobs/job_pcb_drc.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_ALL_TRACK_ERRORS "--all-track-errors"
34#define ARG_UNITS "--units"
35#define ARG_SEVERITY_ALL "--severity-all"
36#define ARG_SEVERITY_ERROR "--severity-error"
37#define ARG_SEVERITY_WARNING "--severity-warning"
38#define ARG_SEVERITY_EXCLUSIONS "--severity-exclusions"
39#define ARG_EXIT_CODE_VIOLATIONS "--exit-code-violations"
40#define ARG_PARITY "--schematic-parity"
41
43{
44 addCommonArgs( true, true, false, false );
46
47 m_argParser.add_description( UTF8STDSTR( _( "Runs the Design Rules Check (DRC) on the PCB "
48 "and creates a report" ) ) );
49
50 m_argParser.add_argument( ARG_FORMAT )
51 .default_value( std::string( "report" ) )
52 .help( UTF8STDSTR( _( "Output file format, options: json, report" ) ) )
53 .metavar( "FORMAT" );
54
55 m_argParser.add_argument( ARG_ALL_TRACK_ERRORS )
56 .help( UTF8STDSTR( _( "Report all errors for each track" ) ) )
57 .flag();
58
59 m_argParser.add_argument( ARG_PARITY )
60 .help( UTF8STDSTR( _( "Test for parity between PCB and schematic" ) ) )
61 .flag();
62
63 m_argParser.add_argument( ARG_UNITS )
64 .default_value( std::string( "mm" ) )
65 .help( UTF8STDSTR( _( "Report units; valid options: in, mm, mils" ) ) )
66 .metavar( "UNITS" );
67
68 m_argParser.add_argument( ARG_SEVERITY_ALL )
69 .help( UTF8STDSTR( _( "Report all DRC violations, this is equivalent to including "
70 "all the other severity arguments" ) ) )
71 .flag();
72
73 m_argParser.add_argument( ARG_SEVERITY_ERROR )
74 .help( UTF8STDSTR( _( "Report all DRC error level violations, this can be combined "
75 "with the other severity arguments" ) ) )
76 .flag();
77
78 m_argParser.add_argument( ARG_SEVERITY_WARNING )
79 .help( UTF8STDSTR( _( "Report all DRC warning level violations, this can be combined "
80 "with the other severity arguments" ) ) )
81 .flag();
82
84 .help( UTF8STDSTR( _( "Report all excluded DRC violations, this can be combined with "
85 "the other severity arguments" ) ) )
86 .flag();
87
89 .help( UTF8STDSTR( _( "Return a nonzero exit code if DRC violations exist" ) ) )
90 .flag();
91}
92
93
95{
96 std::unique_ptr<JOB_PCB_DRC> drcJob( new JOB_PCB_DRC( true ) );
97
98 drcJob->m_outputFile = m_argOutput;
99 drcJob->m_filename = m_argInput;
100 drcJob->SetVarOverrides( m_argDefineVars );
101 drcJob->m_reportAllTrackErrors = m_argParser.get<bool>( ARG_ALL_TRACK_ERRORS );
102 drcJob->m_exitCodeViolations = m_argParser.get<bool>( ARG_EXIT_CODE_VIOLATIONS );
103
104 int severity = 0;
105 if( m_argParser.get<bool>( ARG_SEVERITY_ALL ) )
106 {
108 }
109
110 if( m_argParser.get<bool>( ARG_SEVERITY_ERROR ) )
111 {
112 severity |= RPT_SEVERITY_ERROR;
113 }
114
115 if( m_argParser.get<bool>( ARG_SEVERITY_WARNING ) )
116 {
117 severity |= RPT_SEVERITY_WARNING;
118 }
119
120 if( m_argParser.get<bool>( ARG_SEVERITY_EXCLUSIONS ) )
121 {
122 severity |= RPT_SEVERITY_EXCLUSION;
123 }
124
125 if( severity ) // override the default only if something we configured
126 drcJob->m_severity = severity;
127
128 drcJob->m_reportAllTrackErrors = m_argParser.get<bool>( ARG_ALL_TRACK_ERRORS );
129
130 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_UNITS ).c_str() );
131
132 if( units == wxS( "mm" ) )
133 {
134 drcJob->m_units = JOB_PCB_DRC::UNITS::MILLIMETERS;
135 }
136 else if( units == wxS( "in" ) )
137 {
138 drcJob->m_units = JOB_PCB_DRC::UNITS::INCHES;
139 }
140 else if( units == wxS( "mils" ) )
141 {
142 drcJob->m_units = JOB_PCB_DRC::UNITS::MILS;
143 }
144 else if( !units.IsEmpty() )
145 {
146 wxFprintf( stderr, _( "Invalid units specified\n" ) );
148 }
149
150 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
151 if( format == "report" )
152 {
153 drcJob->m_format = JOB_PCB_DRC::OUTPUT_FORMAT::REPORT;
154 }
155 else if( format == "json" )
156 {
157 drcJob->m_format = JOB_PCB_DRC::OUTPUT_FORMAT::JSON;
158 }
159 else
160 {
161 wxFprintf( stderr, _( "Invalid report format\n" ) );
163 }
164
165 drcJob->m_parity = m_argParser.get<bool>( ARG_PARITY );
166
167 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, drcJob.get() );
168
169 return exitCode;
170}
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:279
int ProcessJob(KIWAY::FACE_T aFace, JOB *job)
Definition: kiway.cpp:709
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:287
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_SEVERITY_EXCLUSIONS
#define ARG_ALL_TRACK_ERRORS
#define ARG_PARITY
#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)