KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_gerber_diff.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
20#include "command_gerber_diff.h"
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_FILE_A "file1"
31#define ARG_FILE_B "file2"
32#define ARG_FORMAT "--format"
33#define ARG_DPI "--dpi"
34#define ARG_NO_ANTIALIAS "--no-antialias"
35#define ARG_TRANSPARENT "--transparent"
36#define ARG_EXIT_CODE_ONLY "--exit-code-only"
37#define ARG_TOLERANCE "--tolerance"
38#define ARG_STRICT "--strict"
39#define ARG_NO_ALIGN "--no-align"
40
41
43 COMMAND( "diff" )
44{
46
47 m_argParser.add_description( UTF8STDSTR( _( "Compare two Gerber or Excellon files and show differences" ) ) );
48
49 m_argParser.add_argument( ARG_FILE_A )
50 .help( UTF8STDSTR( _( "Reference file (first Gerber/Excellon file)" ) ) )
51 .metavar( "FILE1" );
52
53 m_argParser.add_argument( ARG_FILE_B )
54 .help( UTF8STDSTR( _( "Comparison file (second Gerber/Excellon file)" ) ) )
55 .metavar( "FILE2" );
56
57 m_argParser.add_argument( ARG_FORMAT )
58 .default_value( std::string( "png" ) )
59 .help( UTF8STDSTR( _( "Output format: png, text, or json (default: png)" ) ) )
60 .metavar( "FORMAT" );
61
62 m_argParser.add_argument( ARG_DPI )
63 .default_value( 300 )
64 .scan<'i', int>()
65 .help( UTF8STDSTR( _( "Resolution in DPI for PNG output (default: 300)" ) ) )
66 .metavar( "DPI" );
67
68 m_argParser.add_argument( ARG_NO_ANTIALIAS )
69 .help( UTF8STDSTR( _( "Disable anti-aliasing for PNG output" ) ) )
70 .flag();
71
72 m_argParser.add_argument( ARG_TRANSPARENT )
73 .help( UTF8STDSTR( _( "Use transparent background for PNG output" ) ) )
74 .flag();
75
76 m_argParser.add_argument( ARG_EXIT_CODE_ONLY )
77 .help( UTF8STDSTR( _( "Only set exit code (0=identical, 1=different), no output" ) ) )
78 .flag();
79
80 m_argParser.add_argument( ARG_TOLERANCE )
81 .default_value( 0 )
82 .scan<'i', int>()
83 .help( UTF8STDSTR( _( "Tolerance in nm for floating point comparisons (default: 0)" ) ) )
84 .metavar( "TOLERANCE" );
85
86 m_argParser.add_argument( ARG_STRICT ).help( UTF8STDSTR( _( "Fail on any parse warnings or errors" ) ) ).flag();
87
88 m_argParser.add_argument( ARG_NO_ALIGN )
89 .help( UTF8STDSTR( _( "Skip bounding-box origin alignment; catches absolute-placement regressions" ) ) )
90 .flag();
91}
92
93
95{
96 std::unique_ptr<JOB_GERBER_DIFF> diffJob = std::make_unique<JOB_GERBER_DIFF>();
97
98 diffJob->m_inputFileA = From_UTF8( m_argParser.get<std::string>( ARG_FILE_A ).c_str() );
99 diffJob->m_inputFileB = From_UTF8( m_argParser.get<std::string>( ARG_FILE_B ).c_str() );
100 diffJob->m_dpi = m_argParser.get<int>( ARG_DPI );
101 diffJob->m_antialias = !m_argParser.get<bool>( ARG_NO_ANTIALIAS );
102 diffJob->m_transparentBackground = m_argParser.get<bool>( ARG_TRANSPARENT );
103 diffJob->m_exitCodeOnly = m_argParser.get<bool>( ARG_EXIT_CODE_ONLY );
104 diffJob->m_tolerance = m_argParser.get<int>( ARG_TOLERANCE );
105 diffJob->m_strict = m_argParser.get<bool>( ARG_STRICT );
106 diffJob->m_noAlign = m_argParser.get<bool>( ARG_NO_ALIGN );
107
108 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
109
110 if( format == wxS( "png" ) )
111 {
112 diffJob->m_outputFormat = JOB_GERBER_DIFF::OUTPUT_FORMAT::PNG;
113 }
114 else if( format == wxS( "text" ) )
115 {
116 diffJob->m_outputFormat = JOB_GERBER_DIFF::OUTPUT_FORMAT::TEXT;
117 }
118 else if( format == wxS( "json" ) )
119 {
120 diffJob->m_outputFormat = JOB_GERBER_DIFF::OUTPUT_FORMAT::JSON;
121 }
122 else
123 {
124 wxFprintf( stderr, _( "Invalid output format: %s\n" ), format );
126 }
127
128 // For PNG, generate a default output path when -o is not given.
129 // For text/JSON, -o is optional — omitting it sends output to stdout.
130 if( diffJob->m_outputFormat == JOB_GERBER_DIFF::OUTPUT_FORMAT::PNG )
131 {
132 if( m_argOutput.IsEmpty() )
133 {
134 wxFileName inputFn( diffJob->m_inputFileA );
135 diffJob->SetConfiguredOutputPath( inputFn.GetPath() + wxFileName::GetPathSeparator() + inputFn.GetName()
136 + wxS( "-diff.png" ) );
137 }
138 else
139 {
140 diffJob->SetConfiguredOutputPath( m_argOutput );
141 }
142 }
143 else if( !m_argOutput.IsEmpty() )
144 {
145 diffJob->SetConfiguredOutputPath( m_argOutput );
146 }
147
148 int exitCode = aKiway.ProcessJob( KIWAY::FACE_GERBVIEW, diffJob.get() );
149
150 return exitCode;
151}
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
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_TRANSPARENT
#define ARG_NO_ANTIALIAS
#define ARG_STRICT
#define ARG_TOLERANCE
#define ARG_NO_ALIGN
#define ARG_FILE_B
#define ARG_FILE_A
#define ARG_EXIT_CODE_ONLY
#define ARG_FORMAT
#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)