2020-09-25 18:26:58 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-02-13 11:54:41 +00:00
|
|
|
* Copyright (C) 2004-2022 KiCad Developers.
|
2020-09-25 18:26:58 +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 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 <cstdio>
|
|
|
|
#include <memory>
|
|
|
|
#include <reporter.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2023-08-21 14:26:03 +00:00
|
|
|
#include <pcbexpr_evaluator.h>
|
2020-09-25 18:26:58 +00:00
|
|
|
|
|
|
|
#include <connectivity/connectivity_data.h>
|
|
|
|
#include <connectivity/connectivity_algo.h>
|
|
|
|
|
|
|
|
#include <connectivity/from_to_cache.h>
|
|
|
|
|
|
|
|
void FROM_TO_CACHE::buildEndpointList( )
|
|
|
|
{
|
|
|
|
m_ftEndpoints.clear();
|
|
|
|
|
2020-11-13 15:15:52 +00:00
|
|
|
for( FOOTPRINT* footprint : m_board->Footprints() )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
2020-11-12 23:50:33 +00:00
|
|
|
for( PAD* pad : footprint->Pads() )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
FT_ENDPOINT ent;
|
2022-02-04 22:44:59 +00:00
|
|
|
ent.name = footprint->GetReference() + wxT( "-" ) + pad->GetNumber();
|
2020-09-25 18:26:58 +00:00
|
|
|
ent.parent = pad;
|
|
|
|
m_ftEndpoints.push_back( ent );
|
2020-11-12 23:50:33 +00:00
|
|
|
ent.name = footprint->GetReference();
|
2020-09-25 18:26:58 +00:00
|
|
|
ent.parent = pad;
|
|
|
|
m_ftEndpoints.push_back( ent );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum PATH_STATUS {
|
|
|
|
PS_OK = 0,
|
|
|
|
PS_MULTIPLE_PATHS = -1,
|
|
|
|
PS_NO_PATH = -2
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static bool isVertexVisited( CN_ITEM* v, const std::vector<CN_ITEM*>& path )
|
|
|
|
{
|
2022-08-15 17:47:23 +00:00
|
|
|
for( CN_ITEM* u : path )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
if ( u == v )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PATH_STATUS uniquePathBetweenNodes( CN_ITEM* u, CN_ITEM* v, std::vector<CN_ITEM*>& outPath )
|
|
|
|
{
|
|
|
|
using Path = std::vector<CN_ITEM*>;
|
|
|
|
std::deque<Path> Q;
|
|
|
|
|
2020-09-27 15:49:54 +00:00
|
|
|
Path pInit;
|
2021-02-01 00:38:20 +00:00
|
|
|
bool pathFound = false;
|
2020-09-27 15:49:54 +00:00
|
|
|
pInit.push_back( u );
|
|
|
|
Q.push_back( pInit );
|
2020-09-25 18:26:58 +00:00
|
|
|
|
|
|
|
while( Q.size() )
|
|
|
|
{
|
|
|
|
Path path = Q.front();
|
|
|
|
Q.pop_front();
|
2020-09-27 15:49:54 +00:00
|
|
|
CN_ITEM* last = path.back();
|
2020-09-25 18:26:58 +00:00
|
|
|
|
|
|
|
if( last == v )
|
|
|
|
{
|
|
|
|
outPath = path;
|
2022-02-13 11:54:41 +00:00
|
|
|
|
2020-09-25 18:26:58 +00:00
|
|
|
if( pathFound )
|
|
|
|
return PS_MULTIPLE_PATHS;
|
2022-02-13 11:54:41 +00:00
|
|
|
|
2020-09-25 18:26:58 +00:00
|
|
|
pathFound = true;
|
|
|
|
}
|
|
|
|
|
2022-08-15 17:47:23 +00:00
|
|
|
for( CN_ITEM* ci : last->ConnectedItems() )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
2020-09-27 15:49:54 +00:00
|
|
|
bool vertexVisited = isVertexVisited( ci, path );
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2022-08-15 17:47:23 +00:00
|
|
|
for( std::vector<CN_ITEM*>& p : Q )
|
2020-09-27 15:49:54 +00:00
|
|
|
if( isVertexVisited( ci, p ) )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
vertexVisited = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-09-27 15:49:54 +00:00
|
|
|
if( !vertexVisited )
|
|
|
|
{
|
|
|
|
Path newpath( path );
|
|
|
|
newpath.push_back( ci );
|
|
|
|
Q.push_back( newpath );
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pathFound ? PS_OK : PS_NO_PATH;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
int FROM_TO_CACHE::cacheFromToPaths( const wxString& aFrom, const wxString& aTo )
|
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
std::vector<FT_PATH> paths;
|
|
|
|
std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
|
|
|
|
std::shared_ptr<CN_CONNECTIVITY_ALGO> cnAlgo = connectivity->GetConnectivityAlgo();
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( FT_ENDPOINT& endpoint : m_ftEndpoints )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
if( WildCompareString( aFrom, endpoint.name, false ) )
|
|
|
|
{
|
|
|
|
FT_PATH p;
|
|
|
|
p.net = endpoint.parent->GetNetCode();
|
|
|
|
p.from = endpoint.parent;
|
|
|
|
p.to = nullptr;
|
|
|
|
paths.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( FT_PATH& path : paths )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
2023-06-24 18:54:50 +00:00
|
|
|
wxString fromName = path.from->GetParentFootprint()->GetReference()
|
|
|
|
+ wxT( "-" ) + path.from->GetNumber();
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2022-03-03 23:49:05 +00:00
|
|
|
auto padCandidates = connectivity->GetConnectedItems( path.from,
|
2023-06-24 18:54:50 +00:00
|
|
|
{ PCB_PAD_T, PCB_ARC_T, PCB_VIA_T,
|
|
|
|
PCB_TRACE_T } );
|
2020-11-12 22:30:02 +00:00
|
|
|
PAD* toPad = nullptr;
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( BOARD_CONNECTED_ITEM* pitem : padCandidates )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
if( pitem == path.from )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( pitem->Type() != PCB_PAD_T )
|
|
|
|
continue;
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
const PAD *pad = static_cast<const PAD*>( pitem );
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2023-06-24 18:54:50 +00:00
|
|
|
wxString toName = pad->GetParentFootprint()->GetReference()
|
|
|
|
+ wxT( "-" ) + pad->GetNumber();
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( const FT_ENDPOINT& endpoint : m_ftEndpoints )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
if( pad == endpoint.parent )
|
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
if( WildCompareString( aTo, endpoint.name, false ) )
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
toPad = endpoint.parent;
|
|
|
|
|
|
|
|
path.to = toPad;
|
|
|
|
path.fromName = fromName;
|
|
|
|
path.toName = toName;
|
|
|
|
path.fromWildcard = aFrom;
|
|
|
|
path.toWildcard = aTo;
|
|
|
|
|
|
|
|
if( count >= 2 )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
// fixme: report this somewhere?
|
|
|
|
path.to = nullptr;
|
2020-09-25 18:26:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-13 11:54:41 +00:00
|
|
|
}
|
2020-09-25 18:26:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int newPaths = 0;
|
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
for( FT_PATH& path : paths )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
if( !path.from || !path.to )
|
|
|
|
continue;
|
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
CN_ITEM* cnFrom = cnAlgo->ItemEntry( path.from ).GetItems().front();
|
|
|
|
CN_ITEM* cnTo = cnAlgo->ItemEntry( path.to ).GetItems().front();
|
|
|
|
std::vector<CN_ITEM*> upath;
|
2020-09-25 18:26:58 +00:00
|
|
|
|
|
|
|
auto result = uniquePathBetweenNodes( cnFrom, cnTo, upath );
|
|
|
|
|
|
|
|
if( result == PS_OK )
|
|
|
|
path.isUnique = true;
|
|
|
|
else
|
|
|
|
path.isUnique = false;
|
|
|
|
|
|
|
|
if( result == PS_NO_PATH )
|
|
|
|
continue;
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
for( const auto item : upath )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
|
|
|
path.pathItems.insert( item->Parent() );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ftPaths.push_back(path);
|
|
|
|
newPaths++;
|
|
|
|
}
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
// reportAux( _("Cached %d paths\n"), newPaths );
|
2020-09-25 18:26:58 +00:00
|
|
|
|
|
|
|
return newPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FROM_TO_CACHE::IsOnFromToPath( BOARD_CONNECTED_ITEM* aItem, const wxString& aFrom, const wxString& aTo )
|
|
|
|
{
|
|
|
|
int nFromTosFound = 0;
|
|
|
|
|
|
|
|
if( !m_board )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for( int attempt = 0; attempt < 2; attempt++ )
|
|
|
|
{
|
|
|
|
// item already belongs to path
|
2022-02-13 11:54:41 +00:00
|
|
|
for( FT_PATH& ftPath : m_ftPaths )
|
2020-09-25 18:26:58 +00:00
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
if( aFrom == ftPath.fromWildcard && aTo == ftPath.toWildcard )
|
|
|
|
{
|
|
|
|
nFromTosFound++;
|
2020-09-25 18:26:58 +00:00
|
|
|
|
2022-02-13 11:54:41 +00:00
|
|
|
if( ftPath.pathItems.count( aItem ) )
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-25 18:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !nFromTosFound )
|
|
|
|
cacheFromToPaths( aFrom, aTo );
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-26 20:41:36 +00:00
|
|
|
|
2020-09-25 18:26:58 +00:00
|
|
|
void FROM_TO_CACHE::Rebuild( BOARD* aBoard )
|
|
|
|
{
|
|
|
|
m_board = aBoard;
|
|
|
|
buildEndpointList();
|
|
|
|
m_ftPaths.clear();
|
|
|
|
}
|
2020-09-26 20:41:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
FROM_TO_CACHE::FT_PATH* FROM_TO_CACHE::QueryFromToPath( const std::set<BOARD_CONNECTED_ITEM*>& aItems )
|
|
|
|
{
|
2022-02-13 11:54:41 +00:00
|
|
|
for( FT_PATH& ftPath : m_ftPaths )
|
2020-09-26 20:41:36 +00:00
|
|
|
{
|
|
|
|
if ( ftPath.pathItems == aItems )
|
|
|
|
return &ftPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2022-03-03 23:49:05 +00:00
|
|
|
}
|