KiCad PCB EDA Suite
from_to_cache.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004-2020 KiCad Developers.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <cstdio>
21 #include <memory>
22 #include <reporter.h>
23 #include <board.h>
24 #include <string_utils.h>
25 
26 #include <pcb_expr_evaluator.h>
27 
30 
32 
34 {
35  m_ftEndpoints.clear();
36 
37  for( FOOTPRINT* footprint : m_board->Footprints() )
38  {
39  for( PAD* pad : footprint->Pads() )
40  {
41  FT_ENDPOINT ent;
42  ent.name = footprint->GetReference() + "-" + pad->GetNumber();
43  ent.parent = pad;
44  m_ftEndpoints.push_back( ent );
45  ent.name = footprint->GetReference();
46  ent.parent = pad;
47  m_ftEndpoints.push_back( ent );
48  }
49  }
50 }
51 
52 
54  PS_OK = 0,
57 };
58 
59 
60 static bool isVertexVisited( CN_ITEM* v, const std::vector<CN_ITEM*>& path )
61 {
62  for( auto u : path )
63  {
64  if ( u == v )
65  return true;
66  }
67 
68  return false;
69 }
70 
71 
72 static PATH_STATUS uniquePathBetweenNodes( CN_ITEM* u, CN_ITEM* v, std::vector<CN_ITEM*>& outPath )
73 {
74  using Path = std::vector<CN_ITEM*>;
75  std::deque<Path> Q;
76 
77  Path pInit;
78  bool pathFound = false;
79  pInit.push_back( u );
80  Q.push_back( pInit );
81 
82  while( Q.size() )
83  {
84  Path path = Q.front();
85  Q.pop_front();
86  CN_ITEM* last = path.back();
87 
88  if( last == v )
89  {
90  outPath = path;
91  if( pathFound )
92  return PS_MULTIPLE_PATHS;
93  pathFound = true;
94  }
95 
96  for( auto ci : last->ConnectedItems() )
97  {
98  bool vertexVisited = isVertexVisited( ci, path );
99 
100  for( auto& p : Q )
101  if( isVertexVisited( ci, p ) )
102  {
103  vertexVisited = true;
104  break;
105  }
106 
107  if( !vertexVisited )
108  {
109  Path newpath( path );
110  newpath.push_back( ci );
111  Q.push_back( newpath );
112  }
113  }
114  }
115 
116  return pathFound ? PS_OK : PS_NO_PATH;
117 };
118 
119 
120 int FROM_TO_CACHE::cacheFromToPaths( const wxString& aFrom, const wxString& aTo )
121 {
122  std::vector<FT_PATH> paths;
123  auto connectivity = m_board->GetConnectivity();
124  auto cnAlgo = connectivity->GetConnectivityAlgo();
125 
126  for( auto& endpoint : m_ftEndpoints )
127  {
128  if( WildCompareString( aFrom, endpoint.name, false ) )
129  {
130  FT_PATH p;
131  p.net = endpoint.parent->GetNetCode();
132  p.from = endpoint.parent;
133  p.to = nullptr;
134  paths.push_back(p);
135  }
136  }
137 
138  for( auto &path : paths )
139  {
140  int count = 0;
141  auto netName = path.from->GetNetname();
142 
143  wxString fromName = path.from->GetParent()->GetReference() + "-" + path.from->GetNumber();
144 
145  const KICAD_T onlyRouting[] = { PCB_PAD_T, PCB_ARC_T, PCB_VIA_T, PCB_TRACE_T, EOT };
146 
147  auto padCandidates = connectivity->GetConnectedItems( path.from, onlyRouting );
148  PAD* toPad = nullptr;
149 
150  for( auto pitem : padCandidates )
151  {
152  if( pitem == path.from )
153  continue;
154 
155  if( pitem->Type() != PCB_PAD_T )
156  continue;
157 
158  const PAD *pad = static_cast<const PAD*>( pitem );
159 
160  wxString toName = pad->GetParent()->GetReference() + "-" + pad->GetNumber();
161 
162 
163  for ( const auto& endpoint : m_ftEndpoints )
164  {
165  if( pad == endpoint.parent )
166  {
167  if( WildCompareString( aTo, endpoint.name, false ) )
168  {
169  count++;
170  toPad = endpoint.parent;
171 
172  path.to = toPad;
173  path.fromName = fromName;
174  path.toName = toName;
175  path.fromWildcard = aFrom;
176  path.toWildcard = aTo;
177 
178  if( count >= 2 )
179  {
180  // fixme: report this somewhere?
181  //printf("Multiple targets found, aborting...\n");
182  path.to = nullptr;
183  }
184  }
185  }
186  }
187  }
188  }
189 
190  int newPaths = 0;
191 
192  for( auto &path : paths )
193  {
194  if( !path.from || !path.to )
195  continue;
196 
197 
198  CN_ITEM *cnFrom = cnAlgo->ItemEntry( path.from ).GetItems().front();
199  CN_ITEM *cnTo = cnAlgo->ItemEntry( path.to ).GetItems().front();
201 
202  auto result = uniquePathBetweenNodes( cnFrom, cnTo, upath );
203 
204  if( result == PS_OK )
205  path.isUnique = true;
206  else
207  path.isUnique = false;
208 
209 
210  //printf( "%s\n", (const char *) wxString::Format( _("Check path: %s -> %s (net %s)"), path.fromName, path.toName, cnFrom->Parent()->GetNetname() ) );
211 
212  if( result == PS_NO_PATH )
213  continue;
214 
215  for( const auto item : upath )
216  {
217  path.pathItems.insert( item->Parent() );
218  }
219 
220  m_ftPaths.push_back(path);
221  newPaths++;
222  }
223 
224  // reportAux( _("Cached %d paths\n"), newPaths );
225 
226  return newPaths;
227 }
228 
229 bool FROM_TO_CACHE::IsOnFromToPath( BOARD_CONNECTED_ITEM* aItem, const wxString& aFrom, const wxString& aTo )
230 {
231  int nFromTosFound = 0;
232 
233  if( !m_board )
234  return false;
235 
236  //printf("Check %d cached paths [%p]\n", m_ftPaths.size(), aItem );
237  for( int attempt = 0; attempt < 2; attempt++ )
238  {
239  // item already belongs to path
240  for( auto& ftPath : m_ftPaths )
241  {
242  if( aFrom == ftPath.fromWildcard &&
243  aTo == ftPath.toWildcard )
244  {
245  nFromTosFound++;
246 
247  if( ftPath.pathItems.count( aItem ) )
248  {
249  // printf("Found cached path for %p [%s->%s]\n", aItem, (const char *)ftPath.fromName, (const char *) ftPath.toName );
250  return true;
251  }
252  }
253  }
254 
255  if( !nFromTosFound )
256  cacheFromToPaths( aFrom, aTo );
257  else
258  return false;
259  }
260 
261  return false;
262 }
263 
264 
266 {
267  m_board = aBoard;
269  m_ftPaths.clear();
270 }
271 
272 
273 FROM_TO_CACHE::FT_PATH* FROM_TO_CACHE::QueryFromToPath( const std::set<BOARD_CONNECTED_ITEM*>& aItems )
274 {
275  for( auto& ftPath : m_ftPaths )
276  {
277  if ( ftPath.pathItems == aItems )
278  return &ftPath;
279  }
280 
281  return nullptr;
282 }
std::vector< CN_ITEM * > CONNECTED_ITEMS
const CONNECTED_ITEMS & ConnectedItems() const
std::vector< FT_ENDPOINT > m_ftEndpoints
Definition: from_to_cache.h:68
class PCB_ARC, an arc track segment on a copper layer
Definition: typeinfo.h:97
static bool isVertexVisited(CN_ITEM *v, const std::vector< CN_ITEM * > &path)
BOARD * m_board
Definition: from_to_cache.h:71
class PAD, a pad in a footprint
Definition: typeinfo.h:89
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
search types array terminator (End Of Types)
Definition: typeinfo.h:81
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:77
class PCB_TRACK, a track segment (segment on a copper layer)
Definition: typeinfo.h:95
std::vector< FT_PATH > m_ftPaths
Definition: from_to_cache.h:69
void Rebuild(BOARD *aBoard)
int cacheFromToPaths(const wxString &aFrom, const wxString &aTo)
static PATH_STATUS uniquePathBetweenNodes(CN_ITEM *u, CN_ITEM *v, std::vector< CN_ITEM * > &outPath)
FOOTPRINTS & Footprints()
Definition: board.h:233
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition: board.h:344
PATH_STATUS
bool IsOnFromToPath(BOARD_CONNECTED_ITEM *aItem, const wxString &aFrom, const wxString &aTo)
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void buildEndpointList()
bool WildCompareString(const wxString &pattern, const wxString &string_to_tst, bool case_sensitive)
Compare a string against wild card (* and ?) pattern using the usual rules.
class PCB_VIA, a via (like a track segment on a copper layer)
Definition: typeinfo.h:96
Definition: pad.h:57
FT_PATH * QueryFromToPath(const std::set< BOARD_CONNECTED_ITEM * > &aItems)