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 ),
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
63 m_epsilon = m_commit.GetBoard()->GetDesignSettings().GetDRCEpsilon();
64 m_maxError = m_commit.GetBoard()->GetDesignSettings().m_MaxError;
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 {
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::ELLIPSE:
107 case SHAPE_T::ELLIPSE_ARC: return aShape->GetEllipseMajorRadius() == 0 || aShape->GetEllipseMinorRadius() == 0;
108
109 case SHAPE_T::POLY:
110 return aShape->GetPointCount() == 0;
111
112 case SHAPE_T::BEZIER:
114
115 // If the Bezier points list contains 2 points, it is equivalent to a segment
116 if( aShape->GetBezierPoints().size() == 2 )
117 return equivalent( aShape->GetStart(), aShape->GetEnd(), m_epsilon );
118
119 // If the Bezier points list contains 1 points, it is equivalent to a point
120 return aShape->GetBezierPoints().size() < 2;
121
122 default:
124 return false;
125 }
126}
127
128
130{
131 if( aShape1->GetShape() != aShape2->GetShape()
132 || aShape1->GetLayer() != aShape2->GetLayer()
133 || aShape1->GetWidth() != aShape2->GetWidth() )
134 {
135 return false;
136 }
137
138 switch( aShape1->GetShape() )
139 {
140 case SHAPE_T::SEGMENT:
142 case SHAPE_T::CIRCLE:
143 return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
144 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
145
146 case SHAPE_T::ARC:
147 return equivalent( aShape1->GetCenter(), aShape2->GetCenter(), m_epsilon )
148 && equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
149 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon );
150
151 case SHAPE_T::POLY:
152 // TODO
153 return false;
154
155 case SHAPE_T::BEZIER:
156 return equivalent( aShape1->GetStart(), aShape2->GetStart(), m_epsilon )
157 && equivalent( aShape1->GetEnd(), aShape2->GetEnd(), m_epsilon )
158 && equivalent( aShape1->GetBezierC1(), aShape2->GetBezierC1(), m_epsilon )
159 && equivalent( aShape1->GetBezierC2(), aShape2->GetBezierC2(), m_epsilon );
160
161 case SHAPE_T::ELLIPSE:
163 return equivalent( aShape1->GetEllipseCenter(), aShape2->GetEllipseCenter(), m_epsilon )
164 && aShape1->GetEllipseMajorRadius() == aShape2->GetEllipseMajorRadius()
165 && aShape1->GetEllipseMinorRadius() == aShape2->GetEllipseMinorRadius()
166 && aShape1->GetEllipseRotation() == aShape2->GetEllipseRotation()
167 && ( aShape1->GetShape() != SHAPE_T::ELLIPSE_ARC
168 || ( aShape1->GetEllipseStartAngle() == aShape2->GetEllipseStartAngle()
169 && aShape1->GetEllipseEndAngle() == aShape2->GetEllipseEndAngle() ) );
170
171 default:
172 wxFAIL_MSG( wxT( "GRAPHICS_CLEANER::areEquivalent unimplemented for " )
173 + aShape1->SHAPE_T_asString() );
174 return false;
175 }
176}
177
178
180{
181 // Remove duplicate shapes (2 superimposed identical shapes):
182 for( auto it = m_drawings.begin(); it != m_drawings.end(); it++ )
183 {
184 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( *it );
185
186 if( !shape || shape->HasFlag( IS_DELETED ) )
187 continue;
188
189 if( isNullShape( shape ) )
190 {
191 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_NULL_GRAPHIC );
192 item->SetItems( shape );
193 m_itemsList->push_back( item );
194
195 if( !m_dryRun )
196 m_commit.Remove( shape );
197
198 continue;
199 }
200
201 for( auto it2 = it + 1; it2 != m_drawings.end(); it2++ )
202 {
203 PCB_SHAPE* shape2 = dynamic_cast<PCB_SHAPE*>( *it2 );
204
205 if( !shape2 || shape2->HasFlag( IS_DELETED ) )
206 continue;
207
208 if( areEquivalent( shape, shape2 ) )
209 {
210 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_DUPLICATE_GRAPHIC );
211 item->SetItems( shape2 );
212 m_itemsList->push_back( item );
213
214 shape2->SetFlags(IS_DELETED );
215
216 if( !m_dryRun )
217 m_commit.Remove( shape2 );
218 }
219 }
220 }
221}
222
223
225{
226 if( m_dryRun )
227 return;
228
229 std::vector<PCB_SHAPE*> shapeList;
230
231 for( BOARD_ITEM* item : m_drawings )
232 {
233 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
234
235 if( !shape || !shape->IsOnLayer( Edge_Cuts ) )
236 continue;
237
238 shapeList.push_back( shape );
239
240 if( !m_dryRun )
241 m_commit.Modify( shape );
242 }
243
245
246 std::vector<PCB_SHAPE*> items_to_select;
247}
248
249
251{
252 struct SIDE_CANDIDATE
253 {
254 SIDE_CANDIDATE( PCB_SHAPE* aShape ) :
255 start( aShape->GetStart() ),
256 end( aShape->GetEnd() ),
257 shape( aShape )
258 {
259 if( start.x > end.x || start.y > end.y )
260 std::swap( start, end );
261 }
262
263 VECTOR2I start;
265 PCB_SHAPE* shape;
266 };
267
268 std::vector<SIDE_CANDIDATE*> sides;
269 std::map<VECTOR2I, std::vector<SIDE_CANDIDATE*>> ptMap;
270
271 // First load all the candidates into the side vector and layer maps
272 for( BOARD_ITEM* item : m_drawings )
273 {
274 PCB_SHAPE* shape = dynamic_cast<PCB_SHAPE*>( item );
275
276 if( !shape || isNullShape( shape ) || shape->GetShape() != SHAPE_T::SEGMENT )
277 continue;
278
279 if( shape->GetStart().x == shape->GetEnd().x || shape->GetStart().y == shape->GetEnd().y )
280 {
281 sides.emplace_back( new SIDE_CANDIDATE( shape ) );
282 ptMap[ sides.back()->start ].push_back( sides.back() );
283 }
284 }
285
286 // Now go through the sides and try and match lines into rectangles
287 for( SIDE_CANDIDATE* side : sides )
288 {
289 if( side->shape->HasFlag( IS_DELETED ) )
290 continue;
291
292 SIDE_CANDIDATE* left = nullptr;
293 SIDE_CANDIDATE* top = nullptr;
294 SIDE_CANDIDATE* right = nullptr;
295 SIDE_CANDIDATE* bottom = nullptr;
296
297 auto viable = [&]( SIDE_CANDIDATE* aCandidate ) -> bool
298 {
299 return aCandidate->shape->GetLayer() == side->shape->GetLayer()
300 && aCandidate->shape->GetWidth() == side->shape->GetWidth()
301 && !aCandidate->shape->HasFlag( IS_DELETED );
302 };
303
304 if( side->start.x == side->end.x )
305 {
306 // We've found a possible left; see if we have a top
307 //
308 left = side;
309
310 for( SIDE_CANDIDATE* candidate : ptMap[ left->start ] )
311 {
312 if( candidate != left && viable( candidate ) )
313 {
314 top = candidate;
315 break;
316 }
317 }
318 }
319 else if( side->start.y == side->end.y )
320 {
321 // We've found a possible top; see if we have a left
322 //
323 top = side;
324
325 for( SIDE_CANDIDATE* candidate : ptMap[ top->start ] )
326 {
327 if( candidate != top && viable( candidate ) )
328 {
329 left = candidate;
330 break;
331 }
332 }
333 }
334
335 if( top && left )
336 {
337 // See if we can fill in the other two sides
338 //
339 for( SIDE_CANDIDATE* candidate : ptMap[ top->end ] )
340 {
341 if( candidate != top && candidate != left && viable( candidate ) )
342 {
343 right = candidate;
344 break;
345 }
346 }
347
348 for( SIDE_CANDIDATE* candidate : ptMap[ left->end ] )
349 {
350 if( candidate != top && candidate != left && viable( candidate ) )
351 {
352 bottom = candidate;
353 break;
354 }
355 }
356
357 if( right && bottom && right->end == bottom->end )
358 {
359 left->shape->SetFlags( IS_DELETED );
360 top->shape->SetFlags( IS_DELETED );
361 right->shape->SetFlags( IS_DELETED );
362 bottom->shape->SetFlags( IS_DELETED );
363
364 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_LINES_TO_RECT );
365 item->SetItems( left->shape, top->shape, right->shape, bottom->shape );
366 m_itemsList->push_back( item );
367
368 if( !m_dryRun )
369 {
371
373 rect->SetFilled( false );
374 rect->SetStart( top->start );
375 rect->SetEnd( bottom->end );
376 rect->SetLayer( top->shape->GetLayer() );
377 rect->SetStroke( top->shape->GetStroke() );
378
379 m_commit.Add( rect );
380 m_commit.Remove( left->shape );
381 m_commit.Remove( top->shape );
382 m_commit.Remove( right->shape );
383 m_commit.Remove( bottom->shape );
384 }
385 }
386 }
387 }
388
389 for( SIDE_CANDIDATE* side : sides )
390 delete side;
391}
392
393
395{
396 wxCHECK_MSG( m_parentFootprint, /*void*/, wxT( "mergePads() is FootprintEditor only" ) );
397
398 PAD_TOOL* padTool = m_toolMgr->GetTool<PAD_TOOL>();
399 std::map<wxString, int> padToNetTieGroupMap = m_parentFootprint->MapPadNumbersToNetTieGroups();
400
401 for( PAD* pad : m_parentFootprint->Pads() )
402 {
403 // Don't merge a pad that's in a net-tie pad group. (We don't care which group.)
404 if( padToNetTieGroupMap[ pad->GetNumber() ] >= 0 )
405 continue;
406
407 if( m_commit.GetStatus( m_parentFootprint ) == 0 )
408 m_commit.Modify( m_parentFootprint );
409
410 std::vector<PCB_SHAPE*> shapes = padTool->RecombinePad( pad, m_dryRun );
411
412 if( !shapes.empty() )
413 {
414 std::shared_ptr<CLEANUP_ITEM> item = std::make_shared<CLEANUP_ITEM>( CLEANUP_MERGE_PAD );
415
416 for( PCB_SHAPE* shape : shapes )
417 item->AddItem( shape );
418
419 item->AddItem( pad );
420
421 m_itemsList->push_back( item );
422 }
423 }
424}
constexpr int ARC_HIGH_DEF
Definition base_units.h:141
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:149
bool HasFlag(EDA_ITEM_FLAGS aFlag) const
Definition eda_item.h:153
int GetEllipseMinorRadius() const
Definition eda_shape.h:306
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:279
const VECTOR2I & GetEllipseCenter() const
Definition eda_shape.h:288
EDA_ANGLE GetEllipseEndAngle() const
Definition eda_shape.h:334
int GetEllipseMajorRadius() const
Definition eda_shape.h:297
EDA_ANGLE GetEllipseRotation() const
Definition eda_shape.h:315
int GetRadius() const
SHAPE_T GetShape() const
Definition eda_shape.h:189
virtual void SetFilled(bool aFlag)
Definition eda_shape.h:156
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:236
void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:198
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:194
void SetShape(SHAPE_T aShape)
Definition eda_shape.h:188
EDA_ANGLE GetEllipseStartAngle() const
Definition eda_shape.h:325
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition eda_shape.h:400
void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:240
wxString SHAPE_T_asString() const
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:276
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:876
Definition pad.h:55
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
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
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
Definition pcb_shape.h:98
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition pcb_shape.h:71
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:56
@ SEGMENT
Definition eda_shape.h:50
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:51
@ ELLIPSE_ARC
Definition eda_shape.h:57
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:112
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:96
std::deque< BOARD_ITEM * > DRAWINGS
const double epsilon
KIBIS top(path, &reporter)
VECTOR2I end
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687