/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright The KiCad Developers. * * 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 2 * 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, you may find one here: * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html * or you may search the http://www.gnu.org website for the version 2 license, * or you may write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Copper clearance test. Checks all copper items (pads, vias, tracks, drawings, zones) for their electrical clearance. Errors generated: - DRCE_CLEARANCE - DRCE_HOLE_CLEARANCE - DRCE_TRACKS_CROSSING - DRCE_ZONES_INTERSECT - DRCE_SHORTING_ITEMS */ class DRC_TEST_PROVIDER_COPPER_CLEARANCE : public DRC_TEST_PROVIDER_CLEARANCE_BASE { public: DRC_TEST_PROVIDER_COPPER_CLEARANCE () : DRC_TEST_PROVIDER_CLEARANCE_BASE(), m_drcEpsilon( 0 ) {} virtual ~DRC_TEST_PROVIDER_COPPER_CLEARANCE() = default; virtual bool Run() override; virtual const wxString GetName() const override { return wxT( "clearance" ); }; private: /** * Checks for track/via/hole <-> clearance * @param item Track to text * @param itemShape Primitive track shape * @param layer Which layer to test (in case of vias this can be multiple * @param other item against which to test the track item * @return false if there is a clearance violation reported, true if there is none */ bool testSingleLayerItemAgainstItem( BOARD_ITEM* item, SHAPE* itemShape, PCB_LAYER_ID layer, BOARD_ITEM* other ); void testTrackClearances(); bool testPadAgainstItem( PAD* pad, SHAPE* padShape, PCB_LAYER_ID layer, BOARD_ITEM* other ); void testPadClearances(); void testGraphicClearances(); void testZonesToZones(); void testItemAgainstZone( BOARD_ITEM* aItem, ZONE* aZone, PCB_LAYER_ID aLayer ); void testKnockoutTextAgainstZone( BOARD_ITEM* aText, NETINFO_ITEM** aInheritedNet, ZONE* aZone ); private: int m_drcEpsilon; }; bool DRC_TEST_PROVIDER_COPPER_CLEARANCE::Run() { m_board = m_drcEngine->GetBoard(); if( m_board->m_DRCMaxClearance <= 0 ) { REPORT_AUX( wxT( "No Clearance constraints found. Tests not run." ) ); return true; // continue with other tests } m_drcEpsilon = m_board->GetDesignSettings().GetDRCEpsilon(); if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) ) { if( !reportPhase( _( "Checking track & via clearances..." ) ) ) return false; // DRC cancelled testTrackClearances(); } else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) ) { if( !reportPhase( _( "Checking hole clearances..." ) ) ) return false; // DRC cancelled testTrackClearances(); } if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) ) { if( !reportPhase( _( "Checking pad clearances..." ) ) ) return false; // DRC cancelled testPadClearances(); } else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS ) || !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ) ) { if( !reportPhase( _( "Checking pads..." ) ) ) return false; // DRC cancelled testPadClearances(); } if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) ) { if( !reportPhase( _( "Checking copper graphic clearances..." ) ) ) return false; // DRC cancelled testGraphicClearances(); } if( !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ) ) { if( !reportPhase( _( "Checking copper zone clearances..." ) ) ) return false; // DRC cancelled testZonesToZones(); } else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_ZONES_INTERSECT ) ) { if( !reportPhase( _( "Checking zones..." ) ) ) return false; // DRC cancelled testZonesToZones(); } return !m_drcEngine->IsCancelled(); } bool DRC_TEST_PROVIDER_COPPER_CLEARANCE::testSingleLayerItemAgainstItem( BOARD_ITEM* item, SHAPE* itemShape, PCB_LAYER_ID layer, BOARD_ITEM* other ) { bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ); bool testShorting = !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS ); bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ); DRC_CONSTRAINT constraint; int clearance = -1; int actual; VECTOR2I pos; bool has_error = false; NETINFO_ITEM* net = nullptr; NETINFO_ITEM* otherNet = nullptr; if( BOARD_CONNECTED_ITEM* connectedItem = dynamic_cast( item ) ) net = connectedItem->GetNet(); NETINFO_ITEM* trackNet = net; if( BOARD_CONNECTED_ITEM* connectedItem = dynamic_cast( other ) ) otherNet = connectedItem->GetNet(); std::shared_ptr otherShapeStorage = other->GetEffectiveShape( layer ); SHAPE* otherShape = otherShapeStorage.get(); if( other->Type() == PCB_PAD_T ) { PAD* pad = static_cast( other ); if( pad->GetAttribute() == PAD_ATTRIB::NPTH && !pad->FlashLayer( layer ) ) testClearance = testShorting = false; } if( testClearance || testShorting ) { constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, item, other, layer ); clearance = constraint.GetValue().Min(); } if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 ) { // Collide (and generate violations) based on a well-defined order so that exclusion // checking against previously-generated violations will work. if( item->m_Uuid > other->m_Uuid ) { std::swap( item, other ); std::swap( itemShape, otherShape ); std::swap( net, otherNet ); } // Special processing for track:track intersections if( item->Type() == PCB_TRACE_T && other->Type() == PCB_TRACE_T ) { PCB_TRACK* track = static_cast( item ); PCB_TRACK* otherTrack = static_cast( other ); SEG trackSeg( track->GetStart(), track->GetEnd() ); SEG otherSeg( otherTrack->GetStart(), otherTrack->GetEnd() ); if( OPT_VECTOR2I intersection = trackSeg.Intersect( otherSeg ) ) { std::shared_ptr drcItem = DRC_ITEM::Create( DRCE_TRACKS_CROSSING ); drcItem->SetItems( item, other ); drcItem->SetViolatingRule( constraint.GetParentRule() ); reportViolation( drcItem, *intersection, layer ); return false; } } if( itemShape->Collide( otherShape, clearance - m_drcEpsilon, &actual, &pos ) ) { if( trackNet && m_drcEngine->IsNetTieExclusion( trackNet->GetNetCode(), layer, pos, other ) ) { // Collision occurred as track was entering a pad marked as a net-tie. We // allow these. } else if( actual == 0 && otherNet && testShorting ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_SHORTING_ITEMS ); wxString msg; msg.Printf( _( "(nets %s and %s)" ), net ? net->GetNetname() : _( "" ), otherNet ? otherNet->GetNetname() : _( "" ) ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( item, other ); reportViolation( drce, pos, layer ); has_error = true; if( !m_drcEngine->GetReportAllTrackErrors() ) return false; } else if( testClearance ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( item, other ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, layer, item, other, layer, actual ); has_error = true; if( !m_drcEngine->GetReportAllTrackErrors() ) return false; } } } if( testHoles && ( item->HasHole() || other->HasHole() ) ) { std::array a{ item, other }; std::array b{ other, item }; std::array a_shape{ itemShape, otherShape }; for( size_t ii = 0; ii < 2; ++ii ) { std::shared_ptr holeShape; // We only test a track item here against an item with a hole. // If either case is not valid, simply move on if( !( dynamic_cast( a[ii] ) ) || !b[ii]->HasHole() ) continue; if( b[ii]->Type() == PCB_VIA_T ) { if( b[ii]->GetLayerSet().Contains( layer ) ) holeShape = b[ii]->GetEffectiveHoleShape(); } else { holeShape = b[ii]->GetEffectiveHoleShape(); } constraint = m_drcEngine->EvalRules( HOLE_CLEARANCE_CONSTRAINT, b[ii], a[ii], layer ); clearance = constraint.GetValue().Min(); // Test for hole to item clearance even if clearance is 0, because the item cannot be // inside (or intersect) the hole. if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE ) { if( a_shape[ii]->Collide( holeShape.get(), std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE ); wxString msg = formatMsg( clearance ? _( "(%s clearance %s; actual %s)" ) : _( "(%s clearance %s; actual < 0)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( a[ii], b[ii] ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, layer, a[ii], b[ii], layer, actual ); return false; } } } } return !has_error; } void DRC_TEST_PROVIDER_COPPER_CLEARANCE::testItemAgainstZone( BOARD_ITEM* aItem, ZONE* aZone, PCB_LAYER_ID aLayer ) { if( !aZone->GetLayerSet().test( aLayer ) ) return; if( aZone->GetNetCode() && aItem->IsConnected() ) { if( aZone->GetNetCode() == static_cast( aItem )->GetNetCode() ) return; } BOX2I itemBBox = aItem->GetBoundingBox(); BOX2I worstCaseBBox = itemBBox; worstCaseBBox.Inflate( m_board->m_DRCMaxClearance ); if( !worstCaseBBox.Intersects( aZone->GetBoundingBox() ) ) return; FOOTPRINT* parentFP = aItem->GetParentFootprint(); // Ignore graphic items which implement a net-tie to the zone's net on the layer being tested. if( parentFP && parentFP->IsNetTie() && dynamic_cast( aItem ) ) { std::set allowedNetTiePads; for( PAD* pad : parentFP->Pads() ) { if( pad->GetNetCode() == aZone->GetNetCode() && aZone->GetNetCode() != 0 ) { if( pad->IsOnLayer( aLayer ) ) allowedNetTiePads.insert( pad ); for( PAD* other : parentFP->GetNetTiePads( pad ) ) { if( other->IsOnLayer( aLayer ) ) allowedNetTiePads.insert( other ); } } } if( !allowedNetTiePads.empty() ) { std::shared_ptr itemShape = aItem->GetEffectiveShape(); for( PAD* pad : allowedNetTiePads ) { if( pad->GetBoundingBox().Intersects( itemBBox ) && pad->GetEffectiveShape( aLayer )->Collide( itemShape.get() ) ) { return; } } } } bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ); bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ); if( !testClearance && !testHoles ) return; DRC_RTREE* zoneTree = m_board->m_CopperZoneRTreeCache[ aZone ].get(); if( !zoneTree ) return; DRC_CONSTRAINT constraint; int clearance = -1; int actual; VECTOR2I pos; if( aItem->Type() == PCB_PAD_T ) { PAD* pad = static_cast( aItem ); bool flashedPad = pad->FlashLayer( aLayer ); bool platedHole = pad->HasHole() && pad->GetAttribute() == PAD_ATTRIB::PTH; if( !flashedPad && !platedHole ) testClearance = false; } if( testClearance ) { constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, aItem, aZone, aLayer ); clearance = constraint.GetValue().Min(); } if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 ) { std::shared_ptr itemShape = aItem->GetEffectiveShape( aLayer, FLASHING::DEFAULT ); if( zoneTree->QueryColliding( itemBBox, itemShape.get(), aLayer, std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( aItem, aZone ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, aLayer, aItem, aZone, aLayer, actual ); } } if( testHoles && aItem->HasHole() ) { std::shared_ptr holeShape; if( aItem->Type() == PCB_VIA_T ) { if( aItem->GetLayerSet().Contains( aLayer ) ) holeShape = aItem->GetEffectiveHoleShape(); } else { holeShape = aItem->GetEffectiveHoleShape(); } if( holeShape ) { constraint = m_drcEngine->EvalRules( HOLE_CLEARANCE_CONSTRAINT, aItem, aZone, aLayer ); clearance = constraint.GetValue().Min(); if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 ) { if( zoneTree->QueryColliding( itemBBox, holeShape.get(), aLayer, std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( aItem, aZone ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, aLayer, aItem, aZone, aLayer, actual ); } } } } } /* * We have to special-case knockout text as it's most often knocked-out of a zone, so it's * presumed to collide with one. However, if it collides with more than one, and they have * different nets, then we have a short. */ void DRC_TEST_PROVIDER_COPPER_CLEARANCE::testKnockoutTextAgainstZone( BOARD_ITEM* aText, NETINFO_ITEM** aInheritedNet, ZONE* aZone ) { bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ); bool testShorts = !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS ); if( !testClearance && !testShorts ) return; PCB_LAYER_ID layer = aText->GetLayer(); if( !aZone->GetLayerSet().test( layer ) ) return; BOX2I itemBBox = aText->GetBoundingBox(); BOX2I worstCaseBBox = itemBBox; worstCaseBBox.Inflate( m_board->m_DRCMaxClearance ); if( !worstCaseBBox.Intersects( aZone->GetBoundingBox() ) ) return; DRC_RTREE* zoneTree = m_board->m_CopperZoneRTreeCache[ aZone ].get(); if( !zoneTree ) return; std::shared_ptr itemShape = aText->GetEffectiveShape( layer, FLASHING::DEFAULT ); if( *aInheritedNet == nullptr ) { if( zoneTree->QueryColliding( itemBBox, itemShape.get(), layer ) ) *aInheritedNet = aZone->GetNet(); } if( *aInheritedNet == aZone->GetNet() ) return; DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, aText, aZone, layer ); int clearance = constraint.GetValue().Min(); int actual; VECTOR2I pos; if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance >= 0 ) { if( zoneTree->QueryColliding( itemBBox, itemShape.get(), layer, std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce; wxString msg; if( testShorts && actual == 0 && *aInheritedNet ) { drce = DRC_ITEM::Create( DRCE_SHORTING_ITEMS ); msg.Printf( _( "(nets %s and %s)" ), ( *aInheritedNet )->GetNetname(), aZone->GetNetname() ); } else { drce = DRC_ITEM::Create( DRCE_CLEARANCE ); msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); } drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( aText, aZone ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, layer, aText, aZone, layer, actual ); } } } void DRC_TEST_PROVIDER_COPPER_CLEARANCE::testTrackClearances() { std::map freePadsUsageMap; std::unordered_map checkedPairs; std::mutex checkedPairsMutex; std::mutex freePadsUsageMapMutex; std::atomic done( 0 ); size_t count = m_board->Tracks().size(); REPORT_AUX( wxString::Format( wxT( "Testing %d tracks & vias..." ), count ) ); LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() ); auto testTrack = [&]( const int trackIdx ) { PCB_TRACK* track = m_board->Tracks()[trackIdx]; for( PCB_LAYER_ID layer : LSET( track->GetLayerSet() & boardCopperLayers ) ) { std::shared_ptr trackShape = track->GetEffectiveShape( layer ); m_board->m_CopperItemRTreeCache->QueryColliding( track, layer, layer, // Filter: [&]( BOARD_ITEM* other ) -> bool { BOARD_CONNECTED_ITEM* otherCItem = dynamic_cast( other ); if( otherCItem && otherCItem->GetNetCode() == track->GetNetCode() ) return false; BOARD_ITEM* a = track; BOARD_ITEM* b = other; // store canonical order so we don't collide in both directions // (a:b and b:a) if( static_cast( a ) > static_cast( b ) ) std::swap( a, b ); std::lock_guard lock( checkedPairsMutex ); auto it = checkedPairs.find( { a, b } ); if( it != checkedPairs.end() && ( it->second.layers.test( layer ) || ( it->second.has_error ) ) ) { return false; } else { checkedPairs[ { a, b } ].layers.set( layer ); return true; } }, // Visitor: [&]( BOARD_ITEM* other ) -> bool { if( m_drcEngine->IsCancelled() ) return false; if( other->Type() == PCB_PAD_T && static_cast( other )->IsFreePad() ) { if( other->GetEffectiveShape( layer )->Collide( trackShape.get() ) ) { std::lock_guard lock( freePadsUsageMapMutex ); auto it = freePadsUsageMap.find( other ); if( it == freePadsUsageMap.end() ) { freePadsUsageMap[ other ] = track->GetNetCode(); return true; // Continue colliding tests } else if( it->second == track->GetNetCode() ) { return true; // Continue colliding tests } } } // If we get an error, mark the pair as having a clearance error already if( !testSingleLayerItemAgainstItem( track, trackShape.get(), layer, other ) ) { if( !m_drcEngine->GetReportAllTrackErrors() ) { BOARD_ITEM* a = track; BOARD_ITEM* b = other; // store canonical order so we don't collide in both directions // (a:b and b:a) if( static_cast( a ) > static_cast( b ) ) std::swap( a, b ); std::lock_guard lock( checkedPairsMutex ); auto it = checkedPairs.find( { a, b } ); if( it != checkedPairs.end() ) it->second.has_error = true; return false; // We're done with this track } } return !m_drcEngine->IsCancelled(); }, m_board->m_DRCMaxClearance ); for( ZONE* zone : m_board->m_DRCCopperZones ) { testItemAgainstZone( track, zone, layer ); if( m_drcEngine->IsCancelled() ) break; } } done.fetch_add( 1 ); }; thread_pool& tp = GetKiCadThreadPool(); auto track_futures = tp.submit_loop( 0, m_board->Tracks().size(), testTrack ); while( done < count ) { reportProgress( done, count ); if( m_drcEngine->IsCancelled() ) { // Wait for the submitted loop tasks to finish track_futures.wait(); break; } std::this_thread::sleep_for( std::chrono::milliseconds( 250 ) ); } } bool DRC_TEST_PROVIDER_COPPER_CLEARANCE::testPadAgainstItem( PAD* pad, SHAPE* padShape, PCB_LAYER_ID aLayer, BOARD_ITEM* other ) { bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ); bool testShorting = !m_drcEngine->IsErrorLimitExceeded( DRCE_SHORTING_ITEMS ); bool testHoles = !m_drcEngine->IsErrorLimitExceeded( DRCE_HOLE_CLEARANCE ); // Disable some tests for net-tie objects in a footprint if( other->GetParent() == pad->GetParent() ) { FOOTPRINT* fp = pad->GetParentFootprint(); std::map padToNetTieGroupMap = fp->MapPadNumbersToNetTieGroups(); int padGroupIdx = padToNetTieGroupMap[ pad->GetNumber() ]; if( other->Type() == PCB_PAD_T ) { PAD* otherPad = static_cast( other ); if( padGroupIdx >= 0 && padGroupIdx == padToNetTieGroupMap[ otherPad->GetNumber() ] ) testClearance = testShorting = false; if( pad->SameLogicalPadAs( otherPad ) ) testHoles = false; } if( other->Type() == PCB_SHAPE_T && padGroupIdx >= 0 ) testClearance = testShorting = false; } BOARD_CONNECTED_ITEM* otherCItem = dynamic_cast( other ); PAD* otherPad = nullptr; PCB_VIA* otherVia = nullptr; if( other->Type() == PCB_PAD_T ) otherPad = static_cast( other ); if( other->Type() == PCB_VIA_T ) otherVia = static_cast( other ); if( !IsCopperLayer( aLayer ) ) testClearance = testShorting = false; // A NPTH has no cylinder, but it may still have pads on some layers if( pad->GetAttribute() == PAD_ATTRIB::NPTH && !pad->FlashLayer( aLayer ) ) testClearance = testShorting = false; if( otherPad && otherPad->GetAttribute() == PAD_ATTRIB::NPTH && !otherPad->FlashLayer( aLayer ) ) testClearance = testShorting = false; // Track clearances are tested in testTrackClearances() if( dynamic_cast( other) ) testClearance = testShorting = false; int padNet = pad->GetNetCode(); int otherNet = otherCItem ? otherCItem->GetNetCode() : 0; // Other objects of the same (defined) net get a waiver on clearance and hole tests if( otherNet && otherNet == padNet ) { testClearance = testShorting = false; testHoles = false; } if( !( pad->GetDrillSize().x > 0 ) && !( otherPad && otherPad->GetDrillSize().x > 0 ) && !( otherVia && otherVia->GetDrill() > 0 ) ) { testHoles = false; } if( !testClearance && !testShorting && !testHoles ) return true; std::shared_ptr otherShape = other->GetEffectiveShape( aLayer ); DRC_CONSTRAINT constraint; int clearance = 0; int actual = 0; VECTOR2I pos; bool has_error = false; if( otherPad && pad->SameLogicalPadAs( otherPad ) ) { // If pads are equivalent (ie: from the same footprint with the same pad number)... // ... and have "real" nets... // then they must be the same net if( testShorting ) { if( pad->GetNetCode() == 0 || pad->GetNetCode() == otherPad->GetNetCode() ) return true; if( pad->GetShortNetname().StartsWith( wxS( "unconnected-(" ) ) && otherPad->GetShortNetname().StartsWith( wxS( "unconnected-(" ) ) ) { return true; } std::shared_ptr drce = DRC_ITEM::Create( DRCE_SHORTING_ITEMS ); wxString msg; msg.Printf( _( "(nets %s and %s)" ), pad->GetNetname(), otherPad->GetNetname() ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( pad, otherPad ); reportViolation( drce, otherPad->GetPosition(), aLayer ); has_error = true; } return !has_error; } if( testClearance || testShorting ) { constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, pad, other, aLayer ); clearance = constraint.GetValue().Min(); if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 ) { if( padShape->Collide( otherShape.get(), std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { if( m_drcEngine->IsNetTieExclusion( pad->GetNetCode(), aLayer, pos, other ) ) { // Pads connected to pads of a net-tie footprint are allowed to collide // with the net-tie footprint's graphics. } else if( actual == 0 && padNet && otherNet && testShorting ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_SHORTING_ITEMS ); wxString msg = wxString::Format( _( "(nets %s and %s)" ), pad->GetNetname(), otherCItem->GetNetname() ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( pad, other ); reportViolation( drce, pos, aLayer ); has_error = true; testHoles = false; // No need for multiple violations } else if( testClearance ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( pad, other ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, aLayer, pad, other, aLayer, actual ); has_error = true; testHoles = false; // No need for multiple violations } } } } if( testHoles ) { constraint = m_drcEngine->EvalRules( HOLE_CLEARANCE_CONSTRAINT, pad, other, aLayer ); clearance = constraint.GetValue().Min(); if( constraint.GetSeverity() == RPT_SEVERITY_IGNORE ) testHoles = false; } if( testHoles && otherPad && pad->FlashLayer( aLayer ) && otherPad->HasHole() ) { if( clearance > 0 && padShape->Collide( otherPad->GetEffectiveHoleShape().get(), std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( pad, other ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, aLayer, pad, other, aLayer, actual ); has_error = true; testHoles = false; // No need for multiple violations } } if( testHoles && otherPad && otherPad->FlashLayer( aLayer ) && pad->HasHole() ) { if( clearance > 0 && otherShape->Collide( pad->GetEffectiveHoleShape().get(), std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( pad, other ); drce->SetViolatingRule( constraint.GetParentRule() ); reportViolation( drce, pos, aLayer ); has_error = true; testHoles = false; // No need for multiple violations } } if( testHoles && otherVia && otherVia->IsOnLayer( aLayer ) ) { if( clearance > 0 && padShape->Collide( otherVia->GetEffectiveHoleShape().get(), std::max( 0, clearance - m_drcEpsilon ), &actual, &pos ) ) { std::shared_ptr drce = DRC_ITEM::Create( DRCE_HOLE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), clearance, actual ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( pad, otherVia ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pos, aLayer, pad, otherVia, aLayer, actual ); has_error = true; } } return !has_error; } void DRC_TEST_PROVIDER_COPPER_CLEARANCE::testPadClearances( ) { thread_pool& tp = GetKiCadThreadPool(); std::atomic done( 1 ); std::unordered_map checkedPairs; std::mutex checkedPairsMutex; LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() ); const auto fp_check = [&]( size_t ii ) { FOOTPRINT* footprint = m_board->Footprints()[ ii ]; for( PAD* pad : footprint->Pads() ) { for( PCB_LAYER_ID layer : LSET( pad->GetLayerSet() & boardCopperLayers ) ) { if( m_drcEngine->IsCancelled() ) return; std::shared_ptr padShape = pad->GetEffectiveShape( layer ); m_board->m_CopperItemRTreeCache->QueryColliding( pad, layer, layer, // Filter: [&]( BOARD_ITEM* other ) -> bool { BOARD_ITEM* a = pad; BOARD_ITEM* b = other; // store canonical order so we don't collide in both // directions (a:b and b:a) if( static_cast( a ) > static_cast( b ) ) std::swap( a, b ); std::lock_guard lock( checkedPairsMutex ); auto it = checkedPairs.find( { a, b } ); if( it != checkedPairs.end() && ( it->second.layers.test( layer ) || it->second.has_error ) ) { return false; } else { checkedPairs[ { a, b } ].layers.set( layer ); return true; } }, // Visitor [&]( BOARD_ITEM* other ) -> bool { if( !testPadAgainstItem( pad, padShape.get(), layer, other ) ) { BOARD_ITEM* a = pad; BOARD_ITEM* b = other; std::lock_guard lock( checkedPairsMutex ); auto it = checkedPairs.find( { a, b } ); if( it != checkedPairs.end() ) it->second.has_error = true; } return !m_drcEngine->IsCancelled(); }, m_board->m_DRCMaxClearance ); for( ZONE* zone : m_board->m_DRCCopperZones ) { testItemAgainstZone( pad, zone, layer ); if( m_drcEngine->IsCancelled() ) return; } } } done.fetch_add( 1 ); }; size_t numFootprints = m_board->Footprints().size(); auto returns = tp.submit_loop( 0, numFootprints, fp_check ); // Wait for all threads to finish for( size_t ii = 0; ii < returns.size(); ++ii ) { while( returns[ii].wait_for( std::chrono::milliseconds( 250 ) ) != std::future_status::ready ) reportProgress( done, numFootprints ); } } void DRC_TEST_PROVIDER_COPPER_CLEARANCE::testGraphicClearances() { thread_pool& tp = GetKiCadThreadPool(); size_t count = m_board->Drawings().size(); std::atomic done( 1 ); for( FOOTPRINT* footprint : m_board->Footprints() ) count += footprint->GraphicalItems().size(); REPORT_AUX( wxString::Format( wxT( "Testing %d graphics..." ), count ) ); auto isKnockoutText = []( BOARD_ITEM* item ) { return item->Type() == PCB_TEXT_T && static_cast( item )->IsKnockout(); }; auto testGraphicAgainstZone = [&]( BOARD_ITEM* item ) { if( item->Type() == PCB_REFERENCE_IMAGE_T ) return; if( !IsCopperLayer( item->GetLayer() ) ) return; // Knockout text is most often knocked-out of a zone, so it's presumed to // collide with one. However, if it collides with more than one, and they // have different nets, then we have a short. NETINFO_ITEM* inheritedNet = nullptr; for( ZONE* zone : m_board->m_DRCCopperZones ) { if( isKnockoutText( item ) ) testKnockoutTextAgainstZone( item, &inheritedNet, zone ); else testItemAgainstZone( item, zone, item->GetLayer() ); if( m_drcEngine->IsCancelled() ) return; } }; std::unordered_map checkedPairs; std::mutex checkedPairsMutex; auto testCopperGraphic = [&]( PCB_SHAPE* aShape ) { PCB_LAYER_ID layer = aShape->GetLayer(); m_board->m_CopperItemRTreeCache->QueryColliding( aShape, layer, layer, // Filter: [&]( BOARD_ITEM* other ) -> bool { auto otherCItem = dynamic_cast( other ); if( otherCItem && otherCItem->GetNetCode() == aShape->GetNetCode() ) return false; // Pads and tracks handled separately if( other->Type() == PCB_PAD_T || other->Type() == PCB_ARC_T || other->Type() == PCB_TRACE_T || other->Type() == PCB_VIA_T ) { return false; } BOARD_ITEM* a = aShape; BOARD_ITEM* b = other; // store canonical order so we don't collide in both directions // (a:b and b:a) if( static_cast( a ) > static_cast( b ) ) std::swap( a, b ); std::lock_guard lock( checkedPairsMutex ); auto it = checkedPairs.find( { a, b } ); if( it != checkedPairs.end() && it->second.layers.test( layer ) ) { return false; } else { checkedPairs[ { a, b } ].layers.set( layer ); return true; } }, // Visitor: [&]( BOARD_ITEM* other ) -> bool { testSingleLayerItemAgainstItem( aShape, aShape->GetEffectiveShape().get(), layer, other ); return !m_drcEngine->IsCancelled(); }, m_board->m_DRCMaxClearance ); }; std::future retn = tp.submit_task( [&]() { for( BOARD_ITEM* item : m_board->Drawings() ) { testGraphicAgainstZone( item ); if( item->Type() == PCB_SHAPE_T && item->IsOnCopperLayer() ) testCopperGraphic( static_cast( item ) ); done.fetch_add( 1 ); if( m_drcEngine->IsCancelled() ) break; } for( FOOTPRINT* footprint : m_board->Footprints() ) { for( BOARD_ITEM* item : footprint->GraphicalItems() ) { testGraphicAgainstZone( item ); done.fetch_add( 1 ); if( m_drcEngine->IsCancelled() ) break; } } } ); std::future_status status = retn.wait_for( std::chrono::milliseconds( 250 ) ); while( status != std::future_status::ready ) { reportProgress( done, count ); status = retn.wait_for( std::chrono::milliseconds( 250 ) ); } } void DRC_TEST_PROVIDER_COPPER_CLEARANCE::testZonesToZones() { bool testClearance = !m_drcEngine->IsErrorLimitExceeded( DRCE_CLEARANCE ); bool testIntersects = !m_drcEngine->IsErrorLimitExceeded( DRCE_ZONES_INTERSECT ); std::vector>> poly_segments; poly_segments.resize( m_board->m_DRCCopperZones.size() ); thread_pool& tp = GetKiCadThreadPool(); std::atomic done( 0 ); size_t count = 0; auto reportZoneZoneViolation = [this]( ZONE* zoneA, ZONE* zoneB, VECTOR2I& pt, int actual, const DRC_CONSTRAINT& constraint, PCB_LAYER_ID layer ) -> void { std::shared_ptr drce; if( constraint.IsNull() ) { drce = DRC_ITEM::Create( DRCE_ZONES_INTERSECT ); wxString msg = _( "(intersecting zones must have distinct priorities)" ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( zoneA, zoneB ); reportViolation( drce, pt, layer ); } else { drce = DRC_ITEM::Create( DRCE_CLEARANCE ); wxString msg = formatMsg( _( "(%s clearance %s; actual %s)" ), constraint.GetName(), constraint.GetValue().Min(), std::max( actual, 0 ) ); drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg ); drce->SetItems( zoneA, zoneB ); drce->SetViolatingRule( constraint.GetParentRule() ); ReportAndShowPathCuToCu( drce, pt, layer, zoneA, zoneB, layer, actual ); } }; auto checkZones = [this, testClearance, testIntersects, reportZoneZoneViolation, &poly_segments, &done]( int zoneA_idx, int zoneB_idx, bool sameNet, PCB_LAYER_ID layer ) -> void { ZONE* zoneA = m_board->m_DRCCopperZones[zoneA_idx]; ZONE* zoneB = m_board->m_DRCCopperZones[zoneB_idx]; int actual = 0; VECTOR2I pt; if( sameNet && testIntersects ) { if( zoneA->Outline()->Collide( zoneB->Outline(), 0, &actual, &pt ) ) { done.fetch_add( 1 ); reportZoneZoneViolation( zoneA, zoneB, pt, actual, DRC_CONSTRAINT(), layer ); return; } } else if( !sameNet && testClearance ) { DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( CLEARANCE_CONSTRAINT, zoneA, zoneB, layer ); int clearance = constraint.GetValue().Min(); if( constraint.GetSeverity() != RPT_SEVERITY_IGNORE && clearance > 0 ) { std::map conflictPoints; std::vector& refSegments = poly_segments[zoneA_idx][layer]; std::vector& testSegments = poly_segments[zoneB_idx][layer]; // Iterate through all the segments in zoneA for( SEG& refSegment : refSegments ) { // Iterate through all the segments in zoneB for( SEG& testSegment : testSegments ) { // We have ensured that the 'A' segment starts before the 'B' segment, so if the // 'A' segment ends before the 'B' segment starts, we can skip to the next 'A' if( refSegment.B.x < testSegment.A.x ) break; int64_t dist_sq = 0; VECTOR2I other_pt; refSegment.NearestPoints( testSegment, pt, other_pt, dist_sq ); actual = std::floor( std::sqrt( dist_sq ) + 0.5 ); if( actual < clearance ) { done.fetch_add( 1 ); reportZoneZoneViolation( zoneA, zoneB, pt, actual, constraint, layer ); return; } } } } } done.fetch_add( 1 ); }; // Pre-sort zones into layers std::map> zone_idx_by_layer; for ( size_t ii = 0; ii < m_board->m_DRCCopperZones.size(); ii++ ) { ZONE* zone = m_board->m_DRCCopperZones[ii]; for( PCB_LAYER_ID layer : zone->GetLayerSet() ) { if( !IsCopperLayer( layer ) ) continue; zone_idx_by_layer[layer].push_back( ii ); } } for( PCB_LAYER_ID layer : LAYER_RANGE( F_Cu, B_Cu, m_board->GetCopperLayerCount() ) ) { // Skip over layers not used on the current board if( !m_board->IsLayerEnabled( layer ) ) continue; for( size_t ii : zone_idx_by_layer[layer] ) { if( SHAPE_POLY_SET* poly = m_board->m_DRCCopperZones[ii]->GetFill( layer ) ) { std::vector& zone_layer_poly_segs = poly_segments[ii][layer]; zone_layer_poly_segs.reserve( poly->FullPointCount() ); for( auto it = poly->IterateSegmentsWithHoles(); it; it++ ) { SEG seg = *it; if( seg.A.x > seg.B.x ) seg.Reverse(); zone_layer_poly_segs.push_back( seg ); } std::sort( zone_layer_poly_segs.begin(), zone_layer_poly_segs.end() ); } } for( auto it_a = zone_idx_by_layer[layer].begin(); it_a != zone_idx_by_layer[layer].end(); ++it_a ) { size_t ia = *it_a; ZONE* zoneA = m_board->m_DRCCopperZones[ia]; for( auto it_a2 = std::next( it_a ); it_a2 != zone_idx_by_layer[layer].end(); ++it_a2 ) { size_t ia2 = *it_a2; ZONE* zoneB = m_board->m_DRCCopperZones[ia2]; bool sameNet = zoneA->GetNetCode() == zoneB->GetNetCode() && zoneA->GetNetCode() >= 0; if( sameNet && zoneA->GetAssignedPriority() != zoneB->GetAssignedPriority() ) continue; // rule areas may overlap at will if( zoneA->GetIsRuleArea() || zoneB->GetIsRuleArea() ) continue; // Examine a candidate zone: compare zoneB to zoneA SHAPE_POLY_SET* polyA = nullptr; SHAPE_POLY_SET* polyB = nullptr; if( sameNet ) { polyA = zoneA->Outline(); polyB = zoneB->Outline(); } else { polyA = zoneA->GetFill( layer ); polyB = zoneB->GetFill( layer ); } if( !polyA->BBoxFromCaches().Intersects( polyB->BBoxFromCaches() ) ) continue; count++; tp.submit_task( [checkZones, ia, ia2, sameNet, layer]() { checkZones(ia, ia2, sameNet, layer); } ); } } } while( true ) { reportProgress( done, count ); if( m_drcEngine->IsCancelled() ) break; if( tp.wait_for( std::chrono::milliseconds( 250 ) ) ) break; } } namespace detail { static DRC_REGISTER_TEST_PROVIDER dummy; }