KiCad PCB EDA Suite
GRAPHICS_CLEANER Class Reference

#include <graphics_cleaner.h>

Public Member Functions

 GRAPHICS_CLEANER (DRAWINGS &aDrawings, FOOTPRINT *aParentFootprint, BOARD_COMMIT &aCommit)
 
void CleanupBoard (bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aMergeRects, bool aDeleteRedundant)
 the cleanup function. More...
 

Private Member Functions

bool isNullShape (PCB_SHAPE *aShape)
 
bool areEquivalent (PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
 
void cleanupSegments ()
 
void mergeRects ()
 

Private Attributes

DRAWINGS & m_drawings
 
FOOTPRINTm_parentFootprint
 
BOARD_COMMITm_commit
 
bool m_dryRun
 
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
 

Detailed Description

Definition at line 35 of file graphics_cleaner.h.

Constructor & Destructor Documentation

◆ GRAPHICS_CLEANER()

GRAPHICS_CLEANER::GRAPHICS_CLEANER ( DRAWINGS &  aDrawings,
FOOTPRINT aParentFootprint,
BOARD_COMMIT aCommit 
)

Definition at line 35 of file graphics_cleaner.cpp.

36  :
37  m_drawings( aDrawings ),
38  m_parentFootprint( aParentFootprint ),
39  m_commit( aCommit ),
40  m_dryRun( true ),
41  m_itemsList( nullptr )
42 {
43 }
BOARD_COMMIT & m_commit
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
DRAWINGS & m_drawings
FOOTPRINT * m_parentFootprint

Member Function Documentation

◆ areEquivalent()

bool GRAPHICS_CLEANER::areEquivalent ( PCB_SHAPE aShape1,
PCB_SHAPE aShape2 
)
private

Definition at line 95 of file graphics_cleaner.cpp.

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 }
const std::vector< wxPoint > & GetBezierPoints() const
Definition: eda_shape.h:190
int GetWidth() const
Definition: eda_shape.h:89
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:97
double GetArcAngle() const
Definition: eda_shape.cpp:495
const wxPoint & GetBezierC1() const
Definition: eda_shape.h:145
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
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
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:68
SHAPE_T GetShape() const
Definition: eda_shape.h:92
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:140

References ARC, BEZIER, CIRCLE, EDA_SHAPE::GetArcAngle(), EDA_SHAPE::GetBezierC1(), EDA_SHAPE::GetBezierC2(), EDA_SHAPE::GetBezierPoints(), PCB_SHAPE::GetCenter(), EDA_SHAPE::GetEnd(), BOARD_ITEM::GetLayer(), EDA_SHAPE::GetShape(), EDA_SHAPE::GetStart(), EDA_SHAPE::GetWidth(), POLY, RECT, SEGMENT, and EDA_SHAPE::SHAPE_T_asString().

Referenced by cleanupSegments().

◆ CleanupBoard()

void GRAPHICS_CLEANER::CleanupBoard ( bool  aDryRun,
std::vector< std::shared_ptr< CLEANUP_ITEM > > *  aItemsList,
bool  aMergeRects,
bool  aDeleteRedundant 
)

the cleanup function.

Parameters
aMergeRects= merge for segments forming a rectangle into a rect
aDeleteRedundant= true to delete null graphics and duplicated graphics

Definition at line 46 of file graphics_cleaner.cpp.

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 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:49
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
#define IS_DELETED
DRAWINGS & m_drawings

References cleanupSegments(), IS_DELETED, m_drawings, m_dryRun, m_itemsList, and mergeRects().

◆ cleanupSegments()

void GRAPHICS_CLEANER::cleanupSegments ( )
private

