From cd9d81d720c1741564359c89850db254d56635bd Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 23 Nov 2017 19:48:49 +0100 Subject: [PATCH] dialog_edit_components_libid: add a tool to remap orphan components. Orphan components are components that have a link (lib_id) pointing to a non existent symbol in any loaded libraries. --- .../dialogs/dialog_edit_components_libid.cpp | 74 +++++++++++++++- .../dialog_edit_components_libid_base.cpp | 7 ++ .../dialog_edit_components_libid_base.fbp | 88 +++++++++++++++++++ .../dialog_edit_components_libid_base.h | 2 + 4 files changed, 167 insertions(+), 4 deletions(-) diff --git a/eeschema/dialogs/dialog_edit_components_libid.cpp b/eeschema/dialogs/dialog_edit_components_libid.cpp index 7630743bba..d440fc734c 100644 --- a/eeschema/dialogs/dialog_edit_components_libid.cpp +++ b/eeschema/dialogs/dialog_edit_components_libid.cpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include #define COL_REFS 0 #define COL_CURR_LIBID 1 @@ -85,11 +87,12 @@ class DIALOG_EDIT_COMPONENTS_LIBID : public DIALOG_EDIT_COMPONENTS_LIBID_BASE public: DIALOG_EDIT_COMPONENTS_LIBID( SCH_EDIT_FRAME* aParent ); - bool IsSchelaticModified() { return m_isModified; } + bool IsSchematicModified() { return m_isModified; } private: SCH_EDIT_FRAME* m_parent; bool m_isModified; // set to true if the schematic is modified + std::vector m_OrphansRowIndexes; // list of rows containing orphan lib_id std::vector m_components; @@ -138,6 +141,9 @@ private: // Undo all changes, and clear the list of new lib_ids void onUndoChangesButton( wxCommandEvent& event ) override; + // Try to find a candidate for non existing symbols + void onClickOrphansButton( wxCommandEvent& event ) override; + // UI event, to enable/disable buttons void updateUIChangesButton( wxUpdateUIEvent& event ) override { @@ -234,7 +240,7 @@ void DIALOG_EDIT_COMPONENTS_LIBID::initDlg() // (can be 0 if the symbol is not found) int unit = candidate.m_Component->GetUnitSelection( &sheetpath ); int unitcount = candidate.m_Component->GetUnitCount(); - candidate.m_IsOrphan = unitcount == 0; + candidate.m_IsOrphan = ( unitcount == 0 ); if( unitcount > 1 || unit > 1 ) { @@ -255,7 +261,7 @@ void DIALOG_EDIT_COMPONENTS_LIBID::initDlg() wxString last_str_libid = m_components.front().GetStringLibId(); int row = 0; wxString refs; - bool mark_cell = false; + bool mark_cell = m_components.front().m_IsOrphan; CMP_CANDIDATE* cmp = nullptr; for( unsigned ii = 0; ii < m_components.size(); ii++ ) @@ -300,12 +306,17 @@ void DIALOG_EDIT_COMPONENTS_LIBID::initDlg() // Allows only the selection by row m_grid->SetSelectionMode( wxGrid::wxGridSelectRows ); + + m_buttonOrphanItems->Enable( m_OrphansRowIndexes.size() > 0 ); } void DIALOG_EDIT_COMPONENTS_LIBID::AddRowToGrid( int aRowId, bool aMarkRow, const wxString& aReferences, const wxString& aStrLibId ) { + if( aMarkRow ) // a orphan component exists, set m_AsOrphanCmp as true + m_OrphansRowIndexes.push_back( aRowId ); + int row = aRowId; if( m_grid->GetNumberRows() <= row ) @@ -401,13 +412,68 @@ void DIALOG_EDIT_COMPONENTS_LIBID::onButtonBrowseLibraries( wxCommandEvent& even } +void DIALOG_EDIT_COMPONENTS_LIBID::onClickOrphansButton( wxCommandEvent& event ) +{ + std::vector< wxString > libs = Prj().SchSymbolLibTable()->GetLogicalLibs(); + wxArrayString aliasNames; + + unsigned fixesCount = 0; + + // Try to find a candidate for non existing symbols in any loaded library + for( unsigned ii = 0; ii < m_OrphansRowIndexes.size(); ii++ ) + { + wxString orphanLibid = m_grid->GetCellValue( m_OrphansRowIndexes[ii], COL_CURR_LIBID ); + + LIB_ID curr_libid( orphanLibid ); + wxString symbName = curr_libid.GetLibItemName(); + + // now try to fin a candidate + for( auto &lib : libs ) + { + aliasNames.Clear(); + + try + { + Prj().SchSymbolLibTable()->EnumerateSymbolLib( lib, aliasNames ); + } + catch( const IO_ERROR& e ) {} // ignore, it is handled below + + if( aliasNames.IsEmpty() ) + continue; + + // Find a symbol name in symbols inside this library: + int index = aliasNames.Index( symbName ); + + if( index != wxNOT_FOUND ) + { + // a candidate is found! + wxString newLibid = lib + ':' + symbName; + m_grid->SetCellValue( m_OrphansRowIndexes[ii], COL_NEW_LIBID, newLibid ); + fixesCount++; + break; + } + } + } + + if( fixesCount < m_OrphansRowIndexes.size() ) // Not all orphan components are fixed + { + wxMessageBox( wxString::Format( _( "%u link(s) mapped, %d not found" ), + fixesCount, m_OrphansRowIndexes.size() - fixesCount ) ); + } + else + wxMessageBox( wxString::Format( _( "All %u link(s) resolved" ), fixesCount ) ); +} + + bool DIALOG_EDIT_COMPONENTS_LIBID::setLibIdByBrowser( int aRow ) { #if 0 + // Use dialog symbol selector to choose a symbol SCH_BASE_FRAME::HISTORY_LIST dummy; SCH_BASE_FRAME::COMPONENT_SELECTION sel = m_parent->SelectComponentFromLibrary( NULL, dummy, true, 0, 0 ); #else + // Use library viewer to choose a symbol LIB_ID aPreselectedLibid; SCH_BASE_FRAME::COMPONENT_SELECTION sel = m_parent->SelectComponentFromLibBrowser( NULL, aPreselectedLibid, 0, 0 ); @@ -510,5 +576,5 @@ bool InvokeDialogEditComponentsLibId( SCH_EDIT_FRAME* aCaller ) DIALOG_EDIT_COMPONENTS_LIBID dlg( aCaller ); dlg.ShowModal(); - return dlg.IsSchelaticModified(); + return dlg.IsSchematicModified(); } \ No newline at end of file diff --git a/eeschema/dialogs/dialog_edit_components_libid_base.cpp b/eeschema/dialogs/dialog_edit_components_libid_base.cpp index 78b8dde747..89451f0398 100644 --- a/eeschema/dialogs/dialog_edit_components_libid_base.cpp +++ b/eeschema/dialogs/dialog_edit_components_libid_base.cpp @@ -95,6 +95,11 @@ DIALOG_EDIT_COMPONENTS_LIBID_BASE::DIALOG_EDIT_COMPONENTS_LIBID_BASE( wxWindow* m_buttonBrowseLibs = new wxButton( this, wxID_ANY, _("Browse Libraries"), wxDefaultPosition, wxDefaultSize, 0 ); bSizerButtons->Add( m_buttonBrowseLibs, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); + m_buttonOrphanItems = new wxButton( this, wxID_ANY, _("Map Orphans"), wxDefaultPosition, wxDefaultSize, 0 ); + m_buttonOrphanItems->SetToolTip( _("If some components are orphan (the linked symbol is found nowhere),\ntry to find a candidate having the same name in one of loaded symbol libraries") ); + + bSizerButtons->Add( m_buttonOrphanItems, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); + bSizerMain->Add( bSizerButtons, 0, wxALIGN_RIGHT, 5 ); @@ -115,6 +120,7 @@ DIALOG_EDIT_COMPONENTS_LIBID_BASE::DIALOG_EDIT_COMPONENTS_LIBID_BASE( wxWindow* m_buttonUndo->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::updateUIChangesButton ), NULL, this ); m_buttonBrowseLibs->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onButtonBrowseLibraries ), NULL, this ); m_buttonBrowseLibs->Connect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::updateUIBrowseButton ), NULL, this ); + m_buttonOrphanItems->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onClickOrphansButton ), NULL, this ); } DIALOG_EDIT_COMPONENTS_LIBID_BASE::~DIALOG_EDIT_COMPONENTS_LIBID_BASE() @@ -130,5 +136,6 @@ DIALOG_EDIT_COMPONENTS_LIBID_BASE::~DIALOG_EDIT_COMPONENTS_LIBID_BASE() m_buttonUndo->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::updateUIChangesButton ), NULL, this ); m_buttonBrowseLibs->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onButtonBrowseLibraries ), NULL, this ); m_buttonBrowseLibs->Disconnect( wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::updateUIBrowseButton ), NULL, this ); + m_buttonOrphanItems->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_EDIT_COMPONENTS_LIBID_BASE::onClickOrphansButton ), NULL, this ); } diff --git a/eeschema/dialogs/dialog_edit_components_libid_base.fbp b/eeschema/dialogs/dialog_edit_components_libid_base.fbp index ac764c204a..2ddd6d935b 100644 --- a/eeschema/dialogs/dialog_edit_components_libid_base.fbp +++ b/eeschema/dialogs/dialog_edit_components_libid_base.fbp @@ -708,6 +708,94 @@ updateUIBrowseButton + + 5 + wxALL|wxALIGN_CENTER_VERTICAL + 0 + + 1 + 1 + 1 + 1 + + + + + + + + 1 + 0 + 1 + + 1 + 0 + 0 + Dock + 0 + Left + 1 + + 1 + + 0 + 0 + wxID_ANY + Map Orphans + + 0 + + + 0 + + 1 + m_buttonOrphanItems + 1 + + + protected + 1 + + Resizable + 1 + + + + 0 + If some components are orphan (the linked symbol is found nowhere), try to find a candidate having the same name in one of loaded symbol libraries + + wxFILTER_NONE + wxDefaultValidator + + + + + onClickOrphansButton + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/eeschema/dialogs/dialog_edit_components_libid_base.h b/eeschema/dialogs/dialog_edit_components_libid_base.h index 8485c666d8..3a9b951266 100644 --- a/eeschema/dialogs/dialog_edit_components_libid_base.h +++ b/eeschema/dialogs/dialog_edit_components_libid_base.h @@ -48,6 +48,7 @@ class DIALOG_EDIT_COMPONENTS_LIBID_BASE : public DIALOG_SHIM wxButton* m_sdbSizerCancel; wxButton* m_buttonUndo; wxButton* m_buttonBrowseLibs; + wxButton* m_buttonOrphanItems; // Virtual event handlers, overide them in your derived class virtual void onCellBrowseLib( wxGridEvent& event ) { event.Skip(); } @@ -57,6 +58,7 @@ class DIALOG_EDIT_COMPONENTS_LIBID_BASE : public DIALOG_SHIM virtual void updateUIChangesButton( wxUpdateUIEvent& event ) { event.Skip(); } virtual void onButtonBrowseLibraries( wxCommandEvent& event ) { event.Skip(); } virtual void updateUIBrowseButton( wxUpdateUIEvent& event ) { event.Skip(); } + virtual void onClickOrphansButton( wxCommandEvent& event ) { event.Skip(); } public: