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 (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
21#include <kiface_base.h>
22#include <sch_plotter.h>
24#include <cli/exit_codes.h>
26#include <layer_ids.h>
27#include <wx/crt.h>
28#include <string_utils.h>
29
30#include <macros.h>
31#include <wx/tokenzr.h>
32
33#define ARG_EXCLUDE_DRAWING_SHEET "--exclude-drawing-sheet"
34#define ARG_NO_BACKGROUND_COLOR "--no-background-color"
35#define ARG_HPGL_PEN_SIZE "--pen-size"
36#define ARG_HPGL_ORIGIN "--origin"
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
47};
48
50 const std::string& aDescription,
51 SCH_PLOT_FORMAT aPlotFormat,
52 bool aOutputIsDir ) :
53 COMMAND( aName ),
54 m_plotFormat( aPlotFormat )
55{
56 m_argParser.add_description( aDescription );
57
58 addCommonArgs( true, true, false, aOutputIsDir );
61
62 if( aPlotFormat != SCH_PLOT_FORMAT::HPGL )
63 {
64 m_argParser.add_argument( "-t", ARG_THEME )
65 .default_value( std::string() )
66 .help( UTF8STDSTR( _( "Color theme to use (will default to schematic "
67 "settings)" ) ) )
68 .metavar( "THEME_NAME" );
69 m_argParser.add_argument( "-b", ARG_BLACKANDWHITE )
71 .flag();
72 }
73
74 m_argParser.add_argument( "-e", ARG_EXCLUDE_DRAWING_SHEET )
75 .help( UTF8STDSTR( _( "No drawing sheet" ) ) )
76 .implicit_value( true )
77 .default_value( false );
78
79 if( aPlotFormat == SCH_PLOT_FORMAT::PDF )
80 {
82 .help( UTF8STDSTR( _( "Do not generate property popups in PDF" ) ) )
83 .flag();
84
86 .help( UTF8STDSTR( _( "Do not generate clickable links for hierarchical elements "
87 "in PDF" ) ) )
88 .flag();
89
91 .help( UTF8STDSTR( _( "Do not generate PDF metadata from AUTHOR and SUBJECT variables" ) ) )
92 .flag();
93 }
94
95 if( aPlotFormat == SCH_PLOT_FORMAT::PDF
96 || aPlotFormat == SCH_PLOT_FORMAT::POST
97 || aPlotFormat == SCH_PLOT_FORMAT::SVG )
98 {
99 m_argParser.add_argument( "-n", ARG_NO_BACKGROUND_COLOR )
100 .help( UTF8STDSTR( _( "Avoid setting a background color (regardless of theme)" ) ) )
101 .flag();
102 }
103
104 m_argParser.add_argument( "-p", ARG_PAGES )
105 .default_value( std::string() )
106 .help( UTF8STDSTR( _( "List of page numbers separated by comma to print, blank or "
107 "unspecified is equivalent to all pages" ) ) )
108 .metavar( "PAGE_LIST" );
109
110 if( aPlotFormat == SCH_PLOT_FORMAT::HPGL )
111 {
112 m_argParser.add_argument( "-p", ARG_HPGL_PEN_SIZE )
113 .help( UTF8STDSTR( _( "Pen size [mm]" ) ) )
114 .scan<'g', double>()
115 .default_value( 0.5 )
116 .metavar( "PEN_SIZE" );
117
118 m_argParser.add_argument( "-r", ARG_HPGL_ORIGIN )
119 .help( UTF8STDSTR( _( "Origin and scale: 0 bottom left, 1 centered, 2 page fit, 3 "
120 "content fit" ) ) )
121 .scan<'d', int>()
122 .default_value( 1 )
123 .metavar( "ORIGIN" );
124 }
125}
126
127
129{
130 wxString filename = m_argInput;
131 if( !wxFile::Exists( filename ) )
132 {
133 wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
135 }
136
137 std::vector<wxString> pages;
138 wxString pagesStr = From_UTF8( m_argParser.get<std::string>( ARG_PAGES ).c_str() );
139 wxStringTokenizer tokenizer( pagesStr, "," );
140 while( tokenizer.HasMoreTokens() )
141 {
142 pages.push_back( tokenizer.GetNextToken().Trim() );
143 }
144
145 std::unique_ptr<JOB_EXPORT_SCH_PLOT> plotJob =
146 std::make_unique<JOB_EXPORT_SCH_PLOT>();
147 plotJob->m_filename = filename;
148 plotJob->m_plotFormat = m_plotFormat;
149 plotJob->m_plotPages = pages;
150 plotJob->m_plotDrawingSheet = !m_argParser.get<bool>( ARG_EXCLUDE_DRAWING_SHEET );
151 plotJob->m_pageSizeSelect = JOB_PAGE_SIZE::PAGE_SIZE_AUTO;
152
153 if( m_plotFormat == SCH_PLOT_FORMAT::PDF
154 || m_plotFormat == SCH_PLOT_FORMAT::POST
155 || m_plotFormat == SCH_PLOT_FORMAT::SVG )
156 {
157 plotJob->m_useBackgroundColor = !m_argParser.get<bool>( ARG_NO_BACKGROUND_COLOR );
158 }
159
160 if ( m_plotFormat != SCH_PLOT_FORMAT::HPGL )
161 {
162 plotJob->m_blackAndWhite = m_argParser.get<bool>( ARG_BLACKANDWHITE );
163 plotJob->m_theme = From_UTF8( m_argParser.get<std::string>( ARG_THEME ).c_str() );
164 }
165
166 if( m_outputArgExpectsDir )
167 plotJob->m_outputDirectory = m_argOutput;
168 else
169 plotJob->m_outputFile = m_argOutput;
170
171 plotJob->m_plotAll = plotJob->m_plotPages.size() == 0;
172
173 plotJob->m_drawingSheet = m_argDrawingSheet;
174 plotJob->SetVarOverrides( m_argDefineVars );
175
176 // HPGL local options
177 if( m_plotFormat == SCH_PLOT_FORMAT::HPGL )
178 {
179 plotJob->m_HPGLPenSize =
180 m_argParser.get<double>( ARG_HPGL_PEN_SIZE ) * schIUScale.IU_PER_MM;
181 int origin = m_argParser.get<int>( ARG_HPGL_ORIGIN );
182 if( origin < 0 || origin > 3 )
183 {
184 wxFprintf( stderr, _( "HPGL origin option must be 0, 1, 2 or 3\n" ) );
186 }
187 plotJob->m_HPGLPlotOrigin = hpgl_origin_ops[origin];
188 }
189 // PDF local options
190 else if( m_plotFormat == SCH_PLOT_FORMAT::PDF )
191 {
192 plotJob->m_PDFPropertyPopups = !m_argParser.get<bool>( ARG_EXCLUDE_PDF_PROPERTY_POPUPS );
193 plotJob->m_PDFHierarchicalLinks =
194 !m_argParser.get<bool>( ARG_EXCLUDE_PDF_HIERARCHICAL_LINKS );
195 plotJob->m_PDFMetadata = !m_argParser.get<bool>( ARG_EXCLUDE_PDF_METADATA );
196 }
197
198 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, plotJob.get() );
199
200 return exitCode;
201}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:110
argparse::ArgumentParser m_argParser
Definition: command.h:100
void addDefineArg()
Set up the drawing sheet arg used by many of the export commands.
Definition: command.cpp:169
void addCommonArgs(bool aInput, bool aOutput, bool aInputIsDir, bool aOutputIsDir)
Set up the most common of args used across cli.
Definition: command.cpp:115
void addDrawingSheetArg()
Set up the drawing sheet arg used by many of the export commands.
Definition: command.cpp:157
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, bool aOutputIsDir=true)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:284
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob)
Definition: kiway.cpp:709
@ FACE_SCH
eeschema DSO
Definition: kiway.h:291
#define UTF8STDSTR(s)
Definition: command.h:27
#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 ARG_EXCLUDE_PDF_HIERARCHICAL_LINKS
#define ARG_HPGL_PEN_SIZE
const JOB_HPGL_PLOT_ORIGIN_AND_UNITS hpgl_origin_ops[4]
#define ARG_PAGES
#define ARG_HPGL_ORIGIN
#define ARG_EXCLUDE_PDF_METADATA
#define _(s)
SCH_PLOT_FORMAT
JOB_HPGL_PLOT_ORIGIN_AND_UNITS
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
wxString From_UTF8(const char *cstring)
const double IU_PER_MM
Definition: base_units.h:76