From fb8d52f19ea8006258d2af20d1898f6346ee644f Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sun, 3 Sep 2023 09:17:51 -0400 Subject: [PATCH] kicad-cli: Remove -O for plot and add --pages to specify list of pages instead --pages 1 is equivalent to -O Fixes https://gitlab.com/kicad/code/kicad/-/issues/15402 --- eeschema/sch_plotter.cpp | 36 ++++++++++++++++++++++++++- eeschema/sch_plotter.h | 6 +++-- eeschema/sch_sheet_path.cpp | 14 +++++++++++ eeschema/sch_sheet_path.h | 7 ++++++ kicad/cli/command_sch_export_plot.cpp | 23 ++++++++++++----- 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/eeschema/sch_plotter.cpp b/eeschema/sch_plotter.cpp index 4efd1e8c0c..d3c2c6e580 100644 --- a/eeschema/sch_plotter.cpp +++ b/eeschema/sch_plotter.cpp @@ -117,13 +117,20 @@ void SCH_PLOTTER::createPDFFile( const SCH_PLOT_SETTINGS& aPlotSettings, */ SCH_SHEET_LIST sheetList; - if( aPlotSettings.m_plotAll ) + if( aPlotSettings.m_plotAll || aPlotSettings.m_plotPages.size() > 0 ) { sheetList.BuildSheetList( &m_schematic->Root(), true ); sheetList.SortByPageNumbers(); + + // remove the non-selected pages if we are in plot pages mode + if( aPlotSettings.m_plotPages.size() > 0 ) + { + sheetList.TrimToPageNumbers( aPlotSettings.m_plotPages ); + } } else { + // in eeschema, this prints the current page sheetList.push_back( m_schematic->CurrentSheet() ); } @@ -302,6 +309,12 @@ void SCH_PLOTTER::createPSFiles( const SCH_PLOT_SETTINGS& aPlotSettings, { sheetList.BuildSheetList( &m_schematic->Root(), true ); sheetList.SortByPageNumbers(); + + // remove the non-selected pages if we are in plot pages mode + if( aPlotSettings.m_plotPages.size() > 0 ) + { + sheetList.TrimToPageNumbers( aPlotSettings.m_plotPages ); + } } else { @@ -462,9 +475,16 @@ void SCH_PLOTTER::createSVGFiles( const SCH_PLOT_SETTINGS& aPlotSettings, { sheetList.BuildSheetList( &m_schematic->Root(), true ); sheetList.SortByPageNumbers(); + + // remove the non-selected pages if we are in plot pages mode + if( aPlotSettings.m_plotPages.size() > 0 ) + { + sheetList.TrimToPageNumbers( aPlotSettings.m_plotPages ); + } } else { + // in eeschema, this prints the current page sheetList.push_back( m_schematic->CurrentSheet() ); } @@ -636,9 +656,16 @@ void SCH_PLOTTER::createHPGLFiles( const SCH_PLOT_SETTINGS& aPlotSettings, { sheetList.BuildSheetList( &m_schematic->Root(), true ); sheetList.SortByPageNumbers(); + + // remove the non-selected pages if we are in plot pages mode + if( aPlotSettings.m_plotPages.size() > 0 ) + { + sheetList.TrimToPageNumbers( aPlotSettings.m_plotPages ); + } } else { + // in eeschema, this prints the current page sheetList.push_back( m_schematic->CurrentSheet() ); } @@ -818,9 +845,16 @@ void SCH_PLOTTER::createDXFFiles( const SCH_PLOT_SETTINGS& aPlotSettings, { sheetList.BuildSheetList( &m_schematic->Root(), true ); sheetList.SortByPageNumbers(); + + // remove the non-selected pages if we are in plot pages mode + if( aPlotSettings.m_plotPages.size() > 0 ) + { + sheetList.TrimToPageNumbers( aPlotSettings.m_plotPages ); + } } else { + // in eeschema, this prints the current page sheetList.push_back( m_schematic->CurrentSheet() ); } diff --git a/eeschema/sch_plotter.h b/eeschema/sch_plotter.h index c0bdf93962..66e7b70fc3 100644 --- a/eeschema/sch_plotter.h +++ b/eeschema/sch_plotter.h @@ -78,8 +78,10 @@ enum class HPGL_PAGE_SIZE struct SCH_PLOT_SETTINGS { - bool m_plotAll; - bool m_plotDrawingSheet; + bool m_plotAll; + bool m_plotDrawingSheet; + std::vector m_plotPages; + bool m_blackAndWhite; int m_pageSizeSelect; bool m_useBackgroundColor; diff --git a/eeschema/sch_sheet_path.cpp b/eeschema/sch_sheet_path.cpp index f3ce3f5498..8bc75819ac 100644 --- a/eeschema/sch_sheet_path.cpp +++ b/eeschema/sch_sheet_path.cpp @@ -767,6 +767,20 @@ bool SCH_SHEET_LIST::PageNumberExists( const wxString& aPageNumber ) const } +void SCH_SHEET_LIST::TrimToPageNumbers( const std::vector& aPageInclusions ) +{ + auto it = std::remove_if( begin(), end(), + [&]( SCH_SHEET_PATH sheet ) + { + return std::find( aPageInclusions.begin(), aPageInclusions.end(), + sheet.GetPageNumber() ) + == aPageInclusions.end(); + } ); + + erase( it, end() ); +} + + bool SCH_SHEET_LIST::IsModified() const { for( const SCH_SHEET_PATH& sheet : *this ) diff --git a/eeschema/sch_sheet_path.h b/eeschema/sch_sheet_path.h index 392d2253d8..3cd5b42a48 100644 --- a/eeschema/sch_sheet_path.h +++ b/eeschema/sch_sheet_path.h @@ -614,6 +614,13 @@ public: bool PageNumberExists( const wxString& aPageNumber ) const; + /** + * Truncates the list by removing sheet's with page numbers not in the given list + * + * @param aPageInclusions List of Page Numbers (non-virtual) to keep + */ + void TrimToPageNumbers( const std::vector& aPageInclusions ); + /** * Update all of the symbol instance information using \a aSymbolInstances. * WARNING: Do not call this on anything other than the full hierarchy. diff --git a/kicad/cli/command_sch_export_plot.cpp b/kicad/cli/command_sch_export_plot.cpp index 5a9d5f137f..89a0f643ac 100644 --- a/kicad/cli/command_sch_export_plot.cpp +++ b/kicad/cli/command_sch_export_plot.cpp @@ -25,15 +25,16 @@ #include "jobs/job_export_sch_plot.h" #include #include +#include #include #include #define ARG_EXCLUDE_DRAWING_SHEET "--exclude-drawing-sheet" #define ARG_NO_BACKGROUND_COLOR "--no-background-color" -#define ARG_PLOT_ONE "--plot-one" #define ARG_HPGL_PEN_SIZE "--pen-size" #define ARG_HPGL_ORIGIN "--origin" +#define ARG_PAGES "--pages" const HPGL_PLOT_ORIGIN_AND_UNITS hpgl_origin_ops[4] = { HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_BOT_LEFT, HPGL_PLOT_ORIGIN_AND_UNITS::PLOTTER_CENTER, @@ -73,10 +74,10 @@ CLI::SCH_EXPORT_PLOT_COMMAND::SCH_EXPORT_PLOT_COMMAND( const std::string& aName, .implicit_value( true ) .default_value( false ); - m_argParser.add_argument( "-O", ARG_PLOT_ONE ) - .help( UTF8STDSTR( _( "Plot only the first page (no sub-sheets)" ) ) ) - .implicit_value( true ) - .default_value( false ); + m_argParser.add_argument( "-p", ARG_PAGES ) + .default_value( std::string() ) + .help( UTF8STDSTR( _( "List of page numbers separated by comma to print, blank or unspecified is equivalent to all pages" ) ) ) + .metavar( "PAGE_LIST" ); if( aPlotFormat == PLOT_FORMAT::HPGL ) { @@ -105,11 +106,19 @@ int CLI::SCH_EXPORT_PLOT_COMMAND::doPerform( KIWAY& aKiway ) return EXIT_CODES::ERR_INVALID_INPUT_FILE; } + std::vector pages; + wxString pagesStr = FROM_UTF8( m_argParser.get( ARG_PAGES ).c_str() ); + wxStringTokenizer tokenizer( pagesStr, "," ); + while( tokenizer.HasMoreTokens() ) + { + pages.push_back( tokenizer.GetNextToken().Trim() ); + } + std::unique_ptr plotJob = std::make_unique( true, m_plotFormat, filename ); SCH_PLOT_SETTINGS& settings = plotJob->settings; - settings.m_plotAll = !m_argParser.get( ARG_PLOT_ONE ); + settings.m_plotPages = pages; settings.m_plotDrawingSheet = !m_argParser.get( ARG_EXCLUDE_DRAWING_SHEET ); settings.m_blackAndWhite = m_argParser.get( ARG_BLACKANDWHITE ); settings.m_pageSizeSelect = PAGE_SIZE_AUTO; @@ -120,6 +129,8 @@ int CLI::SCH_EXPORT_PLOT_COMMAND::doPerform( KIWAY& aKiway ) else settings.m_outputFile = m_argOutput; + settings.m_plotAll = settings.m_plotPages.size() == 0; + plotJob->m_drawingSheet = m_argDrawingSheet; plotJob->SetVarOverrides( m_argDefineVars );