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