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_TENTING "--generate-tenting"
42#define ARG_MAP_FORMAT "--map-format"
43#define ARG_DRILL_ORIGIN "--drill-origin"
44
45
47 PCB_EXPORT_BASE_COMMAND( "drill", false, true )
48{
49 m_argParser.add_description( UTF8STDSTR( _( "Generate Drill Files" ) ) );
50
51 m_argParser.add_argument( ARG_FORMAT )
52 .default_value( std::string( "excellon" ) )
53 .help( UTF8STDSTR( _( "Valid options excellon, gerber." ) ) )
54 .metavar( "FORMAT" );
55
56 m_argParser.add_argument( ARG_DRILL_ORIGIN )
57 .default_value( std::string( "absolute" ) )
58 .help( UTF8STDSTR( _( "Valid options are: absolute,plot" ) ) )
59 .metavar( "DRILL_ORIGIN" );
60
62 .default_value( std::string( "decimal" ) )
63 .help( UTF8STDSTR(
64 _( "Valid options are: decimal,suppressleading,suppresstrailing,keep." ) ) )
65 .metavar( "ZEROS_FORMAT" );
66
68 .default_value( std::string( "alternate" ) )
69 .help( UTF8STDSTR( _( "Valid options are: route,alternate." ) ) )
70 .metavar( "OVAL_FORMAT" );
71
72 m_argParser.add_argument( "-u", ARG_EXCELLON_UNITS )
73 .default_value( std::string( "mm" ) )
74 .help( UTF8STDSTR( _( "Output units, valid options:in,mm" ) ) )
75 .metavar( "UNITS" );
76
77 m_argParser.add_argument( ARG_EXCELLON_MIRRORY )
78 .help( UTF8STDSTR( _( "Mirror Y axis" ) ) )
79 .flag();
80
82 .help( UTF8STDSTR( _( "Minimal header" ) ) )
83 .flag();
84
86 .help( UTF8STDSTR( _( "Generate independent files for NPTH and PTH holes" ) ) )
87 .flag();
88
89 m_argParser.add_argument( ARG_GENERATE_MAP )
90 .help( UTF8STDSTR( _( "Generate map / summary of drill hits" ) ) )
91 .flag();
92
93 m_argParser.add_argument( ARG_GENERATE_TENTING )
94 .help( UTF8STDSTR( _( "Generate a file specifically for tenting" ) ) )
95 .flag();
96
97 m_argParser.add_argument( ARG_MAP_FORMAT )
98 .default_value( std::string( "pdf" ) )
99 .help( UTF8STDSTR( _( "Valid options: pdf,gerberx2,ps,dxf,svg" ) ) )
100 .metavar( "MAP_FORMAT" );
101
102 m_argParser.add_argument( ARG_GERBER_PRECISION )
103 .help( UTF8STDSTR( _( "Precision of Gerber coordinates (5 or 6)" ) ) )
104 .default_value( 6 )
105 .scan<'i', int>();
106}
107
108
110{
111 std::unique_ptr<JOB_EXPORT_PCB_DRILL> drillJob( new JOB_EXPORT_PCB_DRILL() );
112
113 drillJob->m_filename = m_argInput;
114 drillJob->SetConfiguredOutputPath( m_argOutput );
115
116 if( !drillJob->GetConfiguredOutputPath().IsEmpty() )
117 {
118 wxFileName fn( drillJob->GetConfiguredOutputPath(), wxEmptyString );
119
120 if( !fn.IsDir() )
121 {
122 wxFprintf( stderr, _( "Output must be a directory\n" ) );
124 }
125 }
126
127 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
128
129 if( format == wxS( "excellon" ) )
130 {
132 }
133 else if( format == wxS( "gerber" ) )
134 {
136 }
137 else
138 {
139 wxFprintf( stderr, _( "Invalid drill format\n" ) );
141 }
142
143 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_UNITS ).c_str() );
144
145 if( units == wxS( "mm" ) )
146 {
147 drillJob->m_drillUnits = JOB_EXPORT_PCB_DRILL::DRILL_UNITS::MM;
148 }
149 else if( units == wxS( "in" ) )
150 {
151 drillJob->m_drillUnits = JOB_EXPORT_PCB_DRILL::DRILL_UNITS::INCH;
152 }
153 else
154 {
155 wxFprintf( stderr, _( "Invalid units specified\n" ) );
157 }
158
159 wxString zeroFormat = From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_ZEROS_FORMAT ).c_str() );
160
161 if( zeroFormat == wxS( "decimal" ) )
162 {
163 drillJob->m_zeroFormat = JOB_EXPORT_PCB_DRILL::ZEROS_FORMAT::DECIMAL;
164 }
165 else if( zeroFormat == wxS( "suppressleading" ) )
166 {
168 }
169 else if( zeroFormat == wxS( "suppresstrailing" ) )
170 {
172 }
173 else if( zeroFormat == wxS( "keep" ) )
174 {
175 drillJob->m_zeroFormat = JOB_EXPORT_PCB_DRILL::ZEROS_FORMAT::KEEP_ZEROS;
176 }
177 else
178 {
179 wxFprintf( stderr, _( "Invalid zeros format specified\n" ) );
181 }
182
183 wxString drillFormat =
184 From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_OVAL_FORMAT ).c_str() );
185
186 if( drillFormat == wxS( "route" ) )
187 {
188 drillJob->m_excellonOvalDrillRoute = true;
189 }
190 else if( drillFormat == wxS( "alternate" ) )
191 {
192 drillJob->m_excellonOvalDrillRoute = false;
193 }
194 else
195 {
196 wxFprintf( stderr, _( "Invalid oval drill format specified\n" ) );
198 }
199
200 wxString mapFormat = From_UTF8( m_argParser.get<std::string>( ARG_MAP_FORMAT ).c_str() );
201
202 if( mapFormat == wxS( "pdf" ) )
203 {
204 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::PDF;
205 }
206 else if( mapFormat == wxS( "ps" ) )
207 {
208 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::POSTSCRIPT;
209 }
210 else if( mapFormat == wxS( "gerberx2" ) )
211 {
212 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::GERBER_X2;
213 }
214 else if( mapFormat == wxS( "dxf" ) )
215 {
216 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::DXF;
217 }
218 else if( mapFormat == wxS( "svg" ) )
219 {
220 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::SVG;
221 }
222 else
223 {
224 wxFprintf( stderr, _( "Invalid map format specified\n" ) );
226 }
227
228 wxString origin = From_UTF8( m_argParser.get<std::string>( ARG_DRILL_ORIGIN ).c_str() );
229
230 if( origin == wxS( "absolute" ) )
231 {
232 drillJob->m_drillOrigin = JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::ABS;
233 }
234 else if( origin == wxS( "plot" ) )
235 {
236 drillJob->m_drillOrigin = JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::PLOT;
237 }
238 else
239 {
240 wxFprintf( stderr, _( "Invalid origin mode specified\n" ) );
242 }
243
244 drillJob->m_excellonMirrorY = m_argParser.get<bool>( ARG_EXCELLON_MIRRORY );
245 drillJob->m_excellonMinimalHeader = m_argParser.get<bool>( ARG_EXCELLON_MINIMALHEAD );
246 drillJob->m_excellonCombinePTHNPTH = !m_argParser.get<bool>( ARG_EXCELLON_SEPARATE_TH );
247 drillJob->m_generateMap = m_argParser.get<bool>( ARG_GENERATE_MAP );
248 drillJob->m_generateTenting = m_argParser.get<bool>( ARG_GENERATE_TENTING );
249 drillJob->m_gerberPrecision = m_argParser.get<int>( ARG_GERBER_PRECISION );
250
251 if( drillJob->m_gerberPrecision != 5 && drillJob->m_gerberPrecision != 6 )
252 {
253 wxFprintf( stderr, _( "Gerber coordinate precision should be either 5 or 6\n" ) );
255 }
256
257 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, drillJob.get() );
258
259 return exitCode;
260}
argparse::ArgumentParser m_argParser
Definition: command.h:100
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:285
@ FACE_PCB
pcbnew DSO
Definition: kiway.h:293
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr)
Definition: kiway.cpp:711
#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_EXCELLON_MINIMALHEAD
#define ARG_EXCELLON_ZEROS_FORMAT
#define ARG_EXCELLON_UNITS
#define ARG_EXCELLON_MIRRORY
#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)