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