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 false, true )
46{
47 m_argParser.add_description( UTF8STDSTR( _( "Generate Drill Files" ) ) );
48
49 m_argParser.add_argument( ARG_FORMAT )
50 .default_value( std::string( "excellon" ) )
51 .help( UTF8STDSTR( _( "Valid options excellon, gerber." ) ) )
52 .metavar( "FORMAT" );
53
54 m_argParser.add_argument( ARG_DRILL_ORIGIN )
55 .default_value( std::string( "absolute" ) )
56 .help( UTF8STDSTR( _( "Valid options are: absolute,plot" ) ) )
57 .metavar( "DRILL_ORIGIN" );
58
60 .default_value( std::string( "decimal" ) )
61 .help( UTF8STDSTR(
62 _( "Valid options are: decimal,suppressleading,suppresstrailing,keep." ) ) )
63 .metavar( "ZEROS_FORMAT" );
64
66 .default_value( std::string( "alternate" ) )
67 .help( UTF8STDSTR( _( "Valid options are: route,alternate." ) ) )
68 .metavar( "OVAL_FORMAT" );
69
70 m_argParser.add_argument( "-u", ARG_EXCELLON_UNITS )
71 .default_value( std::string( "mm" ) )
72 .help( UTF8STDSTR( _( "Output units, valid options:in,mm" ) ) )
73 .metavar( "UNITS" );
74
75 m_argParser.add_argument( ARG_EXCELLON_MIRRORY )
76 .help( UTF8STDSTR( _( "Mirror Y axis" ) ) )
77 .flag();
78
80 .help( UTF8STDSTR( _( "Minimal header" ) ) )
81 .flag();
82
84 .help( UTF8STDSTR( _( "Generate independent files for NPTH and PTH holes" ) ) )
85 .flag();
86
87 m_argParser.add_argument( ARG_GENERATE_MAP )
88 .help( UTF8STDSTR( _( "Generate map / summary of drill hits" ) ) )
89 .flag();
90
91 m_argParser.add_argument( ARG_MAP_FORMAT )
92 .default_value( std::string( "pdf" ) )
93 .help( UTF8STDSTR( _( "Valid options: pdf,gerberx2,ps,dxf,svg" ) ) )
94 .metavar( "MAP_FORMAT" );
95
96 m_argParser.add_argument( ARG_GERBER_PRECISION )
97 .help( UTF8STDSTR( _( "Precision of Gerber coordinates (5 or 6)" ) ) )
98 .default_value( 6 )
99 .scan<'i', int>();
100}
101
102
104{
105 std::unique_ptr<JOB_EXPORT_PCB_DRILL> drillJob( new JOB_EXPORT_PCB_DRILL( true ) );
106
107 drillJob->m_filename = m_argInput;
108 drillJob->m_outputDir = m_argOutput;
109
110 if( !drillJob->m_outputDir.IsEmpty() )
111 {
112 wxFileName fn( drillJob->m_outputDir );
113 if( !fn.IsDir() )
114 {
115 wxFprintf( stderr, _( "Output must be a directory\n" ) );
117 }
118 }
119
120 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
121
122 if( format == wxS( "excellon" ) )
123 {
125 }
126 else if( format == wxS( "gerber" ) )
127 {
129 }
130 else
131 {
132 wxFprintf( stderr, _( "Invalid drill format\n" ) );
134 }
135
136 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_UNITS ).c_str() );
137
138 if( units == wxS( "mm" ) )
139 {
140 drillJob->m_drillUnits = JOB_EXPORT_PCB_DRILL::DRILL_UNITS::MILLIMETERS;
141 }
142 else if( units == wxS( "in" ) )
143 {
144 drillJob->m_drillUnits = JOB_EXPORT_PCB_DRILL::DRILL_UNITS::INCHES;
145 }
146 else
147 {
148 wxFprintf( stderr, _( "Invalid units specified\n" ) );
150 }
151
152 wxString zeroFormat = From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_ZEROS_FORMAT ).c_str() );
153
154 if( zeroFormat == wxS( "decimal" ) )
155 {
156 drillJob->m_zeroFormat = JOB_EXPORT_PCB_DRILL::ZEROS_FORMAT::DECIMAL;
157 }
158 else if( zeroFormat == wxS( "suppressleading" ) )
159 {
161 }
162 else if( zeroFormat == wxS( "suppresstrailing" ) )
163 {
165 }
166 else if( zeroFormat == wxS( "keep" ) )
167 {
168 drillJob->m_zeroFormat = JOB_EXPORT_PCB_DRILL::ZEROS_FORMAT::KEEP_ZEROS;
169 }
170 else
171 {
172 wxFprintf( stderr, _( "Invalid zeros format specified\n" ) );
174 }
175
176 wxString drillFormat =
177 From_UTF8( m_argParser.get<std::string>( ARG_EXCELLON_OVAL_FORMAT ).c_str() );
178 if( drillFormat == wxS( "route" ) )
179 {
180 drillJob->m_excellonOvalDrillRoute = true;
181 }
182 else if( drillFormat == wxS( "alternate" ) )
183 {
184 drillJob->m_excellonOvalDrillRoute = false;
185 }
186 else
187 {
188 wxFprintf( stderr, _( "Invalid oval drill format specified\n" ) );
190 }
191
192 wxString mapFormat = From_UTF8( m_argParser.get<std::string>( ARG_MAP_FORMAT ).c_str() );
193
194 if( mapFormat == wxS( "pdf" ) )
195 {
196 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::PDF;
197 }
198 else if( mapFormat == wxS( "ps" ) )
199 {
200 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::POSTSCRIPT;
201 }
202 else if( mapFormat == wxS( "gerberx2" ) )
203 {
204 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::GERBER_X2;
205 }
206 else if( mapFormat == wxS( "dxf" ) )
207 {
208 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::DXF;
209 }
210 else if( mapFormat == wxS( "svg" ) )
211 {
212 drillJob->m_mapFormat = JOB_EXPORT_PCB_DRILL::MAP_FORMAT::SVG;
213 }
214 else
215 {
216 wxFprintf( stderr, _( "Invalid map format specified\n" ) );
218 }
219
220 wxString origin = From_UTF8( m_argParser.get<std::string>( ARG_DRILL_ORIGIN ).c_str() );
221
222 if( origin == wxS( "absolute" ) )
223 {
224 drillJob->m_drillOrigin = JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::ABS;
225 }
226 else if( origin == wxS( "plot" ) )
227 {
228 drillJob->m_drillOrigin = JOB_EXPORT_PCB_DRILL::DRILL_ORIGIN::PLOT;
229 }
230 else
231 {
232 wxFprintf( stderr, _( "Invalid origin mode specified\n" ) );
234 }
235
236 drillJob->m_excellonMirrorY = m_argParser.get<bool>( ARG_EXCELLON_MIRRORY );
237 drillJob->m_excellonMinimalHeader = m_argParser.get<bool>( ARG_EXCELLON_MINIMALHEAD );
238 drillJob->m_excellonCombinePTHNPTH = !m_argParser.get<bool>( ARG_EXCELLON_SEPARATE_TH );
239 drillJob->m_generateMap = m_argParser.get<bool>( ARG_GENERATE_MAP );
240 drillJob->m_gerberPrecision = m_argParser.get<int>( ARG_GERBER_PRECISION );
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: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: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_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)