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 isNullSegment (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 34 of file graphics_cleaner.cpp.

35  :
36  m_drawings( aDrawings ),
37  m_parentFootprint( aParentFootprint ),
38  m_commit( aCommit ),
39  m_dryRun( true ),
40  m_itemsList( nullptr )
41 {
42 }
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 98 of file graphics_cleaner.cpp.

99 {
100  if( aShape1->GetShape() != aShape2->GetShape()
101  || aShape1->GetLayer() != aShape2->GetLayer()
102  || aShape1->GetWidth() != aShape2->GetWidth() )
103  {
104  return false;
105  }
106 
107  switch( aShape1->GetShape() )
108  {
109  case S_SEGMENT:
110  case S_RECT:
111  case S_CIRCLE:
112  return aShape1->GetStart() == aShape2->GetStart()
113  && aShape1->GetEnd() == aShape2->GetEnd();
114 
115  case S_ARC:
116  return aShape1->GetCenter() == aShape2->GetCenter()
117  && aShape1->GetArcStart() == aShape2->GetArcStart()
118  && aShape1->GetAngle() == aShape2->GetAngle();
119 
120  case S_POLYGON:
121  // TODO
122  return false;
123 
124  case S_CURVE:
125  return aShape1->GetBezControl1() == aShape2->GetBezControl1()
126  && aShape1->GetBezControl2() == aShape2->GetBezControl2()
127  && aShape1->GetBezierPoints() == aShape2->GetBezierPoints();
128 
129  default:
130  wxFAIL_MSG( "GRAPHICS_CLEANER::areEquivalent unsupported PCB_SHAPE shape: "
131  + PCB_SHAPE_TYPE_T_asString( aShape1->GetShape() ) );
132  return false;
133  }
134 }
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:156
int GetWidth() const
Definition: pcb_shape.h:118
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:54
usual segment : line with rounded ends
Definition: board_item.h:50
Arcs (with rounded ends)
Definition: board_item.h:52
segment with non rounded ends
Definition: board_item.h:51
wxPoint GetArcStart() const
Definition: pcb_shape.h:179
static wxString PCB_SHAPE_TYPE_T_asString(PCB_SHAPE_TYPE_T a)
Definition: board_item.h:59
wxPoint GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.cpp:342
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:145
const std::vector< wxPoint > & GetBezierPoints() const
Definition: pcb_shape.h:253
const wxPoint & GetBezControl2() const
Definition: pcb_shape.h:136
double GetAngle() const
Definition: pcb_shape.h:127
ring
Definition: board_item.h:53
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:130
Bezier Curve.
Definition: board_item.h:55
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:173
const wxPoint & GetBezControl1() const
Definition: pcb_shape.h:133

References PCB_SHAPE::GetAngle(), PCB_SHAPE::GetArcStart(), PCB_SHAPE::GetBezControl1(), PCB_SHAPE::GetBezControl2(), PCB_SHAPE::GetBezierPoints(), PCB_SHAPE::GetCenter(), PCB_SHAPE::GetEnd(), BOARD_ITEM::GetLayer(), PCB_SHAPE::GetShape(), PCB_SHAPE::GetStart(), PCB_SHAPE::GetWidth(), PCB_SHAPE_TYPE_T_asString(), S_ARC, S_CIRCLE, S_CURVE, S_POLYGON, S_RECT, and S_SEGMENT.

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 45 of file graphics_cleaner.cpp.

48 {
49  m_dryRun = aDryRun;
50  m_itemsList = aItemsList;
51 
52  // Clear the flag used to mark some segments as deleted, in dry run:
53  for( BOARD_ITEM* drawing : m_drawings )
54  drawing->ClearFlags( IS_DELETED );
55 
56  if( aDeleteRedundant )
58 
59  if( aMergeRects )
60  mergeRects();
61 
62  // Clear the flag used to mark some segments:
63  for( BOARD_ITEM* drawing : m_drawings )
64  drawing->ClearFlags( IS_DELETED );
65 }
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
#define IS_DELETED
Definition: eda_item.h:110
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
DRAWINGS & m_drawings

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

◆ cleanupSegments()

void GRAPHICS_CLEANER::cleanupSegments ( )
private

Definition at line 137 of file graphics_cleaner.cpp.

138 {
139  // Remove duplicate segments (2 superimposed identical segments):
140  for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
141  {
142  PCB_SHAPE* segment = dynamic_cast<PCB_SHAPE*>( *it );
143 
144  if( !segment || segment->GetShape() != S_SEGMENT || segment->HasFlag( IS_DELETED ) )
145  continue;
146 
147  if( isNullSegment( segment ) )
148  {
149  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
150  item->SetItems( segment );
151  m_itemsList->push_back( item );
152 
153  if( !m_dryRun )
154  m_commit.Removed( segment );
155 
156  continue;
157  }
158 
159  for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
160  {
161  PCB_SHAPE* segment2 = dynamic_cast<PCB_SHAPE*>( *it2 );
162 
163  if( !segment2 || segment2->HasFlag( IS_DELETED ) )
164  continue;
165 
166  if( areEquivalent( segment, segment2 ) )
167  {
168  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
169  item->SetItems( segment2 );
170  m_itemsList->push_back( item );
171 
172  segment2->SetFlags( IS_DELETED );
173 
174  if( !m_dryRun )
175  m_commit.Removed( segment2 );
176  }
177  }
178  }
179 }
bool isNullSegment(PCB_SHAPE *aShape)
BOARD_COMMIT & m_commit
#define IS_DELETED
Definition: eda_item.h:110
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
usual segment : line with rounded ends
Definition: board_item.h:50
COMMIT & Removed(EDA_ITEM *aItem)
Modify a given item in the model.
Definition: commit.h:96
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:203
bool areEquivalent(PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:130
DRAWINGS & m_drawings
bool HasFlag(STATUS_FLAGS aFlag) const
Definition: eda_item.h:206

References areEquivalent(), CLEANUP_DUPLICATE_GRAPHIC, CLEANUP_NULL_GRAPHIC, PCB_SHAPE::GetShape(), EDA_ITEM::HasFlag(), IS_DELETED, isNullSegment(), m_commit, m_drawings, m_dryRun, m_itemsList, COMMIT::Removed(), S_SEGMENT, and EDA_ITEM::SetFlags().

Referenced by CleanupBoard().

◆ isNullSegment()

bool GRAPHICS_CLEANER::isNullSegment ( PCB_SHAPE aShape)
private

Definition at line 68 of file graphics_cleaner.cpp.

69 {
70  switch( aSegment->GetShape() )
71  {
72  case S_SEGMENT:
73  case S_RECT:
74  return aSegment->GetStart() == aSegment->GetEnd();
75 
76  case S_CIRCLE:
77  return aSegment->GetRadius() == 0;
78 
79  case S_ARC:
80  return aSegment->GetCenter().x == aSegment->GetArcStart().x
81  && aSegment->GetCenter().y == aSegment->GetArcStart().y;
82 
83  case S_POLYGON:
84  return aSegment->GetPointCount() == 0;
85 
86  case S_CURVE:
87  aSegment->RebuildBezierToSegmentsPointsList( aSegment->GetWidth() );
88  return aSegment->GetBezierPoints().empty();
89 
90  default:
91  wxFAIL_MSG( "GRAPHICS_CLEANER::isNullSegment unsupported PCB_SHAPE shape: "
92  + PCB_SHAPE_TYPE_T_asString( aSegment->GetShape() ) );
93  return false;
94  }
95 }
polygon (not yet used for tracks, but could be in microwave apps)
Definition: board_item.h:54
usual segment : line with rounded ends
Definition: board_item.h:50
Arcs (with rounded ends)
Definition: board_item.h:52
segment with non rounded ends
Definition: board_item.h:51
static wxString PCB_SHAPE_TYPE_T_asString(PCB_SHAPE_TYPE_T a)
Definition: board_item.h:59
ring
Definition: board_item.h:53
Bezier Curve.
Definition: board_item.h:55

References PCB_SHAPE::GetArcStart(), PCB_SHAPE::GetBezierPoints(), PCB_SHAPE::GetCenter(), PCB_SHAPE::GetEnd(), PCB_SHAPE::GetPointCount(), PCB_SHAPE::GetRadius(), PCB_SHAPE::GetShape(), PCB_SHAPE::GetStart(), PCB_SHAPE::GetWidth(), PCB_SHAPE_TYPE_T_asString(), PCB_SHAPE::RebuildBezierToSegmentsPointsList(), S_ARC, S_CIRCLE, S_CURVE, S_POLYGON, S_RECT, and S_SEGMENT.

Referenced by cleanupSegments().

◆ mergeRects()

void GRAPHICS_CLEANER::mergeRects ( )
private

Definition at line 182 of file graphics_cleaner.cpp.

183 {
184  struct SIDE_CANDIDATE
185  {
186  SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
187  start( aShape->GetStart() ),
188  end( aShape->GetEnd() ),
189  shape( aShape )
190  {
191  if( start.x > end.x || start.y > end.y )
192  std::swap( start, end );
193  }
194 
195  wxPoint start;
196  wxPoint end;
197  PCB_SHAPE* shape;
198  };
199 
200  std::vector<SIDE_CANDIDATE*> sides;
201  std::map<wxPoint, std::vector<SIDE_CANDIDATE*>> ptMap;
202 
203  // First load all the candidates into the side vector and layer maps
204  for( BOARD_ITEM* item : m_drawings )
205  {
206  PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
207 
208  if( !shape || shape->GetShape() != S_SEGMENT )
209  continue;
210 
211  if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
212  {
213  sides.emplace_back( new SIDE_CANDIDATE( shape ) );
214  ptMap[ sides.back()->start ].push_back( sides.back() );
215  }
216  }
217 
218  // Now go through the sides and try and match lines into rectangles
219  for( SIDE_CANDIDATE* side : sides )
220  {
221  if( side->shape->HasFlag( IS_DELETED ) )
222  continue;
223 
224  SIDE_CANDIDATE* left = nullptr;
225  SIDE_CANDIDATE* top = nullptr;
226  SIDE_CANDIDATE* right = nullptr;
227  SIDE_CANDIDATE* bottom = nullptr;
228 
229  auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
230  {
231  return aCandidate->shape->GetLayer() == side->shape->GetLayer()
232  && aCandidate->shape->GetWidth() == side->shape->GetWidth()
233  && !aCandidate->shape->HasFlag( IS_DELETED );
234  };
235 
236  if( side->start.x == side->end.x )
237  {
238  // We've found a possible left; see if we have a top
239  //
240  left = side;
241 
242  for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
243  {
244  if( candidate != left && viable( candidate ) )
245  {
246  top = candidate;
247  break;
248  }
249  }
250  }
251  else if( side->start.y == side->end.y )
252  {
253  // We've found a possible top; see if we have a left
254  //
255  top = side;
256 
257  for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
258  {
259  if( candidate != top && viable( candidate ) )
260  {
261  left = candidate;
262  break;
263  }
264  }
265  }
266 
267  if( top && left )
268  {
269  // See if we can fill in the other two sides
270  //
271  for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
272  {
273  if( candidate != top && viable( candidate ) )
274  {
275  right = candidate;
276  break;
277  }
278  }
279 
280  for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
281  {
282  if( candidate != left && viable( candidate ) )
283  {
284  bottom = candidate;
285  break;
286  }
287  }
288 
289  if( right && bottom && right->end == bottom->end )
290  {
291  left->shape->SetFlags( IS_DELETED );
292  top->shape->SetFlags( IS_DELETED );
293  right->shape->SetFlags( IS_DELETED );
294  bottom->shape->SetFlags( IS_DELETED );
295 
296  std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
297  item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
298  m_itemsList->push_back( item );
299 
300  if( !m_dryRun )
301  {
302  PCB_SHAPE* rect;
303 
304  if( m_parentFootprint )
305  rect = new FP_SHAPE( m_parentFootprint );
306  else
307  rect = new PCB_SHAPE();
308 
309  rect->SetShape( S_RECT );
310  rect->SetFilled( false );
311  rect->SetStart( top->start );
312  rect->SetEnd( bottom->end );
313  rect->SetLayer( top->shape->GetLayer() );
314  rect->SetWidth( top->shape->GetWidth() );
315 
316  m_commit.Add( rect );
317  m_commit.Remove( left->shape );
318  m_commit.Remove( top->shape );
319  m_commit.Remove( right->shape );
320  m_commit.Remove( bottom->shape );
321  }
322  }
323  }
324  }
325 
326  for( SIDE_CANDIDATE* side : sides )
327  delete side;
328 }
BOARD_COMMIT & m_commit
const wxPoint & GetEnd() const
Function GetEnd returns the ending point of the graphic.
Definition: pcb_shape.h:156
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:194
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
#define IS_DELETED
Definition: eda_item.h:110
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
void SetFilled(bool aFlag)
Definition: pcb_shape.h:94
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
usual segment : line with rounded ends
Definition: board_item.h:50
segment with non rounded ends
Definition: board_item.h:51
void SetShape(PCB_SHAPE_TYPE_T aShape)
Definition: pcb_shape.h:129
const wxPoint & GetStart() const
Function GetStart returns the starting point of the graphic.
Definition: pcb_shape.h:145
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
void SetStart(const wxPoint &aStart)
Definition: pcb_shape.h:148
void SetWidth(int aWidth)
Definition: pcb_shape.h:117
PCB_SHAPE_TYPE_T GetShape() const
Definition: pcb_shape.h:130
DRAWINGS & m_drawings
FOOTPRINT * m_parentFootprint
void SetEnd(const wxPoint &aEnd)
Definition: pcb_shape.h:159

References COMMIT::Add(), CLEANUP_LINES_TO_RECT, PCB_SHAPE::GetEnd(), PCB_SHAPE::GetShape(), PCB_SHAPE::GetStart(), IS_DELETED, left, m_commit, m_drawings, m_dryRun, m_itemsList, m_parentFootprint, COMMIT::Remove(), right, S_RECT, S_SEGMENT, PCB_SHAPE::SetEnd(), PCB_SHAPE::SetFilled(), BOARD_ITEM::SetLayer(), PCB_SHAPE::SetShape(), PCB_SHAPE::SetStart(), and PCB_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: