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