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