2020-01-29 16:33:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Alexander Shuklin <Jasuramme@gmail.com>
|
2020-06-08 18:36:25 +00:00
|
|
|
* Copyright (C) 2004-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-01-29 16:33:57 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <backannotate.h>
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <dsnlexer.h>
|
|
|
|
#include <ptree.h>
|
|
|
|
#include <reporter.h>
|
|
|
|
#include <sch_edit_frame.h>
|
|
|
|
#include <sch_sheet_path.h>
|
2020-05-13 02:00:37 +00:00
|
|
|
#include <schematic.h>
|
2020-10-14 11:03:58 +00:00
|
|
|
#include <kicad_string.h>
|
2020-06-08 18:36:25 +00:00
|
|
|
#include <kiface_i.h>
|
2020-07-10 16:59:06 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-07-16 17:31:36 +00:00
|
|
|
BACK_ANNOTATE::BACK_ANNOTATE( SCH_EDIT_FRAME* aFrame, REPORTER& aReporter, bool aRelinkFootprints,
|
2020-06-08 18:36:25 +00:00
|
|
|
bool aProcessFootprints, bool aProcessValues,
|
|
|
|
bool aProcessReferences, bool aProcessNetNames,
|
2020-09-30 22:32:30 +00:00
|
|
|
bool aDryRun ) :
|
2020-06-08 18:36:25 +00:00
|
|
|
m_reporter( aReporter ),
|
2020-07-16 17:31:36 +00:00
|
|
|
m_matchByReference( aRelinkFootprints ),
|
2020-06-08 18:36:25 +00:00
|
|
|
m_processFootprints( aProcessFootprints ),
|
|
|
|
m_processValues( aProcessValues ),
|
|
|
|
m_processReferences( aProcessReferences ),
|
|
|
|
m_processNetNames( aProcessNetNames ),
|
|
|
|
m_dryRun( aDryRun ),
|
2020-03-25 13:13:03 +00:00
|
|
|
m_frame( aFrame ),
|
2020-07-13 11:21:40 +00:00
|
|
|
m_changesCount( 0 ),
|
|
|
|
m_appendUndo( false )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BACK_ANNOTATE::~BACK_ANNOTATE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BACK_ANNOTATE::BackAnnotateSymbols( const std::string& aNetlist )
|
|
|
|
{
|
|
|
|
m_changesCount = 0;
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = false;
|
2020-01-29 16:33:57 +00:00
|
|
|
wxString msg;
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
if( !m_processValues && !m_processFootprints && !m_processReferences && !m_processNetNames )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
m_reporter.ReportTail( _( "Select at least one property to back annotate." ),
|
|
|
|
RPT_SEVERITY_ERROR );
|
2020-01-29 16:33:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
getPcbModulesFromString( aNetlist );
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-05-13 02:00:37 +00:00
|
|
|
SCH_SHEET_LIST sheets = m_frame->Schematic().GetSheets();
|
2020-01-29 16:33:57 +00:00
|
|
|
sheets.GetComponents( m_refs, false );
|
|
|
|
sheets.GetMultiUnitComponents( m_multiUnitsRefs );
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
getChangeList();
|
|
|
|
checkForUnusedSymbols();
|
2020-01-29 16:33:57 +00:00
|
|
|
|
|
|
|
applyChangelist();
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
return true;
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BACK_ANNOTATE::FetchNetlistFromPCB( std::string& aNetlist )
|
|
|
|
{
|
|
|
|
if( Kiface().IsSingle() )
|
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
DisplayErrorMessage( m_frame, _( "Cannot fetch PCB netlist because eeschema is opened "
|
|
|
|
"in stand-alone mode.\n"
|
|
|
|
"You must launch the KiCad project manager and create "
|
|
|
|
"a project." ) );
|
2020-01-29 16:33:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-10 16:59:06 +00:00
|
|
|
KIWAY_PLAYER* frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, false );
|
|
|
|
|
|
|
|
if( !frame )
|
|
|
|
{
|
|
|
|
wxFileName fn( m_frame->Prj().GetProjectFullName() );
|
|
|
|
fn.SetExt( PcbFileExtension );
|
|
|
|
|
|
|
|
frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, true );
|
|
|
|
frame->OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ) );
|
|
|
|
}
|
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
m_frame->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_PCB_GET_NETLIST, aNetlist );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-16 17:31:36 +00:00
|
|
|
void BACK_ANNOTATE::PushNewLinksToPCB()
|
|
|
|
{
|
|
|
|
std::string nullPayload;
|
|
|
|
|
|
|
|
m_frame->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_PCB_UPDATE_LINKS, nullPayload );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
void BACK_ANNOTATE::getPcbModulesFromString( const std::string& aPayload )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
auto getStr = []( const PTREE& pt ) -> wxString
|
|
|
|
{
|
|
|
|
return UTF8( pt.front().first );
|
|
|
|
};
|
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
DSNLEXER lexer( aPayload, FROM_UTF8( __func__ ) );
|
|
|
|
PTREE doc;
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
// NOTE: KiCad's PTREE scanner constructs a property *name* tree, not a property tree.
|
|
|
|
// Every token in the s-expr is stored as a property name; the property's value is then
|
|
|
|
// either the nested s-exprs or an empty PTREE; there are *no* literal property values.
|
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
Scan( &doc, &lexer );
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
PTREE& tree = doc.get_child( "pcb_netlist" );
|
|
|
|
wxString msg;
|
2020-01-29 16:33:57 +00:00
|
|
|
m_pcbModules.clear();
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
for( const std::pair<const std::string, PTREE>& item : tree )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
|
|
|
wxString path, value, footprint;
|
2020-06-08 18:36:25 +00:00
|
|
|
std::map<wxString, wxString> pinNetMap;
|
2020-01-29 16:33:57 +00:00
|
|
|
wxASSERT( item.first == "ref" );
|
2020-06-08 18:36:25 +00:00
|
|
|
wxString ref = getStr( item.second );
|
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
try
|
|
|
|
{
|
2020-07-16 17:31:36 +00:00
|
|
|
if( m_matchByReference )
|
|
|
|
path = ref;
|
|
|
|
else
|
|
|
|
path = getStr( item.second.get_child( "timestamp" ) );
|
2020-01-29 16:33:57 +00:00
|
|
|
|
|
|
|
if( path == "" )
|
|
|
|
{
|
2020-02-01 12:29:46 +00:00
|
|
|
msg.Printf( _( "Footprint \"%s\" has no symbol associated." ), ref );
|
2020-06-08 18:36:25 +00:00
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_WARNING );
|
2020-01-29 16:33:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
footprint = getStr( item.second.get_child( "fpid" ) );
|
|
|
|
value = getStr( item.second.get_child( "value" ) );
|
|
|
|
|
|
|
|
boost::optional<const PTREE&> nets = item.second.get_child_optional( "nets" );
|
|
|
|
|
|
|
|
if( nets )
|
|
|
|
{
|
|
|
|
for( const std::pair<const std::string, PTREE>& pin_net : nets.get() )
|
|
|
|
{
|
|
|
|
wxASSERT( pin_net.first == "pin_net" );
|
|
|
|
wxString pinNumber = UTF8( pin_net.second.front().first );
|
|
|
|
wxString netName = UTF8( pin_net.second.back().first );
|
|
|
|
pinNetMap[ pinNumber ] = netName;
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxLogWarning( "Cannot parse PCB netlist for back-annotation" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use lower_bound for not to iterate over map twice
|
|
|
|
auto nearestItem = m_pcbModules.lower_bound( path );
|
|
|
|
|
|
|
|
if( nearestItem != m_pcbModules.end() && nearestItem->first == path )
|
|
|
|
{
|
|
|
|
// Module with this path already exists - generate error
|
2020-02-01 12:29:46 +00:00
|
|
|
msg.Printf( _( "Pcb footprints \"%s\" and \"%s\" linked to same symbol" ),
|
2020-06-08 18:36:25 +00:00
|
|
|
nearestItem->second->m_ref, ref );
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add module to the map
|
2020-06-08 18:36:25 +00:00
|
|
|
auto data = std::make_shared<PCB_MODULE_DATA>( ref, footprint, value, pinNetMap );
|
|
|
|
m_pcbModules.insert( nearestItem, std::make_pair( path, data ) );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
void BACK_ANNOTATE::getChangeList()
|
|
|
|
{
|
|
|
|
for( std::pair<const wxString, std::shared_ptr<PCB_MODULE_DATA>>& module : m_pcbModules )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
const wxString& pcbPath = module.first;
|
|
|
|
auto& pcbData = module.second;
|
2020-07-16 17:31:36 +00:00
|
|
|
int refIndex;
|
2020-06-08 18:36:25 +00:00
|
|
|
bool foundInMultiunit = false;
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-07-16 17:31:36 +00:00
|
|
|
for( std::pair<const wxString, SCH_REFERENCE_LIST>& item : m_multiUnitsRefs )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
SCH_REFERENCE_LIST& refList = item.second;
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-07-16 17:31:36 +00:00
|
|
|
if( m_matchByReference )
|
|
|
|
refIndex = refList.FindRef( pcbPath );
|
|
|
|
else
|
|
|
|
refIndex = refList.FindRefByPath( pcbPath );
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-07-16 17:31:36 +00:00
|
|
|
if( refIndex >= 0 )
|
|
|
|
{
|
2020-01-29 16:33:57 +00:00
|
|
|
// If module linked to multi unit symbol, we add all symbol's units to
|
|
|
|
// the change list
|
|
|
|
foundInMultiunit = true;
|
2020-07-10 16:59:06 +00:00
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
for( size_t i = 0; i < refList.GetCount(); ++i )
|
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
refList[i].GetComp()->ClearFlags( SKIP_STRUCT );
|
|
|
|
m_changelist.emplace_back( CHANGELIST_ITEM( refList[i], pcbData ) );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
2020-06-08 18:36:25 +00:00
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( foundInMultiunit )
|
|
|
|
continue;
|
|
|
|
|
2020-07-16 17:31:36 +00:00
|
|
|
if( m_matchByReference )
|
|
|
|
refIndex = m_refs.FindRef( pcbPath );
|
|
|
|
else
|
|
|
|
refIndex = m_refs.FindRefByPath( pcbPath );
|
2020-01-29 16:33:57 +00:00
|
|
|
|
|
|
|
if( refIndex >= 0 )
|
2020-06-08 18:36:25 +00:00
|
|
|
{
|
|
|
|
m_refs[refIndex].GetComp()->ClearFlags( SKIP_STRUCT );
|
|
|
|
m_changelist.emplace_back( CHANGELIST_ITEM( m_refs[refIndex], pcbData ) );
|
|
|
|
}
|
2020-01-29 16:33:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Haven't found linked symbol in multiunits or common refs. Generate error
|
|
|
|
wxString msg;
|
2020-06-08 18:36:25 +00:00
|
|
|
msg.Printf( _( "Cannot find symbol for \"%s\" footprint" ), pcbData->m_ref );
|
|
|
|
m_reporter.ReportTail( msg, RPT_SEVERITY_ERROR );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
void BACK_ANNOTATE::checkForUnusedSymbols()
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
2020-03-07 07:43:30 +00:00
|
|
|
m_refs.SortByTimeStamp();
|
|
|
|
|
2020-03-25 13:13:03 +00:00
|
|
|
std::sort( m_changelist.begin(), m_changelist.end(),
|
|
|
|
[]( const CHANGELIST_ITEM& a, const CHANGELIST_ITEM& b )
|
|
|
|
{
|
|
|
|
return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
|
|
|
|
} );
|
2020-01-29 16:33:57 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
2020-06-08 18:36:25 +00:00
|
|
|
|
2020-01-29 16:33:57 +00:00
|
|
|
for( auto& item : m_changelist )
|
|
|
|
{
|
|
|
|
// Refs and changelist are both sorted by paths, so we just go over m_refs and
|
|
|
|
// generate errors before we will find m_refs member to which item linked
|
|
|
|
while( i < m_refs.GetCount() && m_refs[i].GetPath() != item.first.GetPath() )
|
|
|
|
{
|
|
|
|
wxString msg;
|
2020-02-01 12:29:46 +00:00
|
|
|
msg.Printf( _( "Cannot find footprint for \"%s\" symbol" ), m_refs[i++].GetFullRef() );
|
2020-06-08 18:36:25 +00:00
|
|
|
m_reporter.ReportTail( msg, RPT_SEVERITY_ERROR );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
2020-07-16 17:31:36 +00:00
|
|
|
|
|
|
|
if( m_matchByReference && !m_frame->ReadyToNetlist() )
|
|
|
|
{
|
|
|
|
m_reporter.ReportTail( _( "Cannot relink footprints because schematic is not fully annotated" ),
|
|
|
|
RPT_SEVERITY_ERROR );
|
|
|
|
}
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BACK_ANNOTATE::applyChangelist()
|
|
|
|
{
|
2020-06-08 18:36:25 +00:00
|
|
|
std::set<wxString> handledNetChanges;
|
|
|
|
wxString msg;
|
2020-01-29 16:33:57 +00:00
|
|
|
|
|
|
|
// Apply changes from change list
|
2020-06-08 18:36:25 +00:00
|
|
|
for( CHANGELIST_ITEM& item : m_changelist )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
|
|
|
SCH_REFERENCE& ref = item.first;
|
|
|
|
PCB_MODULE_DATA& module = *item.second;
|
2020-07-13 11:21:40 +00:00
|
|
|
SCH_COMPONENT* comp = ref.GetComp();
|
|
|
|
SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
|
2020-08-29 13:41:47 +00:00
|
|
|
wxString oldFootprint = ref.GetFootprint();
|
|
|
|
wxString oldValue = ref.GetValue();
|
2020-06-08 18:36:25 +00:00
|
|
|
bool skip = ( ref.GetComp()->GetFlags() & SKIP_STRUCT ) > 0;
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
if( m_processReferences && ref.GetRef() != module.m_ref && !skip )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
|
|
|
++m_changesCount;
|
2020-06-08 18:36:25 +00:00
|
|
|
msg.Printf( _( "Change \"%s\" reference designator to \"%s\"." ),
|
|
|
|
ref.GetFullRef(),
|
|
|
|
module.m_ref );
|
|
|
|
|
|
|
|
if( !m_dryRun )
|
2020-07-13 11:21:40 +00:00
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( screen, comp, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
|
|
|
comp->SetRef( &ref.GetSheetPath(), module.m_ref );
|
|
|
|
}
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
if( m_processFootprints && oldFootprint != module.m_footprint && !skip )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
|
|
|
++m_changesCount;
|
2020-06-08 18:36:25 +00:00
|
|
|
msg.Printf( _( "Change %s footprint from \"%s\" to \"%s\"." ),
|
|
|
|
ref.GetFullRef(),
|
2020-08-29 13:41:47 +00:00
|
|
|
oldFootprint,
|
2020-06-08 18:36:25 +00:00
|
|
|
module.m_footprint );
|
2020-01-29 16:33:57 +00:00
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
if( !m_dryRun )
|
2020-07-13 11:21:40 +00:00
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( screen, comp, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
2020-08-29 13:41:47 +00:00
|
|
|
comp->SetFootprint( &ref.GetSheetPath(), module.m_footprint );
|
2020-07-13 11:21:40 +00:00
|
|
|
}
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
if( m_processValues && oldValue != module.m_value && !skip )
|
2020-01-29 16:33:57 +00:00
|
|
|
{
|
|
|
|
++m_changesCount;
|
2020-06-08 18:36:25 +00:00
|
|
|
msg.Printf( _( "Change %s value from \"%s\" to \"%s\"." ),
|
|
|
|
ref.GetFullRef(),
|
2020-08-29 13:41:47 +00:00
|
|
|
oldValue,
|
2020-06-08 18:36:25 +00:00
|
|
|
module.m_value );
|
|
|
|
|
|
|
|
if( !m_dryRun )
|
2020-07-13 11:21:40 +00:00
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( screen, comp, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
2020-08-29 13:41:47 +00:00
|
|
|
comp->SetValue( &ref.GetSheetPath(), module.m_value );
|
2020-07-13 11:21:40 +00:00
|
|
|
}
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_processNetNames )
|
|
|
|
{
|
|
|
|
for( const std::pair<const wxString, wxString>& entry : module.m_pinMap )
|
|
|
|
{
|
|
|
|
const wxString& pinNumber = entry.first;
|
|
|
|
const wxString& shortNetName = entry.second;
|
2020-08-21 15:54:24 +00:00
|
|
|
SCH_PIN* pin = comp->GetPin( pinNumber );
|
2020-10-07 14:40:12 +00:00
|
|
|
SCH_CONNECTION* conn = pin->Connection( &ref.GetSheetPath() );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
wxString key = shortNetName + ref.GetSheetPath().PathAsString();
|
|
|
|
|
|
|
|
if( handledNetChanges.count( key ) )
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
handledNetChanges.insert( key );
|
|
|
|
|
|
|
|
if( conn && conn->Name( true ) != shortNetName )
|
|
|
|
processNetNameChange( conn, conn->Name( true ), shortNetName );
|
|
|
|
}
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 11:21:40 +00:00
|
|
|
|
2020-08-13 21:30:30 +00:00
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
|
|
|
m_frame->RecalculateConnections( NO_CLEANUP );
|
|
|
|
m_frame->UpdateNetHighlightStatus();
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_INFO );
|
2020-01-29 16:33:57 +00:00
|
|
|
}
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
|
2020-07-19 00:52:34 +00:00
|
|
|
static LABEL_SPIN_STYLE orientLabel( SCH_PIN* aPin )
|
|
|
|
{
|
|
|
|
LABEL_SPIN_STYLE spin = LABEL_SPIN_STYLE::RIGHT;
|
|
|
|
|
|
|
|
// Initial orientation from the pin
|
|
|
|
switch( aPin->GetLibPin()->GetOrientation() )
|
|
|
|
{
|
|
|
|
case PIN_UP: spin = LABEL_SPIN_STYLE::BOTTOM; break;
|
|
|
|
case PIN_DOWN: spin = LABEL_SPIN_STYLE::UP; break;
|
|
|
|
case PIN_LEFT: spin = LABEL_SPIN_STYLE::LEFT; break;
|
|
|
|
case PIN_RIGHT: spin = LABEL_SPIN_STYLE::RIGHT; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reorient based on the actual component orientation now
|
|
|
|
struct ORIENT
|
|
|
|
{
|
|
|
|
int flag;
|
|
|
|
int n_rots;
|
|
|
|
int mirror_x;
|
|
|
|
int mirror_y;
|
|
|
|
}
|
|
|
|
orientations[] =
|
|
|
|
{
|
|
|
|
{ CMP_ORIENT_0, 0, 0, 0 },
|
|
|
|
{ CMP_ORIENT_90, 1, 0, 0 },
|
|
|
|
{ CMP_ORIENT_180, 2, 0, 0 },
|
|
|
|
{ CMP_ORIENT_270, 3, 0, 0 },
|
|
|
|
{ CMP_MIRROR_X + CMP_ORIENT_0, 0, 1, 0 },
|
|
|
|
{ CMP_MIRROR_X + CMP_ORIENT_90, 1, 1, 0 },
|
|
|
|
{ CMP_MIRROR_Y, 0, 0, 1 },
|
|
|
|
{ CMP_MIRROR_X + CMP_ORIENT_270, 3, 1, 0 },
|
|
|
|
{ CMP_MIRROR_Y + CMP_ORIENT_0, 0, 0, 1 },
|
|
|
|
{ CMP_MIRROR_Y + CMP_ORIENT_90, 1, 0, 1 },
|
|
|
|
{ CMP_MIRROR_Y + CMP_ORIENT_180, 2, 0, 1 },
|
|
|
|
{ CMP_MIRROR_Y + CMP_ORIENT_270, 3, 0, 1 }
|
|
|
|
};
|
|
|
|
|
|
|
|
ORIENT o = orientations[ 0 ];
|
|
|
|
|
|
|
|
SCH_COMPONENT* comp = aPin->GetParentComponent();
|
|
|
|
|
|
|
|
if( !comp )
|
|
|
|
return spin;
|
|
|
|
|
|
|
|
int compOrient = comp->GetOrientation();
|
|
|
|
|
|
|
|
for( auto& i : orientations )
|
|
|
|
{
|
|
|
|
if( i.flag == compOrient )
|
|
|
|
{
|
|
|
|
o = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( int i = 0; i < o.n_rots; i++ )
|
|
|
|
spin = spin.RotateCCW();
|
|
|
|
|
|
|
|
if( o.mirror_x )
|
|
|
|
spin = spin.MirrorX();
|
|
|
|
|
|
|
|
if( o.mirror_y )
|
|
|
|
spin = spin.MirrorY();
|
|
|
|
|
|
|
|
return spin;
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:36:25 +00:00
|
|
|
void BACK_ANNOTATE::processNetNameChange( SCH_CONNECTION* aConn, const wxString& aOldName,
|
|
|
|
const wxString& aNewName )
|
|
|
|
{
|
|
|
|
wxString msg;
|
|
|
|
SCH_ITEM* driver = aConn->Driver();
|
|
|
|
|
|
|
|
auto editMatchingLabels =
|
|
|
|
[this]( SCH_SCREEN* aScreen, KICAD_T aType, const wxString& oldName,
|
|
|
|
const wxString& newName )
|
|
|
|
{
|
|
|
|
for( SCH_ITEM* schItem : aScreen->Items().OfType( aType ) )
|
|
|
|
{
|
|
|
|
SCH_TEXT* label = static_cast<SCH_TEXT*>( schItem );
|
|
|
|
|
|
|
|
if( EscapeString( label->GetShownText(), CTX_NETNAME ) == oldName )
|
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( aScreen, label, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
2020-06-08 18:36:25 +00:00
|
|
|
static_cast<SCH_TEXT*>( label )->SetText( newName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
switch( driver->Type() )
|
|
|
|
{
|
|
|
|
case SCH_LABEL_T:
|
|
|
|
++m_changesCount;
|
2020-07-13 11:21:40 +00:00
|
|
|
msg.Printf( _( "Change \"%s\" labels to \"%s\"." ), aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
2020-10-07 14:40:12 +00:00
|
|
|
SCH_SHEET_PATH sheet = aConn->Sheet();
|
|
|
|
SCH_SCREEN* screen = sheet.LastScreen();
|
2020-06-08 18:36:25 +00:00
|
|
|
|
2020-07-13 11:21:40 +00:00
|
|
|
for( SCH_ITEM* label : screen->Items().OfType( SCH_LABEL_T ) )
|
2020-06-08 18:36:25 +00:00
|
|
|
{
|
2020-10-07 14:40:12 +00:00
|
|
|
SCH_CONNECTION* conn = label->Connection( &sheet );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
if( conn && conn->Driver() == driver )
|
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( screen, label, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
2020-06-08 18:36:25 +00:00
|
|
|
static_cast<SCH_TEXT*>( label )->SetText( aNewName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCH_GLOBAL_LABEL_T:
|
|
|
|
++m_changesCount;
|
2020-07-13 11:21:40 +00:00
|
|
|
msg.Printf( _( "Change \"%s\" global labels to \"%s\"." ), aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
2020-07-13 11:21:40 +00:00
|
|
|
for( const SCH_SHEET_PATH& sheet : m_frame->Schematic().GetSheets() )
|
|
|
|
editMatchingLabels( sheet.LastScreen(), SCH_GLOBAL_LABEL_T, aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCH_HIER_LABEL_T:
|
|
|
|
++m_changesCount;
|
2020-07-13 11:21:40 +00:00
|
|
|
msg.Printf( _( "Change \"%s\" hierarchical label to \"%s\"." ), aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
2020-07-13 11:21:40 +00:00
|
|
|
SCH_SCREEN* screen = aConn->Sheet().LastScreen();
|
|
|
|
editMatchingLabels( screen, SCH_HIER_LABEL_T, aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
2020-07-13 11:21:40 +00:00
|
|
|
SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( driver->GetParent() );
|
2020-07-29 07:21:31 +00:00
|
|
|
wxASSERT( sheet );
|
|
|
|
|
|
|
|
if( !sheet )
|
|
|
|
break;
|
|
|
|
|
2020-07-13 11:21:40 +00:00
|
|
|
screen = sheet->GetScreen();
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
for( SCH_SHEET_PIN* pin : sheet->GetPins() )
|
|
|
|
{
|
|
|
|
if( EscapeString( pin->GetShownText(), CTX_NETNAME ) == aOldName )
|
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( screen, pin, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
2020-06-08 18:36:25 +00:00
|
|
|
static_cast<SCH_TEXT*>( pin )->SetText( aNewName );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SCH_SHEET_PIN_T:
|
|
|
|
++m_changesCount;
|
2020-07-13 11:21:40 +00:00
|
|
|
msg.Printf( _( "Change \"%s\" hierarchical label to \"%s\"." ), aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
2020-07-13 11:21:40 +00:00
|
|
|
SCH_SCREEN* screen = aConn->Sheet().LastScreen();
|
2020-08-26 18:04:32 +00:00
|
|
|
m_frame->SaveCopyInUndoList( screen, driver, UNDO_REDO::CHANGED, m_appendUndo );
|
2020-07-13 11:21:40 +00:00
|
|
|
m_appendUndo = true;
|
2020-06-08 18:36:25 +00:00
|
|
|
static_cast<SCH_TEXT*>( driver )->SetText( aNewName );
|
|
|
|
|
|
|
|
SCH_SHEET* sheet = static_cast<SCH_SHEET_PIN*>( driver )->GetParent();
|
2020-07-13 11:21:40 +00:00
|
|
|
screen = sheet->GetScreen();
|
|
|
|
editMatchingLabels( screen, SCH_HIER_LABEL_T, aOldName, aNewName );
|
2020-06-08 18:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
|
|
|
break;
|
|
|
|
|
2020-07-19 00:52:34 +00:00
|
|
|
case SCH_PIN_T:
|
2020-06-08 18:36:25 +00:00
|
|
|
{
|
2020-07-19 00:52:34 +00:00
|
|
|
SCH_PIN* schPin = static_cast<SCH_PIN*>( driver );
|
|
|
|
LABEL_SPIN_STYLE spin = orientLabel( schPin );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
2020-07-19 00:52:34 +00:00
|
|
|
if( schPin->IsPowerConnection() )
|
2020-06-08 18:36:25 +00:00
|
|
|
{
|
|
|
|
msg.Printf( _( "Net \"%s\" cannot be changed to \"%s\" because it "
|
|
|
|
"is driven by a power pin." ),
|
|
|
|
aOldName,
|
|
|
|
aNewName );
|
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ERROR );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++m_changesCount;
|
2020-07-13 11:21:40 +00:00
|
|
|
msg.Printf( _( "Add label \"%s\" to net \"%s\"." ), aNewName, aOldName );
|
2020-06-08 18:36:25 +00:00
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
|
|
|
SCHEMATIC_SETTINGS& settings = m_frame->Schematic().Settings();
|
|
|
|
SCH_LABEL* label = new SCH_LABEL( driver->GetPosition(), aNewName );
|
|
|
|
label->SetParent( &m_frame->Schematic() );
|
|
|
|
label->SetTextSize( wxSize( settings.m_DefaultTextSize,
|
|
|
|
settings.m_DefaultTextSize ) );
|
|
|
|
label->SetLabelSpinStyle( spin );
|
|
|
|
label->SetFlags( IS_NEW );
|
|
|
|
|
2020-07-13 11:21:40 +00:00
|
|
|
SCH_SCREEN* screen = aConn->Sheet().LastScreen();
|
|
|
|
m_frame->AddItemToScreenAndUndoList( screen, label, m_appendUndo );
|
|
|
|
m_appendUndo = true;
|
2020-06-08 18:36:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_reporter.ReportHead( msg, RPT_SEVERITY_ACTION );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-07-19 00:52:34 +00:00
|
|
|
}
|