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
104 // If the Bezier points list contains 2 points, it is equivalent to a segment
105 if( aShape->GetBezierPoints().size() == 2 )
106 return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
107
108 // If the Bezier points list contains 1 points, it is equivalent to a point
109 return aShape->GetBezierPoints().size() < 2;
110
111 default:
113 return false;
114 }
115}
116
117
119{
120 if( aShape1->GetShape() != aShape2->GetShape()
121 || aShape1->GetLayer() != aShape2->GetLayer()
122 || aShape1->GetWidth() != aShape2->GetWidth() )
123 {
124 return false;
125 }
126
127 switch( aShape1->GetShape() )
128 {
129 case SHAPE_T::SEGMENT:
130 case SHAPE_T::RECT:
131 case SHAPE_T::CIRCLE:
132 return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
133 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
134
135 case SHAPE_T::ARC:
136 return equivalent( aShape1->GetCenter(), aShape2->GetCenter(), m_epsilon )
137 && equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
138 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
139
140 case SHAPE_T::POLY:
141 // TODO
142 return false;
143
144 case SHAPE_T::BEZIER:
145 return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
146 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon )
147 && equivalent( aShape1->GetBezierC1(), aShape2->GetBezierC1(), m_epsilon )
148 && equivalent( aShape1->GetBezierC2(), aShape2->GetBezierC2(), m_epsilon );
149
150 default:
151 wxFAIL_MSG( wxT( "GRAPHICS_CLEANER::areEquivalent unimplemented for " )
152 + aShape1->SHAPE_T_asString() );
153 return false;
154 }
155}
156
157
159{
160 // Remove duplicate shapes (2 superimposed identical shapes):
161 for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
162 {
163 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( *it );
164
165 if( !shape || shape->HasFlag( IS_DELETED ) )
166 continue;
167
168 if( isNullShape( shape ) )
169 {
170 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
171 item->SetItems( shape );
172 m_itemsList->push_back( item );
173
174 if( !m_dryRun )
175 m_commit.Remove( shape );
176
177 continue;
178 }
179
180 for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
181 {
182 PCB_SHAPE* shape2 = dynamic_cast<PCB_SHAPE*>( *it2 );
183
184 if( !shape2 || shape2->HasFlag( IS_DELETED ) )
185 continue;
186
187 if( areEquivalent( shape, shape2 ) )
188 {
189 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
190 item->SetItems( shape2 );
191 m_itemsList->push_back( item );
192
193 shape2->SetFlags(IS_DELETED );
194
195 if( !m_dryRun )
196 m_commit.Remove( shape2 );
197 }
198 }
199 }
200}
201
202
204{
205 struct SIDE_CANDIDATE
206 {
207 SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
208 start( aShape->GetStart() ),
209 end( aShape->GetEnd() ),
210 shape( aShape )
211 {
212 if( start.x > end.x || start.y > end.y )
213 std::swap( start, end );
214 }
215
216 VECTOR2I start;
217 VECTOR2I end;
218 PCB_SHAPE* shape;
219 };
220
221 std::vector<SIDE_CANDIDATE*> sides;
222 std::map<VECTOR2I, std::vector<SIDE_CANDIDATE*>> ptMap;
223
224 // First load all the candidates into the side vector and layer maps
225 for( BOARD_ITEM* item : m_drawings )
226 {
227 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
228
229 if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
230 continue;
231
232 if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
233 {
234 sides.emplace_back( new SIDE_CANDIDATE( shape ) );
235 ptMap[ sides.back()->start ].push_back( sides.back() );
236 }
237 }
238
239 // Now go through the sides and try and match lines into rectangles
240 for( SIDE_CANDIDATE* side : sides )
241 {
242 if( side->shape->HasFlag( IS_DELETED ) )
243 continue;
244
245 SIDE_CANDIDATE* left = nullptr;
246 SIDE_CANDIDATE* top = nullptr;
247 SIDE_CANDIDATE* right = nullptr;
248 SIDE_CANDIDATE* bottom = nullptr;
249
250 auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
251 {
252 return aCandidate->shape->GetLayer() == side->shape->GetLayer()
253 && aCandidate->shape->GetWidth() == side->shape->GetWidth()
254 && !aCandidate->shape->HasFlag( IS_DELETED );
255 };
256
257 if( side->start.x == side->end.x )
258 {
259 // We've found a possible left; see if we have a top
260 //
261 left = side;
262
263 for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
264 {
265 if( candidate != left && viable( candidate ) )
266 {
267 top = candidate;
268 break;
269 }
270 }
271 }
272 else if( side->start.y == side->end.y )
273 {
274 // We've found a possible top; see if we have a left
275 //
276 top = side;
277
278 for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
279 {
280 if( candidate != top && viable( candidate ) )
281 {
282 left = candidate;
283 break;
284 }
285 }
286 }
287
288 if( top && left )
289 {
290 // See if we can fill in the other two sides
291 //
292 for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
293 {
294 if( candidate != top && candidate != left && viable( candidate ) )
295 {
296 right = candidate;
297 break;
298 }
299 }
300
301 for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
302 {
303 if( candidate != top && candidate != left && viable( candidate ) )
304 {
305 bottom = candidate;
306 break;
307 }
308 }
309
310 if( right && bottom && right->end == bottom->end )
311 {
312 left->shape->SetFlags( IS_DELETED );
313 top->shape->SetFlags( IS_DELETED );
314 right->shape->SetFlags( IS_DELETED );
315 bottom->shape->SetFlags( IS_DELETED );
316
317 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
318 item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
319 m_itemsList->push_back( item );
320
321 if( !m_dryRun )
322 {
323 PCB_SHAPE* rect;
324 FP_SHAPE* fp_rect = nullptr;
325
327 rect = fp_rect = new FP_SHAPE( m_parentFootprint );
328 else
329 rect = new PCB_SHAPE();
330
331 rect->SetShape( SHAPE_T::RECT );
332 rect->SetFilled( false );
333 rect->SetStart( top->start );
334 rect->SetEnd( bottom->end );
335 rect->SetLayer( top->shape->GetLayer() );
336 rect->SetStroke( top->shape->GetStroke() );
337
338 if( fp_rect )
339 fp_rect->SetLocalCoord();
340
341 m_commit.Add( rect );
342 m_commit.Remove( left->shape );
343 m_commit.Remove( top->shape );
344 m_commit.Remove( right->shape );
345 m_commit.Remove( bottom->shape );
346 }
347 }
348 }
349 }
350
351 for( SIDE_CANDIDATE* side : sides )
352 delete side;
353}
354
355
357{
358 wxCHECK_MSG( m_parentFootprint, /*void*/, wxT( "mergePads() is FootprintEditor only" ) );
359
360 PAD_TOOL* padTool = m_toolMgr->GetTool<PAD_TOOL>();
361 std::map<wxString, int> padToNetTieGroupMap = m_parentFootprint->MapPadNumbersToNetTieGroups();
362
363 for( PAD* pad : m_parentFootprint->Pads() )
364 {
365 // Don't merge a pad that's in a net-tie pad group. (We don't care which group.)
366 if( padToNetTieGroupMap[ pad->GetNumber() ] >= 0 )
367 continue;
368
369 std::vector<FP_SHAPE*> shapes = padTool->RecombinePad( pad, m_dryRun, m_commit );
370
371 if( !shapes.empty() )
372 {
373 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_MERGE_PAD );
374
375 for( FP_SHAPE* shape : shapes )
376 item->AddItem( shape );
377
378 item->AddItem( pad );
379
380 m_itemsList->push_back( item );
381 }
382 }
383}
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:70
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:192
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition: board_item.h:226
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition: board.cpp:704
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:139
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition: eda_item.h:143
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:523
SHAPE_T GetShape() const
Definition: eda_shape.h:113
int GetPointCount() const
Definition: eda_shape.cpp:1254
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:2302
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:732
Definition: pad.h:60
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:418