2022-10-13 23:47:59 +00:00
|
|
|
/*
|
|
|
|
* 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 <drc/drc_interactive_courtyard_clearance.h>
|
|
|
|
#include <drc/drc_engine.h>
|
|
|
|
#include <drc/drc_item.h>
|
|
|
|
#include <drc/drc_rule.h>
|
|
|
|
#include <pad.h>
|
|
|
|
#include <zone.h>
|
|
|
|
#include <geometry/shape_segment.h>
|
|
|
|
#include <footprint.h>
|
|
|
|
|
|
|
|
void DRC_INTERACTIVE_COURTYARD_CLEARANCE::testCourtyardClearances()
|
|
|
|
{
|
2024-06-23 23:00:30 +00:00
|
|
|
std::vector<BOX2I> fpBBBoxes( m_FpInMove.size() );
|
|
|
|
std::vector<BOX2I> frontBBBoxes( m_FpInMove.size() );
|
|
|
|
std::vector<BOX2I> backBBBoxes( m_FpInMove.size() );
|
|
|
|
|
|
|
|
// GetCourtyard updates courtyard caches if needed
|
|
|
|
for( size_t i = 0; i < m_FpInMove.size(); i++ )
|
|
|
|
{
|
|
|
|
FOOTPRINT* fpB = m_FpInMove[i];
|
|
|
|
fpBBBoxes[i] = fpB->GetBoundingBox();
|
|
|
|
frontBBBoxes[i] = fpB->GetCourtyard( F_CrtYd ).BBoxFromCaches();
|
|
|
|
backBBBoxes[i] = fpB->GetCourtyard( B_CrtYd ).BBoxFromCaches();
|
|
|
|
}
|
|
|
|
|
2022-10-13 23:47:59 +00:00
|
|
|
for( FOOTPRINT* fpA: m_board->Footprints() )
|
|
|
|
{
|
|
|
|
if( fpA->IsSelected() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const SHAPE_POLY_SET& frontA = fpA->GetCourtyard( F_CrtYd );
|
|
|
|
const SHAPE_POLY_SET& backA = fpA->GetCourtyard( B_CrtYd );
|
|
|
|
|
|
|
|
if( frontA.OutlineCount() == 0 && backA.OutlineCount() == 0 )
|
|
|
|
// No courtyards defined and no hole testing against other footprint's courtyards
|
|
|
|
continue;
|
|
|
|
|
2024-06-23 23:00:30 +00:00
|
|
|
BOX2I frontABBox = frontA.BBoxFromCaches();
|
|
|
|
BOX2I backABBox = backA.BBoxFromCaches();
|
2022-10-13 23:47:59 +00:00
|
|
|
|
2024-06-23 23:00:30 +00:00
|
|
|
frontABBox.Inflate( m_largestCourtyardClearance );
|
|
|
|
backABBox.Inflate( m_largestCourtyardClearance );
|
2022-10-13 23:47:59 +00:00
|
|
|
|
|
|
|
BOX2I fpABBox = fpA->GetBoundingBox();
|
|
|
|
|
2024-06-23 23:00:30 +00:00
|
|
|
for( size_t inMoveId = 0; inMoveId < m_FpInMove.size(); inMoveId++ )
|
2022-10-13 23:47:59 +00:00
|
|
|
{
|
2024-06-23 23:00:30 +00:00
|
|
|
FOOTPRINT* fpB = m_FpInMove[inMoveId];
|
|
|
|
const SHAPE_POLY_SET& frontB = fpB->GetCachedCourtyard( F_CrtYd );
|
|
|
|
const SHAPE_POLY_SET& backB = fpB->GetCachedCourtyard( B_CrtYd );
|
|
|
|
const BOX2I fpBBBox = fpBBBoxes[inMoveId];
|
|
|
|
const BOX2I frontBBBox = frontBBBoxes[inMoveId];
|
|
|
|
const BOX2I backBBBox = backBBBoxes[inMoveId];
|
2022-10-13 23:47:59 +00:00
|
|
|
int clearance;
|
|
|
|
int actual;
|
|
|
|
VECTOR2I pos;
|
|
|
|
|
|
|
|
if( frontA.OutlineCount() > 0 && frontB.OutlineCount() > 0
|
2024-06-23 23:00:30 +00:00
|
|
|
&& frontABBox.Intersects( frontBBBox ) )
|
2022-10-13 23:47:59 +00:00
|
|
|
{
|
|
|
|
// Currently, do not use DRC engine for calculation time reasons
|
2024-06-23 23:00:30 +00:00
|
|
|
// DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( COURTYARD_CLEARANCE_CONSTRAINT, fpA, fpB, B_Cu );
|
2022-10-13 23:47:59 +00:00
|
|
|
// constraint.GetValue().Min();
|
|
|
|
clearance = 0;
|
|
|
|
|
|
|
|
if( frontA.Collide( &frontB, clearance, &actual, &pos ) )
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.insert( fpA );
|
|
|
|
m_itemsInConflict.insert( fpB );
|
2022-10-13 23:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( backA.OutlineCount() > 0 && backB.OutlineCount() > 0
|
2024-06-23 23:00:30 +00:00
|
|
|
&& backABBox.Intersects( backBBBox ) )
|
2022-10-13 23:47:59 +00:00
|
|
|
{
|
|
|
|
// Currently, do not use DRC engine for calculation time reasons
|
2024-06-23 23:00:30 +00:00
|
|
|
// DRC_CONSTRAINT constraint = m_drcEngine->EvalRules( COURTYARD_CLEARANCE_CONSTRAINT, fpA, fpB, B_Cu );
|
2022-10-13 23:47:59 +00:00
|
|
|
// constraint.GetValue().Min();
|
|
|
|
clearance = 0;
|
|
|
|
|
|
|
|
if( backA.Collide( &backB, clearance, &actual, &pos ) )
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.insert( fpA );
|
|
|
|
m_itemsInConflict.insert( fpB );
|
2022-10-13 23:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now test if a pad hole of some other footprint is inside the courtyard area
|
|
|
|
// of the moved footprint
|
|
|
|
auto testPadAgainstCourtyards =
|
|
|
|
[&]( const PAD* pad, FOOTPRINT* footprint ) -> bool
|
|
|
|
{
|
|
|
|
if( pad->HasHole() )
|
|
|
|
{
|
|
|
|
std::shared_ptr<SHAPE_SEGMENT> hole = pad->GetEffectiveHoleShape();
|
2024-06-23 23:00:30 +00:00
|
|
|
const SHAPE_POLY_SET& front = footprint->GetCachedCourtyard( F_CrtYd );
|
|
|
|
const SHAPE_POLY_SET& back = footprint->GetCachedCourtyard( B_CrtYd );
|
2022-10-13 23:47:59 +00:00
|
|
|
|
|
|
|
if( front.OutlineCount() > 0 && front.Collide( hole.get(), 0 ) )
|
|
|
|
return true;
|
|
|
|
else if( back.OutlineCount() > 0 && back.Collide( hole.get(), 0 ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool skipNextCmp = false;
|
|
|
|
|
2024-06-23 23:00:30 +00:00
|
|
|
if( ( frontA.OutlineCount() > 0 && frontABBox.Intersects( fpBBBox ) )
|
|
|
|
|| ( backA.OutlineCount() > 0 && backABBox.Intersects( fpBBBox ) ) )
|
2022-10-13 23:47:59 +00:00
|
|
|
{
|
|
|
|
for( const PAD* padB : fpB->Pads() )
|
|
|
|
{
|
|
|
|
if( testPadAgainstCourtyards( padB, fpA ) )
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.insert( fpA );
|
|
|
|
m_itemsInConflict.insert( fpB );
|
2022-10-13 23:47:59 +00:00
|
|
|
skipNextCmp = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( skipNextCmp )
|
|
|
|
continue; // fpA and fpB are already in list
|
|
|
|
|
2024-06-23 23:00:30 +00:00
|
|
|
if( ( frontB.OutlineCount() > 0 && frontBBBox.Intersects( fpABBox ) )
|
|
|
|
|| ( backB.OutlineCount() > 0 && backBBBox.Intersects( fpABBox ) ) )
|
2022-10-13 23:47:59 +00:00
|
|
|
{
|
|
|
|
for( const PAD* padA : fpA->Pads() )
|
|
|
|
{
|
|
|
|
if( testPadAgainstCourtyards( padA, fpB ) )
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.insert( fpA );
|
|
|
|
m_itemsInConflict.insert( fpB );
|
2022-10-13 23:47:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ZONE* zone : m_board->Zones() )
|
|
|
|
{
|
|
|
|
if( !zone->GetIsRuleArea() || !zone->GetDoNotAllowFootprints() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool disallowFront = ( zone->GetLayerSet() & LSET::FrontMask() ).any();
|
|
|
|
bool disallowBack = ( zone->GetLayerSet() & LSET::BackMask() ).any();
|
|
|
|
|
|
|
|
for( FOOTPRINT* fp : m_FpInMove )
|
|
|
|
{
|
|
|
|
if( disallowFront )
|
|
|
|
{
|
|
|
|
const SHAPE_POLY_SET& frontCourtyard = fp->GetCourtyard( F_CrtYd );
|
|
|
|
|
|
|
|
if( !frontCourtyard.IsEmpty() )
|
|
|
|
{
|
|
|
|
if( zone->Outline()->Collide( &frontCourtyard.Outline( 0 ) ) )
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.insert( fp );
|
|
|
|
m_itemsInConflict.insert( zone );
|
2022-10-13 23:47:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( disallowBack )
|
|
|
|
{
|
|
|
|
const SHAPE_POLY_SET& backCourtyard = fp->GetCourtyard( B_CrtYd );
|
|
|
|
|
|
|
|
if( !backCourtyard.IsEmpty() )
|
|
|
|
{
|
|
|
|
if( zone->Outline()->Collide( &backCourtyard.Outline( 0 ) ) )
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.insert( fp );
|
|
|
|
m_itemsInConflict.insert( zone );
|
2022-10-13 23:47:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DRC_INTERACTIVE_COURTYARD_CLEARANCE::Init( BOARD* aBoard )
|
|
|
|
{
|
|
|
|
m_board = aBoard;
|
|
|
|
|
|
|
|
// Update courtyard data and clear the COURTYARD_CONFLICT flag
|
|
|
|
for( FOOTPRINT* fp: m_board->Footprints() )
|
|
|
|
{
|
|
|
|
fp->ClearFlags( COURTYARD_CONFLICT );
|
|
|
|
fp->BuildCourtyardCaches();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DRC_INTERACTIVE_COURTYARD_CLEARANCE::Run()
|
|
|
|
{
|
2022-10-14 09:42:07 +00:00
|
|
|
m_itemsInConflict.clear();
|
2022-10-13 23:47:59 +00:00
|
|
|
m_largestCourtyardClearance = 0;
|
|
|
|
|
|
|
|
DRC_CONSTRAINT constraint;
|
|
|
|
|
|
|
|
if( m_drcEngine->QueryWorstConstraint( COURTYARD_CLEARANCE_CONSTRAINT, constraint ) )
|
|
|
|
m_largestCourtyardClearance = constraint.GetValue().Min();
|
|
|
|
|
|
|
|
testCourtyardClearances();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-10-14 09:42:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
void DRC_INTERACTIVE_COURTYARD_CLEARANCE::UpdateConflicts( KIGFX::VIEW* aView,
|
|
|
|
bool aHighlightMoved )
|
|
|
|
{
|
|
|
|
// Ensure the "old" conflicts are cleared
|
|
|
|
for( BOARD_ITEM* item: m_lastItemsInConflict )
|
|
|
|
{
|
|
|
|
item->ClearFlags(COURTYARD_CONFLICT );
|
|
|
|
aView->Update( item );
|
|
|
|
aView->MarkTargetDirty( KIGFX::TARGET_OVERLAY );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lastItemsInConflict.clear();
|
|
|
|
|
|
|
|
for( BOARD_ITEM* item: m_itemsInConflict )
|
|
|
|
{
|
|
|
|
if( aHighlightMoved || !alg::contains( m_FpInMove, item ) )
|
|
|
|
{
|
|
|
|
if( !item->HasFlag( COURTYARD_CONFLICT ) )
|
|
|
|
{
|
|
|
|
item->SetFlags( COURTYARD_CONFLICT );
|
|
|
|
aView->Update( item );
|
|
|
|
aView->MarkTargetDirty( KIGFX::TARGET_OVERLAY );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_lastItemsInConflict.push_back( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DRC_INTERACTIVE_COURTYARD_CLEARANCE::ClearConflicts( KIGFX::VIEW* aView )
|
|
|
|
{
|
|
|
|
for( BOARD_ITEM* item: m_lastItemsInConflict )
|
|
|
|
{
|
|
|
|
item->ClearFlags( COURTYARD_CONFLICT );
|
|
|
|
aView->Update( item );
|
|
|
|
aView->MarkTargetDirty( KIGFX::TARGET_OVERLAY );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|