KiCad PCB EDA Suite
Loading...
Searching...
No Matches
edit_tool_move_fct.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) 2013-2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 * @author Tomasz Wlostowski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <functional>
28#include <algorithm>
29#include <limits>
30#include <kiplatform/ui.h>
31#include <board.h>
32#include <board_commit.h>
33#include <collectors.h>
34#include <footprint.h>
37#include <pad.h>
38#include <pcb_group.h>
39#include <pcb_generator.h>
40#include <pcb_edit_frame.h>
41#include <spread_footprints.h>
42#include <tool/tool_manager.h>
43#include <tools/pcb_actions.h>
45#include <tools/edit_tool.h>
47#include <tools/drc_tool.h>
49#include <router/router_tool.h>
51#include <zone_filler.h>
52#include <drc/drc_engine.h>
54#include <view/view_controls.h>
55
57#include <wx/richmsgdlg.h>
58#include <wx/choicdlg.h>
59#include <unordered_set>
60#include <unordered_map>
61
62
63static bool PromptConnectedPadDecision( PCB_BASE_EDIT_FRAME* aFrame, const std::vector<PAD*>& aPads,
64 const wxString& aDialogTitle, bool& aIncludeConnectedPads )
65{
66 if( aPads.empty() )
67 {
68 aIncludeConnectedPads = true;
69 return true;
70 }
71
72 std::unordered_set<PAD*> uniquePads( aPads.begin(), aPads.end() );
73
74 wxString msg;
75 msg.Printf( _( "%zu unselected pad(s) are connected to these nets. How do you want to proceed?" ),
76 uniquePads.size() );
77
78 wxString details;
79 details << _( "Connected tracks, vias, and other non-zone copper items will still swap nets"
80 " even if you ignore the unselected pads." )
81 << "\n \n" // Add space so GTK doesn't eat the newlines
82 << _( "Unselected pads:" ) << '\n';
83
84 for( PAD* pad : uniquePads )
85 {
86 const FOOTPRINT* fp = pad->GetParentFootprint();
87 details << wxS( " • " ) << ( fp ? fp->GetReference() : _( "<no reference designator>" ) ) << wxS( ":" )
88 << pad->GetNumber() << '\n';
89 }
90
91
92 wxRichMessageDialog dlg( aFrame, msg, aDialogTitle, wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING );
93 dlg.SetYesNoLabels( _( "Ignore Unselected Pads" ), _( "Swap All Connected Pads" ) );
94 dlg.SetExtendedMessage( details );
95
96 int ret = dlg.ShowModal();
97
98 if( ret == wxID_CANCEL )
99 return false;
100
101 aIncludeConnectedPads = ( ret == wxID_NO );
102 return true;
103}
104
105
106int EDIT_TOOL::Swap( const TOOL_EVENT& aEvent )
107{
108 if( isRouterActive() )
109 {
110 wxBell();
111 return 0;
112 }
113
114 PCB_SELECTION& selection = m_selectionTool->RequestSelection(
115 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
116 {
117 sTool->FilterCollectorForMarkers( aCollector );
118 sTool->FilterCollectorForHierarchy( aCollector, true );
119 sTool->FilterCollectorForFreePads( aCollector );
120
121 // Iterate from the back so we don't have to worry about removals.
122 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
123 {
124 BOARD_ITEM* item = aCollector[i];
125
126 if( item->Type() == PCB_TRACE_T )
127 aCollector.Remove( item );
128 }
129
130 sTool->FilterCollectorForLockedItems( aCollector );
131 } );
132
133 if( selection.Size() < 2 )
134 return 0;
135
136 BOARD_COMMIT localCommit( this );
137 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
138
139 if( !commit )
140 commit = &localCommit;
141
142 std::vector<EDA_ITEM*> sorted = selection.GetItemsSortedBySelectionOrder();
143
144 // Save items, so changes can be undone
145 for( EDA_ITEM* item : selection )
146 commit->Modify( item, nullptr, RECURSE_MODE::RECURSE );
147
148 for( size_t i = 0; i < sorted.size() - 1; i++ )
149 {
150 EDA_ITEM* edaItemA = sorted[i];
151 EDA_ITEM* edaItemB = sorted[( i + 1 ) % sorted.size()];
152
153 if( !edaItemA->IsBOARD_ITEM() || !edaItemB->IsBOARD_ITEM() )
154 continue;
155
156 BOARD_ITEM* a = static_cast<BOARD_ITEM*>( edaItemA );
157 BOARD_ITEM* b = static_cast<BOARD_ITEM*>( edaItemB );
158
159 // Swap X,Y position
160 VECTOR2I aPos = a->GetPosition(), bPos = b->GetPosition();
161 std::swap( aPos, bPos );
162 a->SetPosition( aPos );
163 b->SetPosition( bPos );
164
165 // Handle footprints specially. They can be flipped to the back of the board which
166 // requires a special transformation.
167 if( a->Type() == PCB_FOOTPRINT_T && b->Type() == PCB_FOOTPRINT_T )
168 {
169 FOOTPRINT* aFP = static_cast<FOOTPRINT*>( a );
170 FOOTPRINT* bFP = static_cast<FOOTPRINT*>( b );
171
172 // Store initial orientation of footprints, before flipping them.
173 EDA_ANGLE aAngle = aFP->GetOrientation();
174 EDA_ANGLE bAngle = bFP->GetOrientation();
175
176 // Flip both if needed
177 if( aFP->IsFlipped() != bFP->IsFlipped() )
178 {
179 aFP->Flip( aPos, FLIP_DIRECTION::TOP_BOTTOM );
180 bFP->Flip( bPos, FLIP_DIRECTION::TOP_BOTTOM );
181 }
182
183 // Set orientation
184 std::swap( aAngle, bAngle );
185 aFP->SetOrientation( aAngle );
186 bFP->SetOrientation( bAngle );
187 }
188 // We can also do a layer swap safely for two objects of the same type,
189 // except groups which don't support layer swaps.
190 else if( a->Type() == b->Type() && a->Type() != PCB_GROUP_T )
191 {
192 // Swap layers
193 PCB_LAYER_ID aLayer = a->GetLayer(), bLayer = b->GetLayer();
194 std::swap( aLayer, bLayer );
195 a->SetLayer( aLayer );
196 b->SetLayer( bLayer );
197 }
198 }
199
200 if( !localCommit.Empty() )
201 localCommit.Push( _( "Swap" ) );
202
204
205 return 0;
206}
207
208
210{
211 if( isRouterActive() )
212 {
213 wxBell();
214 return 0;
215 }
216
218
219 if( selection.Size() < 2 || !selection.OnlyContains( { PCB_PAD_T } ) )
220 return 0;
221
222 // Get selected pads in selection order, because swapping is cyclic and we let the user pick
223 // the rotation order
224 std::vector<EDA_ITEM*> orderedPads = selection.GetItemsSortedBySelectionOrder();
225 std::vector<PAD*> pads;
226 const size_t padsCount = orderedPads.size();
227
228 for( EDA_ITEM* it : orderedPads )
229 pads.push_back( static_cast<PAD*>( static_cast<BOARD_ITEM*>( it ) ) );
230
231 // Record original nets and build selected set for quick membership tests
232 std::vector<int> originalNets( padsCount );
233 std::unordered_set<PAD*> selectedPads;
234
235 for( size_t i = 0; i < padsCount; ++i )
236 {
237 originalNets[i] = pads[i]->GetNetCode();
238 selectedPads.insert( pads[i] );
239 }
240
241 // If all nets are the same, nothing to do
242 bool allSame = true;
243
244 for( size_t i = 1; i < padsCount; ++i )
245 {
246 if( originalNets[i] != originalNets[0] )
247 {
248 allSame = false;
249 break;
250 }
251 }
252
253 if( allSame )
254 return 0;
255
256 // Desired new nets are a cyclic rotation of original nets (like Swap positions)
257 auto newNetForIndex =
258 [&]( size_t i )
259 {
260 return originalNets[( i + 1 ) % padsCount];
261 };
262
263 // Take an event commit since we will eventually support this while actively routing the board
264 BOARD_COMMIT localCommit( this );
265 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
266
267 if( !commit )
268 commit = &localCommit;
269
270 // Connectivity to find items connected to each pad
271 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board()->GetConnectivity();
272
273 // Accumulate changes: for each item, assign the resulting new net
274 std::unordered_map<BOARD_CONNECTED_ITEM*, int> itemNewNets;
275 std::vector<PAD*> nonSelectedPadsToChange;
276
277 for( size_t i = 0; i < padsCount; ++i )
278 {
279 PAD* pad = pads[i];
280 int fromNet = originalNets[i];
281 int toNet = newNetForIndex( i );
282
283 // For each connected item, if it matches fromNet, schedule it for toNet
284 for( BOARD_CONNECTED_ITEM* ci : connectivity->GetConnectedItems( pad, 0 ) )
285 {
286 switch( ci->Type() )
287 {
288 case PCB_TRACE_T:
289 case PCB_ARC_T:
290 case PCB_VIA_T:
291 case PCB_PAD_T:
292 break;
293 // Exclude zones, user probably doesn't want to change zone nets
294 default:
295 continue;
296 }
297
298 if( ci->GetNetCode() != fromNet )
299 continue;
300
301 // Track conflicts: if already assigned a different new net, just overwrite (last wins)
302 itemNewNets[ci] = toNet;
303
304 if( ci->Type() == PCB_PAD_T )
305 {
306 PAD* otherPad = static_cast<PAD*>( ci );
307
308 if( !selectedPads.count( otherPad ) )
309 nonSelectedPadsToChange.push_back( otherPad );
310 }
311 }
312 }
313
314 bool includeConnectedPads = true;
315
316 if( !PromptConnectedPadDecision( frame(), nonSelectedPadsToChange, _( "Swap Pad Nets" ), includeConnectedPads ) )
317 return 0;
318
319 // Apply changes
320 // 1) Selected pads get their new nets directly
321 for( size_t i = 0; i < padsCount; ++i )
322 {
323 commit->Modify( pads[i] );
324 pads[i]->SetNetCode( newNetForIndex( i ) );
325 }
326
327 // 2) Connected items propagate, depending on user choice
328 for( const auto& itemNewNet : itemNewNets )
329 {
330 BOARD_CONNECTED_ITEM* item = itemNewNet.first;
331 int newNet = itemNewNet.second;
332
333 if( item->Type() == PCB_PAD_T )
334 {
335 PAD* p = static_cast<PAD*>( item );
336
337 if( selectedPads.count( p ) )
338 continue; // already changed above
339
340 if( !includeConnectedPads )
341 continue; // skip non-selected pads if requested
342 }
343
344 commit->Modify( item );
345 item->SetNetCode( newNet );
346 }
347
348 if( !localCommit.Empty() )
349 localCommit.Push( _( "Swap Pad Nets" ) );
350
351 // Ensure connectivity visuals update
354
355 return 0;
356}
357
358
360{
361 if( isRouterActive() )
362 {
363 wxBell();
364 return 0;
365 }
366
367 auto showError =
368 [this]()
369 {
370 frame()->ShowInfoBarError( _( "Gate swapping must be performed on pads within one multi-gate "
371 "footprint." ) );
372 };
373
375
376 // Get our sanity checks out of the way to clean up later loops
377 FOOTPRINT* targetFp = nullptr;
378 bool fail = false;
379
380 for( EDA_ITEM* it : selection )
381 {
382 // This shouldn't happen due to the filter, but just in case
383 if( it->Type() != PCB_PAD_T )
384 {
385 fail = true;
386 break;
387 }
388
389 FOOTPRINT* fp = static_cast<PAD*>( static_cast<BOARD_ITEM*>( it ) )->GetParentFootprint();
390
391 if( !targetFp )
392 {
393 targetFp = fp;
394 }
395 else if( fp && targetFp != fp )
396 {
397 fail = true;
398 break;
399 }
400 }
401
402 if( fail || !targetFp || targetFp->GetUnitInfo().size() < 2 )
403 {
404 showError();
405 return 0;
406 }
407
408
409 const auto& units = targetFp->GetUnitInfo();
410
411 // Collect unit hits and ordered unit list based on selection order
412 std::vector<bool> unitHit( units.size(), false );
413 std::vector<int> unitOrder;
414
415 std::vector<EDA_ITEM*> orderedPads = selection.GetItemsSortedBySelectionOrder();
416
417 for( EDA_ITEM* it : orderedPads )
418 {
419 PAD* pad = static_cast<PAD*>( static_cast<BOARD_ITEM*>( it ) );
420
421 const wxString& padNum = pad->GetNumber();
422 int unitIdx = -1;
423
424 for( size_t i = 0; i < units.size(); ++i )
425 {
426 for( const auto& p : units[i].m_pins )
427 {
428 if( p == padNum )
429 {
430 unitIdx = static_cast<int>( i );
431
432 if( !unitHit[i] )
433 unitOrder.push_back( unitIdx );
434
435 unitHit[i] = true;
436 break;
437 }
438 }
439
440 if( unitIdx >= 0 )
441 break;
442 }
443 }
444
445 // Determine active units from selection order: 0 -> bail, 1 -> single-unit flow, 2+ -> cycle
446 std::vector<int> activeUnitIdx;
447 int sourceIdx = -1;
448
449 if( unitOrder.size() >= 2 )
450 {
451 activeUnitIdx = unitOrder;
452 sourceIdx = unitOrder.front();
453 }
454 // If we only have one gate selected, we must have a target unit name parameter to proceed
455 else if( unitOrder.size() == 1 && aEvent.HasParameter() )
456 {
457 sourceIdx = unitOrder.front();
458 wxString targetUnitByName = aEvent.Parameter<wxString>();
459
460 int targetIdx = -1;
461
462 for( size_t i = 0; i < units.size(); ++i )
463 {
464 if( static_cast<int>( i ) == sourceIdx )
465 continue;
466
467 if( units[i].m_pins.size() == units[sourceIdx].m_pins.size() && units[i].m_unitName == targetUnitByName )
468 targetIdx = static_cast<int>( i );
469 }
470
471 if( targetIdx < 0 )
472 {
473 showError();
474 return 0;
475 }
476
477 activeUnitIdx.push_back( sourceIdx );
478 activeUnitIdx.push_back( targetIdx );
479 }
480 else
481 {
482 showError();
483 return 0;
484 }
485
486 // Verify equal pin counts across all active units
487 const size_t pinCount = units[activeUnitIdx.front()].m_pins.size();
488
489 for( int idx : activeUnitIdx )
490 {
491 if( units[idx].m_pins.size() != pinCount )
492 {
493 frame()->ShowInfoBarError( _( "Gate swapping must be performed on gates with equal pin counts." ) );
494 return 0;
495 }
496 }
497
498 // Build per-unit pad arrays and net vectors
499 const size_t unitCount = activeUnitIdx.size();
500 std::vector<std::vector<PAD*>> unitPads( unitCount );
501 std::vector<std::vector<int>> unitNets( unitCount );
502
503 for( size_t ui = 0; ui < unitCount; ++ui )
504 {
505 int uidx = activeUnitIdx[ui];
506 const auto& pins = units[uidx].m_pins;
507
508 for( size_t pi = 0; pi < pinCount; ++pi )
509 {
510 PAD* p = targetFp->FindPadByNumber( pins[pi] );
511
512 if( !p )
513 {
514 frame()->ShowInfoBarError( _( "Gate swapping failed: pad in unit missing from footprint." ) );
515 return 0;
516 }
517
518 unitPads[ui].push_back( p );
519 unitNets[ui].push_back( p->GetNetCode() );
520 }
521 }
522
523 // If all unit nets match across positions, nothing to do
524 bool allSame = true;
525
526 for( size_t pi = 0; pi < pinCount && allSame; ++pi )
527 {
528 int refNet = unitNets[0][pi];
529
530 for( size_t ui = 1; ui < unitCount; ++ui )
531 {
532 if( unitNets[ui][pi] != refNet )
533 {
534 allSame = false;
535 break;
536 }
537 }
538 }
539
540 if( allSame )
541 {
542 frame()->ShowInfoBarError( _( "Gate swapping has no effect: all selected gates have identical nets." ) );
543 return 0;
544 }
545
546 // TODO: someday support swapping while routing and take that commit
547 BOARD_COMMIT localCommit( this );
548 BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() );
549
550 if( !commit )
551 commit = &localCommit;
552
553 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board()->GetConnectivity();
554
555 // Accumulate changes: item -> new net
556 std::unordered_map<BOARD_CONNECTED_ITEM*, int> itemNewNets;
557 std::vector<PAD*> nonSelectedPadsToChange;
558
559 // Selected pads in the swap (for suppressing re-adding in connected pad handling)
560 std::unordered_set<PAD*> swapPads;
561
562 for( const auto& v : unitPads )
563 swapPads.insert( v.begin(), v.end() );
564
565 // Schedule net swaps for connectivity-attached items
566 auto scheduleForPad = [&]( PAD* pad, int fromNet, int toNet )
567 {
568 for( BOARD_CONNECTED_ITEM* ci : connectivity->GetConnectedItems( pad, 0 ) )
569 {
570 switch( ci->Type() )
571 {
572 case PCB_TRACE_T:
573 case PCB_ARC_T:
574 case PCB_VIA_T:
575 case PCB_PAD_T:
576 break;
577
578 default:
579 continue;
580 }
581
582 if( ci->GetNetCode() != fromNet )
583 continue;
584
585 itemNewNets[ ci ] = toNet;
586
587 if( ci->Type() == PCB_PAD_T )
588 {
589 PAD* other = static_cast<PAD*>( ci );
590
591 if( !swapPads.count( other ) )
592 nonSelectedPadsToChange.push_back( other );
593 }
594 }
595 };
596
597 // For each position, rotate nets among units forward
598 for( size_t pi = 0; pi < pinCount; ++pi )
599 {
600 for( size_t ui = 0; ui < unitCount; ++ui )
601 {
602 size_t fromIdx = ui;
603 size_t toIdx = ( ui + 1 ) % unitCount;
604
605 PAD* padFrom = unitPads[fromIdx][pi];
606 int fromNet = unitNets[fromIdx][pi];
607 int toNet = unitNets[toIdx][pi];
608
609 scheduleForPad( padFrom, fromNet, toNet );
610 }
611 }
612
613 bool includeConnectedPads = true;
614
615 if( !PromptConnectedPadDecision( frame(), nonSelectedPadsToChange, _( "Swap Gate Nets" ), includeConnectedPads ) )
616 {
617 return 0;
618 }
619
620 // Apply pad net swaps: rotate per position
621 for( size_t pi = 0; pi < pinCount; ++pi )
622 {
623 // First write back nets for each unit's pad at this position
624 for( size_t ui = 0; ui < unitCount; ++ui )
625 {
626 size_t toIdx = ( ui + 1 ) % unitCount;
627 PAD* pad = unitPads[ui][pi];
628 int newNet = unitNets[toIdx][pi];
629
630 commit->Modify( pad );
631 pad->SetNetCode( newNet );
632 }
633 }
634
635 // Apply connected items
636 for( const auto& kv : itemNewNets )
637 {
638 BOARD_CONNECTED_ITEM* item = kv.first;
639 int newNet = kv.second;
640
641 if( item->Type() == PCB_PAD_T )
642 {
643 PAD* p = static_cast<PAD*>( item );
644
645 if( swapPads.count( p ) )
646 continue;
647
648 if( !includeConnectedPads )
649 continue;
650 }
651
652 commit->Modify( item );
653 item->SetNetCode( newNet );
654 }
655
656 if( !localCommit.Empty() )
657 localCommit.Push( _( "Swap Gate Nets" ) );
658
661
662 return 0;
663}
664
665
667{
668 if( isRouterActive() || m_dragging )
669 {
670 wxBell();
671 return 0;
672 }
673
674 BOARD_COMMIT commit( this );
675 PCB_SELECTION& selection = m_selectionTool->RequestSelection(
676 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
677 {
678 sTool->FilterCollectorForMarkers( aCollector );
679 sTool->FilterCollectorForHierarchy( aCollector, true );
680 sTool->FilterCollectorForFreePads( aCollector, true );
681
682 // Iterate from the back so we don't have to worry about removals.
683 for( int i = aCollector.GetCount() - 1; i >= 0; --i )
684 {
685 BOARD_ITEM* item = aCollector[i];
686
687 if( !dynamic_cast<FOOTPRINT*>( item ) )
688 aCollector.Remove( item );
689 }
690
691 sTool->FilterCollectorForLockedItems( aCollector );
692 } );
693
694 std::vector<FOOTPRINT*> footprintsToPack;
695
696 for( EDA_ITEM* item : selection )
697 footprintsToPack.push_back( static_cast<FOOTPRINT*>( item ) );
698
699 if( footprintsToPack.empty() )
700 return 0;
701
702 BOX2I footprintsBbox;
703
704 for( FOOTPRINT* fp : footprintsToPack )
705 {
706 commit.Modify( fp );
707 fp->SetFlags( IS_MOVING );
708 footprintsBbox.Merge( fp->GetBoundingBox( false ) );
709 }
710
711 SpreadFootprints( &footprintsToPack, footprintsBbox.Normalize().GetOrigin(), false );
712
713 if( doMoveSelection( aEvent, &commit, true ) )
714 commit.Push( _( "Pack Footprints" ) );
715 else
716 commit.Revert();
717
718 return 0;
719}
720
721
722int EDIT_TOOL::Move( const TOOL_EVENT& aEvent )
723{
724 if( isRouterActive() || m_dragging )
725 {
726 wxBell();
727 return 0;
728 }
729
730 if( BOARD_COMMIT* commit = dynamic_cast<BOARD_COMMIT*>( aEvent.Commit() ) )
731 {
732 // Most moves will be synchronous unless they are coming from the API
733 if( aEvent.SynchronousState() )
734 aEvent.SynchronousState()->store( STS_RUNNING );
735
736 if( doMoveSelection( aEvent, commit, true ) )
737 {
738 if( aEvent.SynchronousState() )
739 aEvent.SynchronousState()->store( STS_FINISHED );
740 }
741 else if( aEvent.SynchronousState() )
742 {
743 aEvent.SynchronousState()->store( STS_CANCELLED );
744 }
745 }
746 else
747 {
748 BOARD_COMMIT localCommit( this );
749
750 if( doMoveSelection( aEvent, &localCommit, false ) )
751 localCommit.Push( _( "Move" ) );
752 else
753 localCommit.Revert();
754 }
755
756 // Notify point editor. (While doMoveSelection() will re-select the items and post this
757 // event, it's done before the edit flags are cleared in BOARD_COMMIT::Push() so the point
758 // editor doesn't fire up.)
759 m_toolMgr->ProcessEvent( EVENTS::SelectedEvent );
760
761 return 0;
762}
763
764
765VECTOR2I EDIT_TOOL::getSafeMovement( const VECTOR2I& aMovement, const BOX2I& aSourceBBox,
766 const VECTOR2D& aBBoxOffset )
767{
768 typedef std::numeric_limits<int> coord_limits;
769
770 static const double max = coord_limits::max() - (int) COORDS_PADDING;
771 static const double min = -max;
772
773 BOX2D testBox( aSourceBBox.GetPosition(), aSourceBBox.GetSize() );
774 testBox.Offset( aBBoxOffset );
775
776 // Do not restrict movement if bounding box is already out of bounds
777 if( testBox.GetLeft() < min || testBox.GetTop() < min || testBox.GetRight() > max
778 || testBox.GetBottom() > max )
779 {
780 return aMovement;
781 }
782
783 testBox.Offset( aMovement );
784
785 if( testBox.GetLeft() < min )
786 testBox.Offset( min - testBox.GetLeft(), 0 );
787
788 if( max < testBox.GetRight() )
789 testBox.Offset( -( testBox.GetRight() - max ), 0 );
790
791 if( testBox.GetTop() < min )
792 testBox.Offset( 0, min - testBox.GetTop() );
793
794 if( max < testBox.GetBottom() )
795 testBox.Offset( 0, -( testBox.GetBottom() - max ) );
796
797 return KiROUND( testBox.GetPosition() - aBBoxOffset - aSourceBBox.GetPosition() );
798}
799
800
801bool EDIT_TOOL::doMoveSelection( const TOOL_EVENT& aEvent, BOARD_COMMIT* aCommit, bool aAutoStart )
802{
803 const bool moveWithReference = aEvent.IsAction( &PCB_ACTIONS::moveWithReference );
804 const bool moveIndividually = aEvent.IsAction( &PCB_ACTIONS::moveIndividually );
805
807 PCBNEW_SETTINGS* cfg = editFrame->GetPcbNewSettings();
808 BOARD* board = editFrame->GetBoard();
810 VECTOR2I originalCursorPos = controls->GetCursorPosition();
811 VECTOR2I originalMousePos = controls->GetMousePosition();
812 std::unique_ptr<STATUS_TEXT_POPUP> statusPopup;
813 size_t itemIdx = 0;
814
815 // Be sure that there is at least one item that we can modify. If nothing was selected before,
816 // try looking for the stuff under mouse cursor (i.e. KiCad old-style hover selection)
817 PCB_SELECTION& selection = m_selectionTool->RequestSelection(
818 []( const VECTOR2I& aPt, GENERAL_COLLECTOR& aCollector, PCB_SELECTION_TOOL* sTool )
819 {
820 sTool->FilterCollectorForMarkers( aCollector );
821 sTool->FilterCollectorForHierarchy( aCollector, true );
822 sTool->FilterCollectorForFreePads( aCollector );
823 sTool->FilterCollectorForTableCells( aCollector );
824 sTool->FilterCollectorForLockedItems( aCollector );
825 } );
826
827 if( m_dragging || selection.Empty() )
828 return false;
829
830 TOOL_EVENT pushedEvent = aEvent;
831 editFrame->PushTool( aEvent );
832 Activate();
833
834 // Must be done after Activate() so that it gets set into the correct context
835 controls->ShowCursor( true );
836 controls->SetAutoPan( true );
837 controls->ForceCursorPosition( false );
838
839 auto displayConstraintsMessage =
840 [editFrame]( LEADER_MODE aMode )
841 {
842 wxString msg;
843
844 switch( aMode )
845 {
847 msg = _( "Angle snap lines: 45°" );
848 break;
849
851 msg = _( "Angle snap lines: 90°" );
852 break;
853
854 default:
855 msg.clear();
856 break;
857 }
858
859 editFrame->DisplayConstraintsMsg( msg );
860 };
861
862 auto updateStatusPopup =
863 [&]( EDA_ITEM* item, size_t ii, size_t count )
864 {
865 wxString popuptext = _( "Click to place %s (item %zu of %zu)\n"
866 "Press <esc> to cancel all; double-click to finish" );
867 wxString msg;
868
869 if( item->Type() == PCB_FOOTPRINT_T )
870 {
871 FOOTPRINT* fp = static_cast<FOOTPRINT*>( item );
872 msg = fp->GetReference();
873 }
874 else if( item->Type() == PCB_PAD_T )
875 {
876 PAD* pad = static_cast<PAD*>( item );
877 FOOTPRINT* fp = pad->GetParentFootprint();
878 msg = wxString::Format( _( "%s pad %s" ), fp->GetReference(), pad->GetNumber() );
879 }
880 else
881 {
882 msg = item->GetTypeDesc().Lower();
883 }
884
885 if( !statusPopup )
886 statusPopup = std::make_unique<STATUS_TEXT_POPUP>( frame() );
887
888 statusPopup->SetText( wxString::Format( popuptext, msg, ii, count ) );
889 };
890
891 std::vector<BOARD_ITEM*> sel_items; // All the items operated on by the move below
892 std::vector<BOARD_ITEM*> orig_items; // All the original items in the selection
893
894 for( EDA_ITEM* item : selection )
895 {
896 if( item->IsBOARD_ITEM() )
897 {
898 BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
899
900 if( !selection.IsHover() )
901 orig_items.push_back( boardItem );
902
903 sel_items.push_back( boardItem );
904 }
905
906 if( item->Type() == PCB_FOOTPRINT_T )
907 {
908 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
909
910 for( PAD* pad : footprint->Pads() )
911 sel_items.push_back( pad );
912
913 // Clear this flag here; it will be set by the netlist updater if the footprint is new
914 // so that it was skipped in the initial connectivity update in OnNetlistChanged
915 footprint->SetAttributes( footprint->GetAttributes() & ~FP_JUST_ADDED );
916 }
917 }
918
919 VECTOR2I pickedReferencePoint;
920
921 if( moveWithReference && !pickReferencePoint( _( "Select reference point for move..." ), "", "",
922 pickedReferencePoint ) )
923 {
924 if( selection.IsHover() )
926
927 editFrame->PopTool( pushedEvent );
928 return false;
929 }
930
931 if( moveIndividually )
932 {
933 orig_items.clear();
934
935 for( EDA_ITEM* item : selection.GetItemsSortedBySelectionOrder() )
936 {
937 if( item->IsBOARD_ITEM() )
938 orig_items.push_back( static_cast<BOARD_ITEM*>( item ) );
939 }
940
941 updateStatusPopup( orig_items[ itemIdx ], itemIdx + 1, orig_items.size() );
942 statusPopup->Popup();
943 statusPopup->Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, 20 ) );
944 canvas()->SetStatusPopup( statusPopup->GetPanel() );
945
946 m_selectionTool->ClearSelection();
947 m_selectionTool->AddItemToSel( orig_items[ itemIdx ] );
948
949 sel_items.clear();
950 sel_items.push_back( orig_items[ itemIdx ] );
951 }
952
953 bool restore_state = false;
954 VECTOR2I originalPos = originalCursorPos; // Initialize to current cursor position
955 VECTOR2D bboxMovement;
956 BOX2I originalBBox;
957 bool updateBBox = true;
958 LSET layers( { editFrame->GetActiveLayer() } );
960 TOOL_EVENT copy = aEvent;
961 TOOL_EVENT* evt = &copy;
962 VECTOR2I prevPos;
963 bool enableLocalRatsnest = true;
964
965 LEADER_MODE angleSnapMode = GetAngleSnapMode();
966 bool eatFirstMouseUp = true;
967 bool allowRedraw3D = cfg->m_Display.m_Live3DRefresh;
968 bool showCourtyardConflicts = !m_isFootprintEditor && cfg->m_ShowCourtyardCollisions;
969
970 // Axis locking for arrow key movement
971 enum class AXIS_LOCK { NONE, HORIZONTAL, VERTICAL };
972 AXIS_LOCK axisLock = AXIS_LOCK::NONE;
973 long lastArrowKeyAction = 0;
974
975 // Used to test courtyard overlaps
976 std::unique_ptr<DRC_INTERACTIVE_COURTYARD_CLEARANCE> drc_on_move = nullptr;
977
978 if( showCourtyardConflicts )
979 {
980 std::shared_ptr<DRC_ENGINE> drcEngine = m_toolMgr->GetTool<DRC_TOOL>()->GetDRCEngine();
981 drc_on_move.reset( new DRC_INTERACTIVE_COURTYARD_CLEARANCE( drcEngine ) );
982 drc_on_move->Init( board );
983 }
984
985 auto configureAngleSnap =
986 [&]( LEADER_MODE aMode )
987 {
988 std::vector<VECTOR2I> directions;
989
990 switch( aMode )
991 {
993 directions = { VECTOR2I( 1, 0 ), VECTOR2I( 0, 1 ), VECTOR2I( 1, 1 ), VECTOR2I( 1, -1 ) };
994 break;
995
997 directions = { VECTOR2I( 1, 0 ), VECTOR2I( 0, 1 ) };
998 break;
999
1000 default:
1001 break;
1002 }
1003
1004 grid.SetSnapLineDirections( directions );
1005
1006 if( directions.empty() )
1007 {
1008 grid.ClearSnapLine();
1009 }
1010 else
1011 {
1012 grid.SetSnapLineOrigin( originalPos );
1013 }
1014 };
1015
1016 configureAngleSnap( angleSnapMode );
1017 displayConstraintsMessage( angleSnapMode );
1018
1019 // Prime the pump
1020 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1021
1022 // Main loop: keep receiving events
1023 do
1024 {
1025 VECTOR2I movement;
1027 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
1028 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
1029
1030 bool isSkip = evt->IsAction( &PCB_ACTIONS::skip ) && moveIndividually;
1031
1032 if( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) )
1033 eatFirstMouseUp = false;
1034
1035 if( evt->IsAction( &PCB_ACTIONS::move )
1036 || evt->IsMotion()
1037 || evt->IsDrag( BUT_LEFT )
1041 {
1042 if( m_dragging && ( evt->IsMotion()
1043 || evt->IsDrag( BUT_LEFT )
1044 || evt->IsAction( &ACTIONS::refreshPreview ) ) )
1045 {
1046 bool redraw3D = false;
1047
1048 GRID_HELPER_GRIDS selectionGrid = grid.GetSelectionGrid( selection );
1049
1050 if( controls->GetSettings().m_lastKeyboardCursorPositionValid )
1051 {
1052 VECTOR2I keyboardPos( controls->GetSettings().m_lastKeyboardCursorPosition );
1053
1054 grid.SetSnap( false );
1055
1056 // Use the keyboard position directly without grid alignment. The position
1057 // was already calculated correctly in CursorControl by adding the grid step
1058 // to the current position. Aligning to grid here would snap to the nearest
1059 // grid point, which causes precision errors when the original position is
1060 // not on a grid point (issue #22805).
1061 m_cursor = keyboardPos;
1062
1063 // Update axis lock based on arrow key press, but skip on refreshPreview
1064 // to avoid double-processing when CursorControl posts refreshPreview after
1065 // handling the arrow key.
1066 if( !evt->IsAction( &ACTIONS::refreshPreview ) )
1067 {
1068 long action = controls->GetSettings().m_lastKeyboardCursorCommand;
1069
1070 if( action == ACTIONS::CURSOR_LEFT || action == ACTIONS::CURSOR_RIGHT )
1071 {
1072 if( axisLock == AXIS_LOCK::HORIZONTAL )
1073 {
1074 // Check if opposite horizontal key pressed to unlock
1075 if( ( lastArrowKeyAction == ACTIONS::CURSOR_LEFT && action == ACTIONS::CURSOR_RIGHT ) ||
1076 ( lastArrowKeyAction == ACTIONS::CURSOR_RIGHT && action == ACTIONS::CURSOR_LEFT ) )
1077 {
1078 axisLock = AXIS_LOCK::NONE;
1079 }
1080 // Same direction axis, keep locked
1081 }
1082 else
1083 {
1084 axisLock = AXIS_LOCK::HORIZONTAL;
1085 }
1086 }
1087 else if( action == ACTIONS::CURSOR_UP || action == ACTIONS::CURSOR_DOWN )
1088 {
1089 if( axisLock == AXIS_LOCK::VERTICAL )
1090 {
1091 // Check if opposite vertical key pressed to unlock
1092 if( ( lastArrowKeyAction == ACTIONS::CURSOR_UP && action == ACTIONS::CURSOR_DOWN ) ||
1093 ( lastArrowKeyAction == ACTIONS::CURSOR_DOWN && action == ACTIONS::CURSOR_UP ) )
1094 {
1095 axisLock = AXIS_LOCK::NONE;
1096 }
1097 // Same direction axis, keep locked
1098 }
1099 else
1100 {
1101 axisLock = AXIS_LOCK::VERTICAL;
1102 }
1103 }
1104
1105 lastArrowKeyAction = action;
1106 }
1107 }
1108 else
1109 {
1110 VECTOR2I mousePos( controls->GetMousePosition() );
1111
1112 m_cursor = grid.BestSnapAnchor( mousePos, layers, selectionGrid, sel_items );
1113 }
1114
1115 if( axisLock == AXIS_LOCK::HORIZONTAL )
1116 m_cursor.y = prevPos.y;
1117 else if( axisLock == AXIS_LOCK::VERTICAL )
1118 m_cursor.x = prevPos.x;
1119
1120 if( !selection.HasReferencePoint() )
1121 originalPos = m_cursor;
1122
1123 if( updateBBox )
1124 {
1125 originalBBox = BOX2I();
1126 bboxMovement = VECTOR2D();
1127
1128 for( EDA_ITEM* item : sel_items )
1129 originalBBox.Merge( item->ViewBBox() );
1130
1131 updateBBox = false;
1132 }
1133
1134 // Constrain selection bounding box to coordinates limits
1135 movement = getSafeMovement( m_cursor - prevPos, originalBBox, bboxMovement );
1136
1137 // Apply constrained movement
1138 m_cursor = prevPos + movement;
1139
1140 controls->ForceCursorPosition( true, m_cursor );
1141 selection.SetReferencePoint( m_cursor );
1142
1143 prevPos = m_cursor;
1144 bboxMovement += movement;
1145
1146 // Drag items to the current cursor position
1147 for( BOARD_ITEM* item : sel_items )
1148 {
1149 // Don't double move child items.
1150 if( !item->GetParent() || !item->GetParent()->IsSelected() )
1151 {
1152 item->Move( movement );
1153
1154 // Images are on non-cached layers and will not be updated automatically in the overlay, so
1155 // explicitly tell the view they've moved.
1156 if( item->Type() == PCB_REFERENCE_IMAGE_T )
1157 view()->Update( item, KIGFX::GEOMETRY );
1158 }
1159
1160 if( item->Type() == PCB_GENERATOR_T && sel_items.size() == 1 )
1161 {
1162 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genUpdateEdit, aCommit,
1163 static_cast<PCB_GENERATOR*>( item ) );
1164 }
1165
1166 if( item->Type() == PCB_FOOTPRINT_T )
1167 redraw3D = true;
1168 }
1169
1170 if( redraw3D && allowRedraw3D )
1171 editFrame->Update3DView( false, true );
1172
1173 if( showCourtyardConflicts && drc_on_move->m_FpInMove.size() )
1174 {
1175 drc_on_move->Run();
1176 drc_on_move->UpdateConflicts( m_toolMgr->GetView(), true );
1177 }
1178
1180 }
1181 else if( !m_dragging && ( aAutoStart || !evt->IsAction( &ACTIONS::refreshPreview ) ) )
1182 {
1183 // Prepare to start dragging
1184 editFrame->HideSolderMask();
1185
1186 m_dragging = true;
1187
1188 for( BOARD_ITEM* item : sel_items )
1189 {
1190 if( item->GetParent() && item->GetParent()->IsSelected() )
1191 continue;
1192
1193 if( !item->IsNew() && !item->IsMoving() )
1194 {
1195 if( item->Type() == PCB_GENERATOR_T && sel_items.size() == 1 )
1196 {
1197 enableLocalRatsnest = false;
1198
1199 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genStartEdit, aCommit,
1200 static_cast<PCB_GENERATOR*>( item ) );
1201 }
1202 else
1203 {
1204 aCommit->Modify( item, nullptr, RECURSE_MODE::RECURSE );
1205 }
1206
1207 item->SetFlags( IS_MOVING );
1208
1209 if( item->Type() == PCB_SHAPE_T )
1210 static_cast<PCB_SHAPE*>( item )->UpdateHatching();
1211
1212 item->RunOnChildren(
1213 [&]( BOARD_ITEM* child )
1214 {
1215 child->SetFlags( IS_MOVING );
1216
1217 if( child->Type() == PCB_SHAPE_T )
1218 static_cast<PCB_SHAPE*>( child )->UpdateHatching();
1219 },
1221 }
1222 }
1223
1224 m_cursor = controls->GetCursorPosition();
1225
1226 if( selection.HasReferencePoint() )
1227 {
1228 // start moving with the reference point attached to the cursor
1229 grid.SetAuxAxes( false );
1230
1231 movement = m_cursor - selection.GetReferencePoint();
1232
1233 // Drag items to the current cursor position
1234 for( EDA_ITEM* item : selection )
1235 {
1236 if( !item->IsBOARD_ITEM() )
1237 continue;
1238
1239 // Don't double move footprint pads, fields, etc.
1240 if( item->GetParent() && item->GetParent()->IsSelected() )
1241 continue;
1242
1243 BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
1244 boardItem->Move( movement );
1245
1246 // Images are on non-cached layers and will not be updated automatically in the overlay, so
1247 // explicitly tell the view they've moved.
1248 if( boardItem->Type() == PCB_REFERENCE_IMAGE_T )
1249 view()->Update( boardItem, KIGFX::GEOMETRY );
1250 }
1251
1252 selection.SetReferencePoint( m_cursor );
1253 }
1254 else
1255 {
1256 if( showCourtyardConflicts )
1257 {
1258 std::vector<FOOTPRINT*>& FPs = drc_on_move->m_FpInMove;
1259
1260 for( BOARD_ITEM* item : sel_items )
1261 {
1262 if( item->Type() == PCB_FOOTPRINT_T )
1263 FPs.push_back( static_cast<FOOTPRINT*>( item ) );
1264
1265 item->RunOnChildren(
1266 [&]( BOARD_ITEM* child )
1267 {
1268 if( child->Type() == PCB_FOOTPRINT_T )
1269 FPs.push_back( static_cast<FOOTPRINT*>( child ) );
1270 },
1272 }
1273 }
1274
1275 // Use the mouse position over cursor, as otherwise large grids will allow only
1276 // snapping to items that are closest to grid points
1277 m_cursor = grid.BestDragOrigin( originalMousePos, sel_items, grid.GetSelectionGrid( selection ),
1278 &m_selectionTool->GetFilter() );
1279
1280 // Set the current cursor position to the first dragged item origin, so the
1281 // movement vector could be computed later
1282 if( moveWithReference )
1283 {
1284 selection.SetReferencePoint( pickedReferencePoint );
1285
1286 if( angleSnapMode != LEADER_MODE::DIRECT )
1287 grid.SetSnapLineOrigin( selection.GetReferencePoint() );
1288
1289 controls->ForceCursorPosition( true, pickedReferencePoint );
1290 m_cursor = pickedReferencePoint;
1291 }
1292 else
1293 {
1294 VECTOR2I dragOrigin = m_cursor;
1295
1296 selection.SetReferencePoint( dragOrigin );
1297
1298 if( angleSnapMode != LEADER_MODE::DIRECT )
1299 grid.SetSnapLineOrigin( dragOrigin );
1300
1301 grid.SetAuxAxes( true, dragOrigin );
1302
1303 if( !editFrame->GetMoveWarpsCursor() )
1304 m_cursor = originalCursorPos;
1305 else
1306 m_cursor = dragOrigin;
1307 }
1308
1309 originalPos = selection.GetReferencePoint();
1310 }
1311
1312 // Update variables for bounding box collision calculations
1313 updateBBox = true;
1314
1315 controls->SetCursorPosition( m_cursor, false );
1316
1317 prevPos = m_cursor;
1318 controls->SetAutoPan( true );
1320 }
1321
1322 if( statusPopup )
1323 statusPopup->Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, 20 ) );
1324
1325 if( enableLocalRatsnest )
1326 m_toolMgr->PostAction( PCB_ACTIONS::updateLocalRatsnest, movement );
1327 }
1328 else if( evt->IsCancelInteractive() || evt->IsActivate() )
1329 {
1330 if( m_dragging && evt->IsCancelInteractive() )
1331 evt->SetPassEvent( false );
1332
1333 restore_state = true; // Canceling the tool means that items have to be restored
1334 break; // Finish
1335 }
1336 else if( evt->IsClick( BUT_RIGHT ) )
1337 {
1338 m_selectionTool->GetToolMenu().ShowContextMenu( selection );
1339 }
1340 else if( evt->IsAction( &ACTIONS::undo ) )
1341 {
1342 restore_state = true; // Perform undo locally
1343 break; // Finish
1344 }
1345 else if( evt->IsAction( &ACTIONS::doDelete ) )
1346 {
1347 evt->SetPassEvent();
1348 // Exit on a delete; there will no longer be anything to drag.
1349 break;
1350 }
1351 else if( evt->IsAction( &ACTIONS::duplicate ) && evt != &copy )
1352 {
1353 wxBell();
1354 }
1355 else if( evt->IsAction( &ACTIONS::cut ) )
1356 {
1357 wxBell();
1358 }
1359 else if( evt->IsAction( &PCB_ACTIONS::rotateCw )
1361 || evt->IsAction( &PCB_ACTIONS::flip )
1362 || evt->IsAction( &PCB_ACTIONS::mirrorH )
1363 || evt->IsAction( &PCB_ACTIONS::mirrorV ) )
1364 {
1365 updateBBox = true;
1366 eatFirstMouseUp = false;
1367 evt->SetPassEvent();
1368 }
1369 else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) || isSkip )
1370 {
1371 // Eat mouse-up/-click events that leaked through from the lock dialog
1372 if( eatFirstMouseUp && !evt->IsAction( &ACTIONS::cursorClick ) )
1373 {
1374 eatFirstMouseUp = false;
1375 continue;
1376 }
1377 else if( moveIndividually && m_dragging )
1378 {
1379 // Put skipped items back where they started
1380 if( isSkip )
1381 orig_items[itemIdx]->SetPosition( originalPos );
1382
1383 view()->Update( orig_items[itemIdx] );
1385
1386 if( ++itemIdx < orig_items.size() )
1387 {
1388 BOARD_ITEM* nextItem = orig_items[itemIdx];
1389
1390 m_selectionTool->ClearSelection();
1391
1392 originalPos = nextItem->GetPosition();
1393 m_selectionTool->AddItemToSel( nextItem );
1394 selection.SetReferencePoint( originalPos );
1395 if( angleSnapMode != LEADER_MODE::DIRECT )
1396 grid.SetSnapLineOrigin( selection.GetReferencePoint() );
1397
1398 sel_items.clear();
1399 sel_items.push_back( nextItem );
1400 updateStatusPopup( nextItem, itemIdx + 1, orig_items.size() );
1401
1402 // Pick up new item
1403 aCommit->Modify( nextItem, nullptr, RECURSE_MODE::RECURSE );
1404 nextItem->Move( controls->GetCursorPosition( true ) - nextItem->GetPosition() );
1405
1406 // Images are on non-cached layers and will not be updated automatically in the overlay, so
1407 // explicitly tell the view they've moved.
1408 if( nextItem->Type() == PCB_REFERENCE_IMAGE_T )
1409 view()->Update( nextItem, KIGFX::GEOMETRY );
1410
1411 continue;
1412 }
1413 }
1414
1415 break; // finish
1416 }
1417 else if( evt->IsDblClick( BUT_LEFT ) )
1418 {
1419 // The first click will move the new item, so put it back
1420 if( moveIndividually )
1421 orig_items[itemIdx]->SetPosition( originalPos );
1422
1423 break; // finish
1424 }
1426 {
1427 angleSnapMode = GetAngleSnapMode();
1428 configureAngleSnap( angleSnapMode );
1429 displayConstraintsMessage( angleSnapMode );
1430 evt->SetPassEvent( true );
1431 }
1432 else if( evt->IsAction( &ACTIONS::increment ) )
1433 {
1434 if( evt->HasParameter() )
1435 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, evt->Parameter<ACTIONS::INCREMENT>() );
1436 else
1437 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, ACTIONS::INCREMENT { 1, 0 } );
1438 }
1445 || evt->IsAction( &ACTIONS::redo ) )
1446 {
1447 wxBell();
1448 }
1449 else
1450 {
1451 evt->SetPassEvent();
1452 }
1453
1454 } while( ( evt = Wait() ) ); // Assignment (instead of equality test) is intentional
1455
1456 // Clear temporary COURTYARD_CONFLICT flag and ensure the conflict shadow is cleared
1457 if( showCourtyardConflicts )
1458 drc_on_move->ClearConflicts( m_toolMgr->GetView() );
1459
1460 controls->ForceCursorPosition( false );
1461 controls->ShowCursor( false );
1462 controls->SetAutoPan( false );
1463
1464 m_dragging = false;
1465
1466 // Discard reference point when selection is "dropped" onto the board
1467 selection.ClearReferencePoint();
1468
1469 // Unselect all items to clear selection flags and then re-select the originally selected
1470 // items.
1471 m_toolMgr->RunAction( ACTIONS::selectionClear );
1472
1473 if( restore_state )
1474 {
1475 if( sel_items.size() == 1 && sel_items.back()->Type() == PCB_GENERATOR_T )
1476 {
1477 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genCancelEdit, aCommit,
1478 static_cast<PCB_GENERATOR*>( sel_items.back() ) );
1479 }
1480 }
1481 else
1482 {
1483 if( sel_items.size() == 1 && sel_items.back()->Type() == PCB_GENERATOR_T )
1484 {
1485 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genFinishEdit, aCommit,
1486 static_cast<PCB_GENERATOR*>( sel_items.back() ) );
1487 }
1488
1489 EDA_ITEMS oItems( orig_items.begin(), orig_items.end() );
1490 m_toolMgr->RunAction<EDA_ITEMS*>( ACTIONS::selectItems, &oItems );
1491 }
1492
1493 // Remove the dynamic ratsnest from the screen
1495
1496 editFrame->PopTool( pushedEvent );
1498
1499 return !restore_state;
1500}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:990
BOX2< VECTOR2D > BOX2D
Definition box2.h:923
@ CURSOR_RIGHT
Definition actions.h:311
@ CURSOR_LEFT
Definition actions.h:309
@ CURSOR_UP
Definition actions.h:305
@ CURSOR_DOWN
Definition actions.h:307
static TOOL_ACTION undo
Definition actions.h:75
static TOOL_ACTION duplicate
Definition actions.h:84
static TOOL_ACTION doDelete
Definition actions.h:85
static TOOL_ACTION cursorClick
Definition actions.h:180
static TOOL_ACTION redo
Definition actions.h:76
static TOOL_ACTION increment
Definition actions.h:94
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:224
static TOOL_ACTION cut
Definition actions.h:77
static TOOL_ACTION refreshPreview
Definition actions.h:159
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition actions.h:232
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
virtual void Revert() override
Revert the commit by restoring the modified items state.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
bool SetNetCode(int aNetCode, bool aNoAssert)
Set net using a net code.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition board_item.h:237
virtual void Move(const VECTOR2I &aMoveVector)
Move this object.
Definition board_item.h:344
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:285
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
constexpr const Vec & GetPosition() const
Definition box2.h:211
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:146
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:658
constexpr coord_type GetLeft() const
Definition box2.h:228
constexpr const Vec & GetOrigin() const
Definition box2.h:210
constexpr coord_type GetRight() const
Definition box2.h:217
constexpr const SizeVec & GetSize() const
Definition box2.h:206
constexpr coord_type GetTop() const
Definition box2.h:229
constexpr void Offset(coord_type dx, coord_type dy)
Definition box2.h:259
constexpr coord_type GetBottom() const
Definition box2.h:222
int GetCount() const
Return the number of objects in the list.
Definition collector.h:83
void Remove(int aIndex)
Remove the item at aIndex (first position is 0).
Definition collector.h:111
bool Empty() const
Definition commit.h:137
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:106
void DisplayConstraintsMsg(const wxString &msg)
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:99
virtual VECTOR2I GetPosition() const
Definition eda_item.h:278
virtual void SetPosition(const VECTOR2I &aPos)
Definition eda_item.h:279
wxString GetTypeDesc() const
Return a translated description of the type for this EDA_ITEM for display in user facing messages.
Definition eda_item.cpp:402
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition eda_item.h:148
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:111
bool IsSelected() const
Definition eda_item.h:128
EDA_ITEM * GetParent() const
Definition eda_item.h:113
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Definition eda_item.cpp:355
bool IsMoving() const
Definition eda_item.h:126
bool IsNew() const
Definition eda_item.h:125
bool isRouterActive() const
int SwapGateNets(const TOOL_EVENT &aEvent)
bool doMoveSelection(const TOOL_EVENT &aEvent, BOARD_COMMIT *aCommit, bool aAutoStart)
Rebuilds the ratsnest for operations that require it outside the commit rebuild.
int Swap(const TOOL_EVENT &aEvent)
Swap currently selected items' positions.
int PackAndMoveFootprints(const TOOL_EVENT &aEvent)
Try to fit selected footprints inside a minimal area and start movement.
bool pickReferencePoint(const wxString &aTooltip, const wxString &aSuccessMessage, const wxString &aCanceledMessage, VECTOR2I &aReferencePoint)
bool m_dragging
Definition edit_tool.h:242
int Move(const TOOL_EVENT &aEvent)
Main loop in which events are handled.
static const unsigned int COORDS_PADDING
Definition edit_tool.h:247
VECTOR2I getSafeMovement(const VECTOR2I &aMovement, const BOX2I &aSourceBBox, const VECTOR2D &aBBoxOffset)
int SwapPadNets(const TOOL_EVENT &aEvent)
Swap nets between selected pads and propagate to connected copper items (tracks, arcs,...
static void PadFilter(const VECTOR2I &, GENERAL_COLLECTOR &aCollector, PCB_SELECTION_TOOL *sTool)
A selection filter which prunes the selection to contain only items of type PCB_PAD_T.
VECTOR2I m_cursor
Definition edit_tool.h:243
void rebuildConnectivity()
PCB_SELECTION_TOOL * m_selectionTool
Definition edit_tool.h:241
static const TOOL_EVENT SelectedEvent
Definition actions.h:345
static const TOOL_EVENT SelectedItemsModified
Selected items were moved, this can be very high frequency on the canvas, use with care.
Definition actions.h:352
static const TOOL_EVENT SelectedItemsMoved
Used to inform tools that the selection should temporarily be non-editable.
Definition actions.h:355
EDA_ANGLE GetOrientation() const
Definition footprint.h:330
void SetOrientation(const EDA_ANGLE &aNewAngle)
const std::vector< FP_UNIT_INFO > & GetUnitInfo() const
Definition footprint.h:850
bool IsFlipped() const
Definition footprint.h:524
void Flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection) override
Flip this object, i.e.
const wxString & GetReference() const
Definition footprint.h:751
PAD * FindPadByNumber(const wxString &aPadNumber, PAD *aSearchAfterMe=nullptr) const
Return a PAD with a matching number.
Used when the right click button is pressed, or when the select tool is in effect.
Definition collectors.h:207
An interface for classes handling user events controlling the view behavior such as zooming,...
bool IsBOARD_ITEM() const
Definition view_item.h:102
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Definition pad.h:55
DISPLAY_OPTIONS m_Display
static TOOL_ACTION mirrorH
Mirroring of selected items.
static TOOL_ACTION genFinishEdit
static TOOL_ACTION hideLocalRatsnest
static TOOL_ACTION genStartEdit
static TOOL_ACTION moveWithReference
move with a reference point
static TOOL_ACTION angleSnapModeChanged
Notification event when angle mode changes.
static TOOL_ACTION moveExact
Activation of the exact move tool.
static TOOL_ACTION copyWithReference
copy command with manual reference point selection
static TOOL_ACTION genCancelEdit
static TOOL_ACTION genUpdateEdit
static TOOL_ACTION updateLocalRatsnest
static TOOL_ACTION moveIndividually
move items one-by-one
static TOOL_ACTION interactiveOffsetTool
static TOOL_ACTION positionRelative
static TOOL_ACTION skip
static TOOL_ACTION move
move or drag an item
static TOOL_ACTION mirrorV
static TOOL_ACTION flip
Flipping of selected objects.
static TOOL_ACTION rotateCw
Rotation of selected objects.
static TOOL_ACTION rotateCcw
Common, abstract interface for edit frames.
PCBNEW_SETTINGS * GetPcbNewSettings() const
virtual MAGNETIC_SETTINGS * GetMagneticItemsSettings()
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual PCB_LAYER_ID GetActiveLayer() const
BOARD * GetBoard() const
virtual void Update3DView(bool aMarkDirty, bool aRefresh, const wxString *aTitle=nullptr)
Update the 3D view, if the viewer is opened by this frame.
The selection tool: currently supports:
void FilterCollectorForMarkers(GENERAL_COLLECTOR &aCollector) const
Drop any PCB_MARKERs from the collector.
void FilterCollectorForFreePads(GENERAL_COLLECTOR &aCollector, bool aForcePromotion=false) const
Check the "allow free pads" setting and if disabled, replace any pads in the collector with their par...
void FilterCollectorForHierarchy(GENERAL_COLLECTOR &aCollector, bool aMultiselect) const
In general we don't want to select both a parent and any of it's children.
void FilterCollectorForLockedItems(GENERAL_COLLECTOR &aCollector)
In the PCB editor strip out any locked items unless the OverrideLocks checkbox is set.
void FilterCollectorForTableCells(GENERAL_COLLECTOR &aCollector) const
Promote any table cell selections to the whole table.
T * frame() const
KIGFX::PCB_VIEW * view() const
LEADER_MODE GetAngleSnapMode() const
Get the current angle snapping mode.
KIGFX::VIEW_CONTROLS * controls() const
BOARD * board() const
PCB_DRAW_PANEL_GAL * canvas() const
const PCB_SELECTION & selection() const
FOOTPRINT * footprint() const
virtual void PopTool(const TOOL_EVENT &aEvent)
Pops a tool from the stack.
bool GetMoveWarpsCursor() const
Indicate that a move operation should warp the mouse pointer to the origin of the move object.
virtual void PushTool(const TOOL_EVENT &aEvent)
NB: the definition of "tool" is different at the user level.
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:186
KIGFX::VIEW_CONTROLS * getViewControls() const
Return the instance of VIEW_CONTROLS object used in the application.
Definition tool_base.cpp:44
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
KIGFX::VIEW * getView() const
Returns the instance of #VIEW object used in the application.
Definition tool_base.cpp:38
Generic, UI-independent tool event.
Definition tool_event.h:171
bool DisableGridSnapping() const
Definition tool_event.h:371
bool HasParameter() const
Definition tool_event.h:464
bool IsCancelInteractive() const
Indicate the event should restart/end an ongoing interactive tool's event loop (eg esc key,...
bool IsActivate() const
Definition tool_event.h:345
COMMIT * Commit() const
Definition tool_event.h:283
bool IsClick(int aButtonMask=BUT_ANY) const
bool IsDrag(int aButtonMask=BUT_ANY) const
Definition tool_event.h:315
int Modifier(int aMask=MD_MODIFIER_MASK) const
Return information about key modifiers state (Ctrl, Alt, etc.).
Definition tool_event.h:366
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:473
bool IsDblClick(int aButtonMask=BUT_ANY) const
std::atomic< SYNCRONOUS_TOOL_STATE > * SynchronousState() const
Definition tool_event.h:280
void SetPassEvent(bool aPass=true)
Definition tool_event.h:256
bool IsMouseUp(int aButtonMask=BUT_ANY) const
Definition tool_event.h:325
bool IsMotion() const
Definition tool_event.h:330
TOOL_EVENT * Wait(const TOOL_EVENT_LIST &aEventList=TOOL_EVENT(TC_ANY, TA_ANY))
Suspend execution of the tool until an event specified in aEventList arrives.
void Activate()
Run the tool.
static bool IsZoneFillAction(const TOOL_EVENT *aEvent)
@ MOVING
Definition cursors.h:48
@ ARROW
Definition cursors.h:46
#define _(s)
@ RECURSE
Definition eda_item.h:52
#define IS_MOVING
Item being moved.
@ NONE
Definition eda_shape.h:69
static bool PromptConnectedPadDecision(PCB_BASE_EDIT_FRAME *aFrame, const std::vector< PAD * > &aPads, const wxString &aDialogTitle, bool &aIncludeConnectedPads)
@ FP_JUST_ADDED
Definition footprint.h:89
a few functions useful in geometry calculations.
LEADER_MODE
The kind of the leader line.
@ DEG45
45 Degree only
@ DIRECT
Unconstrained point-to-point.
@ DEG90
90 Degree only
GRID_HELPER_GRIDS
Definition grid_helper.h:44
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ TOP_BOTTOM
Flip top to bottom (around the X axis)
Definition mirror.h:29
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:55
wxPoint GetMousePosition()
Returns the mouse position in screen coordinates.
Definition wxgtk/ui.cpp:721
Class to handle a set of BOARD_ITEMs.
std::vector< EDA_ITEM * > EDA_ITEMS
void SpreadFootprints(std::vector< FOOTPRINT * > *aFootprints, VECTOR2I aTargetBoxPosition, bool aGroupBySheet, int aComponentGap, int aGroupGap)
Footprints (after loaded by reading a netlist for instance) are moved to be in a small free area (out...
@ STS_CANCELLED
Definition tool_event.h:164
@ STS_FINISHED
Definition tool_event.h:163
@ STS_RUNNING
Definition tool_event.h:162
@ MD_SHIFT
Definition tool_event.h:143
@ BUT_LEFT
Definition tool_event.h:132
@ BUT_RIGHT
Definition tool_event.h:133
#define kv
@ PCB_SHAPE_T
class PCB_SHAPE, a segment not on copper layers
Definition typeinfo.h:88
@ PCB_GENERATOR_T
class PCB_GENERATOR, generator on a layer
Definition typeinfo.h:91
@ PCB_VIA_T
class PCB_VIA, a via (like a track segment on a copper layer)
Definition typeinfo.h:97
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:111
@ PCB_REFERENCE_IMAGE_T
class PCB_REFERENCE_IMAGE, bitmap on a layer
Definition typeinfo.h:89
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86
@ PCB_PAD_T
class PAD, a pad in a footprint
Definition typeinfo.h:87
@ PCB_ARC_T
class PCB_ARC, an arc track segment on a copper layer
Definition typeinfo.h:98
@ PCB_TRACE_T
class PCB_TRACK, a track segment (segment on a copper layer)
Definition typeinfo.h:96
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694