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-2022 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 <pad.h>
33#include <footprint.h>
34#include <graphics_cleaner.h>
36#include <tool/tool_manager.h>
37#include <tools/pad_tool.h>
38
39GRAPHICS_CLEANER::GRAPHICS_CLEANER( DRAWINGS& aDrawings, FOOTPRINT* aParentFootprint,
40 BOARD_COMMIT& aCommit, TOOL_MANAGER* aToolMgr ) :
41 m_drawings( aDrawings ),
42 m_parentFootprint( aParentFootprint ),
43 m_commit( aCommit ),
44 m_toolMgr( aToolMgr ),
45 m_dryRun( true ),
46 m_epsilon( 0 ),
47 m_itemsList( nullptr )
48{
49}
50
51
53 std::vector<std::shared_ptr<CLEANUP_ITEM>>* aItemsList,
54 bool aMergeRects, bool aDeleteRedundant, bool aMergePads )
55{
56 m_dryRun = aDryRun;
57 m_itemsList = aItemsList;
58
60
61 // Clear the flag used to mark some shapes as deleted, in dry run:
62 for( BOARD_ITEM* drawing : m_drawings )
63 drawing->ClearFlags( IS_DELETED );
64
65 if( aDeleteRedundant )
67
68 if( aMergeRects )
69 mergeRects();
70
71 if( aMergePads )
72 mergePads();
73
74 // Clear the flag used to mark some shapes:
75 for( BOARD_ITEM* drawing : m_drawings )
76 drawing->ClearFlags( IS_DELETED );
77}
78
79
80bool equivalent( const VECTOR2I& a, const VECTOR2I& b, int epsilon )
81{
82 return abs( a.x - b.x ) < epsilon && abs( a.y - b.y ) < epsilon;
83};
84
85
87{
88 switch( aShape->GetShape() )
89 {
91 case SHAPE_T::RECT:
92 case SHAPE_T::ARC:
93 return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
94
95 case SHAPE_T::CIRCLE:
96 return aShape->GetRadius() == 0;
97
98 case SHAPE_T::POLY:
99 return aShape->GetPointCount() == 0;
100
101 case SHAPE_T::BEZIER:
102 aShape->RebuildBezierToSegmentsPointsList( aShape->GetWidth() );
103 return aShape->GetBezierPoints().empty();
104
105 default:
107 return false;
108 }
109}
110
111
113{
114 if( aShape1->GetShape() != aShape2->GetShape()
115 || aShape1->GetLayer() != aShape2->GetLayer()
116 || aShape1->GetWidth() != aShape2->GetWidth() )
117 {
118 return false;
119 }
120
121 switch( aShape1->GetShape() )
122 {
123 case SHAPE_T::SEGMENT:
124 case SHAPE_T::RECT:
125 case SHAPE_T::CIRCLE:
126 return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
127 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
128
129 case SHAPE_T::ARC:
130 return equivalent( aShape1->GetCenter(), aShape2->GetCenter(), m_epsilon )
131 && equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
132 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
133
134 case SHAPE_T::POLY:
135 // TODO
136 return false;
137
138 case SHAPE_T::BEZIER:
139 return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
140 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon )
141 && equivalent( aShape1->GetBezierC1(), aShape2->GetBezierC1(), m_epsilon )
142 && equivalent( aShape1->GetBezierC2(), aShape2->GetBezierC2(), m_epsilon );
143
144 default:
145 wxFAIL_MSG( wxT( "GRAPHICS_CLEANER::areEquivalent unimplemented for " )
146 + aShape1->SHAPE_T_asString() );
147 return false;
148 }
149}
150
151
153{
154 // Remove duplicate shapes (2 superimposed identical shapes):
155 for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
156 {
157 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( *it );
158
159 if( !shape || shape->HasFlag( IS_DELETED ) )
160 continue;
161
162 if( isNullShape( shape ) )
163 {
164 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
165 item->SetItems( shape );
166 m_itemsList->push_back( item );
167
168 if( !m_dryRun )
169 m_commit.Remove( shape );
170
171 continue;
172 }
173
174 for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
175 {
176 PCB_SHAPE* shape2 = dynamic_cast<PCB_SHAPE*>( *it2 );
177
178 if( !shape2 || shape2->HasFlag( IS_DELETED ) )
179 continue;
180
181 if( areEquivalent( shape, shape2 ) )
182 {
183 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
184 item->SetItems( shape2 );
185 m_itemsList->push_back( item );
186
187 shape2->SetFlags(IS_DELETED );
188
189 if( !m_dryRun )
190 m_commit.Remove( shape2 );
191 }
192 }
193 }
194}
195
196
198{
199 struct SIDE_CANDIDATE
200 {
201 SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
202 start( aShape->GetStart() ),
203 end( aShape->GetEnd() ),
204 shape( aShape )
205 {
206 if( start.x > end.x || start.y > end.y )
207 std::swap( start, end );
208 }
209
210 VECTOR2I start;
211 VECTOR2I end;
212 PCB_SHAPE* shape;
213 };
214
215 std::vector<SIDE_CANDIDATE*> sides;
216 std::map<VECTOR2I, std::vector<SIDE_CANDIDATE*>> ptMap;
217
218 // First load all the candidates into the side vector and layer maps
219 for( BOARD_ITEM* item : m_drawings )
220 {
221 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
222
223 if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
224 continue;
225
226 if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
227 {
228 sides.emplace_back( new SIDE_CANDIDATE( shape ) );
229 ptMap[ sides.back()->start ].push_back( sides.back() );
230 }
231 }
232
233 // Now go through the sides and try and match lines into rectangles
234 for( SIDE_CANDIDATE* side : sides )
235 {
236 if( side->shape->HasFlag( IS_DELETED ) )
237 continue;
238
239 SIDE_CANDIDATE* left = nullptr;
240 SIDE_CANDIDATE* top = nullptr;
241 SIDE_CANDIDATE* right = nullptr;
242 SIDE_CANDIDATE* bottom = nullptr;
243
244 auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
245 {
246 return aCandidate->shape->GetLayer() == side->shape->GetLayer()
247 && aCandidate->shape->GetWidth() == side->shape->GetWidth()
248 && !aCandidate->shape->HasFlag( IS_DELETED );
249 };
250
251 if( side->start.x == side->end.x )
252 {
253 // We've found a possible left; see if we have a top
254 //
255 left = side;
256
257 for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
258 {
259 if( candidate != left && viable( candidate ) )
260 {
261 top = candidate;
262 break;
263 }
264 }
265 }
266 else if( side->start.y == side->end.y )
267 {
268 // We've found a possible top; see if we have a left
269 //
270 top = side;
271
272 for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
273 {
274 if( candidate != top && viable( candidate ) )
275 {
276 left = candidate;
277 break;
278 }
279 }
280 }
281
282 if( top && left )
283 {
284 // See if we can fill in the other two sides
285 //
286 for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
287 {
288 if( candidate != top && candidate != left && viable( candidate ) )
289 {
290 right = candidate;
291 break;
292 }
293 }
294
295 for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
296 {
297 if( candidate != top && candidate != left && viable( candidate ) )
298 {
299 bottom = candidate;
300 break;
301 }
302 }
303
304 if( right && bottom && right->end == bottom->end )
305 {
306 left->shape->SetFlags( IS_DELETED );
307 top->shape->SetFlags( IS_DELETED );
308 right->shape->SetFlags( IS_DELETED );
309 bottom->shape->SetFlags( IS_DELETED );
310
311 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
312 item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
313 m_itemsList->push_back( item );
314
315 if( !m_dryRun )
316 {
317 PCB_SHAPE* rect;
318 FP_SHAPE* fp_rect = nullptr;
319
321 rect = fp_rect = new FP_SHAPE( m_parentFootprint );
322 else
323 rect = new PCB_SHAPE();
324
325 rect->SetShape( SHAPE_T::RECT );
326 rect->SetFilled( false );
327 rect->SetStart( top->start );
328 rect->SetEnd( bottom->end );
329 rect->SetLayer( top->shape->GetLayer() );
330 rect->SetStroke( top->shape->GetStroke() );
331
332 if( fp_rect )
333 fp_rect->SetLocalCoord();
334
335 m_commit.Add( rect );
336 m_commit.Remove( left->shape );
337 m_commit.Remove( top->shape );
338 m_commit.Remove( right->shape );
339 m_commit.Remove( bottom->shape );
340 }
341 }
342 }
343 }
344
345 for( SIDE_CANDIDATE* side : sides )
346 delete side;
347}
348
349
351{
352 wxCHECK_MSG( m_parentFootprint, /*void*/, wxT( "mergePads() is FootprintEditor only" ) );
353
354 PAD_TOOL* padTool = m_toolMgr->GetTool<PAD_TOOL>();
355 std::map<wxString, int> padToNetTieGroupMap = m_parentFootprint->MapPadNumbersToNetTieGroups();
356
357 for( PAD* pad : m_parentFootprint->Pads() )
358 {
359 // Don't merge a pad that's in a net-tie pad group. (We don't care which group.)
360 if( padToNetTieGroupMap[ pad->GetNumber() ] >= 0 )
361 continue;
362
363 std::vector<FP_SHAPE*> shapes = padTool->RecombinePad( pad, m_dryRun, m_commit );
364
365 if( !shapes.empty() )
366 {
367 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_MERGE_PAD );
368
369 for( FP_SHAPE* shape : shapes )
370 item->AddItem( shape );
371
372 item->AddItem( pad );
373
374 m_itemsList->push_back( item );
375 }
376 }
377}
BOARD * GetBoard() const
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:58
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:180
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:214
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:643
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:142
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:145
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:179
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:405
void SetFilled(bool aFlag)
Definition: eda_shape.h:95
int GetRadius() const
Definition: eda_shape.cpp:511
SHAPE_T GetShape() const
Definition: eda_shape.h:113
int GetPointCount() const
Definition: eda_shape.cpp:1242
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:145
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:124
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:120
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:112
int GetWidth() const
Definition: eda_shape.h:109
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition: eda_shape.h:230
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:149
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:75
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:176
std::map< wxString, int > MapPadNumbersToNetTieGroups() const
Definition: footprint.cpp:2272
PADS & Pads()
Definition: footprint.h:170
virtual void SetLocalCoord()
Set relative coordinates from draw coordinates.
Definition: fp_shape.cpp:52
TOOL_MANAGER * m_toolMgr
DRAWINGS & m_drawings
void CleanupBoard(bool aDryRun, std::vector< std::shared_ptr< CLEANUP_ITEM > > *aItemsList, bool aMergeRects, bool aDeleteRedundant, bool aMergePads)
the cleanup function.
BOARD_COMMIT & m_commit
std::vector< std::shared_ptr< CLEANUP_ITEM > > * m_itemsList
FOOTPRINT * m_parentFootprint
GRAPHICS_CLEANER(DRAWINGS &aDrawings, FOOTPRINT *aParentFootprint, BOARD_COMMIT &aCommit, TOOL_MANAGER *aToolManager)
bool isNullShape(PCB_SHAPE *aShape)
bool areEquivalent(PCB_SHAPE *aShape1, PCB_SHAPE *aShape2)
Tool relating to pads and pad settings.
Definition: pad_tool.h:37
std::vector< FP_SHAPE * > RecombinePad(PAD *aPad, bool aIsDryRun, BOARD_COMMIT &aCommit)
Recombine an exploded pad (or one produced with overlapping polygons in an older version).
Definition: pad_tool.cpp:704
Definition: pad.h:59
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:67
void SetStroke(const STROKE_PARAMS &aStroke) override
Definition: pcb_shape.h:72
Master controller class:
Definition: tool_manager.h:55
@ CLEANUP_NULL_GRAPHIC
Definition: cleanup_item.h:43
@ CLEANUP_MERGE_PAD
Definition: cleanup_item.h:46
@ CLEANUP_DUPLICATE_GRAPHIC
Definition: cleanup_item.h:44
@ CLEANUP_LINES_TO_RECT
Definition: cleanup_item.h:45
#define IS_DELETED
bool equivalent(const VECTOR2I &a, const VECTOR2I &b, int epsilon)
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition: macros.h:120
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition: eda_angle.h:401