2020-08-13 17:51:26 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2021-02-24 13:48:02 +00:00
|
|
|
* Copyright (C) 2020-2021 CERN
|
2022-09-03 18:29:02 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-08-13 17:51:26 +00:00
|
|
|
*
|
|
|
|
* @author Wayne Stambaugh <stambaughw@gmail.com>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include <bitmaps.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h> // WildCompareString
|
2020-08-13 17:51:26 +00:00
|
|
|
#include <kiway.h>
|
|
|
|
#include <lib_id.h>
|
2021-04-18 15:23:15 +00:00
|
|
|
#include <refdes_utils.h>
|
2020-10-24 12:53:11 +00:00
|
|
|
#include <core/kicad_algo.h>
|
2020-08-13 17:51:26 +00:00
|
|
|
#include <dialog_change_symbols.h>
|
2021-02-24 13:48:02 +00:00
|
|
|
#include <sch_symbol.h>
|
2020-08-13 17:51:26 +00:00
|
|
|
#include <sch_edit_frame.h>
|
|
|
|
#include <sch_screen.h>
|
|
|
|
#include <sch_sheet_path.h>
|
|
|
|
#include <schematic.h>
|
|
|
|
#include <template_fieldnames.h>
|
2022-09-03 18:29:02 +00:00
|
|
|
#include <widgets/wx_html_report_panel.h>
|
2020-08-13 17:51:26 +00:00
|
|
|
|
2020-12-18 13:00:22 +00:00
|
|
|
bool g_selectRefDes = false;
|
|
|
|
bool g_selectValue = false;
|
2021-01-15 22:18:51 +00:00
|
|
|
// { change, update }
|
|
|
|
bool g_removeExtraFields[2] = { false, false };
|
|
|
|
bool g_resetEmptyFields[2] = { false, false };
|
2021-02-22 00:35:37 +00:00
|
|
|
bool g_resetFieldText[2] = { true, true };
|
2021-01-15 22:18:51 +00:00
|
|
|
bool g_resetFieldVisibilities[2] = { true, false };
|
|
|
|
bool g_resetFieldEffects[2] = { true, false };
|
|
|
|
bool g_resetFieldPositions[2] = { true, false };
|
|
|
|
bool g_resetAttributes[2] = { true, false };
|
2020-09-02 13:11:51 +00:00
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
DIALOG_CHANGE_SYMBOLS::DIALOG_CHANGE_SYMBOLS( SCH_EDIT_FRAME* aParent, SCH_SYMBOL* aSymbol,
|
2020-09-27 13:07:48 +00:00
|
|
|
MODE aMode ) :
|
2020-08-13 17:51:26 +00:00
|
|
|
DIALOG_CHANGE_SYMBOLS_BASE( aParent ),
|
|
|
|
m_symbol( aSymbol),
|
|
|
|
m_mode( aMode )
|
|
|
|
{
|
|
|
|
wxASSERT( aParent );
|
|
|
|
|
|
|
|
if( m_mode == MODE::UPDATE )
|
|
|
|
{
|
|
|
|
m_newIdSizer->Show( false );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-17 02:57:07 +00:00
|
|
|
m_matchAll->SetLabel( _( "Change all symbols in schematic" ) );
|
|
|
|
SetTitle( _( "Change Symbols" ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
m_matchSizer->FindItem( m_matchAll )->Show( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_symbol )
|
|
|
|
{
|
2020-09-06 12:05:07 +00:00
|
|
|
SCH_SHEET_PATH* currentSheet = &aParent->Schematic().CurrentSheet();
|
|
|
|
|
2020-10-17 02:57:07 +00:00
|
|
|
if( m_mode == MODE::CHANGE )
|
2021-06-17 22:34:19 +00:00
|
|
|
m_matchBySelection->SetLabel( _( "Change selected symbol(s)" ) );
|
2020-10-17 02:57:07 +00:00
|
|
|
|
2021-07-28 15:39:40 +00:00
|
|
|
m_newId->ChangeValue( UnescapeString( m_symbol->GetLibId().Format() ) );
|
2020-09-06 12:05:07 +00:00
|
|
|
m_specifiedReference->ChangeValue( m_symbol->GetRef( currentSheet ) );
|
2020-11-17 21:36:17 +00:00
|
|
|
m_specifiedValue->ChangeValue( m_symbol->GetValue( currentSheet, false ) );
|
2021-07-28 15:39:40 +00:00
|
|
|
m_specifiedId->ChangeValue( UnescapeString( m_symbol->GetLibId().Format() ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_matchSizer->FindItem( m_matchBySelection )->Show( false );
|
|
|
|
}
|
|
|
|
|
2021-03-08 02:59:07 +00:00
|
|
|
m_matchIdBrowserButton->SetBitmap( KiBitmap( BITMAPS::small_library ) );
|
|
|
|
m_newIdBrowserButton->SetBitmap( KiBitmap( BITMAPS::small_library ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
2020-10-17 02:57:07 +00:00
|
|
|
if( m_mode == MODE::CHANGE )
|
|
|
|
{
|
|
|
|
m_matchByReference->SetLabel( _( "Change symbols matching reference designator:" ) );
|
|
|
|
m_matchByValue->SetLabel( _( "Change symbols matching value:" ) );
|
|
|
|
m_matchById->SetLabel( _( "Change symbols matching library identifier:" ) );
|
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
m_matchSizer->SetEmptyCellSize( wxSize( 0, 0 ) );
|
|
|
|
m_matchSizer->Layout();
|
|
|
|
|
2020-09-27 13:07:48 +00:00
|
|
|
for( int i = 0; i < MANDATORY_FIELDS; ++i )
|
|
|
|
{
|
2022-05-12 11:05:06 +00:00
|
|
|
m_fieldsBox->Append( TEMPLATE_FIELDNAME::GetDefaultFieldName( i, DO_TRANSLATE ) );
|
2020-09-27 13:07:48 +00:00
|
|
|
|
2020-12-18 13:00:22 +00:00
|
|
|
if( i == REFERENCE_FIELD )
|
|
|
|
m_fieldsBox->Check( i, g_selectRefDes );
|
|
|
|
else if( i == VALUE_FIELD )
|
|
|
|
m_fieldsBox->Check( i, g_selectValue );
|
|
|
|
else
|
2020-09-27 13:07:48 +00:00
|
|
|
m_fieldsBox->Check( i, true );
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
m_messagePanel->SetLazyUpdate( true );
|
2021-02-25 15:05:26 +00:00
|
|
|
m_messagePanel->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
if( aSymbol && aSymbol->IsSelected() )
|
|
|
|
{
|
|
|
|
m_matchBySelection->SetValue( true );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aMode == MODE::UPDATE )
|
|
|
|
m_matchAll->SetValue( true );
|
|
|
|
else
|
|
|
|
m_matchByReference->SetValue( true );
|
|
|
|
}
|
|
|
|
|
2020-12-02 14:17:59 +00:00
|
|
|
updateFieldsList();
|
|
|
|
|
2020-10-17 02:57:07 +00:00
|
|
|
if( m_mode == MODE::CHANGE )
|
|
|
|
{
|
2020-12-02 14:23:03 +00:00
|
|
|
m_updateFieldsSizer->GetStaticBox()->SetLabel( _( "Update Fields" ) );
|
2020-10-17 02:57:07 +00:00
|
|
|
m_removeExtraBox->SetLabel( _( "Remove fields if not in new symbol" ) );
|
|
|
|
m_resetEmptyFields->SetLabel( _( "Reset fields if empty in new symbol" ) );
|
2021-02-22 00:35:37 +00:00
|
|
|
m_resetFieldText->SetLabel( _( "Update field text" ) );
|
2020-10-17 02:57:07 +00:00
|
|
|
m_resetFieldVisibilities->SetLabel( _( "Update field visibilities" ) );
|
|
|
|
m_resetFieldEffects->SetLabel( _( "Update field sizes and styles" ) );
|
|
|
|
m_resetFieldPositions->SetLabel( _( "Update field positions" ) );
|
2021-02-22 00:35:37 +00:00
|
|
|
m_resetAttributes->SetLabel( _( "Update symbol attributes" ) );
|
2020-10-17 02:57:07 +00:00
|
|
|
}
|
2020-09-15 22:52:53 +00:00
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
m_removeExtraBox->SetValue( g_removeExtraFields[ (int) m_mode ] );
|
|
|
|
m_resetEmptyFields->SetValue( g_resetEmptyFields[ (int) m_mode ] );
|
2021-02-22 00:35:37 +00:00
|
|
|
m_resetFieldText->SetValue( g_resetFieldText[ (int) m_mode ] );
|
2021-01-15 22:18:51 +00:00
|
|
|
m_resetFieldVisibilities->SetValue( g_resetFieldVisibilities[ (int) m_mode ] );
|
|
|
|
m_resetFieldEffects->SetValue( g_resetFieldEffects[ (int) m_mode ] );
|
|
|
|
m_resetFieldPositions->SetValue( g_resetFieldPositions[ (int) m_mode ] );
|
|
|
|
m_resetAttributes->SetValue( g_resetAttributes[ (int) m_mode ] );
|
2020-09-02 13:11:51 +00:00
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
// DIALOG_SHIM needs a unique hash_key because classname is not sufficient
|
|
|
|
// because the update and change versions of this dialog have different controls.
|
|
|
|
m_hash_key = TO_UTF8( GetTitle() );
|
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
wxString okLabel = m_mode == MODE::CHANGE ? _( "Change" ) : _( "Update" );
|
2020-10-17 02:57:07 +00:00
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
SetupStandardButtons( { { wxID_OK, okLabel },
|
|
|
|
{ wxID_CANCEL, _( "Close" ) } } );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
// Now all widgets have the size fixed, call FinishDialogSettings
|
2020-11-16 11:16:44 +00:00
|
|
|
finishDialogSettings();
|
2020-08-13 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-27 13:07:48 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchByAll( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
updateFieldsList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchBySelected( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
updateFieldsList();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchByReference( wxCommandEvent& aEvent )
|
|
|
|
{
|
2020-09-27 13:07:48 +00:00
|
|
|
updateFieldsList();
|
2020-08-13 17:51:26 +00:00
|
|
|
m_specifiedReference->SetFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchByValue( wxCommandEvent& aEvent )
|
|
|
|
{
|
2020-09-27 13:07:48 +00:00
|
|
|
updateFieldsList();
|
2020-08-13 17:51:26 +00:00
|
|
|
m_specifiedValue->SetFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchById( wxCommandEvent& aEvent )
|
|
|
|
{
|
2020-09-27 13:07:48 +00:00
|
|
|
updateFieldsList();
|
2020-08-13 17:51:26 +00:00
|
|
|
m_specifiedId->SetFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-27 13:07:48 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchTextKillFocus( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
updateFieldsList();
|
2021-10-27 15:17:17 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2020-09-27 13:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-28 19:37:51 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::onMatchIDKillFocus( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
updateFieldsList();
|
2021-10-27 15:17:17 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2021-06-28 19:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_CHANGE_SYMBOLS::onNewLibIDKillFocus( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
updateFieldsList();
|
2021-10-27 15:17:17 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2021-06-28 19:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
DIALOG_CHANGE_SYMBOLS::~DIALOG_CHANGE_SYMBOLS()
|
|
|
|
{
|
2020-12-18 13:00:22 +00:00
|
|
|
g_selectRefDes = m_fieldsBox->IsChecked( REFERENCE_FIELD );
|
|
|
|
g_selectValue = m_fieldsBox->IsChecked( VALUE_FIELD );
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
g_removeExtraFields[ (int) m_mode ] = m_removeExtraBox->GetValue();
|
|
|
|
g_resetEmptyFields[ (int) m_mode ] = m_resetEmptyFields->GetValue();
|
2021-02-22 00:35:37 +00:00
|
|
|
g_resetFieldText[ (int) m_mode ] = m_resetFieldText->GetValue();
|
2021-01-15 22:18:51 +00:00
|
|
|
g_resetFieldVisibilities[ (int) m_mode ] = m_resetFieldVisibilities->GetValue();
|
|
|
|
g_resetFieldEffects[ (int) m_mode ] = m_resetFieldEffects->GetValue();
|
|
|
|
g_resetFieldPositions[ (int) m_mode ] = m_resetFieldPositions->GetValue();
|
|
|
|
g_resetAttributes[ (int) m_mode ] = m_resetAttributes->GetValue();
|
2020-08-13 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-28 15:39:40 +00:00
|
|
|
wxString getLibIdValue( const wxTextCtrl* aCtrl )
|
|
|
|
{
|
|
|
|
wxString rawValue = aCtrl->GetValue();
|
|
|
|
wxString itemName;
|
|
|
|
wxString libName = rawValue.BeforeFirst( ':', &itemName );
|
|
|
|
|
|
|
|
return EscapeString( libName, CTX_LIBID ) + ':' + EscapeString( itemName, CTX_LIBID );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::launchMatchIdSymbolBrowser( wxCommandEvent& aEvent )
|
|
|
|
{
|
2021-07-28 15:39:40 +00:00
|
|
|
wxString newName = getLibIdValue( m_specifiedId );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
KIWAY_PLAYER* frame = Kiway().Player( FRAME_SCH_VIEWER_MODAL, true );
|
|
|
|
|
|
|
|
if( frame->ShowModal( &newName, this ) )
|
2020-09-27 13:07:48 +00:00
|
|
|
{
|
2021-07-28 15:39:40 +00:00
|
|
|
m_specifiedId->SetValue( UnescapeString( newName ) );
|
2020-09-27 13:07:48 +00:00
|
|
|
updateFieldsList();
|
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
frame->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_CHANGE_SYMBOLS::launchNewIdSymbolBrowser( wxCommandEvent& aEvent )
|
|
|
|
{
|
2021-07-28 15:39:40 +00:00
|
|
|
wxString newName = getLibIdValue( m_newId );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
KIWAY_PLAYER* frame = Kiway().Player( FRAME_SCH_VIEWER_MODAL, true );
|
|
|
|
|
|
|
|
if( frame->ShowModal( &newName, this ) )
|
2020-09-27 13:07:48 +00:00
|
|
|
{
|
2021-07-28 15:39:40 +00:00
|
|
|
m_newId->SetValue( UnescapeString( newName ) );
|
2020-09-27 13:07:48 +00:00
|
|
|
updateFieldsList();
|
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
frame->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-27 13:07:48 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::updateFieldsList()
|
|
|
|
{
|
|
|
|
SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
|
2020-12-16 23:54:53 +00:00
|
|
|
|
|
|
|
wxCHECK( frame, /* void */ );
|
|
|
|
|
2020-09-27 13:07:48 +00:00
|
|
|
SCH_SHEET_LIST hierarchy = frame->Schematic().GetSheets();
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// Load non-mandatory fields from all matching symbols and their library symbols
|
2020-12-15 15:06:19 +00:00
|
|
|
std::vector<SCH_FIELD*> fields;
|
|
|
|
std::vector<LIB_FIELD*> libFields;
|
|
|
|
std::set<wxString> fieldNames;
|
2020-09-27 13:07:48 +00:00
|
|
|
|
|
|
|
for( SCH_SHEET_PATH& instance : hierarchy )
|
|
|
|
{
|
|
|
|
SCH_SCREEN* screen = instance.LastScreen();
|
|
|
|
|
|
|
|
wxCHECK2( screen, continue );
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
|
2020-09-27 13:07:48 +00:00
|
|
|
{
|
2021-06-10 14:10:55 +00:00
|
|
|
SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item );
|
2020-09-27 13:07:48 +00:00
|
|
|
|
|
|
|
wxCHECK2( symbol, continue );
|
|
|
|
|
|
|
|
if( !isMatch( symbol, &instance ) )
|
|
|
|
continue;
|
|
|
|
|
2021-01-04 00:32:19 +00:00
|
|
|
fields.clear();
|
2020-12-15 15:06:19 +00:00
|
|
|
symbol->GetFields( fields, false );
|
2020-09-27 13:07:48 +00:00
|
|
|
|
2020-12-02 14:17:59 +00:00
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < fields.size(); ++i )
|
2020-12-15 15:06:19 +00:00
|
|
|
fieldNames.insert( fields[i]->GetName() );
|
|
|
|
|
2021-02-22 00:35:37 +00:00
|
|
|
if( m_mode == MODE::UPDATE && symbol->GetLibId().IsValid() )
|
2020-12-15 15:06:19 +00:00
|
|
|
{
|
2021-06-17 21:22:10 +00:00
|
|
|
LIB_SYMBOL* libSymbol = frame->GetLibSymbol( symbol->GetLibId() );
|
2020-12-16 17:58:55 +00:00
|
|
|
|
2021-02-22 00:35:37 +00:00
|
|
|
if( libSymbol )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
|
2020-12-15 15:06:19 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
flattenedSymbol->GetFields( libFields );
|
2020-12-16 17:58:55 +00:00
|
|
|
|
2021-02-22 00:35:37 +00:00
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
|
|
|
|
fieldNames.insert( libFields[i]->GetName() );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
libFields.clear(); // flattenedSymbol is about to go out of scope...
|
2021-02-22 00:35:37 +00:00
|
|
|
}
|
2020-12-15 15:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// Load non-mandatory fields from the change-to library symbol
|
2020-12-15 15:06:19 +00:00
|
|
|
if( m_mode == MODE::CHANGE )
|
|
|
|
{
|
|
|
|
LIB_ID newId;
|
|
|
|
|
2021-07-28 15:39:40 +00:00
|
|
|
newId.Parse( getLibIdValue( m_newId ) );
|
2020-12-15 15:06:19 +00:00
|
|
|
|
|
|
|
if( newId.IsValid() )
|
|
|
|
{
|
2021-06-17 21:22:10 +00:00
|
|
|
LIB_SYMBOL* libSymbol = frame->GetLibSymbol( newId );
|
2020-12-15 15:06:19 +00:00
|
|
|
|
|
|
|
if( libSymbol )
|
|
|
|
{
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
|
2020-12-15 15:06:19 +00:00
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
flattenedSymbol->GetFields( libFields );
|
2020-12-15 15:06:19 +00:00
|
|
|
|
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
|
|
|
|
fieldNames.insert( libFields[i]->GetName() );
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
libFields.clear(); // flattenedSymbol is about to go out of scope...
|
2020-12-15 15:06:19 +00:00
|
|
|
}
|
2020-09-27 13:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the listbox widget
|
|
|
|
for( unsigned i = m_fieldsBox->GetCount() - 1; i >= MANDATORY_FIELDS; --i )
|
|
|
|
m_fieldsBox->Delete( i );
|
|
|
|
|
|
|
|
for( const wxString& fieldName : fieldNames )
|
|
|
|
m_fieldsBox->Append( fieldName );
|
|
|
|
|
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < m_fieldsBox->GetCount(); ++i )
|
|
|
|
m_fieldsBox->Check( i, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_CHANGE_SYMBOLS::checkAll( bool aCheck )
|
|
|
|
{
|
|
|
|
for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
|
|
|
|
m_fieldsBox->Check( i, aCheck );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
void DIALOG_CHANGE_SYMBOLS::onOkButtonClicked( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
wxBusyCursor dummy;
|
|
|
|
SCH_EDIT_FRAME* parent = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
|
|
|
|
|
|
|
|
wxCHECK( parent, /* void */ );
|
|
|
|
|
|
|
|
m_messagePanel->Clear();
|
|
|
|
m_messagePanel->Flush( false );
|
|
|
|
|
2021-11-13 12:46:54 +00:00
|
|
|
// Create the set of fields to be updated. Use non translated (canonical) names
|
|
|
|
// for mandatory fields
|
2020-09-27 13:07:48 +00:00
|
|
|
m_updateFields.clear();
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
|
|
|
|
{
|
|
|
|
if( m_fieldsBox->IsChecked( i ) )
|
2021-11-13 12:46:54 +00:00
|
|
|
{
|
|
|
|
if( i < MANDATORY_FIELDS )
|
|
|
|
{
|
|
|
|
LIB_FIELD dummy_field( i );
|
|
|
|
m_updateFields.insert( dummy_field.GetCanonicalName() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_updateFields.insert( m_fieldsBox->GetString( i ) );
|
|
|
|
}
|
2020-09-27 13:07:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
if( processMatchingSymbols() )
|
|
|
|
{
|
|
|
|
parent->TestDanglingEnds(); // This will also redraw the changed symbols.
|
|
|
|
parent->OnModify();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_messagePanel->Flush( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
bool DIALOG_CHANGE_SYMBOLS::isMatch( SCH_SYMBOL* aSymbol, SCH_SHEET_PATH* aInstance )
|
2020-08-13 17:51:26 +00:00
|
|
|
{
|
|
|
|
LIB_ID id;
|
|
|
|
|
|
|
|
wxCHECK( aSymbol, false );
|
|
|
|
|
|
|
|
SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
|
|
|
|
|
|
|
|
wxCHECK( frame, false );
|
|
|
|
|
|
|
|
if( m_matchAll->GetValue() )
|
2020-09-06 12:05:07 +00:00
|
|
|
{
|
2020-08-13 17:51:26 +00:00
|
|
|
return true;
|
2020-09-06 12:05:07 +00:00
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
else if( m_matchBySelection->GetValue() )
|
2020-09-06 12:05:07 +00:00
|
|
|
{
|
2021-06-17 22:34:19 +00:00
|
|
|
return aSymbol == m_symbol || aSymbol->IsSelected();
|
2020-09-06 12:05:07 +00:00
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
else if( m_matchByReference->GetValue() )
|
2020-09-06 12:05:07 +00:00
|
|
|
{
|
2020-11-17 21:36:17 +00:00
|
|
|
return WildCompareString( m_specifiedReference->GetValue(),
|
|
|
|
aSymbol->GetRef( aInstance, false ), false );
|
2020-09-06 12:05:07 +00:00
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
else if( m_matchByValue->GetValue() )
|
2020-09-06 12:05:07 +00:00
|
|
|
{
|
2020-11-17 21:36:17 +00:00
|
|
|
return WildCompareString( m_specifiedValue->GetValue(),
|
|
|
|
aSymbol->GetValue( aInstance, false ), false );
|
2020-09-06 12:05:07 +00:00
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
else if( m_matchById )
|
|
|
|
{
|
2021-07-28 15:39:40 +00:00
|
|
|
id.Parse( getLibIdValue( m_specifiedId ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
return aSymbol->GetLibId() == id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_CHANGE_SYMBOLS::processMatchingSymbols()
|
|
|
|
{
|
|
|
|
SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
|
|
|
|
|
|
|
|
wxCHECK( frame, false );
|
|
|
|
|
|
|
|
LIB_ID newId;
|
|
|
|
bool appendToUndo = false;
|
|
|
|
bool changed = false;
|
|
|
|
SCH_SHEET_LIST hierarchy = frame->Schematic().GetSheets();
|
|
|
|
|
|
|
|
if( m_mode == MODE::CHANGE )
|
|
|
|
{
|
2021-07-28 15:39:40 +00:00
|
|
|
newId.Parse( getLibIdValue( m_newId ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
if( !newId.IsValid() )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( SCH_SHEET_PATH& instance : hierarchy )
|
|
|
|
{
|
|
|
|
SCH_SCREEN* screen = instance.LastScreen();
|
|
|
|
|
|
|
|
wxCHECK2( screen, continue );
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
std::vector<SCH_SYMBOL*> symbols;
|
2020-08-13 17:51:26 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_ITEM* item : screen->Items().OfType( SCH_SYMBOL_T ) )
|
|
|
|
symbols.push_back( static_cast<SCH_SYMBOL*>( item ) );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
for( SCH_SYMBOL* symbol : symbols )
|
2020-12-02 17:32:59 +00:00
|
|
|
{
|
2020-08-13 17:51:26 +00:00
|
|
|
if( !isMatch( symbol, &instance ) )
|
|
|
|
continue;
|
|
|
|
|
2020-11-23 21:23:40 +00:00
|
|
|
if( m_mode == MODE::UPDATE )
|
|
|
|
{
|
|
|
|
if( processSymbol( symbol, &instance, symbol->GetLibId(), appendToUndo ) )
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( processSymbol( symbol, &instance, newId, appendToUndo ) )
|
|
|
|
changed = true;
|
|
|
|
}
|
2020-08-18 18:13:16 +00:00
|
|
|
|
2020-11-23 21:23:40 +00:00
|
|
|
if( changed )
|
|
|
|
appendToUndo = true;
|
2020-08-13 17:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 21:23:40 +00:00
|
|
|
frame->GetCurrentSheet().UpdateAllScreenReferences();
|
|
|
|
|
2020-08-13 17:51:26 +00:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-10 14:10:55 +00:00
|
|
|
bool DIALOG_CHANGE_SYMBOLS::processSymbol( SCH_SYMBOL* aSymbol, const SCH_SHEET_PATH* aInstance,
|
2020-11-23 21:23:40 +00:00
|
|
|
const LIB_ID& aNewId, bool aAppendToUndo )
|
2020-08-13 17:51:26 +00:00
|
|
|
{
|
|
|
|
wxCHECK( aSymbol, false );
|
|
|
|
wxCHECK( aNewId.IsValid(), false );
|
|
|
|
|
|
|
|
SCH_EDIT_FRAME* frame = dynamic_cast<SCH_EDIT_FRAME*>( GetParent() );
|
2020-11-23 21:23:40 +00:00
|
|
|
SCH_SCREEN* screen = aInstance->LastScreen();
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
wxCHECK( frame, false );
|
|
|
|
|
|
|
|
LIB_ID oldId = aSymbol->GetLibId();
|
|
|
|
wxString msg;
|
|
|
|
wxString references;
|
|
|
|
|
2020-11-15 17:03:27 +00:00
|
|
|
for( SYMBOL_INSTANCE_REFERENCE instance : aSymbol->GetInstanceReferences() )
|
2020-08-13 17:51:26 +00:00
|
|
|
{
|
|
|
|
if( references.IsEmpty() )
|
|
|
|
references = instance.m_Reference;
|
|
|
|
else
|
|
|
|
references += " " + instance.m_Reference;
|
|
|
|
}
|
|
|
|
|
2020-11-23 21:23:40 +00:00
|
|
|
if( m_mode == MODE::UPDATE )
|
|
|
|
{
|
|
|
|
if( aSymbol->GetInstanceReferences().size() == 1 )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Update symbol %s from '%s' to '%s'" ),
|
|
|
|
references,
|
2021-07-28 15:39:40 +00:00
|
|
|
UnescapeString( oldId.Format() ),
|
|
|
|
UnescapeString( aNewId.Format() ) );
|
2020-11-23 21:23:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Update symbols %s from '%s' to '%s'" ),
|
|
|
|
references,
|
2021-07-28 15:39:40 +00:00
|
|
|
UnescapeString( oldId.Format() ),
|
|
|
|
UnescapeString( aNewId.Format() ) );
|
2020-11-23 21:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( aSymbol->GetInstanceReferences().size() == 1 )
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Change symbol %s from '%s' to '%s'" ),
|
|
|
|
references,
|
2021-07-28 15:39:40 +00:00
|
|
|
UnescapeString( oldId.Format() ),
|
|
|
|
UnescapeString( aNewId.Format() ) );
|
2020-11-23 21:23:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg.Printf( _( "Change symbols %s from '%s' to '%s'" ),
|
|
|
|
references,
|
2021-07-28 15:39:40 +00:00
|
|
|
UnescapeString( oldId.Format() ),
|
|
|
|
UnescapeString( aNewId.Format() ) );
|
2020-11-23 21:23:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 17:51:26 +00:00
|
|
|
|
2021-06-17 21:22:10 +00:00
|
|
|
LIB_SYMBOL* libSymbol = frame->GetLibSymbol( aNewId );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
if( !libSymbol )
|
|
|
|
{
|
|
|
|
msg << ": " << _( "*** symbol not found ***" );
|
|
|
|
m_messagePanel->Report( msg, RPT_SEVERITY_ERROR );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unique_ptr<LIB_SYMBOL> flattenedSymbol = libSymbol->Flatten();
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
if( flattenedSymbol->GetUnitCount() < aSymbol->GetUnit() )
|
|
|
|
{
|
|
|
|
msg << ": " << _( "*** new symbol has too few units ***" );
|
|
|
|
m_messagePanel->Report( msg, RPT_SEVERITY_ERROR );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
// Removing the symbol needs to be done before the LIB_SYMBOL is changed to prevent stale
|
2020-08-13 17:51:26 +00:00
|
|
|
// library symbols in the schematic file.
|
2020-11-23 21:23:40 +00:00
|
|
|
screen->Remove( aSymbol );
|
|
|
|
frame->SaveCopyInUndoList( screen, aSymbol, UNDO_REDO::CHANGED, aAppendToUndo );
|
2020-08-13 17:51:26 +00:00
|
|
|
|
|
|
|
if( aNewId != aSymbol->GetLibId() )
|
|
|
|
aSymbol->SetLibId( aNewId );
|
|
|
|
|
|
|
|
aSymbol->SetLibSymbol( flattenedSymbol.release() );
|
2020-09-02 13:11:51 +00:00
|
|
|
|
2021-01-18 15:15:20 +00:00
|
|
|
if( m_resetAttributes->GetValue() )
|
2021-01-15 22:18:51 +00:00
|
|
|
{
|
|
|
|
aSymbol->SetIncludeInBom( libSymbol->GetIncludeInBom() );
|
|
|
|
aSymbol->SetIncludeOnBoard( libSymbol->GetIncludeOnBoard() );
|
|
|
|
}
|
|
|
|
|
2020-09-02 13:11:51 +00:00
|
|
|
bool removeExtras = m_removeExtraBox->GetValue();
|
|
|
|
bool resetVis = m_resetFieldVisibilities->GetValue();
|
|
|
|
bool resetEffects = m_resetFieldEffects->GetValue();
|
|
|
|
bool resetPositions = m_resetFieldPositions->GetValue();
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < aSymbol->GetFields().size(); ++i )
|
|
|
|
{
|
2021-02-28 13:28:23 +00:00
|
|
|
SCH_FIELD& field = aSymbol->GetFields()[i];
|
2020-09-02 13:11:51 +00:00
|
|
|
LIB_FIELD* libField = nullptr;
|
|
|
|
|
2021-11-11 09:56:09 +00:00
|
|
|
// Mandatory fields always exist in m_updateFields, but these names can be translated.
|
2021-11-13 12:46:54 +00:00
|
|
|
// so use GetCanonicalName().
|
|
|
|
if( !alg::contains( m_updateFields, field.GetCanonicalName() ) )
|
2020-09-27 13:07:48 +00:00
|
|
|
continue;
|
|
|
|
|
2020-09-02 13:11:51 +00:00
|
|
|
if( i < MANDATORY_FIELDS )
|
2021-02-28 13:28:23 +00:00
|
|
|
libField = libSymbol->GetFieldById( (int) i );
|
2020-09-02 13:11:51 +00:00
|
|
|
else
|
2021-02-28 13:28:23 +00:00
|
|
|
libField = libSymbol->FindField( field.GetName() );
|
2020-09-02 13:11:51 +00:00
|
|
|
|
|
|
|
if( libField )
|
|
|
|
{
|
2021-02-22 00:35:37 +00:00
|
|
|
bool resetText = libField->GetText().IsEmpty() ? m_resetEmptyFields->GetValue()
|
|
|
|
: m_resetFieldText->GetValue();
|
|
|
|
|
|
|
|
if( resetText )
|
2020-10-17 23:44:38 +00:00
|
|
|
{
|
2020-11-23 21:23:40 +00:00
|
|
|
if( i == REFERENCE_FIELD )
|
2021-04-18 15:23:15 +00:00
|
|
|
aSymbol->SetRef( aInstance, UTIL::GetRefDesUnannotated( libField->GetText() ) );
|
2020-11-23 21:23:40 +00:00
|
|
|
else if( i == VALUE_FIELD )
|
2021-06-30 10:53:04 +00:00
|
|
|
aSymbol->SetValue( aInstance, UnescapeString( libField->GetText() ) );
|
2020-11-23 21:23:40 +00:00
|
|
|
else if( i == FOOTPRINT_FIELD )
|
|
|
|
aSymbol->SetFootprint( aInstance, libField->GetText() );
|
|
|
|
else
|
2021-02-28 13:28:23 +00:00
|
|
|
field.SetText( libField->GetText() );
|
2020-10-17 23:44:38 +00:00
|
|
|
}
|
2020-09-02 13:11:51 +00:00
|
|
|
|
|
|
|
if( resetVis )
|
2021-02-28 13:28:23 +00:00
|
|
|
field.SetVisible( libField->IsVisible() );
|
2020-09-02 13:11:51 +00:00
|
|
|
|
|
|
|
if( resetEffects )
|
|
|
|
{
|
2021-12-28 22:13:54 +00:00
|
|
|
// Careful: the visible bit and position are also set by SetAttributes()
|
2022-01-01 06:04:08 +00:00
|
|
|
bool visible = field.IsVisible();
|
|
|
|
VECTOR2I pos = field.GetPosition();
|
2021-02-14 18:29:27 +00:00
|
|
|
|
2021-12-28 22:13:54 +00:00
|
|
|
field.SetAttributes( *libField );
|
2021-02-14 18:29:27 +00:00
|
|
|
|
2021-02-28 13:28:23 +00:00
|
|
|
field.SetVisible( visible );
|
|
|
|
field.SetPosition( pos );
|
2020-09-02 13:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( resetPositions )
|
2022-01-01 06:04:08 +00:00
|
|
|
field.SetTextPos( aSymbol->GetPosition() + libField->GetTextPos() );
|
2020-09-02 13:11:51 +00:00
|
|
|
}
|
|
|
|
else if( i >= MANDATORY_FIELDS && removeExtras )
|
|
|
|
{
|
2021-02-28 13:28:23 +00:00
|
|
|
aSymbol->RemoveField( field.GetName() );
|
2020-09-02 13:11:51 +00:00
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-15 15:06:19 +00:00
|
|
|
std::vector<LIB_FIELD*> libFields;
|
2020-09-02 13:11:51 +00:00
|
|
|
libSymbol->GetFields( libFields );
|
|
|
|
|
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
|
|
|
|
{
|
2020-12-15 15:06:19 +00:00
|
|
|
const LIB_FIELD& libField = *libFields[i];
|
2020-09-02 13:11:51 +00:00
|
|
|
|
2021-11-13 12:46:54 +00:00
|
|
|
if( !alg::contains( m_updateFields, libField.GetCanonicalName() ) )
|
2020-09-27 13:07:48 +00:00
|
|
|
continue;
|
|
|
|
|
2020-09-02 13:11:51 +00:00
|
|
|
if( !aSymbol->FindField( libField.GetName(), false ) )
|
|
|
|
{
|
|
|
|
wxString fieldName = libField.GetCanonicalName();
|
2022-09-11 22:13:37 +00:00
|
|
|
SCH_FIELD newField( VECTOR2I( 0, 0 ), aSymbol->GetFieldCount(), aSymbol, fieldName );
|
2020-09-02 13:11:51 +00:00
|
|
|
SCH_FIELD* schField = aSymbol->AddField( newField );
|
|
|
|
|
2021-12-28 22:13:54 +00:00
|
|
|
// Careful: the visible bit and position are also set by SetAttributes()
|
|
|
|
schField->SetAttributes( libField );
|
2020-09-02 13:11:51 +00:00
|
|
|
schField->SetText( libField.GetText() );
|
2022-01-01 06:04:08 +00:00
|
|
|
schField->SetTextPos( aSymbol->GetPosition() + libField.GetTextPos() );
|
2020-09-02 13:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 14:11:13 +00:00
|
|
|
if( resetPositions && frame->eeconfig()->m_AutoplaceFields.enable )
|
|
|
|
aSymbol->AutoAutoplaceFields( screen );
|
|
|
|
|
2021-03-04 22:59:24 +00:00
|
|
|
aSymbol->SetSchSymbolLibraryName( wxEmptyString );
|
2020-11-23 21:23:40 +00:00
|
|
|
screen->Append( aSymbol );
|
2020-08-13 17:51:26 +00:00
|
|
|
frame->GetCanvas()->GetView()->Update( aSymbol );
|
|
|
|
|
|
|
|
msg += ": OK";
|
|
|
|
m_messagePanel->Report( msg, RPT_SEVERITY_ACTION );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|