KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_sch_export_bom.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) 2023 Mike Williams <[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
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#include <wx/tokenzr.h>
29#include <schematic_settings.h>
30
31#include <macros.h>
32
33
35{
36 m_argParser.add_description( UTF8STDSTR( _( "Generate a Bill of Materials (BOM)" ) ) );
37 addCommonArgs( true, true, false, false );
38
39 // Preset options
40 m_argParser.add_argument( ARG_PRESET )
41 .help( UTF8STDSTR( _( ARG_PRESET_DESC ) ) )
42 .default_value( std::string( "" ) )
43 .metavar( "PRESET" );
44
45 m_argParser.add_argument( ARG_FMT_PRESET )
46 .help( UTF8STDSTR( _( ARG_FMT_PRESET_DESC ) ) )
47 .default_value( std::string( "" ) )
48 .metavar( "FMT_PRESET" );
49
50 // Field output options
51 m_argParser.add_argument( ARG_FIELDS )
52 .help( UTF8STDSTR( _( ARG_FIELDS_DESC ) ) )
53 .default_value( std::string( "Reference,Value,Footprint,${QUANTITY},${DNP}" ) )
54 .metavar( "FIELDS" );
55
56 m_argParser.add_argument( ARG_LABELS )
57 .help( UTF8STDSTR( _( ARG_LABELS_DESC ) ) )
58 .default_value( std::string( "Refs,Value,Footprint,Qty,DNP" ) )
59 .metavar( "LABELS" );
60
61 m_argParser.add_argument( ARG_GROUP_BY )
62 .help( UTF8STDSTR( _( ARG_GROUP_BY_DESC ) ) )
63 .default_value( std::string( "" ) )
64 .metavar( "GROUP_BY" );
65
66 m_argParser.add_argument( ARG_SORT_FIELD )
67 .help( UTF8STDSTR( _( ARG_SORT_FIELD_DESC ) ) )
68 .default_value( std::string( "Reference" ) )
69 .metavar( "SORT_BY" );
70
71 m_argParser.add_argument( ARG_SORT_ASC )
72 .help( UTF8STDSTR( _( ARG_SORT_ASC_DESC ) ) )
73 .implicit_value( true )
74 .default_value( true );
75
76 m_argParser.add_argument( ARG_FILTER )
77 .help( UTF8STDSTR( _( ARG_FILTER_DESC ) ) )
78 .default_value( std::string( "" ) )
79 .metavar( "FILTER" );
80
81 m_argParser.add_argument( ARG_EXCLUDE_DNP )
82 .help( UTF8STDSTR( _( ARG_EXCLUDE_DNP_DESC ) ) )
83 .flag();
84
87 .flag();
88
89 // Output formatting options
90 m_argParser.add_argument( ARG_FIELD_DELIMITER )
92 .default_value( std::string( "," ) )
93 .metavar( "FIELD_DELIM" );
94
95 m_argParser.add_argument( ARG_STRING_DELIMITER )
97 .default_value( std::string( "\"" ) )
98 .metavar( "STR_DELIM" );
99
100 m_argParser.add_argument( ARG_REF_DELIMITER )
101 .help( UTF8STDSTR( _( ARG_REF_DELIMITER_DESC ) ) )
102 .default_value( std::string( "," ) )
103 .metavar( "REF_DELIM" );
104
107 .default_value( std::string( "-" ) )
108 .metavar( "REF_RANGE_DELIM" );
109
110 m_argParser.add_argument( ARG_KEEP_TABS )
111 .help( UTF8STDSTR( _( ARG_KEEP_TABS_DESC ) ) )
112 .flag();
113
114 m_argParser.add_argument( ARG_KEEP_LINE_BREAKS )
116 .flag();
117}
118
119
120std::vector<wxString> CLI::SCH_EXPORT_BOM_COMMAND::convertStringList( const wxString& aList )
121{
122 std::vector<wxString> v;
123
124 if( !aList.IsEmpty() )
125 {
126 wxStringTokenizer layerTokens( aList, "," );
127
128 while( layerTokens.HasMoreTokens() )
129 v.emplace_back( layerTokens.GetNextToken() );
130 }
131
132 return v;
133}
134
136{
137 std::unique_ptr<JOB_EXPORT_SCH_BOM> bomJob = std::make_unique<JOB_EXPORT_SCH_BOM>();
138
139 // Basic options
140 bomJob->m_filename = m_argInput;
141 bomJob->SetConfiguredOutputPath( m_argOutput );
142
143 bomJob->m_bomPresetName = From_UTF8( m_argParser.get<std::string>( ARG_PRESET ).c_str() );
144 bomJob->m_bomFmtPresetName =
145 From_UTF8( m_argParser.get<std::string>( ARG_FMT_PRESET ).c_str() );
146
147 // Format options
148 bomJob->m_fieldDelimiter =
149 From_UTF8( m_argParser.get<std::string>( ARG_FIELD_DELIMITER ).c_str() );
150 bomJob->m_stringDelimiter =
151 From_UTF8( m_argParser.get<std::string>( ARG_STRING_DELIMITER ).c_str() );
152 bomJob->m_refDelimiter = From_UTF8( m_argParser.get<std::string>( ARG_REF_DELIMITER ).c_str() );
153 bomJob->m_refRangeDelimiter =
154 From_UTF8( m_argParser.get<std::string>( ARG_REF_RANGE_DELIMITER ).c_str() );
155 bomJob->m_keepTabs = m_argParser.get<bool>( ARG_KEEP_TABS );
156 bomJob->m_keepLineBreaks = m_argParser.get<bool>( ARG_KEEP_LINE_BREAKS );
157
158 // Output fields options
159 bomJob->m_fieldsOrdered =
160 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_FIELDS ).c_str() ) );
161 bomJob->m_fieldsLabels =
162 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_LABELS ).c_str() ) );
163
164 // We only apply the default labels if the default fields are used
165 if( m_argParser.is_used( ARG_FIELDS ) && !m_argParser.is_used( ARG_LABELS ) )
166 {
167 bomJob->m_fieldsLabels.clear();
168 }
169
170 bomJob->m_fieldsGroupBy =
171 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_GROUP_BY ).c_str() ) );
172 bomJob->m_sortField = From_UTF8( m_argParser.get<std::string>( ARG_SORT_FIELD ).c_str() );
173 bomJob->m_sortAsc = m_argParser.get<bool>( ARG_SORT_ASC );
174 bomJob->m_filterString = From_UTF8( m_argParser.get<std::string>( ARG_FILTER ).c_str() );
175 bomJob->m_excludeDNP = m_argParser.get<bool>( ARG_EXCLUDE_DNP );
176 bomJob->m_includeExcludedFromBOM = m_argParser.get<bool>( ARG_INCLUDE_EXCLUDED_FROM_BOM );
177
178 if( !wxFile::Exists( bomJob->m_filename ) )
179 {
180 wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
182 }
183
184 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, bomJob.get() );
185
186 return exitCode;
187}
argparse::ArgumentParser m_argParser
Definition: command.h:100
void addCommonArgs(bool aInput, bool aOutput, bool aInputIsDir, bool aOutputIsDir)
Set up the most common of args used across cli.
Definition: command.cpp:115
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
std::vector< wxString > convertStringList(const wxString &aList)
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition: kiway.h:285
@ FACE_SCH
eeschema DSO
Definition: kiway.h:292
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr)
Definition: kiway.cpp:711
#define UTF8STDSTR(s)
Definition: command.h:27
#define ARG_EXCLUDE_DNP
#define ARG_PRESET
#define ARG_STRING_DELIMITER
#define ARG_GROUP_BY_DESC
#define ARG_FIELDS
#define ARG_INCLUDE_EXCLUDED_FROM_BOM
#define ARG_FILTER_DESC
#define ARG_KEEP_LINE_BREAKS_DESC
#define ARG_KEEP_TABS
#define ARG_SORT_ASC
#define ARG_LABELS
#define ARG_EXCLUDE_DNP_DESC
#define ARG_PRESET_DESC
#define ARG_SORT_FIELD_DESC
#define ARG_GROUP_BY
#define ARG_REF_DELIMITER_DESC
#define ARG_FILTER
#define ARG_KEEP_TABS_DESC
#define ARG_STRING_DELIMITER_DESC
#define ARG_LABELS_DESC
#define ARG_KEEP_LINE_BREAKS
#define ARG_FMT_PRESET_DESC
#define ARG_INCLUDE_EXCLUDED_FROM_BOM_DESC
#define ARG_FIELDS_DESC
#define ARG_FIELD_DELIMITER_DESC
#define ARG_FIELD_DELIMITER
#define ARG_REF_DELIMITER
#define ARG_REF_RANGE_DELIMITER
#define ARG_FMT_PRESET
#define ARG_SORT_ASC_DESC
#define ARG_REF_RANGE_DELIMITER_DESC
#define ARG_SORT_FIELD
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static const int ERR_INVALID_INPUT_FILE
Definition: exit_codes.h:33
wxString From_UTF8(const char *cstring)