Definition at line 134 of file graphics_cleaner.cpp.

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 }
BOARD_COMMIT & m_commit
bool isNullShape(PCB_SHAPE *aShape)
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:156
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:153
#define IS_DELETED
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
bool areEquivalent(PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
SHAPE_T GetShape() const
Definition: eda_shape.h:92
DRAWINGS & m_drawings

References areEquivalent(), CLEANUP_DUPLICATE_GRAPHIC, CLEANUP_NULL_GRAPHIC, EDA_SHAPE::GetShape(), EDA_ITEM::HasFlag(), IS_DELETED, isNullShape(), m_commit, m_drawings, m_dryRun, m_itemsList, COMMIT::Remove(), SEGMENT, and EDA_ITEM::SetFlags().

Referenced by CleanupBoard().

◆ isNullShape()

bool GRAPHICS_CLEANER::isNullShape ( PCB_SHAPE aShape)
private

Definition at line 69 of file graphics_cleaner.cpp.

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 }
const std::vector< wxPoint > & GetBezierPoints() const
Definition: eda_shape.h:190
int GetWidth() const
Definition: eda_shape.h:89
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:97
int GetPointCount() const
Definition: eda_shape.cpp:1158
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:68
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_BezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:363
SHAPE_T GetShape() const
Definition: eda_shape.h:92
int GetRadius() const
Definition: eda_shape.cpp:463

References ARC, BEZIER, CIRCLE, EDA_SHAPE::GetBezierPoints(), EDA_SHAPE::GetEnd(), EDA_SHAPE::GetPointCount(), EDA_SHAPE::GetRadius(), EDA_SHAPE::GetShape(), EDA_SHAPE::GetStart(), EDA_SHAPE::GetWidth(), POLY, EDA_SHAPE::RebuildBezierToSegmentsPointsList(), RECT, SEGMENT, EDA_SHAPE::SHAPE_T_asString(), and UNIMPLEMENTED_FOR.

Referenced by cleanupSegments(), and mergeRects().

◆ mergeRects()

void GRAPHICS_CLEANER::mergeRects ( )
private

Definition at line 179 of file graphics_cleaner.cpp.

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 }
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:161
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
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
void SetStart(const wxPoint &aStart)
Definition: eda_shape.h:101
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
#define IS_DELETED
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
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:91
SHAPE_T GetShape() const
Definition: eda_shape.h:92
DRAWINGS & m_drawings
FOOTPRINT * m_parentFootprint

References COMMIT::Add(), CLEANUP_LINES_TO_RECT, EDA_SHAPE::GetEnd(), EDA_SHAPE::GetShape(), EDA_SHAPE::GetStart(), IS_DELETED, isNullShape(), left, m_commit, m_drawings, m_dryRun, m_itemsList, m_parentFootprint, RECT, COMMIT::Remove(), right, SEGMENT, EDA_SHAPE::SetEnd(), EDA_SHAPE::SetFilled(), BOARD_ITEM::SetLayer(), EDA_SHAPE::SetShape(), EDA_SHAPE::SetStart(), and EDA_SHAPE::SetWidth().

Referenced by CleanupBoard().

Member Data Documentation

◆ m_commit

BOARD_COMMIT& GRAPHICS_CLEANER::m_commit
private

Definition at line 58 of file graphics_cleaner.h.

Referenced by cleanupSegments(), and mergeRects().

◆ m_drawings

DRAWINGS& GRAPHICS_CLEANER::m_drawings
private

Definition at line 56 of file graphics_cleaner.h.

Referenced by CleanupBoard(), cleanupSegments(), and mergeRects().

◆ m_dryRun

bool GRAPHICS_CLEANER::m_dryRun
private

Definition at line 59 of file graphics_cleaner.h.

Referenced by CleanupBoard(), cleanupSegments(), and mergeRects().

◆ m_itemsList

std::vector<std::shared_ptr<CLEANUP_ITEM> >* GRAPHICS_CLEANER::m_itemsList
private

Definition at line 60 of file graphics_cleaner.h.

Referenced by CleanupBoard(), cleanupSegments(), and mergeRects().

◆ m_parentFootprint

FOOTPRINT* GRAPHICS_CLEANER::m_parentFootprint
private

Definition at line 57 of file graphics_cleaner.h.

Referenced by mergeRects().


The documentation for this class was generated from the following files: