From 91ee987cfebce4478442adeac6906f57d3a9f424 Mon Sep 17 00:00:00 2001 From: Tomasz Wlostowski <tomasz.wlostowski@cern.ch> Date: Thu, 6 Mar 2025 23:03:22 +0100 Subject: [PATCH] connectivity: TOPO_MATCH now prioritizes component candidates by the ratio of matching pad nets. This improves cloning of circuits with numerous of matching component mappings (e.g. LED matrices) by taking preference for mappings with same order of net connections. --- pcbnew/connectivity/topo_match.cpp | 26 +++++++++++++++++++++----- pcbnew/connectivity/topo_match.h | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pcbnew/connectivity/topo_match.cpp b/pcbnew/connectivity/topo_match.cpp index 1cbadd2db3..a6a8d07449 100644 --- a/pcbnew/connectivity/topo_match.cpp +++ b/pcbnew/connectivity/topo_match.cpp @@ -118,7 +118,7 @@ bool PIN::IsIsomorphic( const PIN& b ) const } // fixme: terrible performance, but computers are fast these days, ain't they? :D -bool checkIfPadNetsMatch( BACKTRACK_STAGE& aMatches, CONNECTION_GRAPH* aRefGraph, COMPONENT* aRef, COMPONENT* aTgt ) +bool checkIfPadNetsMatch( const BACKTRACK_STAGE& aMatches, CONNECTION_GRAPH* aRefGraph, COMPONENT* aRef, COMPONENT* aTgt ) { std::map<PIN*, PIN*> pairs; std::vector<PIN*> pref, ptgt; @@ -197,7 +197,7 @@ bool checkIfPadNetsMatch( BACKTRACK_STAGE& aMatches, CONNECTION_GRAPH* aRefGraph std::vector<COMPONENT*> -CONNECTION_GRAPH::findMatchingComponents( CONNECTION_GRAPH* aRefGraph, COMPONENT* aRef, BACKTRACK_STAGE& partialMatches ) +CONNECTION_GRAPH::findMatchingComponents( CONNECTION_GRAPH* aRefGraph, COMPONENT* aRef, const BACKTRACK_STAGE& partialMatches ) { std::vector<COMPONENT*> matches; for( auto cmpTarget : m_components ) @@ -228,11 +228,27 @@ CONNECTION_GRAPH::findMatchingComponents( CONNECTION_GRAPH* aRefGraph, COMPONENT { wxLogTrace( traceTopoMatch, wxT("reject\n") ); } - - - } + auto padSimilarity=[]( COMPONENT*a, COMPONENT*b ) -> double { + int n=0; + for(int i=0;i<a->m_pins.size();i++) + { + PIN* pa = a->m_pins[i]; + PIN* pb = b->m_pins[i]; + if( pa->GetNetCode() == pb->GetNetCode() ) + n++; + } + return (double)n / (double) a->m_pins.size(); + }; + + + std::sort(matches.begin(), matches.end(), [&] ( COMPONENT*a, COMPONENT*b ) -> int + { + return padSimilarity( aRef,a ) > padSimilarity( aRef, b ); + } +); + return matches; } diff --git a/pcbnew/connectivity/topo_match.h b/pcbnew/connectivity/topo_match.h index 08b222f89b..d91fb9a69e 100644 --- a/pcbnew/connectivity/topo_match.h +++ b/pcbnew/connectivity/topo_match.h @@ -184,7 +184,7 @@ private: std::vector<COMPONENT*> findMatchingComponents( CONNECTION_GRAPH* aRefGraph, COMPONENT* ref, - BACKTRACK_STAGE& partialMatches ); + const BACKTRACK_STAGE& partialMatches ); std::vector<COMPONENT*> m_components;