2022-06-17 21:50:59 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 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>
|
2023-09-06 21:19:38 +00:00
|
|
|
#include <core/thread_pool.h>
|
2022-06-17 21:50:59 +00:00
|
|
|
#include <zone.h>
|
2023-03-25 10:44:46 +00:00
|
|
|
#include <connectivity/connectivity_data.h>
|
2022-06-17 21:50:59 +00:00
|
|
|
#include <drc/drc_engine.h>
|
|
|
|
#include <drc/drc_rtree.h>
|
|
|
|
#include <drc/drc_cache_generator.h>
|
|
|
|
|
|
|
|
bool DRC_CACHE_GENERATOR::Run()
|
|
|
|
{
|
|
|
|
m_board = m_drcEngine->GetBoard();
|
|
|
|
|
2023-07-26 19:36:13 +00:00
|
|
|
int& largestClearance = m_board->m_DRCMaxClearance;
|
|
|
|
int& largestPhysicalClearance = m_board->m_DRCMaxPhysicalClearance;
|
2022-06-17 21:50:59 +00:00
|
|
|
DRC_CONSTRAINT worstConstraint;
|
2023-02-03 13:20:45 +00:00
|
|
|
LSET boardCopperLayers = LSET::AllCuMask( m_board->GetCopperLayerCount() );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2023-07-26 19:36:13 +00:00
|
|
|
largestClearance = std::max( largestClearance, m_board->GetMaxClearanceValue() );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
if( m_drcEngine->QueryWorstConstraint( PHYSICAL_CLEARANCE_CONSTRAINT, worstConstraint ) )
|
2023-07-26 19:36:13 +00:00
|
|
|
largestPhysicalClearance = worstConstraint.GetValue().Min();
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
if( m_drcEngine->QueryWorstConstraint( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT, worstConstraint ) )
|
2023-07-26 19:36:13 +00:00
|
|
|
largestPhysicalClearance = std::max( largestPhysicalClearance, worstConstraint.GetValue().Min() );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-07-06 02:28:46 +00:00
|
|
|
std::set<ZONE*> allZones;
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
for( ZONE* zone : m_board->Zones() )
|
|
|
|
{
|
2022-07-06 02:28:46 +00:00
|
|
|
allZones.insert( zone );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
if( !zone->GetIsRuleArea() )
|
|
|
|
{
|
|
|
|
m_board->m_DRCZones.push_back( zone );
|
|
|
|
|
2023-02-03 13:20:45 +00:00
|
|
|
if( ( zone->GetLayerSet() & boardCopperLayers ).any() )
|
2022-06-17 21:50:59 +00:00
|
|
|
{
|
|
|
|
m_board->m_DRCCopperZones.push_back( zone );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( FOOTPRINT* footprint : m_board->Footprints() )
|
|
|
|
{
|
|
|
|
for( ZONE* zone : footprint->Zones() )
|
|
|
|
{
|
2022-07-06 02:28:46 +00:00
|
|
|
allZones.insert( zone );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
if( !zone->GetIsRuleArea() )
|
|
|
|
{
|
|
|
|
m_board->m_DRCZones.push_back( zone );
|
|
|
|
|
2023-02-03 13:20:45 +00:00
|
|
|
if( ( zone->GetLayerSet() & boardCopperLayers ).any() )
|
2022-06-17 21:50:59 +00:00
|
|
|
m_board->m_DRCCopperZones.push_back( zone );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the number of tests between 2 calls to the progress bar
|
2022-08-03 09:10:23 +00:00
|
|
|
size_t progressDelta = 200;
|
2022-06-17 21:50:59 +00:00
|
|
|
size_t count = 0;
|
|
|
|
size_t ii = 0;
|
|
|
|
|
|
|
|
auto countItems =
|
|
|
|
[&]( BOARD_ITEM* item ) -> bool
|
|
|
|
{
|
|
|
|
++count;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto addToCopperTree =
|
|
|
|
[&]( BOARD_ITEM* item ) -> bool
|
|
|
|
{
|
2022-08-03 09:10:23 +00:00
|
|
|
if( !reportProgress( ii++, count, progressDelta ) )
|
2022-06-17 21:50:59 +00:00
|
|
|
return false;
|
|
|
|
|
2023-02-03 13:20:45 +00:00
|
|
|
LSET copperLayers = item->GetLayerSet() & boardCopperLayers;
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
// Special-case pad holes which pierce all the copper layers
|
|
|
|
if( item->Type() == PCB_PAD_T )
|
|
|
|
{
|
|
|
|
PAD* pad = static_cast<PAD*>( item );
|
|
|
|
|
2022-07-15 15:14:11 +00:00
|
|
|
if( pad->HasHole() )
|
2023-02-03 13:20:45 +00:00
|
|
|
copperLayers = boardCopperLayers;
|
2022-06-17 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 13:20:45 +00:00
|
|
|
for( PCB_LAYER_ID layer : copperLayers.Seq() )
|
2022-06-17 21:50:59 +00:00
|
|
|
{
|
|
|
|
if( IsCopperLayer( layer ) )
|
2023-07-26 19:36:13 +00:00
|
|
|
m_board->m_CopperItemRTreeCache->Insert( item, layer, largestClearance );
|
2022-06-17 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if( !reportPhase( _( "Gathering copper items..." ) ) )
|
|
|
|
return false; // DRC cancelled
|
|
|
|
|
|
|
|
static const std::vector<KICAD_T> itemTypes = {
|
|
|
|
PCB_TRACE_T, PCB_ARC_T, PCB_VIA_T,
|
|
|
|
PCB_PAD_T,
|
2023-03-30 11:49:23 +00:00
|
|
|
PCB_SHAPE_T,
|
2023-06-06 15:09:34 +00:00
|
|
|
PCB_FIELD_T, PCB_TEXT_T, PCB_TEXTBOX_T,
|
2022-06-17 21:50:59 +00:00
|
|
|
PCB_DIMENSION_T
|
|
|
|
};
|
|
|
|
|
|
|
|
forEachGeometryItem( itemTypes, LSET::AllCuMask(), countItems );
|
2023-05-12 21:03:54 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> cacheLock( m_board->m_CachesMutex );
|
|
|
|
|
|
|
|
if( !m_board->m_CopperItemRTreeCache )
|
|
|
|
m_board->m_CopperItemRTreeCache = std::make_shared<DRC_RTREE>();
|
|
|
|
|
|
|
|
forEachGeometryItem( itemTypes, LSET::AllCuMask(), addToCopperTree );
|
|
|
|
}
|
2022-06-17 21:50:59 +00:00
|
|
|
|
|
|
|
if( !reportPhase( _( "Tessellating copper zones..." ) ) )
|
|
|
|
return false; // DRC cancelled
|
|
|
|
|
|
|
|
// Cache zone bounding boxes, triangulation, copper zone rtrees, and footprint courtyards
|
|
|
|
// before we start.
|
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
for( FOOTPRINT* footprint : m_board->Footprints() )
|
2022-07-31 21:14:24 +00:00
|
|
|
footprint->BuildCourtyardCaches();
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
thread_pool& tp = GetKiCadThreadPool();
|
|
|
|
std::vector<std::future<size_t>> returns;
|
2022-08-03 09:10:23 +00:00
|
|
|
std::atomic<size_t> done( 1 );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
returns.reserve( allZones.size() );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-08-03 09:10:23 +00:00
|
|
|
auto cache_zones =
|
|
|
|
[this, &done]( ZONE* aZone ) -> size_t
|
2022-06-15 00:41:10 +00:00
|
|
|
{
|
|
|
|
if( m_drcEngine->IsCancelled() )
|
|
|
|
return 0;
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
aZone->CacheBoundingBox();
|
|
|
|
aZone->CacheTriangulation();
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
if( !aZone->GetIsRuleArea() && aZone->IsOnCopperLayer() )
|
|
|
|
{
|
|
|
|
std::unique_ptr<DRC_RTREE> rtree = std::make_unique<DRC_RTREE>();
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
for( PCB_LAYER_ID layer : aZone->GetLayerSet().Seq() )
|
|
|
|
{
|
|
|
|
if( IsCopperLayer( layer ) )
|
|
|
|
rtree->Insert( aZone, layer );
|
|
|
|
}
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
std::unique_lock<std::mutex> cacheLock( m_board->m_CachesMutex );
|
|
|
|
m_board->m_CopperZoneRTreeCache[ aZone ] = std::move( rtree );
|
2022-08-03 09:10:23 +00:00
|
|
|
|
|
|
|
done.fetch_add( 1 );
|
2022-06-15 00:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
for( ZONE* zone : allZones )
|
|
|
|
returns.emplace_back( tp.submit( cache_zones, zone ) );
|
2022-06-17 21:50:59 +00:00
|
|
|
|
2022-09-14 20:28:46 +00:00
|
|
|
for( const std::future<size_t>& ret : returns )
|
2022-06-17 21:50:59 +00:00
|
|
|
{
|
2022-09-14 20:28:46 +00:00
|
|
|
std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
|
2022-06-15 00:41:10 +00:00
|
|
|
|
2022-09-14 20:28:46 +00:00
|
|
|
while( status != std::future_status::ready )
|
2022-06-15 00:41:10 +00:00
|
|
|
{
|
2022-08-03 09:10:23 +00:00
|
|
|
m_drcEngine->ReportProgress( static_cast<double>( done ) / allZones.size() );
|
2022-09-14 20:28:46 +00:00
|
|
|
status = ret.wait_for( std::chrono::milliseconds( 250 ) );
|
2022-08-03 09:10:23 +00:00
|
|
|
}
|
2022-06-17 21:50:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 10:44:46 +00:00
|
|
|
m_board->m_ZoneIsolatedIslandsMap.clear();
|
|
|
|
|
|
|
|
for( ZONE* zone : m_board->Zones() )
|
|
|
|
{
|
2023-05-12 21:03:54 +00:00
|
|
|
if( !zone->GetIsRuleArea() && !zone->IsTeardropArea() )
|
2023-03-25 10:44:46 +00:00
|
|
|
{
|
|
|
|
for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
|
|
|
|
m_board->m_ZoneIsolatedIslandsMap[ zone ][ layer ] = ISOLATED_ISLANDS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
|
|
|
|
|
|
|
|
connectivity->ClearRatsnest();
|
|
|
|
connectivity->Build( m_board, m_drcEngine->GetProgressReporter() );
|
|
|
|
connectivity->FillIsolatedIslandsMap( m_board->m_ZoneIsolatedIslandsMap, true );
|
|
|
|
|
2022-06-17 21:50:59 +00:00
|
|
|
return !m_drcEngine->IsCancelled();
|
|
|
|
}
|
|
|
|
|