From b7a77a94981ae25c4a726b2c1124960e60837f72 Mon Sep 17 00:00:00 2001 From: Mike Williams Date: Mon, 9 May 2022 09:41:00 -0400 Subject: [PATCH] Schematic: Add CSV export to Symbol Fields Table --- common/wildcards_and_files_ext.cpp | 3 +- .../dialogs/dialog_symbol_fields_table.cpp | 76 +++++++++++++ eeschema/dialogs/dialog_symbol_fields_table.h | 1 + .../dialog_symbol_fields_table_base.cpp | 11 +- .../dialog_symbol_fields_table_base.fbp | 104 +++++++++++++++++- .../dialogs/dialog_symbol_fields_table_base.h | 9 +- include/wildcards_and_files_ext.h | 1 + pcbnew/build_BOM_from_board.cpp | 2 - 8 files changed, 195 insertions(+), 12 deletions(-) diff --git a/common/wildcards_and_files_ext.cpp b/common/wildcards_and_files_ext.cpp index 841a989ec3..b7daa17b21 100644 --- a/common/wildcards_and_files_ext.cpp +++ b/common/wildcards_and_files_ext.cpp @@ -170,6 +170,7 @@ const std::string WorkbookFileExtension( "wbk" ); const std::string PngFileExtension( "png" ); const std::string JpegFileExtension( "jpg" ); const std::string TextFileExtension( "txt" ); +const std::string CsvFileExtension( "csv" ); bool IsProtelExtension( const wxString& ext ) @@ -425,7 +426,7 @@ wxString HtmlFileWildcard() wxString CsvFileWildcard() { - return _( "CSV Files" ) + AddFileExtListToFilter( { "csv" } ); + return _( "CSV Files" ) + AddFileExtListToFilter( { CsvFileExtension } ); } diff --git a/eeschema/dialogs/dialog_symbol_fields_table.cpp b/eeschema/dialogs/dialog_symbol_fields_table.cpp index 3b07bd016e..b2de7f4d85 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table.cpp @@ -41,12 +41,15 @@ #include #include #include +#include #include #include #include +#include #include "dialog_symbol_fields_table.h" #include "eda_list_dialog.h" +#include #define DISPLAY_NAME_COLUMN 0 #define SHOW_FIELD_COLUMN 1 @@ -285,6 +288,16 @@ public: } } + wxString GetRawValue( int aRow, int aCol ) + { + return GetValue( m_rows[ aRow ], aCol ); + } + + GROUP_TYPE GetRowFlags( int aRow ) + { + return m_rows[ aRow ].m_Flag; + } + std::vector GetRowReferences( int aRow ) const { wxCHECK( aRow < (int)m_rows.size(), std::vector() ); @@ -1306,6 +1319,69 @@ void DIALOG_SYMBOL_FIELDS_TABLE::OnSaveAndContinue( wxCommandEvent& aEvent ) } +void DIALOG_SYMBOL_FIELDS_TABLE::OnExport( wxCommandEvent& aEvent ) +{ + if( m_dataModel->IsEdited() ) + if( OKOrCancelDialog( nullptr, _( "Unsaved data" ), + _( "Changes are unsaved. Export unsaved data?" ), "", _( "OK" ), + _( "Cancel" ) ) + == wxID_CANCEL ) + return; + + + // Calculate the netlist filename + wxFileName fn = m_parent->Schematic().GetFileName(); + fn.SetExt( CsvFileExtension ); + + wxFileDialog saveDlg( this, _( "Save as CSV" ), wxPathOnly( Prj().GetProjectFullName() ), + fn.GetFullName(), CsvFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT ); + + if( saveDlg.ShowModal() == wxID_CANCEL ) + return; + + wxFFile out( saveDlg.GetPath(), "wb" ); + + if( !out.IsOpened() ) + return; + + // Column names + for( int col = 0; col < m_grid->GetNumberCols(); col++ ) + { + if( !m_grid->IsColShown( col ) ) + continue; + + wxString escapedValue = m_grid->GetColLabelValue( col ); + escapedValue.Replace( "\"", "\"\"" ); + + wxString format = col == m_grid->GetNumberCols() - 1 ? "\"%s\"\r\n" : "\"%s\","; + + out.Write( wxString::Format( format, escapedValue ) ); + } + + // Data rows + for( int row = 0; row < m_grid->GetNumberRows(); row++ ) + { + // Don't output child rows + if( m_dataModel->GetRowFlags( row ) == CHILD_ITEM ) + continue; + + for( int col = 0; col < m_grid->GetNumberCols(); col++ ) + { + if( !m_grid->IsColShown( col ) ) + continue; + + // Get the unanottated version of the field, e.g. no "> " or "v " by + wxString escapedValue = m_dataModel->GetRawValue( row, col ); + escapedValue.Replace( "\"", "\"\"" ); + + wxString format = col == m_grid->GetNumberCols() - 1 ? "\"%s\"\r\n" : "\"%s\","; + + out.Write( wxString::Format( format, escapedValue ) ); + } + } +} + + void DIALOG_SYMBOL_FIELDS_TABLE::OnCancel( wxCommandEvent& event ) { Close(); diff --git a/eeschema/dialogs/dialog_symbol_fields_table.h b/eeschema/dialogs/dialog_symbol_fields_table.h index b0759ad5d8..6abd98c879 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table.h +++ b/eeschema/dialogs/dialog_symbol_fields_table.h @@ -65,6 +65,7 @@ private: void OnSizeFieldList( wxSizeEvent& event ) override; void OnAddField( wxCommandEvent& event ) override; void OnRemoveField( wxCommandEvent& event ) override; + void OnExport( wxCommandEvent& aEvent ) override; void OnSaveAndContinue( wxCommandEvent& aEvent ) override; void OnCancel( wxCommandEvent& aEvent ) override; void OnClose( wxCloseEvent& aEvent ) override; diff --git a/eeschema/dialogs/dialog_symbol_fields_table_base.cpp b/eeschema/dialogs/dialog_symbol_fields_table_base.cpp index 7927334e25..741302303f 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table_base.cpp +++ b/eeschema/dialogs/dialog_symbol_fields_table_base.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version Oct 26 2018) +// C++ code generated with wxFormBuilder (version 3.10.1-133-g388db8e4) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -120,10 +120,13 @@ DIALOG_SYMBOL_FIELDS_TABLE_BASE::DIALOG_SYMBOL_FIELDS_TABLE_BASE( wxWindow* pare bButtonsSizer = new wxBoxSizer( wxHORIZONTAL ); - bButtonsSizer->Add( 0, 0, 1, wxEXPAND, 5 ); + bButtonsSizer->Add( 0, 0, 9, wxEXPAND, 5 ); + + m_buttonExport = new wxButton( this, wxID_ANY, _("Export as CSV..."), wxDefaultPosition, wxDefaultSize, 0 ); + bButtonsSizer->Add( m_buttonExport, 0, wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT, 5 ); m_buttonApply = new wxButton( this, wxID_ANY, _("Apply, Save Schematic && Continue"), wxDefaultPosition, wxDefaultSize, 0 ); - bButtonsSizer->Add( m_buttonApply, 0, wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxRIGHT, 5 ); + bButtonsSizer->Add( m_buttonApply, 0, wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT, 5 ); m_sdbSizer = new wxStdDialogButtonSizer(); m_sdbSizerOK = new wxButton( this, wxID_OK ); @@ -159,6 +162,7 @@ DIALOG_SYMBOL_FIELDS_TABLE_BASE::DIALOG_SYMBOL_FIELDS_TABLE_BASE( wxWindow* pare m_grid->Connect( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnTableCellClick ), NULL, this ); m_grid->Connect( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnTableItemContextMenu ), NULL, this ); m_grid->Connect( wxEVT_GRID_COL_SIZE, wxGridSizeEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnTableColSize ), NULL, this ); + m_buttonExport->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnExport ), NULL, this ); m_buttonApply->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnSaveAndContinue ), NULL, this ); m_sdbSizerCancel->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnCancel ), NULL, this ); } @@ -180,6 +184,7 @@ DIALOG_SYMBOL_FIELDS_TABLE_BASE::~DIALOG_SYMBOL_FIELDS_TABLE_BASE() m_grid->Disconnect( wxEVT_GRID_CELL_LEFT_DCLICK, wxGridEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnTableCellClick ), NULL, this ); m_grid->Disconnect( wxEVT_GRID_CELL_RIGHT_CLICK, wxGridEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnTableItemContextMenu ), NULL, this ); m_grid->Disconnect( wxEVT_GRID_COL_SIZE, wxGridSizeEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnTableColSize ), NULL, this ); + m_buttonExport->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnExport ), NULL, this ); m_buttonApply->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnSaveAndContinue ), NULL, this ); m_sdbSizerCancel->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_SYMBOL_FIELDS_TABLE_BASE::OnCancel ), NULL, this ); diff --git a/eeschema/dialogs/dialog_symbol_fields_table_base.fbp b/eeschema/dialogs/dialog_symbol_fields_table_base.fbp index 05b50ad867..33dd5790be 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table_base.fbp +++ b/eeschema/dialogs/dialog_symbol_fields_table_base.fbp @@ -1,6 +1,6 @@ - + C++ @@ -14,6 +14,7 @@ dialog_symbol_fields_table_base 1000 none + 1 dialog_symbol_fields_table_base @@ -25,6 +26,7 @@ 1 1 UI + 0 0 0 @@ -34,6 +36,7 @@ wxBOTH 1 + 0 1 impl_virtual @@ -50,6 +53,7 @@ DIALOG_SHIM; dialog_shim.h Symbol Fields Table + 0 @@ -84,6 +88,7 @@ Dock 0 Left + 0 1 1 @@ -142,6 +147,7 @@ Dock 0 Left + 0 1 1 @@ -185,6 +191,7 @@ 1 + 0 1 @@ -219,6 +226,7 @@ + 0 @@ -235,6 +243,7 @@ Dock 0 Left + 0 1 1 @@ -292,6 +301,7 @@ + 0 @@ -308,6 +318,7 @@ Dock 0 Left + 0 1 1 @@ -377,6 +388,7 @@ Dock 0 Left + 0 1 1 @@ -447,6 +459,7 @@ Dock 0 Left + 0 1 1 @@ -501,6 +514,7 @@ + 0 @@ -517,6 +531,7 @@ Dock 0 Left + 0 0 1 @@ -586,6 +601,7 @@ Dock 0 Left + 0 1 1 @@ -638,6 +654,7 @@ + 0 @@ -654,6 +671,7 @@ Dock 0 Left + 0 0 1 @@ -710,6 +728,7 @@ + 0 @@ -726,6 +745,7 @@ Dock 0 Left + 0 1 1 @@ -810,6 +830,7 @@ Dock 0 Left + 0 1 1 0 @@ -882,7 +903,7 @@ 5 wxEXPAND - 1 + 9 0 protected @@ -891,7 +912,7 @@ 5 - wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxRIGHT + wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT 0 1 @@ -902,6 +923,7 @@ + 0 @@ -918,6 +940,82 @@ Dock 0 Left + 0 + 1 + + 1 + + + 0 + 0 + wxID_ANY + Export as CSV... + + 0 + + 0 + + + 0 + + 1 + m_buttonExport + 1 + + + protected + 1 + + + + Resizable + 1 + + + ; forward_declare + 0 + + + wxFILTER_NONE + wxDefaultValidator + + + + + OnExport + + + + 5 + wxALIGN_CENTER_VERTICAL|wxBOTTOM|wxLEFT|wxRIGHT + 0 + + 1 + 1 + 1 + 1 + + + + + 0 + + + + + 1 + 0 + 1 + + 1 + + 0 + 0 + + Dock + 0 + Left + 0 1 1 diff --git a/eeschema/dialogs/dialog_symbol_fields_table_base.h b/eeschema/dialogs/dialog_symbol_fields_table_base.h index e0581e0876..23f9595a21 100644 --- a/eeschema/dialogs/dialog_symbol_fields_table_base.h +++ b/eeschema/dialogs/dialog_symbol_fields_table_base.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////// -// C++ code generated with wxFormBuilder (version Oct 26 2018) +// C++ code generated with wxFormBuilder (version 3.10.1-133-g388db8e4) // http://www.wxformbuilder.org/ // // PLEASE DO *NOT* EDIT THIS FILE! @@ -20,10 +20,10 @@ class WX_GRID; #include #include #include +#include #include #include #include -#include #include #include #include @@ -57,12 +57,13 @@ class DIALOG_SYMBOL_FIELDS_TABLE_BASE : public DIALOG_SHIM BITMAP_BUTTON* m_separator2; wxBitmapButton* m_bRefresh; WX_GRID* m_grid; + wxButton* m_buttonExport; wxButton* m_buttonApply; wxStdDialogButtonSizer* m_sdbSizer; wxButton* m_sdbSizerOK; wxButton* m_sdbSizerCancel; - // Virtual event handlers, overide them in your derived class + // Virtual event handlers, override them in your derived class virtual void OnClose( wxCloseEvent& event ) { event.Skip(); } virtual void OnColumnItemToggled( wxDataViewEvent& event ) { event.Skip(); } virtual void OnSizeFieldList( wxSizeEvent& event ) { event.Skip(); } @@ -76,6 +77,7 @@ class DIALOG_SYMBOL_FIELDS_TABLE_BASE : public DIALOG_SHIM virtual void OnTableCellClick( wxGridEvent& event ) { event.Skip(); } virtual void OnTableItemContextMenu( wxGridEvent& event ) { event.Skip(); } virtual void OnTableColSize( wxGridSizeEvent& event ) { event.Skip(); } + virtual void OnExport( wxCommandEvent& event ) { event.Skip(); } virtual void OnSaveAndContinue( wxCommandEvent& event ) { event.Skip(); } virtual void OnCancel( wxCommandEvent& event ) { event.Skip(); } @@ -83,6 +85,7 @@ class DIALOG_SYMBOL_FIELDS_TABLE_BASE : public DIALOG_SHIM public: DIALOG_SYMBOL_FIELDS_TABLE_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Symbol Fields Table"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( -1,-1 ), long style = wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxRESIZE_BORDER ); + ~DIALOG_SYMBOL_FIELDS_TABLE_BASE(); }; diff --git a/include/wildcards_and_files_ext.h b/include/wildcards_and_files_ext.h index 53a8d63bfb..098a90e153 100644 --- a/include/wildcards_and_files_ext.h +++ b/include/wildcards_and_files_ext.h @@ -160,6 +160,7 @@ extern const std::string WorkbookFileExtension; extern const std::string PngFileExtension; extern const std::string JpegFileExtension; extern const std::string TextFileExtension; +extern const std::string CsvFileExtension; bool IsProtelExtension( const wxString& ext ); diff --git a/pcbnew/build_BOM_from_board.cpp b/pcbnew/build_BOM_from_board.cpp index c9316f00a4..b7861d9bc2 100644 --- a/pcbnew/build_BOM_from_board.cpp +++ b/pcbnew/build_BOM_from_board.cpp @@ -53,8 +53,6 @@ * 12;"C1,C4,C5,C6";"CP6";4;"47uF";;; */ -const wxString CsvFileExtension( wxT( "csv" ) ); // BOM file extension - class BOM_ENTRY { public: