2020-08-25 17:42:52 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2020 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 <geometry/shape_poly_set.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <drc/drc_engine.h>
|
2020-09-11 15:04:11 +00:00
|
|
|
#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>
|
2021-06-03 18:05:43 +00:00
|
|
|
#include <footprint.h>
|
2020-08-25 17:42:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Couartyard clearance. Tests for malformed component courtyards and overlapping footprints.
|
|
|
|
Generated errors:
|
|
|
|
- DRCE_OVERLAPPING_FOOTPRINTS
|
|
|
|
- DRCE_MISSING_COURTYARD
|
|
|
|
- DRCE_MALFORMED_COURTYARD
|
|
|
|
|
|
|
|
TODO: do an actual clearance check instead of polygon intersection. Treat closed outlines
|
|
|
|
as filled and allow open curves in the courtyard.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DRC_TEST_PROVIDER_COURTYARD_CLEARANCE : public DRC_TEST_PROVIDER_CLEARANCE_BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DRC_TEST_PROVIDER_COURTYARD_CLEARANCE ()
|
|
|
|
{
|
|
|
|
m_isRuleDriven = false;
|
|
|
|
}
|
|
|
|
|
2021-06-06 19:03:10 +00:00
|
|
|
virtual ~DRC_TEST_PROVIDER_COURTYARD_CLEARANCE ()
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Run() override;
|
|
|
|
|
2021-06-06 19:03:10 +00:00
|
|
|
virtual const wxString GetName() const override
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
|
|
|
return "courtyard_clearance";
|
2020-09-11 16:24:27 +00:00
|
|
|
}
|
2020-08-25 17:42:52 +00:00
|
|
|
|
|
|
|
virtual const wxString GetDescription() const override
|
|
|
|
{
|
2020-12-18 01:44:16 +00:00
|
|
|
return "Tests footprints' courtyard clearance";
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 22:29:15 +00:00
|
|
|
virtual std::set<DRC_CONSTRAINT_T> GetConstraintTypes() const override;
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-09-14 17:54:14 +00:00
|
|
|
int GetNumPhases() const override;
|
|
|
|
|
2020-08-25 17:42:52 +00:00
|
|
|
private:
|
2021-02-27 13:43:41 +00:00
|
|
|
bool testFootprintCourtyardDefinitions();
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2021-02-27 13:43:41 +00:00
|
|
|
bool testCourtyardClearances();
|
2020-08-25 17:42:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-02-27 13:43:41 +00:00
|
|
|
bool DRC_TEST_PROVIDER_COURTYARD_CLEARANCE::testFootprintCourtyardDefinitions()
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2020-09-16 15:03:55 +00:00
|
|
|
const int delta = 100; // This is the number of tests between 2 calls to the progress bar
|
|
|
|
|
2020-09-11 21:50:53 +00:00
|
|
|
// Detects missing (or malformed) footprint courtyards
|
2021-02-27 13:43:41 +00:00
|
|
|
if( !m_drcEngine->IsErrorLimitExceeded( DRCE_MALFORMED_COURTYARD)
|
|
|
|
|| !m_drcEngine->IsErrorLimitExceeded( DRCE_MISSING_COURTYARD) )
|
|
|
|
{
|
|
|
|
if( !reportPhase( _( "Checking footprint courtyard definitions..." ) ) )
|
|
|
|
return false; // DRC cancelled
|
|
|
|
}
|
|
|
|
else if( !m_drcEngine->IsErrorLimitExceeded( DRCE_OVERLAPPING_FOOTPRINTS) )
|
|
|
|
{
|
|
|
|
if( !reportPhase( _( "Gathering footprint courtyards..." ) ) )
|
|
|
|
return false; // DRC cancelled
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reportAux( "All courtyard violations ignored. Tests not run." );
|
|
|
|
return true; // continue with other tests
|
|
|
|
}
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-09-16 15:03:55 +00:00
|
|
|
int ii = 0;
|
|
|
|
|
2020-11-13 15:15:52 +00:00
|
|
|
for( FOOTPRINT* footprint : m_board->Footprints() )
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2020-11-12 23:50:33 +00:00
|
|
|
if( !reportProgress( ii++, m_board->Footprints().size(), delta ) )
|
2021-02-27 13:43:41 +00:00
|
|
|
return false; // DRC cancelled
|
2020-09-16 15:03:55 +00:00
|
|
|
|
2020-11-20 13:55:10 +00:00
|
|
|
if( ( footprint->GetFlags() & MALFORMED_COURTYARDS ) != 0 )
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2020-09-12 19:28:22 +00:00
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_MALFORMED_COURTYARD) )
|
|
|
|
continue;
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-11-21 20:42:27 +00:00
|
|
|
OUTLINE_ERROR_HANDLER errorHandler =
|
|
|
|
[&]( const wxString& msg, BOARD_ITEM* , BOARD_ITEM* , const wxPoint& pt )
|
|
|
|
{
|
|
|
|
std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MALFORMED_COURTYARD );
|
|
|
|
drcItem->SetErrorMessage( drcItem->GetErrorText() + wxS( " " ) + msg );
|
|
|
|
drcItem->SetItems( footprint );
|
|
|
|
reportViolation( drcItem, pt );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Re-run courtyard tests to generate DRC_ITEMs
|
|
|
|
footprint->BuildPolyCourtyards( &errorHandler );
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
2020-10-25 15:02:49 +00:00
|
|
|
else if( footprint->GetPolyCourtyardFront().OutlineCount() == 0
|
|
|
|
&& footprint->GetPolyCourtyardBack().OutlineCount() == 0 )
|
|
|
|
{
|
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_MISSING_COURTYARD ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::shared_ptr<DRC_ITEM> drcItem = DRC_ITEM::Create( DRCE_MISSING_COURTYARD );
|
|
|
|
drcItem->SetItems( footprint );
|
2020-11-20 13:55:10 +00:00
|
|
|
reportViolation( drcItem, footprint->GetPosition() );
|
2020-10-25 15:02:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
footprint->GetPolyCourtyardFront().BuildBBoxCaches();
|
|
|
|
footprint->GetPolyCourtyardBack().BuildBBoxCaches();
|
|
|
|
}
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
2021-02-27 13:43:41 +00:00
|
|
|
|
|
|
|
return true;
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-27 13:43:41 +00:00
|
|
|
bool DRC_TEST_PROVIDER_COURTYARD_CLEARANCE::testCourtyardClearances()
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2020-09-16 15:03:55 +00:00
|
|
|
const int delta = 100; // This is the number of tests between 2 calls to the progress bar
|
|
|
|
|
2021-02-27 13:43:41 +00:00
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_OVERLAPPING_FOOTPRINTS) )
|
|
|
|
return true; // continue with other tests
|
|
|
|
|
2020-10-04 11:44:22 +00:00
|
|
|
if( !reportPhase( _( "Checking footprints for overlapping courtyards..." ) ) )
|
2021-02-27 13:43:41 +00:00
|
|
|
return false; // DRC cancelled
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-09-16 15:03:55 +00:00
|
|
|
int ii = 0;
|
|
|
|
|
2020-11-12 23:50:33 +00:00
|
|
|
for( auto it1 = m_board->Footprints().begin(); it1 != m_board->Footprints().end(); it1++ )
|
2020-09-11 21:50:53 +00:00
|
|
|
{
|
2020-11-12 23:50:33 +00:00
|
|
|
if( !reportProgress( ii++, m_board->Footprints().size(), delta ) )
|
2021-02-27 13:43:41 +00:00
|
|
|
return false; // DRC cancelled
|
2020-09-16 15:03:55 +00:00
|
|
|
|
2020-09-12 19:28:22 +00:00
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_OVERLAPPING_FOOTPRINTS) )
|
|
|
|
break;
|
|
|
|
|
2021-02-20 13:25:16 +00:00
|
|
|
FOOTPRINT* footprint = *it1;
|
|
|
|
const SHAPE_POLY_SET& footprintFront = footprint->GetPolyCourtyardFront();
|
|
|
|
const SHAPE_POLY_SET& footprintBack = footprint->GetPolyCourtyardBack();
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-09-11 21:50:53 +00:00
|
|
|
if( footprintFront.OutlineCount() == 0 && footprintBack.OutlineCount() == 0 )
|
|
|
|
continue; // No courtyards defined
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-12-23 17:33:39 +00:00
|
|
|
BOX2I frontBBox = footprintFront.BBoxFromCaches();
|
|
|
|
BOX2I backBBox = footprintBack.BBoxFromCaches();
|
|
|
|
|
|
|
|
frontBBox.Inflate( m_largestClearance );
|
|
|
|
backBBox.Inflate( m_largestClearance );
|
|
|
|
|
2020-11-12 23:50:33 +00:00
|
|
|
for( auto it2 = it1 + 1; it2 != m_board->Footprints().end(); it2++ )
|
2020-09-11 21:50:53 +00:00
|
|
|
{
|
2021-02-20 13:25:16 +00:00
|
|
|
FOOTPRINT* test = *it2;
|
|
|
|
const SHAPE_POLY_SET& testFront = test->GetPolyCourtyardFront();
|
|
|
|
const SHAPE_POLY_SET& testBack = test->GetPolyCourtyardBack();
|
|
|
|
DRC_CONSTRAINT constraint;
|
|
|
|
int clearance;
|
|
|
|
int actual;
|
|
|
|
VECTOR2I pos;
|
2020-09-11 21:50:53 +00:00
|
|
|
|
|
|
|
if( footprintFront.OutlineCount() > 0 && testFront.OutlineCount() > 0
|
2020-12-23 17:33:39 +00:00
|
|
|
&& frontBBox.Intersects( testFront.BBoxFromCaches() ) )
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2021-02-08 14:53:49 +00:00
|
|
|
constraint = m_drcEngine->EvalRules( COURTYARD_CLEARANCE_CONSTRAINT, footprint,
|
|
|
|
test, F_Cu );
|
2020-12-23 17:33:39 +00:00
|
|
|
clearance = constraint.GetValue().Min();
|
2020-09-11 21:50:53 +00:00
|
|
|
|
2021-01-23 00:09:18 +00:00
|
|
|
if( clearance >= 0 && footprintFront.Collide( &testFront, clearance, &actual, &pos ) )
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2020-12-23 17:33:39 +00:00
|
|
|
std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_OVERLAPPING_FOOTPRINTS );
|
|
|
|
|
|
|
|
if( clearance > 0 )
|
|
|
|
{
|
|
|
|
m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
|
|
|
|
constraint.GetName(),
|
|
|
|
MessageTextFromValue( userUnits(), clearance ),
|
|
|
|
MessageTextFromValue( userUnits(), actual ) );
|
|
|
|
|
|
|
|
drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + m_msg );
|
|
|
|
drce->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
}
|
|
|
|
|
|
|
|
drce->SetItems( footprint, test );
|
|
|
|
reportViolation( drce, (wxPoint) pos );
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
2020-09-11 21:50:53 +00:00
|
|
|
}
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-09-11 21:50:53 +00:00
|
|
|
if( footprintBack.OutlineCount() > 0 && testBack.OutlineCount() > 0
|
2020-12-23 17:33:39 +00:00
|
|
|
&& backBBox.Intersects( testBack.BBoxFromCaches() ) )
|
2020-09-11 21:50:53 +00:00
|
|
|
{
|
2021-02-08 14:53:49 +00:00
|
|
|
constraint = m_drcEngine->EvalRules( COURTYARD_CLEARANCE_CONSTRAINT, footprint,
|
|
|
|
test, B_Cu );
|
2020-12-23 17:33:39 +00:00
|
|
|
clearance = constraint.GetValue().Min();
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2021-01-23 00:09:18 +00:00
|
|
|
if( clearance >= 0 && footprintBack.Collide( &testBack, clearance, &actual, &pos ) )
|
2020-09-11 21:50:53 +00:00
|
|
|
{
|
2020-12-23 17:33:39 +00:00
|
|
|
std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_OVERLAPPING_FOOTPRINTS );
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-12-23 17:33:39 +00:00
|
|
|
if( clearance > 0 )
|
|
|
|
{
|
|
|
|
m_msg.Printf( _( "(%s clearance %s; actual %s)" ),
|
|
|
|
constraint.GetName(),
|
|
|
|
MessageTextFromValue( userUnits(), clearance ),
|
|
|
|
MessageTextFromValue( userUnits(), actual ) );
|
|
|
|
|
|
|
|
drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + m_msg );
|
|
|
|
drce->SetViolatingRule( constraint.GetParentRule() );
|
|
|
|
}
|
|
|
|
|
|
|
|
drce->SetItems( footprint, test );
|
|
|
|
reportViolation( drce, (wxPoint) pos );
|
|
|
|
}
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-11 21:50:53 +00:00
|
|
|
}
|
2021-02-27 13:43:41 +00:00
|
|
|
|
|
|
|
return true;
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-11 16:24:27 +00:00
|
|
|
bool DRC_TEST_PROVIDER_COURTYARD_CLEARANCE::Run()
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
|
|
|
m_board = m_drcEngine->GetBoard();
|
2020-12-23 17:33:39 +00:00
|
|
|
DRC_CONSTRAINT constraint;
|
|
|
|
|
|
|
|
if( m_drcEngine->QueryWorstConstraint( COURTYARD_CLEARANCE_CONSTRAINT, constraint ) )
|
|
|
|
m_largestClearance = constraint.GetValue().Min();
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2020-12-23 17:33:39 +00:00
|
|
|
reportAux( "Worst courtyard clearance : %d nm", m_largestClearance );
|
2020-08-25 17:42:52 +00:00
|
|
|
|
2021-02-27 13:43:41 +00:00
|
|
|
if( !testFootprintCourtyardDefinitions() )
|
|
|
|
return false;
|
2020-09-11 16:24:27 +00:00
|
|
|
|
2021-02-27 13:43:41 +00:00
|
|
|
if( !testCourtyardClearances() )
|
|
|
|
return false;
|
2020-08-25 17:42:52 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-14 17:54:14 +00:00
|
|
|
int DRC_TEST_PROVIDER_COURTYARD_CLEARANCE::GetNumPhases() const
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-01 22:29:15 +00:00
|
|
|
std::set<DRC_CONSTRAINT_T> DRC_TEST_PROVIDER_COURTYARD_CLEARANCE::GetConstraintTypes() const
|
2020-08-25 17:42:52 +00:00
|
|
|
{
|
2020-11-02 16:20:00 +00:00
|
|
|
return { COURTYARD_CLEARANCE_CONSTRAINT };
|
2020-08-25 17:42:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
2020-09-11 16:24:27 +00:00
|
|
|
static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_COURTYARD_CLEARANCE> dummy;
|
2021-06-03 18:05:43 +00:00
|
|
|
}
|