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