KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_sch_export_plot.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 The 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
21#include <kiface_base.h>
22#include <sch_plotter.h>
24#include <cli/exit_codes.h>
27#include <layer_ids.h>
29#include <wx/crt.h>
30#include <string_utils.h>
31
32#include <macros.h>
33#include <wx/tokenzr.h>
34
35#define ARG_EXCLUDE_DRAWING_SHEET "--exclude-drawing-sheet"
36#define ARG_NO_BACKGROUND_COLOR "--no-background-color"
37#define ARG_PAGES "--pages"
38#define ARG_EXCLUDE_PDF_PROPERTY_POPUPS "--exclude-pdf-property-popups"
39#define ARG_EXCLUDE_PDF_HIERARCHICAL_LINKS "--exclude-pdf-hierarchical-links"
40#define ARG_EXCLUDE_PDF_METADATA "--exclude-pdf-metadata"
41#define ARG_FONT_NAME "--default-font"
42#define ARG_DRAW_HOP_OVER "--draw-hop-over"
43#define ARG_DPI "--dpi"
44#define ARG_NO_ANTIALIAS "--no-antialias"
45
46#define DEPRECATED_ARG_HPGL_PEN_SIZE "--pen-size"
47#define DEPRECATED_ARG_HPGL_ORIGIN "--origin"
48
50 const std::string& aDescription,
51 SCH_PLOT_FORMAT aPlotFormat,
52 IO_TYPE aOutputType ) :
53 COMMAND( aName ),
54 m_plotFormat( aPlotFormat )
55{
56 m_argParser.add_description( aDescription );
57
58 addCommonArgs( true, true, IO_TYPE::FILE, aOutputType );
62
63 m_argParser.add_argument( "-t", ARG_THEME )
64 .default_value( std::string() )
65 .help( UTF8STDSTR( _( "Color theme to use (will default to schematic settings)" ) ) )
66 .metavar( "THEME_NAME" );
67 m_argParser.add_argument( "-b", ARG_BLACKANDWHITE )
69 .flag();
70
71 m_argParser.add_argument( "-e", ARG_EXCLUDE_DRAWING_SHEET )
72 .help( UTF8STDSTR( _( "No drawing sheet" ) ) )
73 .flag();
74
75 m_argParser.add_argument( ARG_FONT_NAME )
76 .help( UTF8STDSTR( _( "Default font name" ) ) )
77 .default_value( wxString( "" ).ToStdString() );
78
79 m_argParser.add_argument( ARG_DRAW_HOP_OVER )
80 .help( UTF8STDSTR( _( "Draw hop over at wire crossings" ) ) )
81 .flag();
82
83 if( aPlotFormat == SCH_PLOT_FORMAT::PDF )
84 {
86 .help( UTF8STDSTR( _( "Do not generate property popups in PDF" ) ) )
87 .flag();
88
90 .help( UTF8STDSTR( _( "Do not generate clickable links for hierarchical elements in PDF" ) ) )
91 .flag();
92
94 .help( UTF8STDSTR( _( "Do not generate PDF metadata from AUTHOR and SUBJECT variables" ) ) )
95 .flag();
96 }
97
98 if( aPlotFormat == SCH_PLOT_FORMAT::PDF
99 || aPlotFormat == SCH_PLOT_FORMAT::POST
100 || aPlotFormat == SCH_PLOT_FORMAT::SVG
101 || aPlotFormat == SCH_PLOT_FORMAT::PNG )
102 {
103 m_argParser.add_argument( "-n", ARG_NO_BACKGROUND_COLOR )
104 .help( UTF8STDSTR( _( "Avoid setting a background color (regardless of theme)" ) ) )
105 .flag();
106 }
107
108 if( aPlotFormat == SCH_PLOT_FORMAT::PNG )
109 {
110 m_argParser.add_argument( ARG_DPI )
111 .help( UTF8STDSTR( wxString::Format( _( "Resolution in dots per inch (default %d)" ),
112 DEFAULT_PNG_DPI ) ) )
113 .scan<'i', int>()
114 .default_value( DEFAULT_PNG_DPI )
115 .metavar( "DPI" );
116
117 m_argParser.add_argument( ARG_NO_ANTIALIAS )
118 .help( UTF8STDSTR( _( "Disable anti-aliasing" ) ) )
119 .flag();
120 }
121
122 m_argParser.add_argument( ARG_PAGES )
123 .default_value( std::string() )
124 .help( UTF8STDSTR( _( "List of page numbers separated by comma to print, blank or "
125 "unspecified is equivalent to all pages" ) ) )
126 .metavar( "PAGE_LIST" );
127
128 if( aPlotFormat == SCH_PLOT_FORMAT::HPGL )
129 {
130 m_argParser.add_argument( "-p", DEPRECATED_ARG_HPGL_PEN_SIZE )
131 .help( UTF8STDSTR( _( "Deprecated. Has no effect." ) ) )
132 .scan<'g', double>()
133 .default_value( 0.5 )
134 .metavar( "PEN_SIZE" );
135
136 m_argParser.add_argument( "-r", DEPRECATED_ARG_HPGL_ORIGIN )
137 .help( UTF8STDSTR( _( "Deprecated. Has no effect." ) ) )
138 .scan<'d', int>()
139 .default_value( 1 )
140 .metavar( "ORIGIN" );
141 }
142}
143
144
146{
148 {
149 wxFprintf( stderr, _( "Plotting to HPGL is no longer supported as of KiCad 10.0.\n" ) );
151 }
152
153 wxString filename = m_argInput;
154
155 if( !wxFile::Exists( filename ) )
156 {
157 wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
159 }
160
161 std::vector<wxString> pages;
162 wxString pagesStr = From_UTF8( m_argParser.get<std::string>( ARG_PAGES ).c_str() );
163 wxStringTokenizer tokenizer( pagesStr, ",", wxTOKEN_STRTOK );
164
165 while( tokenizer.HasMoreTokens() )
166 pages.push_back( tokenizer.GetNextToken().Trim( true ).Trim( false ) );
167
168 std::unique_ptr<JOB_EXPORT_SCH_PLOT> plotJob;
169
170 switch( m_plotFormat )
171 {
172 case SCH_PLOT_FORMAT::PDF: plotJob = std::make_unique<JOB_EXPORT_SCH_PLOT_PDF>( false ); break;
173 case SCH_PLOT_FORMAT::DXF: plotJob = std::make_unique<JOB_EXPORT_SCH_PLOT_DXF>(); break;
174 case SCH_PLOT_FORMAT::SVG: plotJob = std::make_unique<JOB_EXPORT_SCH_PLOT_SVG>(); break;
175 case SCH_PLOT_FORMAT::POST: plotJob = std::make_unique<JOB_EXPORT_SCH_PLOT_PS>(); break;
176 case SCH_PLOT_FORMAT::PNG: plotJob = std::make_unique<JOB_EXPORT_SCH_PLOT_PNG>(); break;
177 case SCH_PLOT_FORMAT::HPGL: /* no longer supported */ break;
178 }
179
180 plotJob->m_filename = filename;
181 plotJob->m_plotFormat = m_plotFormat;
182 plotJob->m_plotPages = pages;
183 plotJob->m_plotDrawingSheet = !m_argParser.get<bool>( ARG_EXCLUDE_DRAWING_SHEET );
184 plotJob->m_pageSizeSelect = JOB_PAGE_SIZE::PAGE_SIZE_AUTO;
185 plotJob->m_defaultFont = From_UTF8( m_argParser.get<std::string>( ARG_FONT_NAME ).c_str() );
186 plotJob->m_show_hop_over = m_argParser.get<bool>( ARG_DRAW_HOP_OVER );
187
192 {
193 plotJob->m_useBackgroundColor = !m_argParser.get<bool>( ARG_NO_BACKGROUND_COLOR );
194 }
195
196 plotJob->m_blackAndWhite = m_argParser.get<bool>( ARG_BLACKANDWHITE );
197 plotJob->m_theme = From_UTF8( m_argParser.get<std::string>( ARG_THEME ).c_str() );
198
199 plotJob->SetConfiguredOutputPath( m_argOutput );
200
201 plotJob->m_plotAll = plotJob->m_plotPages.size() == 0;
202
203 plotJob->m_drawingSheet = m_argDrawingSheet;
204 plotJob->SetVarOverrides( m_argDefineVars );
205 plotJob->m_variantNames = m_argVariantNames;
206
207 // PDF local options
209 {
210 plotJob->m_PDFPropertyPopups = !m_argParser.get<bool>( ARG_EXCLUDE_PDF_PROPERTY_POPUPS );
211 plotJob->m_PDFHierarchicalLinks = !m_argParser.get<bool>( ARG_EXCLUDE_PDF_HIERARCHICAL_LINKS );
212 plotJob->m_PDFMetadata = !m_argParser.get<bool>( ARG_EXCLUDE_PDF_METADATA );
213 }
214
216 {
217 JOB_EXPORT_SCH_PLOT_PNG* pngJob = static_cast<JOB_EXPORT_SCH_PLOT_PNG*>( plotJob.get() );
218 int dpi = m_argParser.get<int>( ARG_DPI );
219
220 if( dpi < MIN_PNG_DPI || dpi > MAX_PNG_DPI )
221 {
222 wxFprintf( stderr, _( "DPI must be between %d and %d\n" ), MIN_PNG_DPI, MAX_PNG_DPI );
224 }
225
226 pngJob->m_dpi = dpi;
227 pngJob->m_antialias = !m_argParser.get<bool>( ARG_NO_ANTIALIAS );
228 }
229
230 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, plotJob.get() );
231
232 return exitCode;
233}
std::map< wxString, wxString > m_argDefineVars
Value of the drawing sheet arg if configured.
Definition command.h:158
void addVariantsArg()
Set up the list of variants to output arguement.
Definition command.cpp:227
argparse::ArgumentParser m_argParser
Definition command.h:113
COMMAND(const std::string &aName)
Define a new COMMAND instance.
Definition command.cpp:30
void addDefineArg()
Set up the drawing sheet arg used by many of the export commands.
Definition command.cpp:212
wxString m_argDrawingSheet
Value of the drawing sheet arg if configured.
Definition command.h:153
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
std::vector< wxString > m_argVariantNames
The list of variant names to output.
Definition command.h:170
void addDrawingSheetArg()
Set up the drawing sheet arg used by many of the export commands.
Definition command.cpp:200
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
SCH_EXPORT_PLOT_COMMAND(const std::string &aName, const std::string &aDescription, SCH_PLOT_FORMAT aPlotFormat, IO_TYPE aOutputType)
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_SCH
eeschema DSO
Definition kiway.h:322
#define UTF8STDSTR(s)
Definition command.h:27
#define ARG_NO_ANTIALIAS
#define ARG_THEME
#define ARG_BLACKANDWHITE_DESC
#define ARG_BLACKANDWHITE
#define ARG_EXCLUDE_DRAWING_SHEET
#define ARG_EXCLUDE_PDF_PROPERTY_POPUPS
#define ARG_NO_BACKGROUND_COLOR
#define DEPRECATED_ARG_HPGL_ORIGIN
#define ARG_EXCLUDE_PDF_HIERARCHICAL_LINKS
#define ARG_FONT_NAME
#define ARG_DRAW_HOP_OVER
#define DEPRECATED_ARG_HPGL_PEN_SIZE
#define ARG_PAGES
#define ARG_EXCLUDE_PDF_METADATA
#define _(s)
SCH_PLOT_FORMAT
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
Definition exit_codes.h:31
static const int ERR_INVALID_INPUT_FILE
Definition exit_codes.h:33
constexpr int DEFAULT_PNG_DPI
Definition plotter_png.h:28
constexpr int MIN_PNG_DPI
Definition plotter_png.h:29
constexpr int MAX_PNG_DPI
Definition plotter_png.h:30
wxString From_UTF8(const char *cstring)