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 (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
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
85 // Output formatting options
86 m_argParser.add_argument( ARG_FIELD_DELIMITER )
88 .default_value( std::string( "," ) )
89 .metavar( "FIELD_DELIM" );
90
91 m_argParser.add_argument( ARG_STRING_DELIMITER )
93 .default_value( std::string( "\"" ) )
94 .metavar( "STR_DELIM" );
95
96 m_argParser.add_argument( ARG_REF_DELIMITER )
98 .default_value( std::string( "," ) )
99 .metavar( "REF_DELIM" );
100
103 .default_value( std::string( "-" ) )
104 .metavar( "REF_RANGE_DELIM" );
105
106 m_argParser.add_argument( ARG_KEEP_TABS )
107 .help( UTF8STDSTR( _( ARG_KEEP_TABS_DESC ) ) )
108 .flag();
109
110 m_argParser.add_argument( ARG_KEEP_LINE_BREAKS )
112 .flag();
113}
114
115
116std::vector<wxString> CLI::SCH_EXPORT_BOM_COMMAND::convertStringList( const wxString& aList )
117{
118 std::vector<wxString> v;
119
120 if( !aList.IsEmpty() )
121 {
122 wxStringTokenizer layerTokens( aList, "," );
123
124 while( layerTokens.HasMoreTokens() )
125 v.emplace_back( layerTokens.GetNextToken() );
126 }
127
128 return v;
129}
130
132{
133 std::unique_ptr<JOB_EXPORT_SCH_BOM> bomJob = std::make_unique<JOB_EXPORT_SCH_BOM>();
134
135 // Basic options
136 bomJob->m_filename = m_argInput;
137 bomJob->SetOutputPath( m_argOutput );
138
139 bomJob->m_bomPresetName = From_UTF8( m_argParser.get<std::string>( ARG_PRESET ).c_str() );
140 bomJob->m_bomFmtPresetName =
141 From_UTF8( m_argParser.get<std::string>( ARG_FMT_PRESET ).c_str() );
142
143 // Format options
144 bomJob->m_fieldDelimiter =
145 From_UTF8( m_argParser.get<std::string>( ARG_FIELD_DELIMITER ).c_str() );
146 bomJob->m_stringDelimiter =
147 From_UTF8( m_argParser.get<std::string>( ARG_STRING_DELIMITER ).c_str() );
148 bomJob->m_refDelimiter = From_UTF8( m_argParser.get<std::string>( ARG_REF_DELIMITER ).c_str() );
149 bomJob->m_refRangeDelimiter =
150 From_UTF8( m_argParser.get<std::string>( ARG_REF_RANGE_DELIMITER ).c_str() );
151 bomJob->m_keepTabs = m_argParser.get<bool>( ARG_KEEP_TABS );
152 bomJob->m_keepLineBreaks = m_argParser.get<bool>( ARG_KEEP_LINE_BREAKS );
153
154 // Output fields options
155 bomJob->m_fieldsOrdered =
156 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_FIELDS ).c_str() ) );
157 bomJob->m_fieldsLabels =
158 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_LABELS ).c_str() ) );
159
160 // We only apply the default labels if the default fields are used
161 if( m_argParser.is_used( ARG_FIELDS ) && !m_argParser.is_used( ARG_LABELS ) )
162 {
163 bomJob->m_fieldsLabels.clear();
164 }
165
166 bomJob->m_fieldsGroupBy =
167 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_GROUP_BY ).c_str() ) );
168 bomJob->m_sortField = From_UTF8( m_argParser.get<std::string>( ARG_SORT_FIELD ).c_str() );
169 bomJob->m_sortAsc = m_argParser.get<bool>( ARG_SORT_ASC );
170 bomJob->m_filterString = From_UTF8( m_argParser.get<std::string>( ARG_FILTER ).c_str() );
171 bomJob->m_excludeDNP = m_argParser.get<bool>( ARG_EXCLUDE_DNP );
172 bomJob->m_includeExcludedFromBOM = m_argParser.get<bool>( ARG_INCLUDE_EXCLUDED_FROM_BOM );
173
174 if( !wxFile::Exists( bomJob->m_filename ) )
175 {
176 wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
178 }
179
180 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, bomJob.get() );
181
182 return exitCode;
183}
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: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_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_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)