2020-06-17 12:46:50 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004-2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
|
|
|
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
|
|
|
|
* Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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 <reporter.h>
|
|
|
|
#include <board_commit.h>
|
|
|
|
#include <cleanup_item.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <pcb_shape.h>
|
|
|
|
#include <fp_shape.h>
|
2020-06-17 12:46:50 +00:00
|
|
|
#include <graphics_cleaner.h>
|
|
|
|
|
|
|
|
|
|
|
|
GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, MODULE* aParentModule,
|
|
|
|
BOARD_COMMIT& aCommit ) :
|
|
|
|
m_drawings( aDrawings ),
|
|
|
|
m_parentModule( aParentModule ),
|
|
|
|
m_commit( aCommit ),
|
|
|
|
m_dryRun( true ),
|
|
|
|
m_itemsList( nullptr )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 13:33:16 +00:00
|
|
|
void GRAPHICS_CLEANER::CleanupBoard( bool aDryRun, std::vector<std::shared_ptr<CLEANUP_ITEM> >* aItemsList,
|
2020-06-17 12:46:50 +00:00
|
|
|
bool aMergeRects, bool aDeleteRedundant )
|
|
|
|
{
|
|
|
|
m_dryRun = aDryRun;
|
|
|
|
m_itemsList = aItemsList;
|
|
|
|
|
|
|
|
// Clear the flag used to mark some segments as deleted, in dry run:
|
|
|
|
for( BOARD_ITEM* drawing : m_drawings )
|
|
|
|
drawing->ClearFlags( IS_DELETED );
|
|
|
|
|
|
|
|
if( aDeleteRedundant )
|
|
|
|
cleanupSegments();
|
|
|
|
|
|
|
|
if( aMergeRects )
|
|
|
|
mergeRects();
|
|
|
|
|
|
|
|
// Clear the flag used to mark some segments:
|
|
|
|
for( BOARD_ITEM* drawing : m_drawings )
|
|
|
|
drawing->ClearFlags( IS_DELETED );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
bool GRAPHICS_CLEANER::isNullSegment( PCB_SHAPE* aSegment )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
|
|
|
switch( aSegment->GetShape() )
|
|
|
|
{
|
|
|
|
case S_SEGMENT:
|
|
|
|
case S_RECT:
|
|
|
|
return aSegment->GetStart() == aSegment->GetEnd();
|
|
|
|
|
|
|
|
case S_CIRCLE:
|
|
|
|
return aSegment->GetRadius() == 0;
|
|
|
|
|
|
|
|
case S_ARC:
|
|
|
|
return aSegment->GetCenter().x == aSegment->GetArcStart().x
|
|
|
|
&& aSegment->GetCenter().y == aSegment->GetArcStart().y;
|
|
|
|
|
|
|
|
case S_POLYGON:
|
|
|
|
return aSegment->GetPointCount() == 0;
|
|
|
|
|
|
|
|
case S_CURVE:
|
|
|
|
aSegment->RebuildBezierToSegmentsPointsList( aSegment->GetWidth() );
|
|
|
|
return aSegment->GetBezierPoints().empty();
|
|
|
|
|
|
|
|
default:
|
2020-10-04 23:34:59 +00:00
|
|
|
wxFAIL_MSG( "GRAPHICS_CLEANER::isNullSegment unsupported PCB_SHAPE shape: "
|
2020-10-04 14:19:33 +00:00
|
|
|
+ PCB_SHAPE_TYPE_T_asString( aSegment->GetShape()) );
|
2020-06-17 12:46:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
bool GRAPHICS_CLEANER::areEquivalent( PCB_SHAPE* aShape1, PCB_SHAPE* aShape2 )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
if( aShape1->GetShape() != aShape2->GetShape()
|
|
|
|
|| aShape1->GetLayer() != aShape2->GetLayer()
|
|
|
|
|| aShape1->GetWidth() != aShape2->GetWidth() )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
switch( aShape1->GetShape() )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
|
|
|
case S_SEGMENT:
|
|
|
|
case S_RECT:
|
|
|
|
case S_CIRCLE:
|
2020-10-04 23:34:59 +00:00
|
|
|
return aShape1->GetStart() == aShape2->GetStart()
|
|
|
|
&& aShape1->GetEnd() == aShape2->GetEnd();
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
case S_ARC:
|
2020-10-04 23:34:59 +00:00
|
|
|
return aShape1->GetCenter() == aShape2->GetCenter()
|
|
|
|
&& aShape1->GetArcStart() == aShape2->GetArcStart()
|
|
|
|
&& aShape1->GetAngle() == aShape2->GetAngle();
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
case S_POLYGON:
|
|
|
|
// TODO
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case S_CURVE:
|
2020-10-04 23:34:59 +00:00
|
|
|
return aShape1->GetBezControl1() == aShape2->GetBezControl1()
|
|
|
|
&& aShape1->GetBezControl2() == aShape2->GetBezControl2()
|
|
|
|
&& aShape1->GetBezierPoints() == aShape2->GetBezierPoints();
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
default:
|
2020-10-04 23:34:59 +00:00
|
|
|
wxFAIL_MSG( "GRAPHICS_CLEANER::areEquivalent unsupported PCB_SHAPE shape: "
|
|
|
|
+ PCB_SHAPE_TYPE_T_asString( aShape1->GetShape()) );
|
2020-06-17 12:46:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRAPHICS_CLEANER::cleanupSegments()
|
|
|
|
{
|
|
|
|
// Remove duplicate segments (2 superimposed identical segments):
|
|
|
|
for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* segment = dynamic_cast<PCB_SHAPE*>( *it );
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
if( !segment || segment->GetShape() != S_SEGMENT || segment->HasFlag( IS_DELETED ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( isNullSegment( segment ) )
|
|
|
|
{
|
2020-08-11 13:33:16 +00:00
|
|
|
std::shared_ptr<CLEANUP_ITEM> item( new CLEANUP_ITEM( CLEANUP_NULL_GRAPHIC ) );
|
2020-06-17 12:46:50 +00:00
|
|
|
item->SetItems( segment );
|
|
|
|
m_itemsList->push_back( item );
|
|
|
|
|
2020-06-29 21:58:05 +00:00
|
|
|
if( !m_dryRun )
|
|
|
|
m_commit.Removed( segment );
|
|
|
|
|
2020-06-17 12:46:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* segment2 = dynamic_cast<PCB_SHAPE*>( *it2 );
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
if( !segment2 || segment2->HasFlag( IS_DELETED ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( areEquivalent( segment, segment2 ) )
|
|
|
|
{
|
2020-08-11 13:33:16 +00:00
|
|
|
std::shared_ptr<CLEANUP_ITEM> item( new CLEANUP_ITEM( CLEANUP_DUPLICATE_GRAPHIC ) );
|
2020-06-17 12:46:50 +00:00
|
|
|
item->SetItems( segment2 );
|
|
|
|
m_itemsList->push_back( item );
|
|
|
|
|
|
|
|
segment2->SetFlags( IS_DELETED );
|
2020-06-29 21:58:05 +00:00
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
m_commit.Removed( segment2 );
|
2020-06-17 12:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void GRAPHICS_CLEANER::mergeRects()
|
|
|
|
{
|
|
|
|
struct SIDE_CANDIDATE
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
|
|
|
|
start( aShape->GetStart() ),
|
|
|
|
end( aShape->GetEnd() ),
|
|
|
|
shape( aShape )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
|
|
|
if( start.x > end.x || start.y > end.y )
|
|
|
|
std::swap( start, end );
|
|
|
|
}
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
wxPoint start;
|
|
|
|
wxPoint end;
|
|
|
|
PCB_SHAPE* shape;
|
2020-06-17 12:46:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<SIDE_CANDIDATE*> sides;
|
|
|
|
std::map<wxPoint, std::vector<SIDE_CANDIDATE*>> ptMap;
|
|
|
|
|
|
|
|
// First load all the candidates into the side vector and layer maps
|
|
|
|
for( BOARD_ITEM* item : m_drawings )
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
|
2020-06-17 12:46:50 +00:00
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
if( !shape || shape->GetShape() != S_SEGMENT )
|
2020-06-17 12:46:50 +00:00
|
|
|
continue;
|
|
|
|
|
2020-10-04 23:34:59 +00:00
|
|
|
if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
sides.emplace_back( new SIDE_CANDIDATE( shape ) );
|
2020-06-17 12:46:50 +00:00
|
|
|
ptMap[ sides.back()->start ].push_back( sides.back() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now go through the sides and try and match lines into rectangles
|
|
|
|
for( SIDE_CANDIDATE* side : sides )
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
if( side->shape->HasFlag( IS_DELETED ) )
|
2020-06-17 12:46:50 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
SIDE_CANDIDATE* left = nullptr;
|
|
|
|
SIDE_CANDIDATE* top = nullptr;
|
|
|
|
SIDE_CANDIDATE* right = nullptr;
|
|
|
|
SIDE_CANDIDATE* bottom = nullptr;
|
|
|
|
|
|
|
|
auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
return aCandidate->shape->GetLayer() == side->shape->GetLayer()
|
|
|
|
&& aCandidate->shape->GetWidth() == side->shape->GetWidth()
|
|
|
|
&& !aCandidate->shape->HasFlag( IS_DELETED );
|
2020-06-17 12:46:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if( side->start.x == side->end.x )
|
|
|
|
{
|
|
|
|
// We've found a possible left; see if we have a top
|
|
|
|
//
|
|
|
|
left = side;
|
|
|
|
|
|
|
|
for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
|
|
|
|
{
|
|
|
|
if( candidate != left && viable( candidate ) )
|
|
|
|
{
|
|
|
|
top = candidate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( side->start.y == side->end.y )
|
|
|
|
{
|
|
|
|
// We've found a possible top; see if we have a left
|
|
|
|
//
|
|
|
|
top = side;
|
|
|
|
|
|
|
|
for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
|
|
|
|
{
|
|
|
|
if( candidate != top && viable( candidate ) )
|
|
|
|
{
|
|
|
|
left = candidate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( top && left )
|
|
|
|
{
|
|
|
|
// See if we can fill in the other two sides
|
|
|
|
//
|
|
|
|
for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
|
|
|
|
{
|
|
|
|
if( candidate != top && viable( candidate ) )
|
|
|
|
{
|
|
|
|
right = candidate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
|
|
|
|
{
|
|
|
|
if( candidate != left && viable( candidate ) )
|
|
|
|
{
|
|
|
|
bottom = candidate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:01:23 +00:00
|
|
|
if( right && bottom && right->end == bottom->end )
|
2020-06-17 12:46:50 +00:00
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
left->shape->SetFlags( IS_DELETED );
|
|
|
|
top->shape->SetFlags( IS_DELETED );
|
|
|
|
right->shape->SetFlags( IS_DELETED );
|
|
|
|
bottom->shape->SetFlags( IS_DELETED );
|
2020-06-17 12:46:50 +00:00
|
|
|
|
2020-08-11 13:33:16 +00:00
|
|
|
std::shared_ptr<CLEANUP_ITEM> item( new CLEANUP_ITEM( CLEANUP_LINES_TO_RECT ) );
|
2020-10-04 23:34:59 +00:00
|
|
|
item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
|
2020-06-17 12:46:50 +00:00
|
|
|
m_itemsList->push_back( item );
|
|
|
|
|
|
|
|
if( !m_dryRun )
|
|
|
|
{
|
2020-10-04 23:34:59 +00:00
|
|
|
PCB_SHAPE* rect;
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
if( m_parentModule )
|
2020-10-04 23:34:59 +00:00
|
|
|
rect = new FP_SHAPE( m_parentModule );
|
2020-06-17 12:46:50 +00:00
|
|
|
else
|
2020-10-04 23:34:59 +00:00
|
|
|
rect = new PCB_SHAPE();
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
rect->SetShape( S_RECT );
|
|
|
|
rect->SetStart( top->start );
|
|
|
|
rect->SetEnd( bottom->end );
|
2020-10-04 23:34:59 +00:00
|
|
|
rect->SetLayer( top->shape->GetLayer() );
|
|
|
|
rect->SetWidth( top->shape->GetWidth() );
|
2020-06-17 12:46:50 +00:00
|
|
|
|
|
|
|
m_commit.Add( rect );
|
2020-10-04 23:34:59 +00:00
|
|
|
m_commit.Remove( left->shape );
|
|
|
|
m_commit.Remove( top->shape );
|
|
|
|
m_commit.Remove( right->shape );
|
|
|
|
m_commit.Remove( bottom->shape );
|
2020-06-17 12:46:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( SIDE_CANDIDATE* side : sides )
|
|
|
|
delete side;
|
|
|
|
}
|
|
|
|
|