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