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 <schematic_settings.h>
29
30#include <macros.h>
31
32
34{
35 m_argParser.add_description( UTF8STDSTR( _( "Generate a Bill of Materials (BOM)" ) ) );
36 addCommonArgs( true, true, false, false );
37
38 // Preset options
39 m_argParser.add_argument( ARG_PRESET )
40 .help( UTF8STDSTR( _( ARG_PRESET_DESC ) ) )
41 .default_value( std::string( "" ) )
42 .metavar( "PRESET" );
43
44 m_argParser.add_argument( ARG_FMT_PRESET )
45 .help( UTF8STDSTR( _( ARG_FMT_PRESET_DESC ) ) )
46 .default_value( std::string( "" ) )
47 .metavar( "FMT_PRESET" );
48
49 // Field output options
50 m_argParser.add_argument( ARG_FIELDS )
51 .help( UTF8STDSTR( _( ARG_FIELDS_DESC ) ) )
52 .default_value( std::string( "Reference,Value,Footprint,${QUANTITY},${DNP}" ) )
53 .metavar( "FIELDS" );
54
55 m_argParser.add_argument( ARG_LABELS )
56 .help( UTF8STDSTR( _( ARG_LABELS_DESC ) ) )
57 .default_value( std::string( "Refs,Value,Footprint,Qty,DNP" ) )
58 .metavar( "LABELS" );
59
60 m_argParser.add_argument( ARG_GROUP_BY )
61 .help( UTF8STDSTR( _( ARG_GROUP_BY_DESC ) ) )
62 .default_value( std::string( "" ) )
63 .metavar( "GROUP_BY" );
64
65 m_argParser.add_argument( ARG_SORT_FIELD )
66 .help( UTF8STDSTR( _( ARG_SORT_FIELD_DESC ) ) )
67 .default_value( std::string( "Reference" ) )
68 .metavar( "SORT_BY" );
69
70 m_argParser.add_argument( ARG_SORT_ASC )
71 .help( UTF8STDSTR( _( ARG_SORT_ASC_DESC ) ) )
72 .implicit_value( true )
73 .default_value( true );
74
75 m_argParser.add_argument( ARG_FILTER )
76 .help( UTF8STDSTR( _( ARG_FILTER_DESC ) ) )
77 .default_value( std::string( "" ) )
78 .metavar( "FILTER" );
79
80 m_argParser.add_argument( ARG_EXCLUDE_DNP )
81 .help( UTF8STDSTR( _( ARG_EXCLUDE_DNP_DESC ) ) )
82 .flag();
83
84 // Output formatting options
85 m_argParser.add_argument( ARG_FIELD_DELIMITER )
87 .default_value( std::string( "," ) )
88 .metavar( "FIELD_DELIM" );
89
90 m_argParser.add_argument( ARG_STRING_DELIMITER )
92 .default_value( std::string( "\"" ) )
93 .metavar( "STR_DELIM" );
94
95 m_argParser.add_argument( ARG_REF_DELIMITER )
97 .default_value( std::string( "," ) )
98 .metavar( "REF_DELIM" );
99
102 .default_value( std::string( "-" ) )
103 .metavar( "REF_RANGE_DELIM" );
104
105 m_argParser.add_argument( ARG_KEEP_TABS )
106 .help( UTF8STDSTR( _( ARG_KEEP_TABS_DESC ) ) )
107 .flag();
108
109 m_argParser.add_argument( ARG_KEEP_LINE_BREAKS )
111 .flag();
112}
113
114
115std::vector<wxString> CLI::SCH_EXPORT_BOM_COMMAND::convertStringList( const wxString& aList )
116{
117 std::vector<wxString> v;
118
119 if( !aList.IsEmpty() )
120 {
121 wxStringTokenizer layerTokens( aList, "," );
122
123 while( layerTokens.HasMoreTokens() )
124 v.emplace_back( layerTokens.GetNextToken() );
125 }
126
127 return v;
128}
129
131{
132 std::unique_ptr<JOB_EXPORT_SCH_BOM> bomJob = std::make_unique<JOB_EXPORT_SCH_BOM>( true );
133
134 // Basic options
135 bomJob->m_filename = m_argInput;
136 bomJob->m_outputFile = m_argOutput;
137
138 bomJob->m_bomPresetName = From_UTF8( m_argParser.get<std::string>( ARG_PRESET ).c_str() );
139 bomJob->m_bomFmtPresetName =
140 From_UTF8( m_argParser.get<std::string>( ARG_FMT_PRESET ).c_str() );
141
142 // Format options
143 bomJob->m_fieldDelimiter =
144 From_UTF8( m_argParser.get<std::string>( ARG_FIELD_DELIMITER ).c_str() );
145 bomJob->m_stringDelimiter =
146 From_UTF8( m_argParser.get<std::string>( ARG_STRING_DELIMITER ).c_str() );
147 bomJob->m_refDelimiter = From_UTF8( m_argParser.get<std::string>( ARG_REF_DELIMITER ).c_str() );
148 bomJob->m_refRangeDelimiter =
149 From_UTF8( m_argParser.get<std::string>( ARG_REF_RANGE_DELIMITER ).c_str() );
150 bomJob->m_keepTabs = m_argParser.get<bool>( ARG_KEEP_TABS );
151 bomJob->m_keepLineBreaks = m_argParser.get<bool>( ARG_KEEP_LINE_BREAKS );
152
153 // Output fields options
154 bomJob->m_fieldsOrdered =
155 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_FIELDS ).c_str() ) );
156 bomJob->m_fieldsLabels =
157 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_LABELS ).c_str() ) );
158
159 // We only apply the default labels if the default fields are used
160 if( m_argParser.is_used( ARG_FIELDS ) && !m_argParser.is_used( ARG_LABELS ) )
161 {
162 bomJob->m_fieldsLabels.clear();
163 }
164
165 bomJob->m_fieldsGroupBy =
166 convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_GROUP_BY ).c_str() ) );
167 bomJob->m_sortField = From_UTF8( m_argParser.get<std::string>( ARG_SORT_FIELD ).c_str() );
168 bomJob->m_sortAsc = m_argParser.get<bool>( ARG_SORT_ASC );
169 bomJob->m_filterString = From_UTF8( m_argParser.get<std::string>( ARG_FILTER ).c_str() );
170 bomJob->m_excludeDNP = m_argParser.get<bool>( ARG_EXCLUDE_DNP );
171
172 if( !wxFile::Exists( bomJob->m_filename ) )
173 {
174 wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
176 }
177
178 int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, bomJob.get() );
179
180 return exitCode;
181}
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:279
int ProcessJob(KIWAY::FACE_T aFace, JOB *job)
Definition: kiway.cpp:709
@ FACE_SCH
eeschema DSO
Definition: kiway.h:286
#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_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)