2011-09-23 13:57:12 +00:00
|
|
|
/**
|
|
|
|
* @file zones_test_and_combine_areas.cpp
|
|
|
|
* @brief Functions to test, merge and cut polygons used as copper areas outlines
|
|
|
|
* some pieces of code come from FreePCB.
|
|
|
|
*/
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2012-06-08 09:56:42 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
|
|
|
|
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* Some code comes from FreePCB.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <fctsys.h>
|
|
|
|
#include <common.h>
|
|
|
|
#include <confirm.h>
|
2018-01-31 08:23:20 +00:00
|
|
|
#include <undo_redo_container.h>
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <class_board.h>
|
|
|
|
#include <class_zone.h>
|
|
|
|
#include <class_marker_pcb.h>
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <pcbnew.h>
|
2019-06-03 13:49:17 +00:00
|
|
|
#include <tools/drc.h>
|
2012-07-30 07:40:25 +00:00
|
|
|
#include <math_for_graphics.h>
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2012-07-31 17:51:58 +00:00
|
|
|
#define STRAIGHT 0 // To be remove after math_for_graphics code cleanup
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
bool BOARD::OnAreaPolygonModified( PICKED_ITEMS_LIST* aModifiedZonesList,
|
2013-03-20 14:50:12 +00:00
|
|
|
ZONE_CONTAINER* modified_area )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
|
|
|
// clip polygon against itself
|
2012-07-31 13:12:51 +00:00
|
|
|
bool modified = NormalizeAreaPolygon( aModifiedZonesList, modified_area );
|
2008-01-04 12:32:10 +00:00
|
|
|
|
|
|
|
// now see if we need to clip against other areas
|
2017-12-05 17:02:53 +00:00
|
|
|
/*
|
2013-03-31 13:27:46 +00:00
|
|
|
LAYER_NUM layer = modified_area->GetLayer();
|
2017-12-05 17:02:53 +00:00
|
|
|
*/
|
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
bool bCheckAllAreas = TestAreaIntersections( modified_area );
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2008-01-04 12:32:10 +00:00
|
|
|
if( bCheckAllAreas )
|
2012-07-31 13:12:51 +00:00
|
|
|
{
|
|
|
|
modified = true;
|
2014-02-25 10:40:34 +00:00
|
|
|
CombineAllAreasInNet( aModifiedZonesList, modified_area->GetNetCode(), true );
|
2012-07-31 13:12:51 +00:00
|
|
|
}
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2017-12-04 18:06:47 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
FIXME : do we really need this?
|
|
|
|
|
2013-04-09 16:00:46 +00:00
|
|
|
if( !IsCopperLayer( layer ) ) // Refill non copper zones on this layer
|
2008-09-26 19:51:36 +00:00
|
|
|
{
|
2012-07-31 13:12:51 +00:00
|
|
|
for( unsigned ia = 0; ia < m_ZoneDescriptorList.size(); ia++ )
|
|
|
|
if( m_ZoneDescriptorList[ia]->GetLayer() == layer )
|
2013-05-01 19:01:14 +00:00
|
|
|
m_ZoneDescriptorList[ia]->BuildFilledSolidAreasPolygons( this );
|
2008-09-26 19:51:36 +00:00
|
|
|
}
|
2009-03-11 13:29:10 +00:00
|
|
|
|
2017-12-04 18:06:47 +00:00
|
|
|
*/
|
|
|
|
|
2008-11-28 20:10:05 +00:00
|
|
|
// Test for bad areas: all zones must have more than 2 corners:
|
|
|
|
// Note: should not happen, but just in case.
|
2012-08-04 09:43:27 +00:00
|
|
|
for( unsigned ii = 0; ii < m_ZoneDescriptorList.size(); )
|
2008-11-28 20:10:05 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
ZONE_CONTAINER* zone = m_ZoneDescriptorList[ii];
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2009-03-11 13:29:10 +00:00
|
|
|
if( zone->GetNumCorners() >= 3 )
|
2012-08-04 09:43:27 +00:00
|
|
|
ii++;
|
2011-09-23 13:57:12 +00:00
|
|
|
else // Remove zone because it is incorrect:
|
2009-08-23 15:22:44 +00:00
|
|
|
RemoveArea( aModifiedZonesList, zone );
|
2008-11-28 20:10:05 +00:00
|
|
|
}
|
2009-03-11 13:29:10 +00:00
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
return modified;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
bool BOARD::CombineAllAreasInNet( PICKED_ITEMS_LIST* aDeletedList, int aNetCode,
|
2013-03-26 09:58:40 +00:00
|
|
|
bool aUseLocalFlags )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2009-03-11 13:29:10 +00:00
|
|
|
if( m_ZoneDescriptorList.size() <= 1 )
|
2012-07-31 13:12:51 +00:00
|
|
|
return false;
|
2009-03-11 13:29:10 +00:00
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
bool modified = false;
|
2009-03-11 13:29:10 +00:00
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
// Loop through all combinations
|
2009-03-11 13:29:10 +00:00
|
|
|
for( unsigned ia1 = 0; ia1 < m_ZoneDescriptorList.size() - 1; ia1++ )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2009-03-11 13:29:10 +00:00
|
|
|
ZONE_CONTAINER* curr_area = m_ZoneDescriptorList[ia1];
|
2013-03-20 14:50:12 +00:00
|
|
|
|
2014-02-25 10:40:34 +00:00
|
|
|
if( curr_area->GetNetCode() != aNetCode )
|
2009-03-11 13:29:10 +00:00
|
|
|
continue;
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2009-03-11 13:29:10 +00:00
|
|
|
// legal polygon
|
2017-03-07 12:06:00 +00:00
|
|
|
BOX2I b1 = curr_area->Outline()->BBox();
|
2009-03-11 13:29:10 +00:00
|
|
|
bool mod_ia1 = false;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2009-03-11 13:29:10 +00:00
|
|
|
for( unsigned ia2 = m_ZoneDescriptorList.size() - 1; ia2 > ia1; ia2-- )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2009-03-11 13:29:10 +00:00
|
|
|
ZONE_CONTAINER* area2 = m_ZoneDescriptorList[ia2];
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2014-02-25 10:40:34 +00:00
|
|
|
if( area2->GetNetCode() != aNetCode )
|
2008-01-04 12:32:10 +00:00
|
|
|
continue;
|
2012-07-13 18:55:29 +00:00
|
|
|
|
2012-01-29 19:29:19 +00:00
|
|
|
if( curr_area->GetPriority() != area2->GetPriority() )
|
|
|
|
continue;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
if( curr_area->GetIsKeepout() != area2->GetIsKeepout() )
|
|
|
|
continue;
|
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
if( curr_area->GetLayer() != area2->GetLayer() )
|
|
|
|
continue;
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
BOX2I b2 = area2->Outline()->BBox();
|
2013-03-20 14:50:12 +00:00
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
if( b1.Intersects( b2 ) )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
// check area2 against curr_area
|
2013-03-26 09:58:40 +00:00
|
|
|
if( curr_area->GetLocalFlags() || area2->GetLocalFlags()
|
|
|
|
|| aUseLocalFlags == false )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2012-08-04 09:43:27 +00:00
|
|
|
bool ret = TestAreaIntersection( curr_area, area2 );
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
if( ret )
|
|
|
|
ret = CombineAreas( aDeletedList, curr_area, area2 );
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
if( ret )
|
|
|
|
{
|
|
|
|
mod_ia1 = true;
|
|
|
|
modified = true;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-11 13:29:10 +00:00
|
|
|
|
|
|
|
if( mod_ia1 )
|
|
|
|
ia1--; // if modified, we need to check it again
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
2008-11-28 20:10:05 +00:00
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
return modified;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool BOARD::TestAreaIntersections( ZONE_CONTAINER* area_to_test )
|
|
|
|
{
|
|
|
|
for( unsigned ia2 = 0; ia2 < m_ZoneDescriptorList.size(); ia2++ )
|
|
|
|
{
|
|
|
|
ZONE_CONTAINER* area2 = m_ZoneDescriptorList[ia2];
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2014-02-25 10:40:34 +00:00
|
|
|
if( area_to_test->GetNetCode() != area2->GetNetCode() )
|
2008-01-04 12:32:10 +00:00
|
|
|
continue;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2008-01-10 20:53:41 +00:00
|
|
|
if( area_to_test == area2 )
|
2008-01-20 19:55:22 +00:00
|
|
|
continue;
|
|
|
|
|
2018-09-16 20:54:43 +00:00
|
|
|
// see if areas are on same layers
|
|
|
|
if( area_to_test->GetLayerSet() != area2->GetLayerSet() )
|
2008-01-20 19:55:22 +00:00
|
|
|
continue;
|
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
// test for different priorities
|
2012-01-29 19:29:19 +00:00
|
|
|
if( area_to_test->GetPriority() != area2->GetPriority() )
|
|
|
|
continue;
|
|
|
|
|
2012-07-13 18:55:29 +00:00
|
|
|
// test for different types
|
|
|
|
if( area_to_test->GetIsKeepout() != area2->GetIsKeepout() )
|
|
|
|
continue;
|
|
|
|
|
2018-09-16 20:54:43 +00:00
|
|
|
// Keepout area-specific tests
|
|
|
|
if( area_to_test->GetIsKeepout() )
|
|
|
|
{
|
|
|
|
if( area_to_test->GetDoNotAllowCopperPour() != area2->GetDoNotAllowCopperPour() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetDoNotAllowTracks() != area2->GetDoNotAllowTracks() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetDoNotAllowVias() != area2->GetDoNotAllowVias() )
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-17 00:14:10 +00:00
|
|
|
// Filled zone specific tests
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( area_to_test->GetClearance() != area2->GetClearance() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetThermalReliefGap() != area2->GetThermalReliefGap() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetThermalReliefCopperBridge() != area2->GetThermalReliefCopperBridge() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetZoneClearance() != area2->GetZoneClearance() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetPadConnection() != area2->GetPadConnection() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetMinThickness() != area2->GetMinThickness() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetCornerSmoothingType() != area2->GetCornerSmoothingType() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( area_to_test->GetCornerRadius() != area2->GetCornerRadius() )
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-16 20:54:43 +00:00
|
|
|
|
2012-08-02 13:23:53 +00:00
|
|
|
if( TestAreaIntersection( area_to_test, area2 ) )
|
|
|
|
return true;
|
2008-01-20 19:55:22 +00:00
|
|
|
}
|
2008-01-04 12:32:10 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 13:23:53 +00:00
|
|
|
bool BOARD::TestAreaIntersection( ZONE_CONTAINER* area_ref, ZONE_CONTAINER* area_to_test )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
|
|
|
// see if areas are on same layer
|
|
|
|
if( area_ref->GetLayer() != area_to_test->GetLayer() )
|
2012-08-02 13:23:53 +00:00
|
|
|
return false;
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
SHAPE_POLY_SET* poly1 = area_ref->Outline();
|
|
|
|
SHAPE_POLY_SET* poly2 = area_to_test->Outline();
|
2008-01-04 12:32:10 +00:00
|
|
|
|
|
|
|
// test bounding rects
|
2017-03-07 12:06:00 +00:00
|
|
|
BOX2I b1 = poly1->BBox();
|
|
|
|
BOX2I b2 = poly2->BBox();
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
if( ! b1.Intersects( b2 ) )
|
2012-08-02 13:23:53 +00:00
|
|
|
return false;
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Now test for intersecting segments
|
|
|
|
for( auto segIterator1 = poly1->IterateSegmentsWithHoles(); segIterator1; segIterator1++ )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
// Build segment
|
|
|
|
SEG firstSegment = *segIterator1;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
for( auto segIterator2 = poly2->IterateSegmentsWithHoles(); segIterator2; segIterator2++ )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
// Build second segment
|
|
|
|
SEG secondSegment = *segIterator2;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Check whether the two segments built collide
|
|
|
|
if( firstSegment.Collide( secondSegment, 0 ) )
|
|
|
|
return true;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// If a contour is inside another contour, no segments intersects, but the zones
|
|
|
|
// can be combined if a corner is inside an outline (only one corner is enough)
|
|
|
|
for( auto iter = poly2->IterateWithHoles(); iter; iter++ )
|
2008-01-20 19:55:22 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
if( poly1->Contains( *iter ) )
|
2012-08-04 09:43:27 +00:00
|
|
|
return true;
|
2012-08-02 13:23:53 +00:00
|
|
|
}
|
2008-01-20 19:55:22 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
for( auto iter = poly1->IterateWithHoles(); iter; iter++ )
|
2012-08-02 13:23:53 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
if( poly2->Contains( *iter ) )
|
2012-08-04 09:43:27 +00:00
|
|
|
return true;
|
2008-01-20 19:55:22 +00:00
|
|
|
}
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
return false;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
bool BOARD::CombineAreas( PICKED_ITEMS_LIST* aDeletedList, ZONE_CONTAINER* area_ref,
|
2013-03-20 14:50:12 +00:00
|
|
|
ZONE_CONTAINER* area_to_combine )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
|
|
|
if( area_ref == area_to_combine )
|
2009-05-21 17:42:42 +00:00
|
|
|
{
|
2009-08-12 10:40:01 +00:00
|
|
|
wxASSERT( 0 );
|
2012-07-31 13:12:51 +00:00
|
|
|
return false;
|
2009-05-21 17:42:42 +00:00
|
|
|
}
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
SHAPE_POLY_SET mergedOutlines = *area_ref->Outline();
|
|
|
|
SHAPE_POLY_SET areaToMergePoly = *area_to_combine->Outline();
|
2012-07-25 07:36:56 +00:00
|
|
|
|
2015-12-15 20:21:25 +00:00
|
|
|
mergedOutlines.BooleanAdd( areaToMergePoly, SHAPE_POLY_SET::PM_FAST );
|
|
|
|
mergedOutlines.Simplify( SHAPE_POLY_SET::PM_FAST );
|
2012-07-25 07:36:56 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
// We should have one polygon with hole
|
|
|
|
// We can have 2 polygons with hole, if the 2 initial polygons have only one common corner
|
|
|
|
// and therefore cannot be merged (they are dectected as intersecting)
|
|
|
|
// but we should never have more than 2 polys
|
2015-07-27 19:45:57 +00:00
|
|
|
if( mergedOutlines.OutlineCount() > 2 )
|
2012-08-04 09:43:27 +00:00
|
|
|
{
|
|
|
|
wxLogMessage(wxT("BOARD::CombineAreas error: more than 2 polys after merging") );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:45:57 +00:00
|
|
|
if( mergedOutlines.OutlineCount() > 1 )
|
2012-08-04 09:43:27 +00:00
|
|
|
return false;
|
2008-05-15 11:20:19 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Update the area with the new merged outline
|
|
|
|
delete area_ref->Outline();
|
|
|
|
area_ref->SetOutline( new SHAPE_POLY_SET( mergedOutlines ) );
|
2012-07-30 07:40:25 +00:00
|
|
|
|
|
|
|
RemoveArea( aDeletedList, area_to_combine );
|
2008-05-30 18:06:21 +00:00
|
|
|
|
2013-03-26 09:58:40 +00:00
|
|
|
area_ref->SetLocalFlags( 1 );
|
2017-03-07 12:06:00 +00:00
|
|
|
area_ref->Hatch();
|
2012-07-25 07:36:56 +00:00
|
|
|
|
2012-07-31 13:12:51 +00:00
|
|
|
return true;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|