You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

997 lines
25 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016-2017 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <connectivity_algo.h>
  25. #ifdef PROFILE
  26. #include <profile.h>
  27. #endif
  28. using namespace std::placeholders;
  29. bool operator<( const CN_ANCHOR_PTR a, const CN_ANCHOR_PTR b )
  30. {
  31. if( a->Pos().x == b->Pos().x )
  32. return a->Pos().y < b->Pos().y;
  33. else
  34. return a->Pos().x < b->Pos().x;
  35. }
  36. bool CN_ANCHOR::IsDirty() const
  37. {
  38. return m_item->Dirty();
  39. }
  40. CN_CLUSTER::CN_CLUSTER()
  41. {
  42. m_items.reserve( 64 );
  43. m_originPad = nullptr;
  44. m_originNet = -1;
  45. m_conflicting = false;
  46. }
  47. CN_CLUSTER::~CN_CLUSTER()
  48. {
  49. }
  50. wxString CN_CLUSTER::OriginNetName() const
  51. {
  52. if( !m_originPad || !m_originPad->Valid() )
  53. return "<none>";
  54. else
  55. return m_originPad->Parent()->GetNetname();
  56. }
  57. bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
  58. {
  59. return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
  60. }
  61. bool CN_CLUSTER::Contains( const BOARD_CONNECTED_ITEM* aItem )
  62. {
  63. for( auto item : m_items )
  64. {
  65. if( item->Valid() && item->Parent() == aItem )
  66. return true;
  67. }
  68. return false;
  69. }
  70. void CN_ITEM::Dump()
  71. {
  72. printf(" valid: %d, connected: \n", !!Valid());
  73. for( auto i : m_connected )
  74. {
  75. TRACK* t = static_cast<TRACK*>( i->Parent() );
  76. printf( " - %p %d\n", t, t->Type() );
  77. }
  78. }
  79. void CN_CLUSTER::Dump()
  80. {
  81. for( auto item : m_items )
  82. {
  83. wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
  84. item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
  85. printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
  86. item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
  87. item->Dump();
  88. }
  89. }
  90. void CN_CLUSTER::Add( CN_ITEM* item )
  91. {
  92. m_items.push_back( item );
  93. if( m_originNet < 0 )
  94. {
  95. m_originNet = item->Net();
  96. }
  97. if( item->Parent()->Type() == PCB_PAD_T )
  98. {
  99. if( !m_originPad )
  100. {
  101. m_originPad = item;
  102. m_originNet = item->Net();
  103. }
  104. if( m_originPad && item->Net() != m_originNet )
  105. {
  106. m_conflicting = true;
  107. }
  108. }
  109. }
  110. CN_CONNECTIVITY_ALGO::CN_CONNECTIVITY_ALGO()
  111. {
  112. }
  113. CN_CONNECTIVITY_ALGO::~CN_CONNECTIVITY_ALGO()
  114. {
  115. Clear();
  116. }
  117. bool CN_CONNECTIVITY_ALGO::Remove( BOARD_ITEM* aItem )
  118. {
  119. markItemNetAsDirty( aItem );
  120. switch( aItem->Type() )
  121. {
  122. case PCB_MODULE_T:
  123. for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
  124. {
  125. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( pad ) ].MarkItemsAsInvalid();
  126. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( pad ) );
  127. }
  128. m_padList.SetDirty( true );
  129. break;
  130. case PCB_PAD_T:
  131. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  132. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  133. m_padList.SetDirty( true );
  134. break;
  135. case PCB_TRACE_T:
  136. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  137. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  138. m_trackList.SetDirty( true );
  139. break;
  140. case PCB_VIA_T:
  141. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  142. m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  143. m_viaList.SetDirty( true );
  144. break;
  145. case PCB_ZONE_AREA_T:
  146. case PCB_ZONE_T:
  147. {
  148. m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
  149. m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
  150. m_zoneList.SetDirty( true );
  151. break;
  152. }
  153. default:
  154. return false;
  155. }
  156. return true;
  157. }
  158. void CN_CONNECTIVITY_ALGO::markItemNetAsDirty( const BOARD_ITEM* aItem )
  159. {
  160. if( aItem->IsConnected() )
  161. {
  162. auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
  163. MarkNetAsDirty( citem->GetNetCode() );
  164. }
  165. else
  166. {
  167. if( aItem->Type() == PCB_MODULE_T )
  168. {
  169. auto mod = static_cast <const MODULE*>( aItem );
  170. for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
  171. MarkNetAsDirty( pad->GetNetCode() );
  172. }
  173. }
  174. }
  175. bool CN_CONNECTIVITY_ALGO::Add( BOARD_ITEM* aItem )
  176. {
  177. markItemNetAsDirty ( aItem );
  178. switch( aItem->Type() )
  179. {
  180. case PCB_NETINFO_T:
  181. {
  182. MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
  183. break;
  184. }
  185. case PCB_MODULE_T:
  186. for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
  187. {
  188. if( m_itemMap.find( pad ) != m_itemMap.end() )
  189. return false;
  190. add( m_padList, pad );
  191. }
  192. break;
  193. case PCB_PAD_T:
  194. if( m_itemMap.find ( static_cast<D_PAD*>( aItem ) ) != m_itemMap.end() )
  195. return false;
  196. add( m_padList, static_cast<D_PAD*>( aItem ) );
  197. break;
  198. case PCB_TRACE_T:
  199. {
  200. if( m_itemMap.find( static_cast<TRACK*>( aItem ) ) != m_itemMap.end() )
  201. return false;
  202. add( m_trackList, static_cast<TRACK*>( aItem ) );
  203. break;
  204. }
  205. case PCB_VIA_T:
  206. if( m_itemMap.find( static_cast<VIA*>( aItem ) ) != m_itemMap.end() )
  207. return false;
  208. add( m_viaList, static_cast<VIA*>( aItem ) );
  209. break;
  210. case PCB_ZONE_AREA_T:
  211. case PCB_ZONE_T:
  212. {
  213. auto zone = static_cast<ZONE_CONTAINER*>( aItem );
  214. if( m_itemMap.find( static_cast<ZONE_CONTAINER*>( aItem ) ) != m_itemMap.end() )
  215. return false;
  216. m_itemMap[zone] = ITEM_MAP_ENTRY();
  217. for( auto zitem : m_zoneList.Add( zone ) )
  218. m_itemMap[zone].Link(zitem);
  219. break;
  220. }
  221. default:
  222. return false;
  223. }
  224. return true;
  225. }
  226. void CN_CONNECTIVITY_ALGO::searchConnections( bool aIncludeZones )
  227. {
  228. int totalDirtyCount = 0;
  229. if( m_lastSearchWithZones != aIncludeZones )
  230. {
  231. m_padList.MarkAllAsDirty();
  232. m_viaList.MarkAllAsDirty();
  233. m_trackList.MarkAllAsDirty();
  234. m_zoneList.MarkAllAsDirty();
  235. }
  236. m_lastSearchWithZones = aIncludeZones;
  237. auto checkForConnection = [] ( const CN_ANCHOR_PTR point, CN_ITEM* aRefItem, int aMaxDist = 0 )
  238. {
  239. const auto parent = aRefItem->Parent();
  240. assert( point->Item() );
  241. assert( point->Item()->Parent() );
  242. assert( aRefItem->Parent() );
  243. if( !point->Item()->Valid() )
  244. return;
  245. if( !aRefItem->Valid() )
  246. return;
  247. if( parent == point->Item()->Parent() )
  248. return;
  249. if( !( parent->GetLayerSet() &
  250. point->Item()->Parent()->GetLayerSet() ).any() )
  251. return;
  252. switch( parent->Type() )
  253. {
  254. case PCB_PAD_T:
  255. case PCB_VIA_T:
  256. if( parent->HitTest( wxPoint( point->Pos().x, point->Pos().y ) ) )
  257. CN_ITEM::Connect( aRefItem, point->Item() );
  258. break;
  259. case PCB_TRACE_T:
  260. {
  261. const auto track = static_cast<TRACK*> ( parent );
  262. const VECTOR2I d_start( VECTOR2I( track->GetStart() ) - point->Pos() );
  263. const VECTOR2I d_end( VECTOR2I( track->GetEnd() ) - point->Pos() );
  264. if( d_start.EuclideanNorm() < aMaxDist
  265. || d_end.EuclideanNorm() < aMaxDist )
  266. CN_ITEM::Connect( aRefItem, point->Item() );
  267. break;
  268. }
  269. case PCB_ZONE_T:
  270. case PCB_ZONE_AREA_T:
  271. {
  272. const auto zone = static_cast<ZONE_CONTAINER*> ( parent );
  273. auto zoneItem = static_cast<CN_ZONE*> ( aRefItem );
  274. if( point->Item()->Net() != parent->GetNetCode() )
  275. return;
  276. if( !( zone->GetLayerSet() &
  277. point->Item()->Parent()->GetLayerSet() ).any() )
  278. return;
  279. if( zoneItem->ContainsAnchor( point ) )
  280. {
  281. CN_ITEM::Connect( zoneItem, point->Item() );
  282. }
  283. break;
  284. }
  285. default :
  286. assert( false );
  287. }
  288. };
  289. auto checkInterZoneConnection = [] ( CN_ZONE* testedZone, CN_ZONE* aRefZone )
  290. {
  291. const auto parentZone = static_cast<const ZONE_CONTAINER*>( aRefZone->Parent() );
  292. if( testedZone->Parent()->Type () != PCB_ZONE_AREA_T )
  293. return;
  294. if( testedZone == aRefZone )
  295. return;
  296. if( testedZone->Parent() == aRefZone->Parent() )
  297. return;
  298. if( testedZone->Net() != parentZone->GetNetCode() )
  299. return; // we only test zones belonging to the same net
  300. if( !( testedZone->Parent()->GetLayerSet() & parentZone->GetLayerSet() ).any() )
  301. return; // and on same layer
  302. const auto& outline = parentZone->GetFilledPolysList().COutline( aRefZone->SubpolyIndex() );
  303. for( int i = 0; i < outline.PointCount(); i++ )
  304. {
  305. if( testedZone->ContainsPoint( outline.CPoint( i ) ) )
  306. {
  307. CN_ITEM::Connect( aRefZone, testedZone );
  308. return;
  309. }
  310. }
  311. const auto testedZoneParent = static_cast<const ZONE_CONTAINER*>( testedZone->Parent() );
  312. const auto& outline2 = testedZoneParent->GetFilledPolysList().COutline( testedZone->SubpolyIndex() );
  313. for( int i = 0; i < outline2.PointCount(); i++ )
  314. {
  315. if( aRefZone->ContainsPoint( outline2.CPoint( i ) ) )
  316. {
  317. CN_ITEM::Connect( aRefZone, testedZone );
  318. return;
  319. }
  320. }
  321. };
  322. #ifdef CONNECTIVITY_DEBUG
  323. printf("Search start\n");
  324. #endif
  325. std::vector<CN_ITEM*> garbage;
  326. garbage.reserve( 1024 );
  327. m_padList.RemoveInvalidItems( garbage );
  328. m_viaList.RemoveInvalidItems( garbage );
  329. m_trackList.RemoveInvalidItems( garbage );
  330. m_zoneList.RemoveInvalidItems( garbage );
  331. for( auto item : garbage )
  332. delete item;
  333. //auto all = allItemsInBoard();
  334. #ifdef CONNECTIVITY_DEBUG
  335. for( auto item : m_padList )
  336. if( all.find( item->Parent() ) == all.end() ) { printf("Failing pad : %p\n", item->Parent() ); assert ( false ); }
  337. for( auto item : m_viaList )
  338. if( all.find( item->Parent() ) == all.end() ) { printf("Failing via : %p\n", item->Parent() ); assert ( false ); }
  339. for( auto item : m_trackList )
  340. if( all.find( item->Parent() ) == all.end() ) { printf("Failing track : %p\n", item->Parent() ); assert ( false ); }
  341. for( auto item : m_zoneList )
  342. if( all.find( item->Parent() ) == all.end() ) { printf("Failing zome : %p\n", item->Parent() ); assert ( false ); }
  343. #endif
  344. #ifdef PROFILE
  345. PROF_COUNTER search_cnt( "search-connections" );
  346. PROF_COUNTER search_basic( "search-basic" );
  347. #endif
  348. if( m_padList.IsDirty() || m_trackList.IsDirty() || m_viaList.IsDirty() )
  349. {
  350. totalDirtyCount++;
  351. for( auto padItem : m_padList )
  352. {
  353. auto pad = static_cast<D_PAD*> ( padItem->Parent() );
  354. auto searchPads = std::bind( checkForConnection, _1, padItem );
  355. m_padList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads );
  356. m_trackList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads );
  357. m_viaList.FindNearby( pad->ShapePos(), pad->GetBoundingRadius(), searchPads );
  358. }
  359. for( auto& trackItem : m_trackList )
  360. {
  361. auto track = static_cast<TRACK*> ( trackItem->Parent() );
  362. int dist_max = track->GetWidth() / 2;
  363. auto searchTracks = std::bind( checkForConnection, _1, trackItem, dist_max );
  364. m_trackList.FindNearby( track->GetStart(), dist_max, searchTracks );
  365. m_trackList.FindNearby( track->GetEnd(), dist_max, searchTracks );
  366. }
  367. for( auto& viaItem : m_viaList )
  368. {
  369. auto via = static_cast<VIA*> ( viaItem->Parent() );
  370. int dist_max = via->GetWidth() / 2;
  371. auto searchVias = std::bind( checkForConnection, _1, viaItem, dist_max );
  372. totalDirtyCount++;
  373. m_viaList.FindNearby( via->GetStart(), dist_max, searchVias );
  374. m_trackList.FindNearby( via->GetStart(), dist_max, searchVias );
  375. }
  376. }
  377. #ifdef PROFILE
  378. search_basic.Show();
  379. #endif
  380. if( aIncludeZones )
  381. {
  382. for( auto& item : m_zoneList )
  383. {
  384. auto zoneItem = static_cast<CN_ZONE *> (item);
  385. auto searchZones = std::bind( checkForConnection, _1, zoneItem );
  386. if( zoneItem->Dirty() || m_padList.IsDirty() || m_trackList.IsDirty() || m_viaList.IsDirty() )
  387. {
  388. totalDirtyCount++;
  389. m_viaList.FindNearby( zoneItem->BBox(), searchZones );
  390. m_trackList.FindNearby( zoneItem->BBox(), searchZones );
  391. m_padList.FindNearby( zoneItem->BBox(), searchZones );
  392. m_zoneList.FindNearbyZones( zoneItem->BBox(), std::bind( checkInterZoneConnection, _1, zoneItem ) );
  393. }
  394. }
  395. m_zoneList.ClearDirtyFlags();
  396. }
  397. m_padList.ClearDirtyFlags();
  398. m_viaList.ClearDirtyFlags();
  399. m_trackList.ClearDirtyFlags();
  400. #ifdef CONNECTIVITY_DEBUG
  401. printf("Search end\n");
  402. #endif
  403. #ifdef PROFILE
  404. search_cnt.Show();
  405. #endif
  406. }
  407. void CN_ITEM::RemoveInvalidRefs()
  408. {
  409. auto lastConn = std::remove_if(m_connected.begin(), m_connected.end(), [] ( CN_ITEM * item) {
  410. return !item->Valid();
  411. } );
  412. m_connected.resize( lastConn - m_connected.begin() );
  413. }
  414. void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
  415. {
  416. auto lastAnchor = std::remove_if(m_anchors.begin(), m_anchors.end(),
  417. [] ( const CN_ANCHOR_PTR anchor ) {
  418. return !anchor->Valid();
  419. } );
  420. m_anchors.resize( lastAnchor - m_anchors.begin() );
  421. auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item ) {
  422. if( !item->Valid() )
  423. {
  424. aGarbage.push_back ( item );
  425. return true;
  426. }
  427. return false;
  428. } );
  429. m_items.resize( lastItem - m_items.begin() );
  430. // fixme: mem leaks
  431. for( auto item : m_items )
  432. item->RemoveInvalidRefs();
  433. }
  434. bool CN_CONNECTIVITY_ALGO::isDirty() const
  435. {
  436. return m_viaList.IsDirty() || m_trackList.IsDirty() || m_zoneList.IsDirty() || m_padList.IsDirty();
  437. }
  438. const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode )
  439. {
  440. constexpr KICAD_T types[] = { PCB_TRACE_T, PCB_PAD_T, PCB_VIA_T, PCB_ZONE_AREA_T, PCB_MODULE_T, EOT };
  441. return SearchClusters( aMode, types, -1 );
  442. }
  443. const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode,
  444. const KICAD_T aTypes[], int aSingleNet )
  445. {
  446. bool includeZones = ( aMode != CSM_PROPAGATE );
  447. bool withinAnyNet = ( aMode != CSM_PROPAGATE );
  448. std::deque<CN_ITEM*> Q;
  449. CN_ITEM* head = nullptr;
  450. CLUSTERS clusters;
  451. if( isDirty() )
  452. searchConnections( includeZones );
  453. auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
  454. {
  455. if( withinAnyNet && aItem->Net() <= 0 )
  456. return;
  457. if( !aItem->Valid() )
  458. return;
  459. if( aSingleNet >=0 && aItem->Net() != aSingleNet )
  460. return;
  461. bool found = false;
  462. for( int i = 0; aTypes[i] != EOT; i++ )
  463. {
  464. if( aItem->Parent()->Type() == aTypes[i] )
  465. {
  466. found = true;
  467. break;
  468. }
  469. }
  470. if( !found )
  471. return;
  472. aItem->ListClear();
  473. aItem->SetVisited( false );
  474. if( !head )
  475. head = aItem;
  476. else
  477. head->ListInsert( aItem );
  478. };
  479. std::for_each( m_padList.begin(), m_padList.end(), addToSearchList );
  480. std::for_each( m_trackList.begin(), m_trackList.end(), addToSearchList );
  481. std::for_each( m_viaList.begin(), m_viaList.end(), addToSearchList );
  482. if( includeZones )
  483. {
  484. std::for_each( m_zoneList.begin(), m_zoneList.end(), addToSearchList );
  485. }
  486. while( head )
  487. {
  488. CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
  489. Q.clear();
  490. CN_ITEM* root = head;
  491. root->SetVisited ( true );
  492. head = root->ListRemove();
  493. Q.push_back( root );
  494. while( Q.size() )
  495. {
  496. CN_ITEM* current = Q.front();
  497. Q.pop_front();
  498. cluster->Add( current );
  499. for( auto n : current->ConnectedItems() )
  500. {
  501. if( withinAnyNet && n->Net() != root->Net() )
  502. continue;
  503. if( !n->Visited() && n->Valid() )
  504. {
  505. n->SetVisited( true );
  506. Q.push_back( n );
  507. head = n->ListRemove();
  508. }
  509. }
  510. }
  511. clusters.push_back( cluster );
  512. }
  513. std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
  514. return a->OriginNet() < b->OriginNet();
  515. } );
  516. #ifdef CONNECTIVITY_DEBUG
  517. printf("Active clusters: %d\n");
  518. for( auto cl : clusters )
  519. {
  520. printf( "Net %d\n", cl->OriginNet() );
  521. cl->Dump();
  522. }
  523. #endif
  524. return clusters;
  525. }
  526. void CN_CONNECTIVITY_ALGO::Build( BOARD* aBoard )
  527. {
  528. for( int i = 0; i<aBoard->GetAreaCount(); i++ )
  529. {
  530. auto zone = aBoard->GetArea( i );
  531. Add( zone );
  532. }
  533. for( auto tv : aBoard->Tracks() )
  534. Add( tv );
  535. for( auto mod : aBoard->Modules() )
  536. {
  537. for( auto pad : mod->Pads() )
  538. Add( pad );
  539. }
  540. /*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
  541. m_zoneList.Size(), m_padList.Size(),
  542. m_viaList.Size(), m_trackList.Size() );*/
  543. }
  544. void CN_CONNECTIVITY_ALGO::Build( const std::vector<BOARD_ITEM*>& aItems )
  545. {
  546. for( auto item : aItems )
  547. {
  548. switch( item->Type() )
  549. {
  550. case PCB_TRACE_T:
  551. case PCB_VIA_T:
  552. case PCB_ZONE_T:
  553. case PCB_PAD_T:
  554. Add( item );
  555. break;
  556. case PCB_MODULE_T:
  557. {
  558. for( auto pad : static_cast<MODULE*>( item )->Pads() )
  559. {
  560. Add( pad );
  561. }
  562. break;
  563. }
  564. default:
  565. break;
  566. }
  567. }
  568. }
  569. void CN_CONNECTIVITY_ALGO::propagateConnections()
  570. {
  571. for( auto cluster : m_connClusters )
  572. {
  573. if( cluster->IsConflicting() )
  574. {
  575. wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
  576. }
  577. else if( cluster->IsOrphaned() )
  578. {
  579. wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
  580. (const char*) cluster->OriginNetName().c_str() );
  581. }
  582. else if( cluster->HasValidNet() )
  583. {
  584. // normal cluster: just propagate from the pads
  585. int n_changed = 0;
  586. for( auto item : *cluster )
  587. {
  588. if( item->CanChangeNet() )
  589. {
  590. if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
  591. {
  592. MarkNetAsDirty( item->Parent()->GetNetCode() );
  593. MarkNetAsDirty( cluster->OriginNet() );
  594. item->Parent()->SetNetCode( cluster->OriginNet() );
  595. n_changed++;
  596. }
  597. }
  598. }
  599. if( n_changed )
  600. wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
  601. cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
  602. else
  603. wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
  604. }
  605. else
  606. {
  607. wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
  608. }
  609. }
  610. }
  611. void CN_CONNECTIVITY_ALGO::PropagateNets()
  612. {
  613. //searchConnections( false );
  614. m_connClusters = SearchClusters( CSM_PROPAGATE );
  615. propagateConnections();
  616. }
  617. void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone, std::vector<int>& aIslands )
  618. {
  619. if( aZone->GetFilledPolysList().IsEmpty() )
  620. return;
  621. aIslands.clear();
  622. Remove( aZone );
  623. Add( aZone );
  624. m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
  625. for( auto cluster : m_connClusters )
  626. {
  627. if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
  628. {
  629. for( auto z : *cluster )
  630. {
  631. if( z->Parent() == aZone )
  632. {
  633. aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
  634. }
  635. }
  636. }
  637. }
  638. wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
  639. }
  640. const CN_CONNECTIVITY_ALGO::CLUSTERS& CN_CONNECTIVITY_ALGO::GetClusters()
  641. {
  642. m_ratsnestClusters = SearchClusters( CSM_RATSNEST );
  643. return m_ratsnestClusters;
  644. }
  645. void CN_CONNECTIVITY_ALGO::MarkNetAsDirty( int aNet )
  646. {
  647. if( aNet < 0 )
  648. return;
  649. if( (int) m_dirtyNets.size() <= aNet )
  650. m_dirtyNets.resize( aNet + 1 );
  651. m_dirtyNets[aNet] = true;
  652. }
  653. int CN_ITEM::AnchorCount() const
  654. {
  655. if( !m_valid )
  656. return 0;
  657. return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
  658. }
  659. const VECTOR2I CN_ITEM::GetAnchor( int n ) const
  660. {
  661. if( !m_valid )
  662. return VECTOR2I();
  663. switch( m_parent->Type() )
  664. {
  665. case PCB_PAD_T:
  666. return static_cast<const D_PAD*>( m_parent )->ShapePos();
  667. break;
  668. case PCB_TRACE_T:
  669. {
  670. auto tr = static_cast<const TRACK*>( m_parent );
  671. return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
  672. break;
  673. }
  674. case PCB_VIA_T:
  675. return static_cast<const VIA*>( m_parent )->GetStart();
  676. default:
  677. assert( false );
  678. return VECTOR2I();
  679. }
  680. }
  681. int CN_ZONE::AnchorCount() const
  682. {
  683. if( !Valid() )
  684. return 0;
  685. const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
  686. const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
  687. return outline.PointCount() ? 1 : 0;
  688. }
  689. const VECTOR2I CN_ZONE::GetAnchor( int n ) const
  690. {
  691. if( !Valid() )
  692. return VECTOR2I();
  693. const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
  694. const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
  695. return outline.CPoint( 0 );
  696. }
  697. int CN_ITEM::Net() const
  698. {
  699. if( !m_parent || !m_valid )
  700. return -1;
  701. return m_parent->GetNetCode();
  702. }
  703. BOARD_CONNECTED_ITEM* CN_ANCHOR::Parent() const
  704. {
  705. assert( m_item->Valid() );
  706. return m_item->Parent();
  707. }
  708. bool CN_ANCHOR::Valid() const
  709. {
  710. if( !m_item )
  711. return false;
  712. return m_item->Valid();
  713. }
  714. void CN_CONNECTIVITY_ALGO::Clear()
  715. {
  716. m_ratsnestClusters.clear();
  717. m_connClusters.clear();
  718. m_itemMap.clear();
  719. m_padList.Clear();
  720. m_trackList.Clear();
  721. m_viaList.Clear();
  722. m_zoneList.Clear();
  723. }
  724. void CN_CONNECTIVITY_ALGO::ForEachItem( std::function<void(CN_ITEM*)> aFunc )
  725. {
  726. for( auto item : m_padList )
  727. aFunc( item );
  728. for( auto item : m_viaList )
  729. aFunc( item );
  730. for( auto item : m_trackList )
  731. aFunc( item );
  732. for( auto item : m_zoneList )
  733. aFunc( item );
  734. }
  735. void CN_CONNECTIVITY_ALGO::ForEachAnchor( std::function<void(CN_ANCHOR_PTR)> aFunc )
  736. {
  737. for( auto anchor : m_padList.Anchors() )
  738. aFunc( anchor );
  739. for( auto anchor : m_viaList.Anchors() )
  740. aFunc( anchor );
  741. for( auto anchor : m_trackList.Anchors() )
  742. aFunc( anchor );
  743. for( auto anchor : m_zoneList.Anchors() )
  744. aFunc( anchor );
  745. }
  746. bool CN_ANCHOR::IsDangling() const
  747. {
  748. if( !m_cluster )
  749. return true;
  750. int validCount = 0;
  751. for( auto item : *m_cluster )
  752. {
  753. if( item->Valid() )
  754. validCount++;
  755. }
  756. return validCount <= 1;
  757. }