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