KiCad PCB EDA Suite
Loading...
Searching...
No Matches
item_modification_routine.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
27#include <geometry/circle.h>
28#include <geometry/oval.h>
29#include <geometry/roundrect.h>
30#include <geometry/shape_rect.h>
32
33#include <pad.h>
34#include <pcb_track.h>
36#include <confirm.h>
37#include <ki_exception.h>
38
39namespace
40{
41
45bool SegmentsShareEndpoint( const SEG& aSegA, const SEG& aSegB )
46{
47 return ( aSegA.A == aSegB.A || aSegA.A == aSegB.B || aSegA.B == aSegB.A || aSegA.B == aSegB.B );
48}
49
50
51std::pair<VECTOR2I*, VECTOR2I*> GetSharedEndpoints( SEG& aSegA, SEG& aSegB )
52{
53 std::pair<VECTOR2I*, VECTOR2I*> result = { nullptr, nullptr };
54
55 if( aSegA.A == aSegB.A )
56 {
57 result = { &aSegA.A, &aSegB.A };
58 }
59 else if( aSegA.A == aSegB.B )
60 {
61 result = { &aSegA.A, &aSegB.B };
62 }
63 else if( aSegA.B == aSegB.A )
64 {
65 result = { &aSegA.B, &aSegB.A };
66 }
67 else if( aSegA.B == aSegB.B )
68 {
69 result = { &aSegA.B, &aSegB.B };
70 }
71
72 return result;
73}
74
75} // namespace
76
77
79 const std::optional<SEG>& aSeg )
80{
81 wxASSERT_MSG( aLine.GetShape() == SHAPE_T::SEGMENT, "Can only modify segments" );
82
83 const bool removed = !aSeg.has_value() || aSeg->Length() == 0;
84
85 if( !removed )
86 {
87 // Mark modified, then change it
89 aLine.SetStart( aSeg->A );
90 aLine.SetEnd( aSeg->B );
91 }
92 else
93 {
94 // The line has become zero length - delete it
95 GetHandler().DeleteItem( aLine );
96 }
97
98 return removed;
99}
100
101
103{
104 return _( "Fillet Lines" );
105}
106
107
108std::optional<wxString> LINE_FILLET_ROUTINE::GetStatusMessage() const
109{
110 if( GetSuccesses() == 0 )
111 {
112 return _( "Unable to fillet the selected lines." );
113 }
114 else if( GetFailures() > 0 )
115 {
116 return _( "Some of the lines could not be filleted." );
117 }
118 return std::nullopt;
119}
120
121
123{
124 if( aLineA.GetLength() == 0.0 || aLineB.GetLength() == 0.0 )
125 return;
126
127 SEG seg_a( aLineA.GetStart(), aLineA.GetEnd() );
128 SEG seg_b( aLineB.GetStart(), aLineB.GetEnd() );
129
130 auto [a_pt, b_pt] = GetSharedEndpoints( seg_a, seg_b );
131
132 if( !a_pt || !b_pt )
133 {
134 // The lines do not share an endpoint, so we can't fillet them
135 AddFailure();
136 return;
137 }
138
139 if( seg_a.Angle( seg_b ).IsHorizontal() )
140 return;
141
142 SHAPE_ARC sArc( seg_a, seg_b, m_filletRadiusIU );
143 VECTOR2I t1newPoint, t2newPoint;
144
145 auto setIfPointOnSeg = []( VECTOR2I& aPointToSet, SEG aSegment, VECTOR2I aVecToTest )
146 {
147 VECTOR2I segToVec = aSegment.NearestPoint( aVecToTest ) - aVecToTest;
148
149 // Find out if we are on the segment (minimum precision)
151 {
152 aPointToSet.x = aVecToTest.x;
153 aPointToSet.y = aVecToTest.y;
154 return true;
155 }
156
157 return false;
158 };
159
160 //Do not draw a fillet if the end points of the arc are not within the track segments
161 if( !setIfPointOnSeg( t1newPoint, seg_a, sArc.GetP0() )
162 && !setIfPointOnSeg( t2newPoint, seg_b, sArc.GetP0() ) )
163 {
164 AddFailure();
165 return;
166 }
167
168 if( !setIfPointOnSeg( t1newPoint, seg_a, sArc.GetP1() )
169 && !setIfPointOnSeg( t2newPoint, seg_b, sArc.GetP1() ) )
170 {
171 AddFailure();
172 return;
173 }
174
175 auto tArc = std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::ARC );
176
177 tArc->SetArcGeometry( sArc.GetP0(), sArc.GetArcMid(), sArc.GetP1() );
178
179 // Copy properties from one of the source lines
180 tArc->SetWidth( aLineA.GetWidth() );
181 tArc->SetLayer( aLineA.GetLayer() );
182 tArc->SetLocked( aLineA.IsLocked() );
183
184 CHANGE_HANDLER& handler = GetHandler();
185
186 handler.AddNewItem( std::move( tArc ) );
187
188 *a_pt = t1newPoint;
189 *b_pt = t2newPoint;
190
191 ModifyLineOrDeleteIfZeroLength( aLineA, seg_a );
192 ModifyLineOrDeleteIfZeroLength( aLineB, seg_b );
193
194 AddSuccess();
195}
196
198{
199 return _( "Chamfer Lines" );
200}
201
202
203std::optional<wxString> LINE_CHAMFER_ROUTINE::GetStatusMessage() const
204{
205 if( GetSuccesses() == 0 )
206 {
207 return _( "Unable to chamfer the selected lines." );
208 }
209 else if( GetFailures() > 0 )
210 {
211 return _( "Some of the lines could not be chamfered." );
212 }
213 return std::nullopt;
214}
215
216
218{
219 if( aLineA.GetLength() == 0.0 || aLineB.GetLength() == 0.0 )
220 return;
221
222 SEG seg_a( aLineA.GetStart(), aLineA.GetEnd() );
223 SEG seg_b( aLineB.GetStart(), aLineB.GetEnd() );
224
225 // If the segments share an endpoint, we won't try to chamfer them
226 // (we could extend to the intersection point, but this gets complicated
227 // and inconsistent when you select more than two lines)
228 if( !SegmentsShareEndpoint( seg_a, seg_b ) )
229 {
230 // not an error, lots of lines in a 2+ line selection will not intersect
231 return;
232 }
233
234 std::optional<CHAMFER_RESULT> chamfer_result =
235 ComputeChamferPoints( seg_a, seg_b, m_chamferParams );
236
237 if( !chamfer_result )
238 {
239 AddFailure();
240 return;
241 }
242
243 auto tSegment = std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::SEGMENT );
244
245 tSegment->SetStart( chamfer_result->m_chamfer.A );
246 tSegment->SetEnd( chamfer_result->m_chamfer.B );
247
248 // Copy properties from one of the source lines
249 tSegment->SetWidth( aLineA.GetWidth() );
250 tSegment->SetLayer( aLineA.GetLayer() );
251 tSegment->SetLocked( aLineA.IsLocked() );
252
253 CHANGE_HANDLER& handler = GetHandler();
254
255 handler.AddNewItem( std::move( tSegment ) );
256
257 ModifyLineOrDeleteIfZeroLength( aLineA, *chamfer_result->m_updated_seg_a );
258 ModifyLineOrDeleteIfZeroLength( aLineB, *chamfer_result->m_updated_seg_b );
259
260 AddSuccess();
261}
262
263
265{
266 return _( "Dogbone Corners" );
267}
268
269
270std::optional<wxString> DOGBONE_CORNER_ROUTINE::GetStatusMessage() const
271{
272 wxString msg;
273
274 if( GetSuccesses() == 0 )
275 {
276 msg += _( "Unable to add dogbone corners to the selected lines." );
277 }
278 else if( GetFailures() > 0 )
279 {
280 msg += _( "Some of the lines could not have dogbone corners added." );
281 }
282
284 {
285 if( !msg.empty() )
286 msg += " ";
287
288 msg += _( "Some of the dogbone corners are too narrow to fit a "
289 "cutter of the specified radius." );
290
291 if( !m_params.AddSlots )
292 msg += _( " Consider enabling the 'Add Slots' option." );
293 else
294 msg += _( " Slots were added." );
295 }
296
297 if( msg.empty() )
298 return std::nullopt;
299
300 return msg;
301}
302
303
305{
306 if( aLineA.GetLength() == 0.0 || aLineB.GetLength() == 0.0 )
307 return;
308
309 SEG seg_a( aLineA.GetStart(), aLineA.GetEnd() );
310 SEG seg_b( aLineB.GetStart(), aLineB.GetEnd() );
311
312 auto [a_pt, b_pt] = GetSharedEndpoints( seg_a, seg_b );
313
314 if( !a_pt || !b_pt )
315 {
316 return;
317 }
318
319 // Cannot handle parallel lines
320 if( seg_a.Angle( seg_b ).IsHorizontal() )
321 {
322 AddFailure();
323 return;
324 }
325
326 std::optional<DOGBONE_RESULT> dogbone_result =
328
329 if( !dogbone_result )
330 {
331 AddFailure();
332 return;
333 }
334
335 if( dogbone_result->m_small_arc_mouth )
336 {
337 // The arc is too small to fit the radius
338 m_haveNarrowMouths = true;
339 }
340
341 CHANGE_HANDLER& handler = GetHandler();
342
343 auto tArc = std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::ARC );
344
345 const auto copyProps = [&]( PCB_SHAPE& aShape )
346 {
347 aShape.SetWidth( aLineA.GetWidth() );
348 aShape.SetLayer( aLineA.GetLayer() );
349 aShape.SetLocked( aLineA.IsLocked() );
350 };
351
352 const auto addSegment = [&]( const SEG& aSeg )
353 {
354 if( aSeg.Length() == 0 )
355 return;
356
357 auto tSegment = std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::SEGMENT );
358 tSegment->SetStart( aSeg.A );
359 tSegment->SetEnd( aSeg.B );
360
361 copyProps( *tSegment );
362 handler.AddNewItem( std::move( tSegment ) );
363 };
364
365 tArc->SetArcGeometry( dogbone_result->m_arc.GetP0(), dogbone_result->m_arc.GetArcMid(),
366 dogbone_result->m_arc.GetP1() );
367
368 // Copy properties from one of the source lines
369 copyProps( *tArc );
370
371 addSegment( SEG{ dogbone_result->m_arc.GetP0(), dogbone_result->m_updated_seg_a->B } );
372 addSegment( SEG{ dogbone_result->m_arc.GetP1(), dogbone_result->m_updated_seg_b->B } );
373
374 handler.AddNewItem( std::move( tArc ) );
375
376 ModifyLineOrDeleteIfZeroLength( aLineA, dogbone_result->m_updated_seg_a );
377 ModifyLineOrDeleteIfZeroLength( aLineB, dogbone_result->m_updated_seg_b );
378
379 AddSuccess();
380}
381
382
384{
385 return _( "Extend Lines to Meet" );
386}
387
388
389std::optional<wxString> LINE_EXTENSION_ROUTINE::GetStatusMessage() const
390{
391 if( GetSuccesses() == 0 )
392 {
393 return _( "Unable to extend the selected lines to meet." );
394 }
395 else if( GetFailures() > 0 )
396 {
397 return _( "Some of the lines could not be extended to meet." );
398 }
399 return std::nullopt;
400}
401
402
404{
405 if( aLineA.GetLength() == 0.0 || aLineB.GetLength() == 0.0 )
406 return;
407
408 SEG seg_a( aLineA.GetStart(), aLineA.GetEnd() );
409 SEG seg_b( aLineB.GetStart(), aLineB.GetEnd() );
410
411 if( seg_a.Intersects( seg_b ) )
412 {
413 // already intersecting, nothing to do
414 return;
415 }
416
417 OPT_VECTOR2I intersection = seg_a.IntersectLines( seg_b );
418
419 if( !intersection )
420 {
421 // This might be an error, but it's also possible that the lines are
422 // parallel and don't intersect. We'll just ignore this case.
423 return;
424 }
425
426 CHANGE_HANDLER& handler = GetHandler();
427
428 const auto line_extender = [&]( const SEG& aSeg, PCB_SHAPE& aLine )
429 {
430 // If the intersection point is not already n the line, we'll extend to it
431 if( !aSeg.Contains( *intersection ) )
432 {
433 const int dist_start = ( *intersection - aSeg.A ).EuclideanNorm();
434 const int dist_end = ( *intersection - aSeg.B ).EuclideanNorm();
435
436 const VECTOR2I& furthest_pt = ( dist_start < dist_end ) ? aSeg.B : aSeg.A;
437 // Note, the drawing tool has COORDS_PADDING of 20mm, but we need a larger buffer
438 // or we are not able to select the generated segments
439 unsigned int edge_padding = static_cast<unsigned>( pcbIUScale.mmToIU( 200 ) );
440 VECTOR2I new_end = GetClampedCoords( *intersection, edge_padding );
441
442 handler.MarkItemModified( aLine );
443 aLine.SetStart( furthest_pt );
444 aLine.SetEnd( new_end );
445 }
446 };
447
448 line_extender( seg_a, aLineA );
449 line_extender( seg_b, aLineB );
450
451 AddSuccess();
452}
453
454
456{
457 std::unique_ptr<SHAPE_POLY_SET> poly;
458
459 switch( aPcbShape.GetShape() )
460 {
461 case SHAPE_T::POLY:
462 {
463 poly = std::make_unique<SHAPE_POLY_SET>( aPcbShape.GetPolyShape() );
464 break;
465 }
466 case SHAPE_T::RECTANGLE:
467 {
468 SHAPE_POLY_SET rect_poly;
469
470 const std::vector<VECTOR2I> rect_pts = aPcbShape.GetRectCorners();
471
472 rect_poly.NewOutline();
473
474 for( const VECTOR2I& pt : rect_pts )
475 {
476 rect_poly.Append( pt );
477 }
478
479 poly = std::make_unique<SHAPE_POLY_SET>( std::move( rect_poly ) );
480 break;
481 }
482 default:
483 {
484 break;
485 }
486 }
487
488 if( !poly )
489 {
490 // Not a polygon or rectangle, nothing to do
491 return;
492 }
493
494 if( m_firstPolygon )
495 {
496 m_width = aPcbShape.GetWidth();
497 m_layer = aPcbShape.GetLayer();
498 m_filled = aPcbShape.IsFilled();
499 m_workingPolygons = std::move( *poly );
500 m_firstPolygon = false;
501
502 GetHandler().DeleteItem( aPcbShape );
503 }
504 else
505 {
506 if( ProcessSubsequentPolygon( *poly ) )
507 {
508 // If we could process the polygon, delete the source
509 GetHandler().DeleteItem( aPcbShape );
510 AddSuccess();
511 }
512 else
513 {
514 AddFailure();
515 }
516 }
517}
518
519
521{
523 {
524 // Nothing to do (no polygons handled or nothing left?)
525 return;
526 }
527
528 CHANGE_HANDLER& handler = GetHandler();
529
530 // If we have disjoint polygons, we'll fix that now and create
531 // new PCB_SHAPEs for each outline
532 for( int i = 0; i < m_workingPolygons.OutlineCount(); ++i )
533 {
534 // If we handled any polygons to get any outline,
535 // there must be a layer set by now.
536 wxASSERT( m_layer >= 0 );
537
538 std::unique_ptr<PCB_SHAPE> new_poly_shape =
539 std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::POLY );
540
542 new_poly_shape->SetPolyShape( poly_set );
543
544 // Copy properties from the source polygon
545 new_poly_shape->SetWidth( m_width );
546 new_poly_shape->SetLayer( m_layer );
547 new_poly_shape->SetFilled( m_filled );
548
549 handler.AddNewItem( std::move( new_poly_shape ) );
550 }
551}
552
553
555{
556 return _( "Merge polygons." );
557}
558
559
560std::optional<wxString> POLYGON_MERGE_ROUTINE::GetStatusMessage() const
561{
562 if( GetSuccesses() == 0 )
563 {
564 return _( "Unable to merge the selected polygons." );
565 }
566 else if( GetFailures() > 0 )
567 {
568 return _( "Some of the polygons could not be merged." );
569 }
570 return std::nullopt;
571}
572
573
575{
576 GetWorkingPolygons().BooleanAdd( aPolygon );
577 return true;
578}
579
580
582{
583 return _( "Subtract polygons." );
584}
585
586
587std::optional<wxString> POLYGON_SUBTRACT_ROUTINE::GetStatusMessage() const
588{
589 if( GetSuccesses() == 0 )
590 {
591 return _( "Unable to subtract the selected polygons." );
592 }
593 else if( GetFailures() > 0 )
594 {
595 return _( "Some of the polygons could not be subtracted." );
596 }
597 return std::nullopt;
598}
599
600
602{
603 SHAPE_POLY_SET& working_polygons = GetWorkingPolygons();
604 SHAPE_POLY_SET working_copy = working_polygons;
605 working_copy.BooleanSubtract( aPolygon );
606
607 // Subtraction can create holes or delete the polygon
608 // In theory we can allow holes as the EDA_SHAPE will fracture for us, but that's
609 // probably not what the user has in mind (?)
610 if( working_copy.OutlineCount() != 1 || working_copy.HoleCount( 0 ) > 0
611 || working_copy.VertexCount( 0 ) == 0 )
612 {
613 // If that happens, just skip the operation
614 return false;
615 }
616
617 working_polygons = std::move( working_copy );
618 return true;
619}
620
621
623{
624 return _( "Intersect polygons." );
625}
626
627
628std::optional<wxString> POLYGON_INTERSECT_ROUTINE::GetStatusMessage() const
629{
630 if( GetSuccesses() == 0 )
631 {
632 return _( "Unable to intersect the selected polygons." );
633 }
634 else if( GetFailures() > 0 )
635 {
636 return _( "Some of the polygons could not be intersected." );
637 }
638 return std::nullopt;
639}
640
641
643{
644 SHAPE_POLY_SET& working_polygons = GetWorkingPolygons();
645 SHAPE_POLY_SET working_copy = working_polygons;
646 working_copy.BooleanIntersection( aPolygon );
647
648 // Is there anything left?
649 if( working_copy.OutlineCount() == 0 )
650 {
651 // There was no intersection. Rather than deleting the working polygon, we'll skip
652 // and report a failure.
653 return false;
654 }
655
656 working_polygons = std::move( working_copy );
657 return true;
658}
659
660
662{
663 return _( "Outset items." );
664}
665
666std::optional<wxString> OUTSET_ROUTINE::GetStatusMessage() const
667{
668 if( GetSuccesses() == 0 )
669 {
670 return _( "Unable to outset the selected items." );
671 }
672 else if( GetFailures() > 0 )
673 {
674 return _( "Some of the items could not be outset." );
675 }
676 return std::nullopt;
677}
678
679
680static SHAPE_RECT GetRectRoundedToGridOutwards( const SHAPE_RECT& aRect, int aGridSize )
681{
682 const VECTOR2I newPos = KIGEOM::RoundNW( aRect.GetPosition(), aGridSize );
683 const VECTOR2I newOpposite =
684 KIGEOM::RoundSE( aRect.GetPosition() + aRect.GetSize(), aGridSize );
685 return SHAPE_RECT( newPos, newOpposite );
686}
687
688
690{
691 /*
692 * This attempts to do exact outsetting, rather than punting to Clipper.
693 * So it can't do all shapes, but it can do the most obvious ones, which are probably
694 * the ones you want to outset anyway, most usually when making a courtyard for a footprint.
695 */
696
698
699 // Not all items have a width, even if the parameters want to copy it
700 // So fall back to the given width if we can't get one.
701 int width = m_params.lineWidth;
703 {
704 std::optional<int> item_width = GetBoardItemWidth( aItem );
705
706 if( item_width.has_value() )
707 {
708 width = *item_width;
709 }
710 }
711
712 CHANGE_HANDLER& handler = GetHandler();
713
714 const auto addPolygonalChain = [&]( const SHAPE_LINE_CHAIN& aChain )
715 {
716 SHAPE_POLY_SET new_poly( aChain );
717
718 std::unique_ptr<PCB_SHAPE> new_shape =
719 std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::POLY );
720
721 new_shape->SetPolyShape( new_poly );
722 new_shape->SetLayer( layer );
723 new_shape->SetWidth( width );
724
725 handler.AddNewItem( std::move( new_shape ) );
726 };
727
728 // Iterate the SHAPE_LINE_CHAIN in the polygon, pulling out
729 // segments and arcs to create new PCB_SHAPE primitives.
730 const auto addChain = [&]( const SHAPE_LINE_CHAIN& aChain )
731 {
732 // Prefer to add a polygonal chain if there are no arcs
733 // as this permits boolean ops
734 if( aChain.ArcCount() == 0 )
735 {
736 addPolygonalChain( aChain );
737 return;
738 }
739
740 for( size_t si = 0; si < aChain.GetSegmentCount(); ++si )
741 {
742 const SEG seg = aChain.GetSegment( si );
743
744 if( seg.Length() == 0 )
745 continue;
746
747 if( aChain.IsArcSegment( si ) )
748 continue;
749
750 std::unique_ptr<PCB_SHAPE> new_shape =
751 std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::SEGMENT );
752 new_shape->SetStart( seg.A );
753 new_shape->SetEnd( seg.B );
754 new_shape->SetLayer( layer );
755 new_shape->SetWidth( width );
756
757 handler.AddNewItem( std::move( new_shape ) );
758 }
759
760 for( size_t ai = 0; ai < aChain.ArcCount(); ++ai )
761 {
762 const SHAPE_ARC& arc = aChain.Arc( ai );
763
764 if( arc.GetRadius() == 0 || arc.GetP0() == arc.GetP1() )
765 continue;
766
767 std::unique_ptr<PCB_SHAPE> new_shape =
768 std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::ARC );
769 new_shape->SetArcGeometry( arc.GetP0(), arc.GetArcMid(), arc.GetP1() );
770 new_shape->SetLayer( layer );
771 new_shape->SetWidth( width );
772
773 handler.AddNewItem( std::move( new_shape ) );
774 }
775 };
776
777 const auto addPoly = [&]( const SHAPE_POLY_SET& aPoly )
778 {
779 for( int oi = 0; oi < aPoly.OutlineCount(); ++oi )
780 {
781 addChain( aPoly.Outline( oi ) );
782 }
783 };
784
785 const auto addRect = [&]( const SHAPE_RECT& aRect )
786 {
787 std::unique_ptr<PCB_SHAPE> new_shape =
788 std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::RECTANGLE );
789
790 if( !m_params.gridRounding.has_value() )
791 {
792 new_shape->SetPosition( aRect.GetPosition() );
793 new_shape->SetRectangleWidth( aRect.GetWidth() );
794 new_shape->SetRectangleHeight( aRect.GetHeight() );
795 }
796 else
797 {
798 const SHAPE_RECT grid_rect =
800 new_shape->SetPosition( grid_rect.GetPosition() );
801 new_shape->SetRectangleWidth( grid_rect.GetWidth() );
802 new_shape->SetRectangleHeight( grid_rect.GetHeight() );
803 }
804
805 new_shape->SetLayer( layer );
806 new_shape->SetWidth( width );
807
808 handler.AddNewItem( std::move( new_shape ) );
809 };
810
811 const auto addCircle = [&]( const CIRCLE& aCircle )
812 {
813 std::unique_ptr<PCB_SHAPE> new_shape =
814 std::make_unique<PCB_SHAPE>( GetBoard(), SHAPE_T::CIRCLE );
815 new_shape->SetCenter( aCircle.Center );
816 new_shape->SetRadius( aCircle.Radius );
817 new_shape->SetLayer( layer );
818 new_shape->SetWidth( width );
819
820 handler.AddNewItem( std::move( new_shape ) );
821 };
822
823 const auto addCircleOrRect = [&]( const CIRCLE& aCircle )
824 {
826 {
827 addCircle( aCircle );
828 }
829 else
830 {
831 const VECTOR2I rVec{ aCircle.Radius, aCircle.Radius };
832 const SHAPE_RECT rect{ aCircle.Center - rVec, aCircle.Center + rVec };
833 addRect( rect );
834 }
835 };
836
837 switch( aItem.Type() )
838 {
839 case PCB_PAD_T:
840 {
841 const PAD& pad = static_cast<const PAD&>( aItem );
842
843 // TODO(JE) padstacks
844 const PAD_SHAPE pad_shape = pad.GetShape( PADSTACK::ALL_LAYERS );
845
846 switch( pad_shape )
847 {
848 case PAD_SHAPE::RECTANGLE:
849 case PAD_SHAPE::ROUNDRECT:
850 case PAD_SHAPE::OVAL:
851 {
852 const VECTOR2I pad_size = pad.GetSize( PADSTACK::ALL_LAYERS );
853
854 BOX2I box{ pad.GetPosition() - pad_size / 2, pad_size };
855 box.Inflate( m_params.outsetDistance );
856
858 if( pad_shape == PAD_SHAPE::ROUNDRECT )
859 {
860 radius += pad.GetRoundRectCornerRadius( PADSTACK::ALL_LAYERS );
861 }
862 else if( pad_shape == PAD_SHAPE::OVAL )
863 {
864 radius += std::min( pad_size.x, pad_size.y ) / 2;
865 }
866
868
869 // No point doing a SHAPE_RECT as we may need to rotate it
870 ROUNDRECT rrect( box, radius );
871 SHAPE_POLY_SET poly;
872 rrect.TransformToPolygon( poly, 0, ERROR_LOC::ERROR_OUTSIDE );
873
874 poly.Rotate( pad.GetOrientation(), pad.GetPosition() );
875 addPoly( poly );
876 AddSuccess();
877 break;
878 }
879 case PAD_SHAPE::CIRCLE:
880 {
881 const int radius = pad.GetSize( PADSTACK::ALL_LAYERS ).x / 2 + m_params.outsetDistance;
882 const CIRCLE circle( pad.GetPosition(), radius );
883 addCircleOrRect( circle );
884 AddSuccess();
885 break;
886 }
887 case PAD_SHAPE::TRAPEZOID:
888 {
889 // Not handled yet, but could use a generic convex polygon outset method.
890 break;
891 }
892 default:
893 // Other pad shapes are not supported with exact outsets
894 break;
895 }
896 break;
897 }
898 case PCB_SHAPE_T:
899 {
900 const PCB_SHAPE& pcb_shape = static_cast<const PCB_SHAPE&>( aItem );
901
902 switch( pcb_shape.GetShape() )
903 {
904 case SHAPE_T::RECTANGLE:
905 {
906 BOX2I box{ pcb_shape.GetPosition(),
907 VECTOR2I{ pcb_shape.GetRectangleWidth(), pcb_shape.GetRectangleHeight() } };
908 box.Inflate( m_params.outsetDistance );
909
910 SHAPE_RECT rect( box );
912 {
913 try
914 {
915 ROUNDRECT rrect( rect, m_params.outsetDistance );
916 SHAPE_POLY_SET poly;
917 rrect.TransformToPolygon( poly, 0, ERROR_LOC::ERROR_OUTSIDE );
918 addPoly( poly );
919 }
920 catch( const KI_PARAM_ERROR& error )
921 {
922 DisplayErrorMessage( nullptr, error.What() );
923 }
924
925 }
926 else
927 {
928 addRect( rect );
929 }
930 AddSuccess();
931 break;
932 }
933 case SHAPE_T::CIRCLE:
934 {
935 const CIRCLE circle( pcb_shape.GetCenter(),
936 pcb_shape.GetRadius() + m_params.outsetDistance );
937 addCircleOrRect( circle );
938 AddSuccess();
939 break;
940 }
941 case SHAPE_T::SEGMENT:
942 {
943 // For now just make the whole stadium shape and let the user delete the unwanted bits
944 const SEG seg( pcb_shape.GetStart(), pcb_shape.GetEnd() );
945
947 {
948 const OVAL oval( seg, m_params.outsetDistance * 2 );
949 addChain( KIGEOM::ConvertToChain( oval ) );
950 }
951 else
952 {
954 const VECTOR2I ext = ( seg.B - seg.A ).Resize( m_params.outsetDistance );
955 const VECTOR2I perp = GetRotated( ext, ANGLE_90 );
956
957 chain.Append( seg.A - ext + perp );
958 chain.Append( seg.A - ext - perp );
959 chain.Append( seg.B + ext - perp );
960 chain.Append( seg.B + ext + perp );
961 chain.SetClosed( true );
962 addChain( chain );
963 }
964
965 AddSuccess();
966 break;
967 }
968 case SHAPE_T::ARC:
969 {
970 // Not 100% sure what a sensible non-round outset of an arc is!
971 // (not sure it's that important in practice)
972
973 // Gets rather complicated if this isn't true
974 if( pcb_shape.GetRadius() >= m_params.outsetDistance )
975 {
976 // Again, include the endcaps and let the user delete the unwanted bits
977 const SHAPE_ARC arc{ pcb_shape.GetCenter(), pcb_shape.GetStart(),
978 pcb_shape.GetArcAngle(), 0 };
979
980 const VECTOR2I startNorm =
981 VECTOR2I( arc.GetP0() - arc.GetCenter() ).Resize( m_params.outsetDistance );
982
983 const SHAPE_ARC inner{ arc.GetCenter(), arc.GetP0() - startNorm,
984 arc.GetCentralAngle(), 0 };
985 const SHAPE_ARC outer{ arc.GetCenter(), arc.GetP0() + startNorm,
986 arc.GetCentralAngle(), 0 };
987
989 chain.Append( outer );
990 // End cap at the P1 end
991 chain.Append( SHAPE_ARC{ arc.GetP1(), outer.GetP1(), ANGLE_180 } );
992
993 if( inner.GetRadius() > 0 )
994 {
995 chain.Append( inner.Reversed() );
996 }
997
998 // End cap at the P0 end back to the start
999 chain.Append( SHAPE_ARC{ arc.GetP0(), inner.GetP0(), ANGLE_180 } );
1000 addChain( chain );
1001 AddSuccess();
1002 }
1003
1004 break;
1005 }
1006
1007 default:
1008 // Other shapes are not supported with exact outsets
1009 // (convex) POLY shouldn't be too traumatic and it would bring trapezoids for free.
1010 break;
1011 }
1012
1013 break;
1014 }
1015 default:
1016 // Other item types are not supported with exact outsets
1017 break;
1018 }
1019
1021 {
1022 handler.DeleteItem( aItem );
1023 }
1024}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:79
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:239
virtual bool IsLocked() const
Definition: board_item.cpp:75
Represent basic circle geometry with utility geometry functions.
Definition: circle.h:33
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
bool IsHorizontal() const
Definition: eda_angle.h:138
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:101
EDA_ANGLE GetArcAngle() const
Definition: eda_shape.cpp:912
int GetRectangleWidth() const
Definition: eda_shape.cpp:419
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:291
bool IsFilled() const
Definition: eda_shape.h:98
int GetRadius() const
Definition: eda_shape.cpp:840
SHAPE_T GetShape() const
Definition: eda_shape.h:132
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:174
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:141
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:137
std::vector< VECTOR2I > GetRectCorners() const
Definition: eda_shape.cpp:1404
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:178
double GetLength() const
Definition: eda_shape.cpp:374
int GetRectangleHeight() const
Definition: eda_shape.cpp:405
virtual void MarkItemModified(BOARD_ITEM &aItem)=0
Report that the tool has modified an item on the board.
virtual void DeleteItem(BOARD_ITEM &aItem)=0
Report that the tool has deleted an item on the board.
virtual void AddNewItem(std::unique_ptr< BOARD_ITEM > aItem)=0
Report that the tools wants to add a new item to the board.
void AddFailure()
Mark that one of the actions failed.
void AddSuccess()
Mark that one of the actions succeeded.
bool ModifyLineOrDeleteIfZeroLength(PCB_SHAPE &aItem, const std::optional< SEG > &aSeg)
Helper function useful for multiple tools: modify a line or delete it if it has zero length.
BOARD_ITEM * GetBoard() const
The BOARD used when creating new shapes.
CHANGE_HANDLER & GetHandler()
Access the handler for making changes to the board.
Hold a translatable error message and may be used when throwing exceptions containing a translated er...
Definition: ki_exception.h:46
const wxString What() const
Definition: ki_exception.h:58
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
const CHAMFER_PARAMS m_chamferParams
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
wxString GetCommitDescription() const override
void ProcessLinePair(PCB_SHAPE &aLineA, PCB_SHAPE &aLineB) override
Perform the action on the pair of lines given.
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
void ProcessItem(BOARD_ITEM &aItem)
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
const PARAMETERS m_params
wxString GetCommitDescription() const override
Class that represents an oval shape (rectangle with semicircular end caps)
Definition: oval.h:45
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition: padstack.h:144
Definition: pad.h:54
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition: pcb_shape.h:79
int GetWidth() const override
Definition: pcb_shape.cpp:301
VECTOR2I GetPosition() const override
Definition: pcb_shape.h:77
PCB_LAYER_ID GetLayer() const override
Return the primary layer this item is on.
Definition: pcb_shape.h:69
SHAPE_POLY_SET m_workingPolygons
This can be disjoint, which will be fixed at the end.
void Finalize()
Clear up any outstanding work.
void ProcessShape(PCB_SHAPE &aPcbShape)
virtual bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon)=0
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon) override
bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon) override
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
wxString GetCommitDescription() const override
std::optional< wxString > GetStatusMessage() const override
Get a status message to show when the routine is complete.
bool ProcessSubsequentPolygon(const SHAPE_POLY_SET &aPolygon) override
A round rectangle shape, based on a rectangle and a radius.
Definition: roundrect.h:36
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aError, ERROR_LOC aErrorLoc) const
Get the polygonal representation of the roundrect.
Definition: roundrect.cpp:82
Definition: seg.h:42
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
bool Intersects(const SEG &aSeg) const
Definition: seg.cpp:248
int Length() const
Return the length (this).
Definition: seg.h:333
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:220
bool Contains(const SEG &aSeg) const
Definition: seg.h:314
EDA_ANGLE Angle(const SEG &aOther) const
Determine the smallest angle between two segments.
Definition: seg.cpp:97
const VECTOR2I & GetArcMid() const
Definition: shape_arc.h:118
const VECTOR2I & GetP1() const
Definition: shape_arc.h:117
double GetRadius() const
Definition: shape_arc.cpp:880
const VECTOR2I & GetP0() const
Definition: shape_arc.h:116
const VECTOR2I & GetCenter() const
Definition: shape_arc.cpp:849
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
void SetWidth(int aWidth)
Set the width of all segments in the chain.
Represent a set of closed polygons.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
void BooleanAdd(const SHAPE_POLY_SET &b)
Perform boolean polyset union.
int VertexCount(int aOutline=-1, int aHole=-1) const
Return the number of vertices in a given outline/hole.
int HoleCount(int aOutline) const
Returns the number of holes in a given outline.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
void BooleanIntersection(const SHAPE_POLY_SET &b)
Perform boolean polyset intersection.
int OutlineCount() const
Return the number of outlines in the set.
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:160
const VECTOR2I GetSize() const
Definition: shape_rect.h:168
int GetWidth() const
Definition: shape_rect.h:176
int GetHeight() const
Definition: shape_rect.h:184
static const int MIN_PRECISION_IU
This is the minimum precision for all the points in a shape.
Definition: shape.h:131
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition: vector2d.h:283
VECTOR2< T > Resize(T aNewLength) const
Return a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:385
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:195
This file is part of the common library.
std::optional< CHAMFER_RESULT > ComputeChamferPoints(const SEG &aSegA, const SEG &aSegB, const CHAMFER_PARAMS &aChamferParams)
Compute the chamfer points for a given line pair and chamfer parameters.
std::optional< DOGBONE_RESULT > ComputeDogbone(const SEG &aSegA, const SEG &aSegB, int aDogboneRadius, bool aAddSlots)
Compute the dogbone geometry for a given line pair and dogbone parameters.
#define _(s)
static constexpr EDA_ANGLE ANGLE_90
Definition: eda_angle.h:403
static constexpr EDA_ANGLE ANGLE_180
Definition: eda_angle.h:405
a few functions useful in geometry calculations.
VECTOR2< ret_type > GetClampedCoords(const VECTOR2< in_type > &aCoords, pad_type aPadding=1u)
Clamps a vector to values that can be negated, respecting numeric limits of coordinates data type wit...
static SHAPE_RECT GetRectRoundedToGridOutwards(const SHAPE_RECT &aRect, int aGridSize)
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:60
VECTOR2I RoundNW(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in the NW direction.
VECTOR2I RoundSE(const VECTOR2I &aVec, int aGridSize)
Round a vector to the nearest grid point in the SE direction.
SHAPE_LINE_CHAIN ConvertToChain(const OVAL &aOval)
Definition: oval.cpp:69
PAD_SHAPE
The set of pad shapes, used with PAD::{Set,Get}Shape()
Definition: padstack.h:52
std::optional< int > GetBoardItemWidth(const BOARD_ITEM &aItem)
Gets the width of a BOARD_ITEM, for items that have a meaningful width.
Utility functions that can be shared by PCB tools.
static bool addSegment(VRML_LAYER &model, IDF_SEGMENT *seg, int icont, int iseg)
std::optional< VECTOR2I > OPT_VECTOR2I
Definition: seg.h:39
constexpr int mmToIU(double mm) const
Definition: base_units.h:88
const SHAPE_LINE_CHAIN chain
int radius
VECTOR2I GetRotated(const VECTOR2I &aVector, const EDA_ANGLE &aAngle)
Return a new VECTOR2I that is the result of rotating aVector by aAngle.
Definition: trigo.h:77
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition: typeinfo.h:88
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition: typeinfo.h:87
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695
Supplemental functions for working with vectors and simple objects that interact with vectors.