KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_pcb_export_drill.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 The 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
22#include <cli/exit_codes.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_EXCELLON_MIRRORY "--excellon-mirror-y"
34#define ARG_EXCELLON_MINIMALHEAD "--excellon-min-header"
35#define ARG_EXCELLON_SEPARATE_TH "--excellon-separate-th"
36#define ARG_EXCELLON_ZEROS_FORMAT "--excellon-zeros-format"
37#define ARG_EXCELLON_OVAL_FORMAT "--excellon-oval-format"
38#define ARG_GERBER_PRECISION "--gerber-precision"
39#define ARG_EXCELLON_UNITS "--excellon-units"
40#define ARG_GENERATE_MAP "--generate-map"
41#define ARG_GENERATE_REPORT "--generate-report"
42#define ARG_REPORT_PATH "--report-path"
43#define ARG_GENERATE_TENTING "--generate-tenting"
44#define ARG_MAP_FORMAT "--map-format"
45#define ARG_DRILL_ORIGIN "--drill-origin"
46
47
49 PCB_EXPORT_BASE_COMMAND( "drill", false, true )
50{
51 m_argParser.add_description( UTF8STDSTR( _( "Generate Drill Files" ) ) );
52
53 m_argParser.add_argument( ARG_FORMAT )
54 .default_value( std::string( "excellon" ) )
55 .help( UTF8STDSTR( _( "Valid options excellon, gerber." ) ) )
56 .metavar( "FORMAT" );
57
58 m_argParser.add_argument( ARG_DRILL_ORIGIN )
59 .default_value( std::string( "absolute" ) )
60 .help( UTF8STDSTR( _( "Valid options are: absolute,plot" ) ) )
61 .metavar( "DRILL_ORIGIN" );
62
64 .default_value( std::string( "decimal" ) )
66 _( "Valid options are: decimal,suppressleading,suppresstrailing,keep." ) ) )
67 .metavar( "ZEROS_FORMAT" );
68
70 .default_value( std::string( "alternate" ) )
71 .help( UTF8STDSTR( _( "Valid options are: route,alternate." ) ) )
72 .metavar( "OVAL_FORMAT" );
73
74 m_argParser.add_argument( "-u", ARG_EXCELLON_UNITS )
75 .default_value( std::string( "mm" ) )
76 .help( UTF8STDSTR( _( "Output units, valid options:in,mm" ) ) )
77 .metavar( "UNITS" );
78
79 m_argParser.add_argument( ARG_EXCELLON_MIRRORY )
80 .help( UTF8STDSTR( _( "Mirror Y axis" ) ) )
81 .flag();
82
84 .help( UTF8STDSTR( _( "Minimal header" ) ) )
85 .flag();
86
88 .help( UTF8STDSTR( _( "Generate independent files for NPTH and PTH holes" ) ) )
89 .flag();
90
91 m_argParser.add_argument( ARG_GENERATE_MAP )
92 .help( UTF8STDSTR( _( "Generate map / summary of drill hits" ) ) )
93 .flag();
94
95 m_argParser.add_argument( ARG_GENERATE_REPORT )
96 .help( UTF8STDSTR( _( "Generate report of drill hits" ) ) )
97 .flag();
98
99 m_argParser.add_argument( ARG_REPORT_PATH )
100 .default_value( std::string( "" ) )
101 .help( UTF8STDSTR( _( "Report output file path" ) ) )
102 .metavar( "REPORT_FILE" );
103
104 m_argParser.add_argument( ARG_GENERATE_TENTING )
105 .help( UTF8STDSTR( _( "Generate a file specifically for tenting" ) ) )
106 .flag();
107
108 m_argParser.add_argument( ARG_MAP_FORMAT )
109 .default_value( std::string( "pdf" ) )
110 .help( UTF8STDSTR( _( "Valid options: pdf,gerberx2,ps,dxf,svg" ) ) )
111 .metavar( "MAP_FORMAT" );
112
113 m_argParser.add_argument( ARG_GERBER_PRECISION )
114 .help( UTF8STDSTR( _( "Precision of Gerber coordinates (5 or 6)" ) ) )
115 .default_value( 6 )
116 .scan<'i', int>();
117}
118
119
121{
122 std::unique_ptr<JOB_EXPORT_PCB_DRILL> drillJob( new JOB_EXPORT_PCB_DRILL() );
123
124 drillJob->m_filename = m_argInput;
125 drillJob->SetConfiguredOutputPath( m_argOutput );
126
127 if( !drillJob->GetConfiguredOutputPath().IsEmpty() )
128 {
129 wxFileName fn( drillJob->GetConfiguredOutputPath(), wxEmptyString );
130
131 if( !fn.IsDir() )
132 {
133 wxFprintf( stderr, _( "Output must be a directory\n" ) );
135 }
136 }
137
138 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
139
140 if( format == wxS( "excellon" ) )
141 {
143 }
144 else if( format == wxS( "gerber" ) )
145 {
147 }
148 else
149 {
150 wxFprintf( stderr, _( "Invalid drill format\n" ) );
152 }
153
154 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_UNITS ).c_str() );
155
156 if( units == wxS( "mm" ) )
157 {
158 drillJob->m_drillUnits = JOB_EXPORT_PCB_DRILL::DRILL_UNITS::MM;
159 }
160 else if( units == wxS( "in" ) )
161 {
162 drillJob->m_drillUnits = JOB_EXPORT_PCB_DRILL::DRILL_UNITS::INCH;
163 }
164 else
165 {
166 wxFprintf( stderr, _( "Invalid units specified\n" ) );
168 }
169
170 wxString zeroFormat = From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_ZEROS_FORMAT ).c_str() );
171
172 if( zeroFormat == wxS( "decimal" ) )
173 {
174 drillJob->m_zeroFormat = JOB_EXPORT_PCB_DRILL::ZEROS_FORMAT::DECIMAL;
175 }
176 else if( zeroFormat == wxS( "suppressleading" ) )
177 {
179 }
180 else if( zeroFormat == wxS( "suppresstrailing" ) )
181 {
183 }
184 else if( zeroFormat == wxS( "keep" ) )
185 {
186 drillJob->m_zeroFormat = JOB_EXPORT_PCB_DRILL::ZEROS_FORMAT::KEEP_ZEROS;
187 }
188 else
189 {
190 wxFprintf( stderr, _( "Invalid zeros format specified\n" ) );
192 }
193
194 wxString drillFormat =
195 From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_OVAL_FORMAT ).c_str() );
196
197 if( drillFormat == wxS( "route" ) )
198 {
199 drillJob->m_excellonOvalDrillRoute = true;
200 }
201 else if( drillFormat == wxS( "alternate" ) )
202 {
203 drillJob->m_excellonOvalDrillRoute = false;
204 }
205 else
206 {
207 wxFprintf( stderr, _( "Invalid oval drill format specified\n" ) );
209 }
210
211 wxString mapFormat = From_UTF8( m_argParser.get<std::string>( ARG_MAP_FORMAT ).c_str() );
212
213 if( mapFormat == wxS( "pdf" ) )
214 {
215 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::PDF;
216 }
217 else if( mapFormat == wxS( "ps" ) )
218 {
219 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::POSTSCRIPT;
220 }
221 else if( mapFormat == wxS( "gerberx2" ) )
222 {
223 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::GERBER_X2;
224 }
225 else if( mapFormat == wxS( "dxf" ) )
226 {
227 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::DXF;
228 }
229 else if( mapFormat == wxS( "svg" ) )
230 {
231 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::SVG;
232 }
233 else
234 {
235 wxFprintf( stderr, _( "Invalid map format specified\n" ) );
237 }
238
239 wxString origin = From_UTF8( m_argParser.get<std::string>( ARG_DRILL_ORIGIN ).c_str() );
240
241 if( origin == wxS( "absolute" ) )
242 {
243 drillJob->m_drillOrigin = JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::ABS;
244 }
245 else if( origin == wxS( "plot" ) )
246 {
247 drillJob->m_drillOrigin = JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::PLOT;
248 }
249 else
250 {
251 wxFprintf( stderr, _( "Invalid origin mode specified\n" ) );
253 }
254
255 drillJob->m_excellonMirrorY = m_argParser.get<bool>( ARG_EXCELLON_MIRRORY );
256 drillJob->m_excellonMinimalHeader = m_argParser.get<bool>( ARG_EXCELLON_MINIMALHEAD );
257 drillJob->m_excellonCombinePTHNPTH = !m_argParser.get<bool>( ARG_EXCELLON_SEPARATE_TH );
258 drillJob->m_generateMap = m_argParser.get<bool>( ARG_GENERATE_MAP );
259 drillJob->m_generateReport = m_argParser.get<bool>( ARG_GENERATE_REPORT );
260 drillJob->m_generateTenting = m_argParser.get<bool>( ARG_GENERATE_TENTING );
261 drillJob->m_gerberPrecision = m_argParser.get<int>( ARG_GERBER_PRECISION );
262
263 if( drillJob->m_gerberPrecision != 5 && drillJob->m_gerberPrecision != 6 )
264 {
265 wxFprintf( stderr, _( "Gerber coordinate precision should be either 5 or 6\n" ) );
267 }
268
269 wxString reportPath = drillJob->m_reportPath =
270 From_UTF8( m_argParser.get<std::string>( ARG_REPORT_PATH ).c_str() );
271 if( drillJob->m_generateReport )
272 {
273 drillJob->m_reportPath = reportPath;
274 }
275 else if( !reportPath.IsEmpty() )
276 {
277 wxFprintf( stderr, _( "Warning: Report path supplied without --generate-report, no report will be generated\n" ) );
278 }
279
280 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, drillJob.get() );
281
282 return exitCode;
283}
argparse::ArgumentParser m_argParser
Definition command.h:106
wxString m_argOutput
Value of the output arg if configured.
Definition command.h:141
wxString m_argInput
Value of the common input arg if configured.
Definition command.h:136
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:292
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Definition kiway.cpp:740
@ FACE_PCB
pcbnew DSO
Definition kiway.h:300
#define UTF8STDSTR(s)
Definition command.h:27
#define ARG_FORMAT
#define ARG_DRILL_ORIGIN
#define ARG_EXCELLON_SEPARATE_TH
#define ARG_GENERATE_MAP
#define ARG_MAP_FORMAT
#define ARG_GENERATE_TENTING
#define ARG_REPORT_PATH
#define ARG_EXCELLON_MINIMALHEAD
#define ARG_EXCELLON_ZEROS_FORMAT
#define ARG_EXCELLON_UNITS
#define ARG_EXCELLON_MIRRORY
#define ARG_GENERATE_REPORT
#define ARG_EXCELLON_OVAL_FORMAT
#define ARG_GERBER_PRECISION
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
Definition exit_codes.h:31
wxString From_UTF8(const char *cstring)
PCB_EXPORT_BASE_COMMAND(const std::string &aName, bool aInputCanBeDir=false, bool aOutputIsDir=false)