kicad/pcbnew/drc/drc_test_provider_hole_to_h...

347 lines
12 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2004-2022 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 <common.h>
#include <board_design_settings.h>
#include <footprint.h>
#include <pad.h>
2021-06-11 21:07:02 +00:00
#include <pcb_track.h>
#include <geometry/shape_segment.h>
2020-10-27 20:15:54 +00:00
#include <geometry/shape_circle.h>
#include <drc/drc_engine.h>
#include <drc/drc_item.h>
#include <drc/drc_rule.h>
2020-09-11 16:24:27 +00:00
#include <drc/drc_test_provider_clearance_base.h>
2020-10-27 17:09:27 +00:00
#include "drc_rtree.h"
/*
Holes clearance test. Checks pad and via holes for their mechanical clearances.
Generated errors:
- DRCE_DRILLED_HOLES_TOO_CLOSE
- DRCE_DRILLED_HOLES_COLOCATED
TODO: vias-in-smd-pads check
*/
class DRC_TEST_PROVIDER_HOLE_TO_HOLE : public DRC_TEST_PROVIDER_CLEARANCE_BASE
{
public:
DRC_TEST_PROVIDER_HOLE_TO_HOLE () :
DRC_TEST_PROVIDER_CLEARANCE_BASE(),
2020-10-27 17:09:27 +00:00
m_board( nullptr )
{
}
virtual ~DRC_TEST_PROVIDER_HOLE_TO_HOLE()
{
}
virtual bool Run() override;
virtual const wxString GetName() const override
{
return wxT( "hole_to_hole_clearance" );
};
virtual const wxString GetDescription() const override
{
return wxT( "Tests hole to hole spacing" );
}
2021-01-01 22:29:15 +00:00
virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
2020-09-14 17:54:14 +00:00
int GetNumPhases() const override;
private:
2020-10-28 12:24:10 +00:00
bool testHoleAgainstHole( BOARD_ITEM* aItem, SHAPE_CIRCLE* aHole, BOARD_ITEM* aOther );
2020-10-27 17:09:27 +00:00
BOARD* m_board;
DRC_RTREE m_holeTree;
};
2020-10-27 17:09:27 +00:00
static std::shared_ptr<SHAPE_CIRCLE> getDrilledHoleShape( BOARD_ITEM* aItem )
{
if( aItem->Type() == PCB_VIA_T )
{
2021-06-11 21:07:02 +00:00
PCB_VIA* via = static_cast<PCB_VIA*>( aItem );
2020-10-27 17:09:27 +00:00
return std::make_shared<SHAPE_CIRCLE>( via->GetCenter(), via->GetDrillValue() / 2 );
}
else if( aItem->Type() == PCB_PAD_T )
{
2020-11-12 22:30:02 +00:00
PAD* pad = static_cast<PAD*>( aItem );
2020-10-27 17:09:27 +00:00
return std::make_shared<SHAPE_CIRCLE>( pad->GetPosition(), pad->GetDrillSize().x / 2 );
}
return std::make_shared<SHAPE_CIRCLE>( VECTOR2I( 0, 0 ), 0 );
}
bool DRC_TEST_PROVIDER_HOLE_TO_HOLE::Run()
{
if( m_drcEngine->IsErrorLimitExceeded( DRCE_DRILLED_HOLES_TOO_CLOSE )
&& m_drcEngine->IsErrorLimitExceeded( DRCE_DRILLED_HOLES_COLOCATED ) )
{
reportAux( wxT( "Hole to hole violations ignored. Tests not run." ) );
return true; // continue with other tests
}
m_board = m_drcEngine->GetBoard();
DRC_CONSTRAINT worstClearanceConstraint;
if( m_drcEngine->QueryWorstConstraint( HOLE_TO_HOLE_CONSTRAINT, worstClearanceConstraint ) )
{
m_largestClearance = worstClearanceConstraint.GetValue().Min();
reportAux( wxT( "Worst hole to hole : %d nm" ), m_largestClearance );
}
else
{
reportAux( wxT( "No hole to hole constraints found. Skipping check." ) );
return true; // continue with other tests
}
if( !reportPhase( _( "Checking hole to hole clearances..." ) ) )
return false; // DRC cancelled
2020-10-27 17:09:27 +00:00
// This is the number of tests between 2 calls to the progress bar
const size_t delta = 50;
size_t count = 0;
size_t ii = 0;
m_holeTree.clear();
2020-10-27 17:09:27 +00:00
auto countItems =
[&]( BOARD_ITEM* item ) -> bool
{
if( item->Type() == PCB_PAD_T )
++count;
else if( item->Type() == PCB_VIA_T )
++count;
2020-10-27 17:09:27 +00:00
return true;
};
2020-10-27 17:09:27 +00:00
auto addToHoleTree =
[&]( BOARD_ITEM* item ) -> bool
{
if( !reportProgress( ii++, count, delta ) )
return false;
2020-10-27 17:09:27 +00:00
if( item->Type() == PCB_PAD_T )
{
2020-11-12 22:30:02 +00:00
PAD* pad = static_cast<PAD*>( item );
// We only care about drilled (ie: round) holes
2020-10-27 17:09:27 +00:00
if( pad->GetDrillSize().x && pad->GetDrillSize().x == pad->GetDrillSize().y )
m_holeTree.Insert( item, F_Cu, m_largestClearance );
2020-10-27 17:09:27 +00:00
}
else if( item->Type() == PCB_VIA_T )
{
2021-06-11 21:07:02 +00:00
PCB_VIA* via = static_cast<PCB_VIA*>( item );
2020-09-11 16:24:27 +00:00
// We only care about mechanically drilled (ie: non-laser) holes
2020-10-27 17:09:27 +00:00
if( via->GetViaType() == VIATYPE::THROUGH )
m_holeTree.Insert( item, F_Cu, m_largestClearance );
2020-10-27 17:09:27 +00:00
}
2020-09-11 16:24:27 +00:00
2020-10-27 17:09:27 +00:00
return true;
};
2020-10-27 17:09:27 +00:00
forEachGeometryItem( { PCB_PAD_T, PCB_VIA_T }, LSET::AllLayersMask(), countItems );
2020-10-27 17:09:27 +00:00
count *= 2; // One for adding to tree; one for checking
2020-10-27 17:09:27 +00:00
forEachGeometryItem( { PCB_PAD_T, PCB_VIA_T }, LSET::AllLayersMask(), addToHoleTree );
std::map< std::pair<BOARD_ITEM*, BOARD_ITEM*>, int> checkedPairs;
2021-06-11 21:07:02 +00:00
for( PCB_TRACK* track : m_board->Tracks() )
{
2020-10-27 17:09:27 +00:00
if( track->Type() != PCB_VIA_T )
continue;
2021-06-11 21:07:02 +00:00
PCB_VIA* via = static_cast<PCB_VIA*>( track );
2020-10-27 17:09:27 +00:00
if( !reportProgress( ii++, count, delta ) )
return false; // DRC cancelled
// We only care about mechanically drilled (ie: non-laser) holes
if( via->GetViaType() == VIATYPE::THROUGH )
{
std::shared_ptr<SHAPE_CIRCLE> holeShape = getDrilledHoleShape( via );
2020-09-11 16:24:27 +00:00
m_holeTree.QueryColliding( via, F_Cu, F_Cu,
// Filter:
2020-10-27 17:09:27 +00:00
[&]( BOARD_ITEM* other ) -> bool
{
BOARD_ITEM* a = via;
BOARD_ITEM* b = other;
// store canonical order so we don't collide in both directions
// (a:b and b:a)
if( static_cast<void*>( a ) > static_cast<void*>( b ) )
std::swap( a, b );
if( checkedPairs.count( { a, b } ) )
{
return false;
}
else
{
checkedPairs[ { a, b } ] = 1;
return true;
}
2020-10-27 17:09:27 +00:00
},
// Visitor:
[&]( BOARD_ITEM* other ) -> bool
2020-10-27 17:09:27 +00:00
{
return testHoleAgainstHole( via, holeShape.get(), other );
2020-10-27 17:09:27 +00:00
},
m_largestClearance );
}
}
checkedPairs.clear();
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( PAD* pad : footprint->Pads() )
{
if( !reportProgress( ii++, count, delta ) )
return false; // DRC cancelled
// We only care about drilled (ie: round) holes
if( pad->GetDrillSize().x && pad->GetDrillSize().x == pad->GetDrillSize().y )
{
std::shared_ptr<SHAPE_CIRCLE> holeShape = getDrilledHoleShape( pad );
m_holeTree.QueryColliding( pad, F_Cu, F_Cu,
// 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<void*>( a ) > static_cast<void*>( b ) )
std::swap( a, b );
if( checkedPairs.count( { a, b } ) )
{
return false;
}
else
{
checkedPairs[ { a, b } ] = 1;
return true;
}
},
// Visitor:
[&]( BOARD_ITEM* other ) -> bool
{
return testHoleAgainstHole( pad, holeShape.get(), other );
},
m_largestClearance );
}
}
}
2020-10-27 17:09:27 +00:00
reportRuleStatistics();
2020-10-27 17:09:27 +00:00
return true;
}
bool DRC_TEST_PROVIDER_HOLE_TO_HOLE::testHoleAgainstHole( BOARD_ITEM* aItem, SHAPE_CIRCLE* aHole,
BOARD_ITEM* aOther )
{
bool reportCoLocation = !m_drcEngine->IsErrorLimitExceeded( DRCE_DRILLED_HOLES_COLOCATED );
bool reportHole2Hole = !m_drcEngine->IsErrorLimitExceeded( DRCE_DRILLED_HOLES_TOO_CLOSE );
if( !reportCoLocation && !reportHole2Hole )
2020-10-27 17:09:27 +00:00
return false;
std::shared_ptr<SHAPE_CIRCLE> otherHole = getDrilledHoleShape( aOther );
int epsilon = m_board->GetDesignSettings().GetDRCEpsilon();
SEG::ecoord epsilon_sq = SEG::Square( epsilon );
// Holes at same location generate a separate violation
if( ( aHole->GetCenter() - otherHole->GetCenter() ).SquaredEuclideanNorm() < epsilon_sq )
{
if( reportCoLocation )
{
std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_DRILLED_HOLES_COLOCATED );
drce->SetItems( aItem, aOther );
reportViolation( drce, (wxPoint) aHole->GetCenter() );
}
}
else if( reportHole2Hole )
{
int actual = ( aHole->GetCenter() - otherHole->GetCenter() ).EuclideanNorm();
actual = std::max( 0, actual - aHole->GetRadius() - otherHole->GetRadius() );
auto constraint = m_drcEngine->EvalRules( HOLE_TO_HOLE_CONSTRAINT, aItem, aOther,
UNDEFINED_LAYER /* holes pierce all layers */ );
int minClearance = constraint.GetValue().Min() - epsilon;
if( minClearance >= 0 && actual < minClearance )
{
std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_DRILLED_HOLES_TOO_CLOSE );
wxString msg;
msg.Printf( _( "(%s min %s; actual %s)" ),
constraint.GetName(),
MessageTextFromValue( userUnits(), minClearance ),
MessageTextFromValue( userUnits(), actual ) );
drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + msg );
drce->SetItems( aItem, aOther );
drce->SetViolatingRule( constraint.GetParentRule() );
reportViolation( drce, (wxPoint) aHole->GetCenter() );
}
}
2020-10-27 17:09:27 +00:00
return true;
}
int DRC_TEST_PROVIDER_HOLE_TO_HOLE::GetNumPhases() const
2020-09-14 17:54:14 +00:00
{
2020-10-27 17:09:27 +00:00
return 1;
2020-09-14 17:54:14 +00:00
}
std::set<DRC_CONSTRAINT_T> DRC_TEST_PROVIDER_HOLE_TO_HOLE::GetConstraintTypes() const
{
return { HOLE_TO_HOLE_CONSTRAINT };
}
namespace detail
{
static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_HOLE_TO_HOLE> dummy;
}