KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_gerber_convert_png.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21#include <cli/exit_codes.h>
23#include <kiface_base.h>
24#include <string_utils.h>
25#include <macros.h>
26#include <wx/crt.h>
27#include <wx/filename.h>
28
29
30#define ARG_DPI "--dpi"
31#define ARG_WIDTH "--width"
32#define ARG_HEIGHT "--height"
33#define ARG_NO_ANTIALIAS "--no-antialias"
34#define ARG_TRANSPARENT "--transparent"
35#define ARG_STRICT "--strict"
36#define ARG_UNITS "--units"
37#define ARG_ORIGIN_X "--origin-x"
38#define ARG_ORIGIN_Y "--origin-y"
39#define ARG_WINDOW_WIDTH "--window-width"
40#define ARG_WINDOW_HEIGHT "--window-height"
41#define ARG_FOREGROUND "--foreground"
42#define ARG_BACKGROUND "--background"
43
44
46 COMMAND( "png" )
47{
49
50 m_argParser.add_description( UTF8STDSTR( _( "Convert a Gerber or Excellon file to PNG image" ) ) );
51
52 m_argParser.add_argument( ARG_DPI )
53 .default_value( 300 )
54 .scan<'i', int>()
55 .help( UTF8STDSTR( _( "Resolution in DPI (default: 300)" ) ) )
56 .metavar( "DPI" );
57
58 m_argParser.add_argument( ARG_WIDTH )
59 .default_value( 0 )
60 .scan<'i', int>()
61 .help( UTF8STDSTR( _( "Output width in pixels (overrides DPI)" ) ) )
62 .metavar( "PIXELS" );
63
64 m_argParser.add_argument( ARG_HEIGHT )
65 .default_value( 0 )
66 .scan<'i', int>()
67 .help( UTF8STDSTR( _( "Output height in pixels (overrides DPI)" ) ) )
68 .metavar( "PIXELS" );
69
70 m_argParser.add_argument( ARG_NO_ANTIALIAS ).help( UTF8STDSTR( _( "Disable anti-aliasing" ) ) ).flag();
71
72 m_argParser.add_argument( ARG_TRANSPARENT )
73 .help( UTF8STDSTR( _( "Use transparent background (default)" ) ) )
74 .flag();
75
76 m_argParser.add_argument( ARG_STRICT ).help( UTF8STDSTR( _( "Fail on any parse warnings or errors" ) ) ).flag();
77
78 m_argParser.add_argument( ARG_UNITS )
79 .default_value( std::string( "mm" ) )
80 .help( UTF8STDSTR( _( "Units for viewport parameters, options: mm, inch, mils (default: mm)" ) ) )
81 .metavar( "UNITS" );
82
83 m_argParser.add_argument( ARG_ORIGIN_X )
84 .default_value( 0.0 )
85 .scan<'g', double>()
86 .help( UTF8STDSTR( _( "Viewport origin X" ) ) )
87 .metavar( "VALUE" );
88
89 m_argParser.add_argument( ARG_ORIGIN_Y )
90 .default_value( 0.0 )
91 .scan<'g', double>()
92 .help( UTF8STDSTR( _( "Viewport origin Y" ) ) )
93 .metavar( "VALUE" );
94
95 m_argParser.add_argument( ARG_WINDOW_WIDTH )
96 .default_value( 0.0 )
97 .scan<'g', double>()
98 .help( UTF8STDSTR( _( "Viewport width (enables viewport mode)" ) ) )
99 .metavar( "VALUE" );
100
101 m_argParser.add_argument( ARG_WINDOW_HEIGHT )
102 .default_value( 0.0 )
103 .scan<'g', double>()
104 .help( UTF8STDSTR( _( "Viewport height (enables viewport mode)" ) ) )
105 .metavar( "VALUE" );
106
107 m_argParser.add_argument( ARG_FOREGROUND )
108 .default_value( std::string() )
109 .help( UTF8STDSTR( _( "Foreground color as hex (e.g., '#FFFFFF')" ) ) )
110 .metavar( "COLOR" );
111
112 m_argParser.add_argument( ARG_BACKGROUND )
113 .default_value( std::string() )
114 .help( UTF8STDSTR( _( "Background color as hex (e.g., '#000000')" ) ) )
115 .metavar( "COLOR" );
116}
117
118
120{
121 std::unique_ptr<JOB_GERBER_EXPORT_PNG> pngJob = std::make_unique<JOB_GERBER_EXPORT_PNG>();
122
123 pngJob->m_inputFile = m_argInput;
124 pngJob->m_dpi = m_argParser.get<int>( ARG_DPI );
125 pngJob->m_width = m_argParser.get<int>( ARG_WIDTH );
126 pngJob->m_height = m_argParser.get<int>( ARG_HEIGHT );
127 pngJob->m_antialias = !m_argParser.get<bool>( ARG_NO_ANTIALIAS );
128 pngJob->m_transparentBackground = m_argParser.get<bool>( ARG_TRANSPARENT );
129 pngJob->m_strict = m_argParser.get<bool>( ARG_STRICT );
130
131 wxString units = From_UTF8( m_argParser.get<std::string>( ARG_UNITS ).c_str() );
132
133 if( units == wxS( "mm" ) )
134 {
135 pngJob->m_units = JOB_GERBER_EXPORT_PNG::UNITS::MM;
136 }
137 else if( units == wxS( "inch" ) )
138 {
139 pngJob->m_units = JOB_GERBER_EXPORT_PNG::UNITS::INCH;
140 }
141 else if( units == wxS( "mils" ) )
142 {
143 pngJob->m_units = JOB_GERBER_EXPORT_PNG::UNITS::MILS;
144 }
145 else
146 {
147 wxFprintf( stderr, _( "Invalid units: %s\n" ), units );
149 }
150
151 pngJob->m_originX = m_argParser.get<double>( ARG_ORIGIN_X );
152 pngJob->m_originY = m_argParser.get<double>( ARG_ORIGIN_Y );
153 pngJob->m_windowWidth = m_argParser.get<double>( ARG_WINDOW_WIDTH );
154 pngJob->m_windowHeight = m_argParser.get<double>( ARG_WINDOW_HEIGHT );
155
156 if( ( pngJob->m_windowWidth > 0.0 ) != ( pngJob->m_windowHeight > 0.0 ) )
157 {
158 wxFprintf( stderr, _( "Error: both --window-width and --window-height must be specified together\n" ) );
160 }
161
162 pngJob->m_foregroundColor = wxString::FromUTF8( m_argParser.get<std::string>( ARG_FOREGROUND ) );
163 pngJob->m_backgroundColor = wxString::FromUTF8( m_argParser.get<std::string>( ARG_BACKGROUND ) );
164
165 // Handle output path
166 if( m_argOutput.IsEmpty() )
167 {
168 wxFileName inputFn( m_argInput );
169 pngJob->SetConfiguredOutputPath( inputFn.GetPath() + wxFileName::GetPathSeparator() + inputFn.GetName()
170 + wxS( ".png" ) );
171 }
172 else
173 {
174 pngJob->SetConfiguredOutputPath( m_argOutput );
175 }
176
177 int exitCode = aKiway.ProcessJob( KIWAY::FACE_GERBVIEW, pngJob.get() );
178
179 return exitCode;
180}
argparse::ArgumentParser m_argParser
Definition command.h:113
COMMAND(const std::string &aName)
Define a new COMMAND instance.
Definition command.cpp:30
void addCommonArgs(bool aInput, bool aOutput, IO_TYPE aInputType, IO_TYPE aOutputType)
Set up the most common of args used across cli.
Definition command.cpp:132
wxString m_argOutput
Value of the output arg if configured.
Definition command.h:148
wxString m_argInput
Value of the common input arg if configured.
Definition command.h:143
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:315
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Definition kiway.cpp:750
@ FACE_GERBVIEW
Definition kiway.h:325
#define UTF8STDSTR(s)
Definition command.h:27
#define ARG_FOREGROUND
#define ARG_BACKGROUND
#define ARG_ORIGIN_Y
#define ARG_HEIGHT
#define ARG_TRANSPARENT
#define ARG_ORIGIN_X
#define ARG_WINDOW_HEIGHT
#define ARG_NO_ANTIALIAS
#define ARG_WINDOW_WIDTH
#define ARG_UNITS
#define ARG_WIDTH
#define ARG_STRICT
#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)