KiCad PCB EDA Suite
graphics_cleaner.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004-2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
6  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <reporter.h>
27 #include <macros.h>
28 #include <board_commit.h>
29 #include <cleanup_item.h>
30 #include <pcb_shape.h>
31 #include <fp_shape.h>
32 #include <graphics_cleaner.h>
33 
34 
35 GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, FOOTPRINT* aParentFootprint,
36  BOARD_COMMIT& aCommit ) :
37  m_drawings( aDrawings ),
38  m_parentFootprint( aParentFootprint ),
39  m_commit( aCommit ),
40  m_dryRun( true ),
41  m_itemsList( nullptr )
42 {
43 }
44 
45 
46 void GRAPHICS_CLEANER::CleanupBoard( bool aDryRun,
47  std::vector<std::shared_ptr<CLEANUP_ITEM>>* aItemsList,
48  bool aMergeRects, bool aDeleteRedundant )
49 {
50  m_dryRun = aDryRun;
51  m_itemsList = aItemsList;
52 
53  // Clear the flag used to mark some segments as deleted, in dry run:
54  for( BOARD_ITEM* drawing : m_drawings )
55  drawing->ClearFlags( IS_DELETED );
56 
57  if( aDeleteRedundant )
59 
60  if( aMergeRects )
61  mergeRects();
62 
63  // Clear the flag used to mark some segments:
64  for( BOARD_ITEM* drawing : m_drawings )
65  drawing->ClearFlags( IS_DELETED );
66 }
67 
68 
70 {
71  switch( aShape->GetShape() )
72  {
73  case SHAPE_T::SEGMENT:
74  case SHAPE_T::RECT:
75  case SHAPE_T::ARC:
76  return aShape->GetStart() == aShape->GetEnd();
77 
78  case SHAPE_T::CIRCLE:
79  return aShape->GetRadius() == 0;
80 
81  case SHAPE_T::POLY:
82  return aShape->GetPointCount() == 0;
83 
84  case SHAPE_T::BEZIER:
85  aShape->RebuildBezierToSegmentsPointsList( aShape->GetWidth() );
86  return aShape->GetBezierPoints().empty();
87 
88  default:
90  return false;
91  }
92 }
93 
94 
96 {
97  if( aShape1->GetShape() != aShape2->GetShape()
98  || aShape1->GetLayer() != aShape2->GetLayer()
99  || aShape1->GetWidth() != aShape2->GetWidth() )
100  {
101  return false;
102  }
103 
104  switch( aShape1->GetShape() )
105  {
106  case SHAPE_T::SEGMENT:
107  case SHAPE_T::RECT:
108  case SHAPE_T::CIRCLE:
109  return aShape1->GetStart() == aShape2->GetStart()
110  && aShape1->GetEnd() == aShape2->GetEnd();
111 
112  case SHAPE_T::ARC:
113  return aShape1->GetCenter() == aShape2->GetCenter()
114  && aShape1->GetStart() == aShape2->GetStart()
115  && aShape1->GetArcAngle() == aShape2->GetArcAngle();
116 
117  case SHAPE_T::POLY:
118  // TODO
119  return false;
120 
121  case SHAPE_T::BEZIER:
122  return aShape1->GetBezierC1() == aShape2->GetBezierC1()
123  && aShape1->GetBezierC2() == aShape2->GetBezierC2()
124  && aShape1->GetBezierPoints() == aShape2->GetBezierPoints();
125 
126  default:
127  wxFAIL_MSG( "GRAPHICS_CLEANER::areEquivalent unimplemented for "
128  + aShape1->SHAPE_T_asString() );
129  return false;
130  }
131 }
132 
133 
135 {
136  // Remove duplicate segments (2 superimposed identical segments):
137  for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
138  {
139  PCB_SHAPE* segment = dynamic_cast<PCB_SHAPE*>( *it );
140 
141  if( !segment || segment->GetShape() != SHAPE_T::SEGMENT || segment->HasFlag( IS_DELETED ) )
142  continue;
143 
144  if( isNullShape( segment ) )
145  {
146  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
147  item->SetItems( segment );
148  m_itemsList->push_back( item );
149 
150  if( !m_dryRun )
151  m_commit.Remove( segment );
152 
153  continue;
154  }
155 
156  for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
157  {
158  PCB_SHAPE* segment2 = dynamic_cast<PCB_SHAPE*>( *it2 );
159 
160  if( !segment2 || segment2->HasFlag( IS_DELETED ) )
161  continue;
162 
163  if( areEquivalent( segment, segment2 ) )
164  {
165  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
166  item->SetItems( segment2 );
167  m_itemsList->push_back( item );
168 
169  segment2->SetFlags( IS_DELETED );
170 
171  if( !m_dryRun )
172  m_commit.Remove( segment2 );
173  }
174  }
175  }
176 }
177 
178 
180 {
181  struct SIDE_CANDIDATE
182  {
183  SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
184  start( aShape->GetStart() ),
185  end( aShape->GetEnd() ),
186  shape( aShape )
187  {
188  if( start.x > end.x || start.y > end.y )
189  std::swap( start, end );
190  }
191 
192  wxPoint start;
193  wxPoint end;
194  PCB_SHAPE* shape;
195  };
196 
197  std::vector<SIDE_CANDIDATE*> sides;
198  std::map<wxPoint, std::vector<SIDE_CANDIDATE*>> ptMap;
199 
200  // First load all the candidates into the side vector and layer maps
201  for( BOARD_ITEM* item : m_drawings )
202  {
203  PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
204 
205  if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
206  continue;
207 
208  if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
209  {
210  sides.emplace_back( new SIDE_CANDIDATE( shape ) );
211  ptMap[ sides.back()->start ].push_back( sides.back() );
212  }
213  }
214 
215  // Now go through the sides and try and match lines into rectangles
216  for( SIDE_CANDIDATE* side : sides )
217  {
218  if( side->shape->HasFlag( IS_DELETED ) )
219  continue;
220 
221  SIDE_CANDIDATE* left = nullptr;
222  SIDE_CANDIDATE* top = nullptr;
223  SIDE_CANDIDATE* right = nullptr;
224  SIDE_CANDIDATE* bottom = nullptr;
225 
226  auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
227  {
228  return aCandidate->shape->GetLayer() == side->shape->GetLayer()
229  && aCandidate->shape->GetWidth() == side->shape->GetWidth()
230  && !aCandidate->shape->HasFlag( IS_DELETED );
231  };
232 
233  if( side->start.x == side->end.x )
234  {
235  // We've found a possible left; see if we have a top
236  //
237  left = side;
238 
239  for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
240  {
241  if( candidate != left && viable( candidate ) )
242  {
243  top = candidate;
244  break;
245  }
246  }
247  }
248  else if( side->start.y == side->end.y )
249  {
250  // We've found a possible top; see if we have a left
251  //
252  top = side;
253 
254  for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
255  {
256  if( candidate != top && viable( candidate ) )
257  {
258  left = candidate;
259  break;
260  }
261  }
262  }
263 
264  if( top && left )
265  {
266  // See if we can fill in the other two sides
267  //
268  for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
269  {
270  if( candidate != top && candidate != left && viable( candidate ) )
271  {
272  right = candidate;
273  break;
274  }
275  }
276 
277  for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
278  {
279  if( candidate != top && candidate != left && viable( candidate ) )
280  {
281  bottom = candidate;
282  break;
283  }
284  }
285 
286  if( right && bottom && right->end == bottom->end )
287  {
288  left->shape->SetFlags( IS_DELETED );
289  top->shape->SetFlags( IS_DELETED );
290  right->shape->SetFlags( IS_DELETED );
291  bottom->shape->SetFlags( IS_DELETED );
292 
293  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
294  item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
295  m_itemsList->push_back( item );
296 
297  if( !m_dryRun )
298  {
299  PCB_SHAPE* rect;
300 
301  if( m_parentFootprint )
302  rect = new FP_SHAPE( m_parentFootprint );
303  else
304  rect = new PCB_SHAPE();
305 
306  rect->SetShape( SHAPE_T::RECT );
307  rect->SetFilled( false );
308  rect->SetStart( top->start );
309  rect->SetEnd( bottom->end );
310  rect->SetLayer( top->shape->GetLayer() );
311  rect->SetWidth( top->shape->GetWidth() );
312 
313  m_commit.Add( rect );
314  m_commit.Remove( left->shape );
315  m_commit.Remove( top->shape );
316  m_commit.Remove( right->shape );
317  m_commit.Remove( bottom->shape );
318  }
319  }
320  }
321  }
322 
323  for( SIDE_CANDIDATE* side : sides )
324  delete side;
325 }
326 
const std::vector< wxPoint > & GetBezierPoints() const
Definition: eda_shape.h:190
int GetWidth() const
Definition: eda_shape.h:89
BOARD_COMMIT & m_commit
bool isNullShape(PCB_SHAPE *aShape)
void SetFilled(bool aFlag)
Definition: eda_shape.h:83
void SetEnd(const wxPoint &aEnd)
Definition: eda_shape.h:126
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:163
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:97
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:155
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:152
double GetArcAngle() const
Definition: eda_shape.cpp:514
GRAPHICS_CLEANER(DRAWINGS &aDrawings, FOOTPRINT *aParentFootprint, BOARD_COMMIT &aCommit)
This file contains miscellaneous commonly used macros and functions.
void SetStart(const wxPoint &aStart)
Definition: eda_shape.h:101
const wxPoint & GetBezierC1() const
Definition: eda_shape.h:145
int GetPointCount() const
Definition: eda_shape.cpp:1197
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
#define IS_DELETED
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:79
const wxPoint & GetBezierC2() const
Definition: eda_shape.h:148
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aMergeRects, bool aDeleteRedundant)
the cleanup function.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
void SetWidth(int aWidth)
Definition: eda_shape.h:88
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
bool areEquivalent(PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:71
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:366
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:91
SHAPE_T GetShape() const
Definition: eda_shape.h:92
int GetRadius() const
Definition: eda_shape.cpp:466
DRAWINGS & m_drawings
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:142
FOOTPRINT * m_parentFootprint