2021-08-11 13:01:30 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-03-11 20:13:47 +00:00
|
|
|
* Copyright (C) 2021-2022 KiCad Developers.
|
2021-08-11 13:01:30 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2022-03-11 22:02:43 +00:00
|
|
|
#include <atomic>
|
2021-08-11 13:01:30 +00:00
|
|
|
#include <board.h>
|
|
|
|
#include <board_design_settings.h>
|
|
|
|
#include <zone.h>
|
|
|
|
#include <footprint.h>
|
|
|
|
#include <pcb_shape.h>
|
|
|
|
#include <geometry/shape_poly_set.h>
|
|
|
|
#include <drc/drc_rule.h>
|
2021-12-01 14:42:44 +00:00
|
|
|
#include <drc/drc_item.h>
|
2021-08-11 13:01:30 +00:00
|
|
|
#include <drc/drc_test_provider.h>
|
|
|
|
#include <advanced_config.h>
|
2022-06-15 00:41:10 +00:00
|
|
|
#include <progress_reporter.h>
|
2023-09-06 21:19:38 +00:00
|
|
|
#include <core/thread_pool.h>
|
2021-08-11 13:01:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Checks for slivers in copper layers
|
|
|
|
|
|
|
|
Errors generated:
|
|
|
|
- DRCE_COPPER_SLIVER
|
|
|
|
*/
|
|
|
|
|
|
|
|
class DRC_TEST_PROVIDER_SLIVER_CHECKER : public DRC_TEST_PROVIDER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DRC_TEST_PROVIDER_SLIVER_CHECKER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DRC_TEST_PROVIDER_SLIVER_CHECKER()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Run() override;
|
|
|
|
|
|
|
|
virtual const wxString GetName() const override
|
|
|
|
{
|
2022-03-11 21:16:52 +00:00
|
|
|
return wxT( "sliver checker" );
|
2021-08-11 13:01:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual const wxString GetDescription() const override
|
|
|
|
{
|
2022-03-11 21:16:52 +00:00
|
|
|
return wxT( "Checks copper layers for slivers" );
|
2021-08-11 13:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxString layerDesc( PCB_LAYER_ID aLayer );
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
wxString DRC_TEST_PROVIDER_SLIVER_CHECKER::layerDesc( PCB_LAYER_ID aLayer )
|
|
|
|
{
|
|
|
|
return wxString::Format( wxT( "(%s)" ), m_drcEngine->GetBoard()->GetLayerName( aLayer ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DRC_TEST_PROVIDER_SLIVER_CHECKER::Run()
|
|
|
|
{
|
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_COPPER_SLIVER ) )
|
|
|
|
return true; // Continue with other tests
|
|
|
|
|
|
|
|
if( !reportPhase( _( "Running sliver detection on copper layers..." ) ) )
|
|
|
|
return false; // DRC cancelled
|
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
int64_t widthTolerance = pcbIUScale.mmToIU( ADVANCED_CFG::GetCfg().m_SliverWidthTolerance );
|
|
|
|
int64_t squared_width = widthTolerance * widthTolerance;
|
|
|
|
|
2021-08-11 13:01:30 +00:00
|
|
|
double angleTolerance = ADVANCED_CFG::GetCfg().m_SliverAngleTolerance;
|
2023-04-25 17:26:03 +00:00
|
|
|
double cosangleTol = 2.0 * cos( DEG2RAD( angleTolerance ) );
|
2022-03-11 20:13:47 +00:00
|
|
|
LSET copperLayerSet = m_drcEngine->GetBoard()->GetEnabledLayers() & LSET::AllCuMask();
|
|
|
|
LSEQ copperLayers = copperLayerSet.Seq();
|
2022-08-01 16:33:31 +00:00
|
|
|
int layerCount = copperLayers.size();
|
2021-08-11 13:01:30 +00:00
|
|
|
|
2022-08-01 16:33:31 +00:00
|
|
|
// Report progress on board zones only. Everything else is in the noise.
|
|
|
|
int zoneLayerCount = 0;
|
|
|
|
std::atomic<size_t> done( 1 );
|
|
|
|
|
|
|
|
for( PCB_LAYER_ID layer : copperLayers )
|
|
|
|
{
|
|
|
|
for( ZONE* zone : m_drcEngine->GetBoard()->Zones() )
|
|
|
|
{
|
|
|
|
if( !zone->GetIsRuleArea() && zone->IsOnLayer( layer ) )
|
|
|
|
zoneLayerCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROGRESS_REPORTER* reporter = m_drcEngine->GetProgressReporter();
|
|
|
|
|
|
|
|
if( reporter && reporter->IsCancelled() )
|
2022-03-12 13:34:25 +00:00
|
|
|
return false; // DRC cancelled
|
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
std::vector<SHAPE_POLY_SET> layerPolys( layerCount );
|
2022-03-11 20:13:47 +00:00
|
|
|
|
2022-11-01 13:19:33 +00:00
|
|
|
auto build_layer_polys =
|
|
|
|
[&]( int layerIdx ) -> size_t
|
2022-07-31 15:02:04 +00:00
|
|
|
{
|
2022-11-01 13:19:33 +00:00
|
|
|
PCB_LAYER_ID layer = copperLayers[layerIdx];
|
|
|
|
SHAPE_POLY_SET& poly = layerPolys[layerIdx];
|
2022-03-12 13:34:25 +00:00
|
|
|
|
2022-07-31 15:02:04 +00:00
|
|
|
if( m_drcEngine->IsCancelled() )
|
|
|
|
return 0;
|
2022-03-11 20:13:47 +00:00
|
|
|
|
2022-07-31 15:02:04 +00:00
|
|
|
SHAPE_POLY_SET fill;
|
|
|
|
|
|
|
|
forEachGeometryItem( s_allBasicItems, LSET().set( layer ),
|
|
|
|
[&]( BOARD_ITEM* item ) -> bool
|
|
|
|
{
|
2022-11-01 13:19:33 +00:00
|
|
|
if( ZONE* zone = dynamic_cast<ZONE*>( item) )
|
2022-06-15 00:41:10 +00:00
|
|
|
{
|
2022-07-31 15:02:04 +00:00
|
|
|
if( !zone->GetIsRuleArea() )
|
2022-06-15 00:41:10 +00:00
|
|
|
{
|
2022-07-31 15:02:04 +00:00
|
|
|
fill = zone->GetFill( layer )->CloneDropTriangulation();
|
2022-11-01 13:19:33 +00:00
|
|
|
poly.Append( fill );
|
2022-08-01 16:33:31 +00:00
|
|
|
|
|
|
|
// Report progress on board zones only. Everything else is
|
|
|
|
// in the noise.
|
|
|
|
done.fetch_add( 1 );
|
2022-07-31 15:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-21 12:48:45 +00:00
|
|
|
item->TransformShapeToPolygon( poly, layer, 0, ARC_LOW_DEF,
|
2023-04-25 17:26:03 +00:00
|
|
|
ERROR_INSIDE );
|
2022-07-31 15:02:04 +00:00
|
|
|
}
|
2022-06-15 00:41:10 +00:00
|
|
|
|
2022-07-31 15:02:04 +00:00
|
|
|
if( m_drcEngine->IsCancelled() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
if( m_drcEngine->IsCancelled() )
|
|
|
|
return 0;
|
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
poly.Simplify( SHAPE_POLY_SET::POLYGON_MODE::PM_FAST );
|
2022-07-31 15:02:04 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
2022-06-15 00:41:10 +00:00
|
|
|
thread_pool& tp = GetKiCadThreadPool();
|
|
|
|
std::vector<std::future<size_t>> returns;
|
|
|
|
|
2022-08-01 16:33:31 +00:00
|
|
|
returns.reserve( copperLayers.size() );
|
2022-03-11 20:13:47 +00:00
|
|
|
|
2022-08-01 16:33:31 +00:00
|
|
|
for( size_t ii = 0; ii < copperLayers.size(); ++ii )
|
2022-11-01 13:19:33 +00:00
|
|
|
returns.emplace_back( tp.submit( build_layer_polys, ii ) );
|
2022-06-15 00:41:10 +00:00
|
|
|
|
2022-09-14 20:28:46 +00:00
|
|
|
for( const std::future<size_t>& ret : returns )
|
2022-03-11 20:13:47 +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-01 16:33:31 +00:00
|
|
|
m_drcEngine->ReportProgress( static_cast<double>( zoneLayerCount ) / done );
|
2022-06-15 00:41:10 +00:00
|
|
|
|
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-03-11 20:13:47 +00:00
|
|
|
}
|
2021-08-11 13:01:30 +00:00
|
|
|
|
2022-08-01 16:33:31 +00:00
|
|
|
for( int ii = 0; ii < layerCount; ++ii )
|
2022-07-31 15:02:04 +00:00
|
|
|
{
|
2022-08-01 16:33:31 +00:00
|
|
|
PCB_LAYER_ID layer = copperLayers[ii];
|
|
|
|
SHAPE_POLY_SET& poly = layerPolys[ii];
|
2022-01-10 10:31:29 +00:00
|
|
|
|
2022-08-01 16:33:31 +00:00
|
|
|
if( m_drcEngine->IsErrorLimitExceeded( DRCE_COPPER_SLIVER ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Frequently, in filled areas, some points of the polygons are very near (dist is only
|
|
|
|
// a few internal units, like 2 or 3 units.
|
|
|
|
// We skip very small vertices: one cannot really compute a valid orientation of
|
|
|
|
// such a vertex
|
|
|
|
// So skip points near than min_len (in internal units).
|
2022-12-04 00:02:07 +00:00
|
|
|
const int min_len = pcbIUScale.mmToIU( ADVANCED_CFG::GetCfg().m_SliverMinimumLength );
|
2022-08-01 16:33:31 +00:00
|
|
|
|
|
|
|
for( int jj = 0; jj < poly.OutlineCount(); ++jj )
|
2021-08-11 13:01:30 +00:00
|
|
|
{
|
2022-08-01 16:33:31 +00:00
|
|
|
const std::vector<VECTOR2I>& pts = poly.Outline( jj ).CPoints();
|
|
|
|
int ptCount = pts.size();
|
2023-04-25 17:26:03 +00:00
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
auto area = [&]( const VECTOR2I& p, const VECTOR2I& q, const VECTOR2I& r ) -> VECTOR2I::extended_type
|
|
|
|
{
|
|
|
|
return static_cast<VECTOR2I::extended_type>( q.y - p.y ) * ( r.x - q.x ) -
|
|
|
|
static_cast<VECTOR2I::extended_type>( q.x - p.x ) * ( r.y - q.y );
|
|
|
|
};
|
|
|
|
|
|
|
|
auto isLocallyInside = [&]( int aA, int aB ) -> bool
|
|
|
|
{
|
|
|
|
int prev = ( ptCount + aA - 1 ) % ptCount;
|
|
|
|
int next = ( aA + 1 ) % ptCount;
|
|
|
|
|
|
|
|
if( area( pts[prev], pts[aA], pts[next] ) < 0 )
|
|
|
|
return area( pts[aA], pts[aB], pts[next] ) >= 0 && area( pts[aA], pts[prev], pts[aB] ) >= 0;
|
|
|
|
else
|
|
|
|
return area( pts[aA], pts[aB], pts[prev] ) < 0 || area( pts[aA], pts[next], pts[aB] ) < 0;
|
|
|
|
};
|
2021-08-11 13:01:30 +00:00
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
if( ptCount <= 5 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( int kk = 0; kk < ptCount; kk += offset )
|
2022-08-01 16:33:31 +00:00
|
|
|
{
|
2023-04-25 17:26:03 +00:00
|
|
|
int prior_index = ( ptCount + kk - 1 ) % ptCount;
|
|
|
|
int next_index = ( kk + 1 ) % ptCount;
|
2022-08-01 16:33:31 +00:00
|
|
|
VECTOR2I pt = pts[ kk ];
|
2023-04-25 17:26:03 +00:00
|
|
|
VECTOR2I ptPrior = pts[ prior_index ];
|
2022-08-01 16:33:31 +00:00
|
|
|
VECTOR2I vPrior = ( ptPrior - pt );
|
2023-04-25 17:26:03 +00:00
|
|
|
int forward_offset = 1;
|
|
|
|
|
|
|
|
offset = 1;
|
2022-08-01 16:33:31 +00:00
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
while( std::abs( vPrior.x ) < min_len && std::abs( vPrior.y ) < min_len
|
|
|
|
&& offset < ptCount )
|
2022-08-01 16:33:31 +00:00
|
|
|
{
|
2023-04-25 17:26:03 +00:00
|
|
|
pt = pts[ ( kk + offset++ ) % ptCount ];
|
2022-08-01 16:33:31 +00:00
|
|
|
vPrior = ( ptPrior - pt );
|
|
|
|
}
|
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
if( offset >= ptCount )
|
|
|
|
break;
|
|
|
|
|
|
|
|
VECTOR2I ptAfter = pts[ next_index ];
|
2022-08-01 16:33:31 +00:00
|
|
|
VECTOR2I vAfter = ( ptAfter - pt );
|
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
while( std::abs( vAfter.x ) < min_len && std::abs( vAfter.y ) < min_len
|
|
|
|
&& forward_offset < ptCount )
|
2022-08-01 16:33:31 +00:00
|
|
|
{
|
2023-04-25 17:26:03 +00:00
|
|
|
next_index = ( kk + forward_offset++ ) % ptCount;
|
|
|
|
ptAfter = pts[ next_index ];
|
2022-08-01 16:33:31 +00:00
|
|
|
vAfter = ( ptAfter - pt );
|
|
|
|
}
|
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
if( offset >= ptCount )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Negative dot product means that the angle is > 90°
|
|
|
|
if( vPrior.Dot( vAfter ) <= 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !isLocallyInside( prior_index, next_index ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VECTOR2I vIncluded = ptAfter - ptPrior;
|
|
|
|
double arm1 = vPrior.SquaredEuclideanNorm();
|
|
|
|
double arm2 = vAfter.SquaredEuclideanNorm();
|
|
|
|
double opp = vIncluded.SquaredEuclideanNorm();
|
|
|
|
|
|
|
|
double cos_ang = std::abs( ( opp - arm1 - arm2 ) / ( std::sqrt( arm1 ) * std::sqrt( arm2 ) ) );
|
2022-08-01 16:33:31 +00:00
|
|
|
|
2023-04-25 17:26:03 +00:00
|
|
|
if( cos_ang > cosangleTol && 2.0 - cos_ang > std::numeric_limits<float>::epsilon() && opp > squared_width )
|
2022-08-01 16:33:31 +00:00
|
|
|
{
|
|
|
|
std::shared_ptr<DRC_ITEM> drce = DRC_ITEM::Create( DRCE_COPPER_SLIVER );
|
|
|
|
drce->SetErrorMessage( drce->GetErrorText() + wxS( " " ) + layerDesc( layer ) );
|
|
|
|
reportViolation( drce, pt, layer );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-11 13:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
static DRC_REGISTER_TEST_PROVIDER<DRC_TEST_PROVIDER_SLIVER_CHECKER> dummy;
|
|
|
|
}
|