2023-03-14 20:03:57 -04:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 Mike Williams <mike@mikebwilliams.com>
|
|
|
|
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-08-19 11:46:18 -04:00
|
|
|
#include "command_sch_export_bom.h"
|
2023-03-14 20:03:57 -04:00
|
|
|
#include <cli/exit_codes.h>
|
|
|
|
#include "jobs/job_export_sch_bom.h"
|
|
|
|
#include <kiface_base.h>
|
|
|
|
#include <layer_ids.h>
|
2023-09-09 00:10:57 -04:00
|
|
|
#include <string_utils.h>
|
2023-03-14 20:03:57 -04:00
|
|
|
#include <wx/crt.h>
|
2024-05-06 04:39:25 +03:00
|
|
|
#include <wx/tokenzr.h>
|
2023-03-14 20:03:57 -04:00
|
|
|
#include <schematic_settings.h>
|
|
|
|
|
|
|
|
#include <macros.h>
|
|
|
|
|
|
|
|
|
2023-08-31 18:43:32 -04:00
|
|
|
CLI::SCH_EXPORT_BOM_COMMAND::SCH_EXPORT_BOM_COMMAND() : COMMAND( "bom" )
|
2023-03-14 20:03:57 -04:00
|
|
|
{
|
2023-11-20 22:28:19 -05:00
|
|
|
m_argParser.add_description( UTF8STDSTR( _( "Generate a Bill of Materials (BOM)" ) ) );
|
2023-09-19 01:47:41 +00:00
|
|
|
addCommonArgs( true, true, false, false );
|
2023-08-31 18:43:32 -04:00
|
|
|
|
2023-09-20 10:54:37 -04:00
|
|
|
// Preset options
|
|
|
|
m_argParser.add_argument( ARG_PRESET )
|
|
|
|
.help( UTF8STDSTR( _( ARG_PRESET_DESC ) ) )
|
|
|
|
.default_value( std::string( "" ) )
|
|
|
|
.metavar( "PRESET" );
|
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_FMT_PRESET )
|
|
|
|
.help( UTF8STDSTR( _( ARG_FMT_PRESET_DESC ) ) )
|
|
|
|
.default_value( std::string( "" ) )
|
|
|
|
.metavar( "FMT_PRESET" );
|
|
|
|
|
2023-03-14 20:03:57 -04:00
|
|
|
// Field output options
|
|
|
|
m_argParser.add_argument( ARG_FIELDS )
|
2023-07-12 11:51:30 -04:00
|
|
|
.help( UTF8STDSTR( _( ARG_FIELDS_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "Reference,Value,Footprint,${QUANTITY},${DNP}" ) )
|
|
|
|
.metavar( "FIELDS" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_LABELS )
|
|
|
|
.help( UTF8STDSTR( _( ARG_LABELS_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "Refs,Value,Footprint,Qty,DNP" ) )
|
|
|
|
.metavar( "LABELS" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_GROUP_BY )
|
|
|
|
.help( UTF8STDSTR( _( ARG_GROUP_BY_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "" ) )
|
|
|
|
.metavar( "GROUP_BY" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_SORT_FIELD )
|
|
|
|
.help( UTF8STDSTR( _( ARG_SORT_FIELD_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "Reference" ) )
|
|
|
|
.metavar( "SORT_BY" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_SORT_ASC )
|
|
|
|
.help( UTF8STDSTR( _( ARG_SORT_ASC_DESC ) ) )
|
|
|
|
.implicit_value( true )
|
|
|
|
.default_value( true );
|
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_FILTER )
|
|
|
|
.help( UTF8STDSTR( _( ARG_FILTER_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "" ) )
|
|
|
|
.metavar( "FILTER" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
2023-03-30 10:17:09 -04:00
|
|
|
m_argParser.add_argument( ARG_EXCLUDE_DNP )
|
|
|
|
.help( UTF8STDSTR( _( ARG_EXCLUDE_DNP_DESC ) ) )
|
2024-01-03 21:39:54 -05:00
|
|
|
.flag();
|
2023-03-30 10:17:09 -04:00
|
|
|
|
2024-12-21 00:48:43 +00:00
|
|
|
m_argParser.add_argument( ARG_INCLUDE_EXCLUDED_FROM_BOM )
|
|
|
|
.help( UTF8STDSTR( _( ARG_INCLUDE_EXCLUDED_FROM_BOM_DESC ) ) )
|
|
|
|
.flag();
|
|
|
|
|
2023-03-14 20:03:57 -04:00
|
|
|
// Output formatting options
|
|
|
|
m_argParser.add_argument( ARG_FIELD_DELIMITER )
|
|
|
|
.help( UTF8STDSTR( _( ARG_FIELD_DELIMITER_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "," ) )
|
|
|
|
.metavar( "FIELD_DELIM" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_STRING_DELIMITER )
|
|
|
|
.help( UTF8STDSTR( _( ARG_STRING_DELIMITER_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "\"" ) )
|
|
|
|
.metavar( "STR_DELIM" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_REF_DELIMITER )
|
|
|
|
.help( UTF8STDSTR( _( ARG_REF_DELIMITER_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "," ) )
|
|
|
|
.metavar( "REF_DELIM" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_REF_RANGE_DELIMITER )
|
|
|
|
.help( UTF8STDSTR( _( ARG_REF_RANGE_DELIMITER_DESC ) ) )
|
2023-08-31 22:01:07 -04:00
|
|
|
.default_value( std::string( "-" ) )
|
|
|
|
.metavar( "REF_RANGE_DELIM" );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_KEEP_TABS )
|
|
|
|
.help( UTF8STDSTR( _( ARG_KEEP_TABS_DESC ) ) )
|
2024-01-03 21:39:54 -05:00
|
|
|
.flag();
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
m_argParser.add_argument( ARG_KEEP_LINE_BREAKS )
|
|
|
|
.help( UTF8STDSTR( _( ARG_KEEP_LINE_BREAKS_DESC ) ) )
|
2024-01-03 21:39:54 -05:00
|
|
|
.flag();
|
2023-03-14 20:03:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-19 11:51:50 -04:00
|
|
|
std::vector<wxString> CLI::SCH_EXPORT_BOM_COMMAND::convertStringList( const wxString& aList )
|
2023-03-14 20:03:57 -04:00
|
|
|
{
|
|
|
|
std::vector<wxString> v;
|
|
|
|
|
|
|
|
if( !aList.IsEmpty() )
|
|
|
|
{
|
|
|
|
wxStringTokenizer layerTokens( aList, "," );
|
|
|
|
|
|
|
|
while( layerTokens.HasMoreTokens() )
|
|
|
|
v.emplace_back( layerTokens.GetNextToken() );
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2023-08-19 11:51:50 -04:00
|
|
|
int CLI::SCH_EXPORT_BOM_COMMAND::doPerform( KIWAY& aKiway )
|
2023-03-14 20:03:57 -04:00
|
|
|
{
|
2024-11-04 20:51:15 -05:00
|
|
|
std::unique_ptr<JOB_EXPORT_SCH_BOM> bomJob = std::make_unique<JOB_EXPORT_SCH_BOM>();
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
// Basic options
|
2023-08-31 18:43:32 -04:00
|
|
|
bomJob->m_filename = m_argInput;
|
2024-07-15 18:20:13 -04:00
|
|
|
bomJob->SetOutputPath( m_argOutput );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
2023-09-20 10:54:37 -04:00
|
|
|
bomJob->m_bomPresetName = From_UTF8( m_argParser.get<std::string>( ARG_PRESET ).c_str() );
|
|
|
|
bomJob->m_bomFmtPresetName =
|
|
|
|
From_UTF8( m_argParser.get<std::string>( ARG_FMT_PRESET ).c_str() );
|
|
|
|
|
2023-03-14 20:03:57 -04:00
|
|
|
// Format options
|
|
|
|
bomJob->m_fieldDelimiter =
|
2023-09-09 00:10:57 -04:00
|
|
|
From_UTF8( m_argParser.get<std::string>( ARG_FIELD_DELIMITER ).c_str() );
|
2023-03-14 20:03:57 -04:00
|
|
|
bomJob->m_stringDelimiter =
|
2023-09-09 00:10:57 -04:00
|
|
|
From_UTF8( m_argParser.get<std::string>( ARG_STRING_DELIMITER ).c_str() );
|
|
|
|
bomJob->m_refDelimiter = From_UTF8( m_argParser.get<std::string>( ARG_REF_DELIMITER ).c_str() );
|
2023-03-14 20:03:57 -04:00
|
|
|
bomJob->m_refRangeDelimiter =
|
2023-09-09 00:10:57 -04:00
|
|
|
From_UTF8( m_argParser.get<std::string>( ARG_REF_RANGE_DELIMITER ).c_str() );
|
2023-03-14 20:03:57 -04:00
|
|
|
bomJob->m_keepTabs = m_argParser.get<bool>( ARG_KEEP_TABS );
|
|
|
|
bomJob->m_keepLineBreaks = m_argParser.get<bool>( ARG_KEEP_LINE_BREAKS );
|
|
|
|
|
|
|
|
// Output fields options
|
|
|
|
bomJob->m_fieldsOrdered =
|
2023-09-09 00:10:57 -04:00
|
|
|
convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_FIELDS ).c_str() ) );
|
2023-03-14 20:03:57 -04:00
|
|
|
bomJob->m_fieldsLabels =
|
2023-09-09 00:10:57 -04:00
|
|
|
convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_LABELS ).c_str() ) );
|
2023-11-15 09:53:18 -05:00
|
|
|
|
|
|
|
// We only apply the default labels if the default fields are used
|
|
|
|
if( m_argParser.is_used( ARG_FIELDS ) && !m_argParser.is_used( ARG_LABELS ) )
|
|
|
|
{
|
|
|
|
bomJob->m_fieldsLabels.clear();
|
|
|
|
}
|
|
|
|
|
2023-03-14 20:03:57 -04:00
|
|
|
bomJob->m_fieldsGroupBy =
|
2023-09-09 00:10:57 -04:00
|
|
|
convertStringList( From_UTF8( m_argParser.get<std::string>( ARG_GROUP_BY ).c_str() ) );
|
|
|
|
bomJob->m_sortField = From_UTF8( m_argParser.get<std::string>( ARG_SORT_FIELD ).c_str() );
|
2023-03-14 20:03:57 -04:00
|
|
|
bomJob->m_sortAsc = m_argParser.get<bool>( ARG_SORT_ASC );
|
2023-09-09 00:10:57 -04:00
|
|
|
bomJob->m_filterString = From_UTF8( m_argParser.get<std::string>( ARG_FILTER ).c_str() );
|
2023-07-23 13:59:28 -04:00
|
|
|
bomJob->m_excludeDNP = m_argParser.get<bool>( ARG_EXCLUDE_DNP );
|
2024-04-15 10:06:08 -04:00
|
|
|
bomJob->m_includeExcludedFromBOM = m_argParser.get<bool>( ARG_INCLUDE_EXCLUDED_FROM_BOM );
|
2023-03-14 20:03:57 -04:00
|
|
|
|
|
|
|
if( !wxFile::Exists( bomJob->m_filename ) )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Schematic file does not exist or is not accessible\n" ) );
|
|
|
|
return EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int exitCode = aKiway.ProcessJob( KIWAY::FACE_SCH, bomJob.get() );
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
}
|