46 m_itemsList( nullptr ),
47 m_reporter( nullptr ),
60 std::vector<std::shared_ptr<CLEANUP_ITEM> >* aItemsList,
61 bool aRemoveMisConnected,
bool aCleanVias,
bool aMergeSegments,
62 bool aDeleteUnconnected,
bool aDeleteTracksinPad,
63 bool aDeleteDanglingVias,
REPORTER* aReporter )
66 bool has_deleted =
false;
81 bool removeNullSegments = aMergeSegments || aRemoveMisConnected;
82 cleanup( aCleanVias, removeNullSegments, aMergeSegments , aMergeSegments );
96 cleanup(
false,
false,
true,
false );
98 if( aRemoveMisConnected )
113 if( aDeleteTracksinPad )
128 if( aDeleteUnconnected || aDeleteDanglingVias )
138 if( aDeleteUnconnected )
141 if( aDeleteDanglingVias )
151 if( has_deleted && aMergeSegments )
163 cleanup(
false,
false,
false,
true );
181 std::set<BOARD_ITEM *> toRemove;
185 if( segment->IsLocked() ||
filterItem( segment ) )
188 for(
PAD* testedPad : connectivity->GetConnectedPads( segment ) )
190 if( segment->GetNetCode() != testedPad->GetNetCode() )
192 std::shared_ptr<CLEANUP_ITEM> item;
199 item->SetItems( segment );
202 toRemove.insert( segment );
206 for(
PCB_TRACK* testedTrack : connectivity->GetConnectedTracks( segment ) )
208 if( segment->GetNetCode() != testedTrack->GetNetCode() )
210 std::shared_ptr<CLEANUP_ITEM> item;
217 item->SetItems( segment );
220 toRemove.insert( segment );
232 if( !( aTstStart && aTstEnd ) )
237 const std::list<CN_ITEM*>& items =
247 if( !item->Valid() || item->Parent() == aTrack || item->Parent()->
HasFlag(
IS_DELETED ) )
256 for(
const std::shared_ptr<CN_ANCHOR>&
anchor : item->Anchors() )
267 return itemcount > 1;
273 bool item_erased =
false;
274 bool modified =
false;
276 if( !aTrack && !aVia )
286 std::deque<PCB_TRACK*> temp_tracks(
m_brd->
Tracks() );
293 if( !aVia && track->Type() ==
PCB_VIA_T )
302 std::shared_ptr<CLEANUP_ITEM> item;
309 item->SetItems( track );
325 }
while( item_erased );
333 std::set<BOARD_ITEM*> toRemove;
340 if( track->IsLocked() ||
filterItem( track ) )
347 for(
PAD*
pad : connectivity->GetConnectedPads( track ) )
349 if(
pad->HitTest( track->GetStart() ) &&
pad->HitTest( track->GetEnd() ) )
352 track->TransformShapeToPolygon( poly, track->GetLayer(), 0,
ARC_HIGH_DEF,
360 item->SetItems( track );
363 toRemove.insert( track );
379 bool aDeleteDuplicateSegments,
bool aMergeSegments )
386 rtree.
Insert( track, track->GetLayer() );
389 std::set<BOARD_ITEM*> toRemove;
396 if( aDeleteDuplicateVias && track->Type() ==
PCB_VIA_T )
400 if(
via->GetStart() !=
via->GetEnd() )
401 via->SetEnd(
via->GetStart() );
421 item->SetItems(
via );
425 toRemove.insert(
via );
437 if( (
pad->GetLayerSet() & all_cu ) == all_cu )
440 item->SetItems(
via,
pad );
444 toRemove.insert(
via );
452 if( aDeleteNullSegments && track->Type() !=
PCB_VIA_T )
454 if( track->IsNull() )
457 item->SetItems( track );
461 toRemove.insert( track );
465 if( aDeleteDuplicateSegments && track->Type() ==
PCB_TRACE_T && !track->IsNull() )
467 rtree.
QueryColliding( track, track->GetLayer(), track->GetLayer(),
471 return aItem->Type() == PCB_TRACE_T
472 && !aItem->HasFlag( SKIP_STRUCT )
473 && !aItem->HasFlag( IS_DELETED )
474 && !static_cast<PCB_TRACK*>( aItem )->IsNull();
479 PCB_TRACK* other = static_cast<PCB_TRACK*>( aItem );
481 if( track->IsPointOnEnds( other->GetStart() )
482 && track->IsPointOnEnds( other->GetEnd() )
483 && track->GetWidth() == other->GetWidth()
484 && track->GetLayer() == other->GetLayer() )
486 auto item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_TRACK );
487 item->SetItems( track );
488 m_itemsList->push_back( item );
490 track->SetFlags( IS_DELETED );
491 toRemove.insert( track );
502 removeItems( toRemove );
507 auto mergeSegments = [&]( std::shared_ptr<CN_CONNECTIVITY_ALGO> connectivity ) ->
bool
509 auto track_loop = [&](
int aStart,
int aEnd ) -> std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>>
511 std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>> tracks;
513 for(
int ii = aStart; ii < aEnd; ++ii )
515 PCB_TRACK* segment = m_brd->Tracks()[ii];
524 if( filterItem( segment ) )
528 for(
CN_ITEM* citem : connectivity->ItemEntry( segment ).GetItems() )
532 std::vector<PCB_TRACK*> sameWidthCandidates;
533 std::vector<PCB_TRACK*> differentWidthCandidates;
537 if( !connected->Valid() )
543 && !filterItem( candidate ) )
549 sameWidthCandidates.push_back( candidateSegment );
553 differentWidthCandidates.push_back( candidateSegment );
559 if( !differentWidthCandidates.empty() )
562 for(
PCB_TRACK* candidate : sameWidthCandidates )
564 if( candidate < segment )
568 && testMergeCollinearSegments( segment, candidate ) )
570 tracks.emplace_back( segment, candidate );
584 auto merge_returns =
tp.parallelize_loop( 0, m_brd->Tracks().size(), track_loop );
587 for(
size_t ii = 0; ii < merge_returns.size(); ++ii )
589 std::future<std::vector<std::pair<PCB_TRACK*, PCB_TRACK*>>>& ret = merge_returns[ii];
593 for(
auto& [seg1, seg2] : ret.get() )
600 mergeCollinearSegments( seg1, seg2 );
612 while( !m_brd->BuildConnectivity() )
615 m_connectedItemsCache.clear();
616 }
while( mergeSegments( m_brd->GetConnectivity()->GetConnectivityAlgo() ) );
619 for(
PCB_TRACK* track : m_brd->Tracks() )
647 const unsigned p1s = 1 << 0;
648 const unsigned p1e = 1 << 1;
649 const unsigned p2s = 1 << 2;
650 const unsigned p2e = 1 << 3;
652 std::atomic<unsigned> flags = 0;
654 auto collectPtsSeg1 =
657 if( std::popcount( flags.load() ) > 2 )
673 if( !( flags & p1s ) && citem->HitTest( aSeg1->
GetStart(), ( aSeg1->
GetWidth() + 1 ) / 2 ) )
676 if( !( flags & p1e ) && citem->HitTest( aSeg1->
GetEnd(), ( aSeg1->
GetWidth() + 1 ) / 2 ) )
681 auto collectPtsSeg2 =
684 if( std::popcount( flags.load() ) > 2 )
700 if( !( flags & p2s ) && citem->HitTest( aSeg2->
GetStart(), ( aSeg2->
GetWidth() + 1 ) / 2 ) )
703 if( !( flags & p2e ) && citem->HitTest( aSeg2->
GetEnd(), ( aSeg2->
GetWidth() + 1 ) / 2 ) )
713 if( item != aSeg1 && item != aSeg2 )
714 collectPtsSeg1( item );
722 if( item != aSeg1 && item != aSeg2 )
723 collectPtsSeg2( item );
727 if( std::popcount( flags.load() ) > 2 )
736 aDummySeg = &dummy_seg;
742 int min_x = std::min( aSeg1->
GetStart().
x,
744 int min_y = std::min( aSeg1->
GetStart().
y,
746 int max_x = std::max( aSeg1->
GetStart().
x,
748 int max_y = std::max( aSeg1->
GetStart().
y,
765 for(
unsigned i = 0; i < 4; ++i )
767 if( ( flags & ( 1 << i ) ) && !aDummySeg->
IsPointOnEnds( pts[i] ) )
786 item->SetItems( aSeg1, aSeg2 );
constexpr int ARC_HIGH_DEF
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
void SetParentGroup(PCB_GROUP *aGroup)
PCB_GROUP * GetParentGroup() const
virtual bool IsLocked() const
Information pertinent to a Pcbnew printed circuit board.
bool BuildConnectivity(PROGRESS_REPORTER *aReporter=nullptr)
Build or rebuild the board connectivity database for the board, especially the list of connected item...
const TRACKS & Tracks() const
void Remove(BOARD_ITEM *aBoardItem, REMOVE_MODE aMode=REMOVE_MODE::NORMAL) override
Removes an item from the container.
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
const std::vector< CN_ITEM * > & ConnectedItems() const
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Modify a given item in the model.
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Implement an R-tree for fast spatial and layer indexing of connectable items.
void Insert(BOARD_ITEM *aItem, PCB_LAYER_ID aLayer, int aWorstClearance=0)
Insert an item into the tree on a particular layer with an optional worst clearance.
int QueryColliding(BOARD_ITEM *aRefItem, PCB_LAYER_ID aRefLayer, PCB_LAYER_ID aTargetLayer, std::function< bool(BOARD_ITEM *)> aFilter=nullptr, std::function< bool(BOARD_ITEM *)> aVisitor=nullptr, int aClearance=0) const
This is a fast test which essentially does bounding-box overlap given a worst-case clearance.
void SetFlags(EDA_ITEM_FLAGS aMask)
KICAD_T Type() const
Returns the type of object.
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
LSET is a set of PCB_LAYER_IDs.
static LSET AllCuMask(int aCuLayerCount=MAX_CU_LAYERS)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
A set of BOARD_ITEMs (i.e., without duplicates).
void SetEnd(const VECTOR2I &aEnd)
void SetStart(const VECTOR2I &aStart)
bool ApproxCollinear(const PCB_TRACK &aTrack)
const VECTOR2I & GetStart() const
const VECTOR2I & GetEnd() const
EDA_ITEM_FLAGS IsPointOnEnds(const VECTOR2I &point, int min_dist=0) const
Return STARTPOINT if point if near (dist = min_dist) start point, ENDPOINT if point if near (dist = m...
virtual int GetWidth() const
VECTOR2I GetPosition() const override
VIATYPE GetViaType() const
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
A pure virtual class used to derive REPORTER objects from.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
Represent a set of closed polygons.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const std::vector< BOARD_CONNECTED_ITEM * > & getConnectedItems(PCB_TRACK *aTrack)
bool testMergeCollinearSegments(PCB_TRACK *aSeg1, PCB_TRACK *aSeg2, PCB_TRACK *aDummySeg=nullptr)
helper function test if 2 segments are colinear.
bool filterItem(BOARD_CONNECTED_ITEM *aItem)
void deleteTracksInPads()
void removeItems(std::set< BOARD_ITEM * > &aItems)
void cleanup(bool aDeleteDuplicateVias, bool aDeleteNullSegments, bool aDeleteDuplicateSegments, bool aMergeSegments)
Geometry-based cleanup: duplicate items, null items, colinear items.
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aCleanVias, bool aRemoveMisConnected, bool aMergeSegments, bool aDeleteUnconnected, bool aDeleteTracksinPad, bool aDeleteDanglingVias, REPORTER *aReporter=nullptr)
the cleanup function.
bool mergeCollinearSegments(PCB_TRACK *aSeg1, PCB_TRACK *aSeg2)
helper function merge aTrackRef and aCandidate, when possible, i.e.
bool deleteDanglingTracks(bool aTrack, bool aVia)
Removes tracks or vias only connected on one end.
bool testTrackEndpointIsNode(PCB_TRACK *aTrack, bool aTstStart, bool aTstEnd)
std::map< PCB_TRACK *, std::vector< BOARD_CONNECTED_ITEM * > > m_connectedItemsCache
std::function< bool(BOARD_CONNECTED_ITEM *aItem)> m_filter
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
TRACKS_CLEANER(BOARD *aPcb, BOARD_COMMIT &aCommit)
void removeShortingTrackSegments()
@ CLEANUP_ZERO_LENGTH_TRACK
#define SKIP_STRUCT
flag indicating that the structure should be ignored
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::thread_pool thread_pool
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
@ PCB_ZONE_T
class ZONE, a copper pour area
@ PCB_PAD_T
class PAD, a pad in a footprint
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
VECTOR2< int32_t > VECTOR2I