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 if( moveIndividually )
956 {
957 orig_items.clear();
958
959 for( EDA_ITEM* item : selection.GetItemsSortedBySelectionOrder() )
960 {
961 if( item->IsBOARD_ITEM() )
962 orig_items.push_back( static_cast<BOARD_ITEM*>( item ) );
963 }
964
965 updateStatusPopup( orig_items[ itemIdx ], itemIdx + 1, orig_items.size() );
966 statusPopup->Popup();
967 statusPopup->Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, 20 ) );
968 canvas()->SetStatusPopup( statusPopup->GetPanel() );
969
970 m_selectionTool->ClearSelection();
971 m_selectionTool->AddItemToSel( orig_items[ itemIdx ] );
972
973 sel_items.clear();
974 sel_items.push_back( orig_items[ itemIdx ] );
975 }
976
977 bool restore_state = false;
978 VECTOR2I originalPos = originalCursorPos; // Initialize to current cursor position
979 VECTOR2D bboxMovement;
980 BOX2I originalBBox;
981 bool updateBBox = true;
982 LSET layers( { editFrame->GetActiveLayer() } );
984 TOOL_EVENT copy = aEvent;
985 TOOL_EVENT* evt = &copy;
986 VECTOR2I prevPos;
987 bool enableLocalRatsnest = true;
988
989 LEADER_MODE angleSnapMode = GetAngleSnapMode();
990 bool eatFirstMouseUp = true;
991 bool allowRedraw3D = cfg->m_Display.m_Live3DRefresh;
992 bool showCourtyardConflicts = !m_isFootprintEditor && cfg->m_ShowCourtyardCollisions;
993
994 // Axis locking for arrow key movement
995 enum class AXIS_LOCK { NONE, HORIZONTAL, VERTICAL };
996 AXIS_LOCK axisLock = AXIS_LOCK::NONE;
997 long lastArrowKeyAction = 0;
998
999 // Used to test courtyard overlaps
1000 std::unique_ptr<DRC_INTERACTIVE_COURTYARD_CLEARANCE> drc_on_move = nullptr;
1001
1002 if( showCourtyardConflicts )
1003 {
1004 std::shared_ptr<DRC_ENGINE> drcEngine = m_toolMgr->GetTool<DRC_TOOL>()->GetDRCEngine();
1005 drc_on_move.reset( new DRC_INTERACTIVE_COURTYARD_CLEARANCE( drcEngine ) );
1006 drc_on_move->Init( board );
1007 }
1008
1009 auto configureAngleSnap =
1010 [&]( LEADER_MODE aMode )
1011 {
1012 std::vector<VECTOR2I> directions;
1013
1014 switch( aMode )
1015 {
1016 case LEADER_MODE::DEG45:
1017 directions = { VECTOR2I( 1, 0 ), VECTOR2I( 0, 1 ), VECTOR2I( 1, 1 ), VECTOR2I( 1, -1 ) };
1018 break;
1019
1020 case LEADER_MODE::DEG90:
1021 directions = { VECTOR2I( 1, 0 ), VECTOR2I( 0, 1 ) };
1022 break;
1023
1024 default:
1025 break;
1026 }
1027
1028 grid.SetSnapLineDirections( directions );
1029
1030 if( directions.empty() )
1031 {
1032 grid.ClearSnapLine();
1033 }
1034 else
1035 {
1036 grid.SetSnapLineOrigin( originalPos );
1037 }
1038 };
1039
1040 configureAngleSnap( angleSnapMode );
1041 displayConstraintsMessage( angleSnapMode );
1042
1043 // Prime the pump
1044 m_toolMgr->PostAction( ACTIONS::refreshPreview );
1045
1046 // Main loop: keep receiving events
1047 do
1048 {
1049 VECTOR2I movement;
1051 grid.SetSnap( !evt->Modifier( MD_SHIFT ) );
1052 grid.SetUseGrid( getView()->GetGAL()->GetGridSnapping() && !evt->DisableGridSnapping() );
1053
1054 bool isSkip = evt->IsAction( &PCB_ACTIONS::skip ) && moveIndividually;
1055
1056 if( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) )
1057 eatFirstMouseUp = false;
1058
1059 if( evt->IsAction( &PCB_ACTIONS::move )
1060 || evt->IsMotion()
1061 || evt->IsDrag( BUT_LEFT )
1065 {
1066 if( m_dragging && ( evt->IsMotion()
1067 || evt->IsDrag( BUT_LEFT )
1068 || evt->IsAction( &ACTIONS::refreshPreview ) ) )
1069 {
1070 bool redraw3D = false;
1071
1072 GRID_HELPER_GRIDS selectionGrid = grid.GetSelectionGrid( selection );
1073
1074 if( controls->GetSettings().m_lastKeyboardCursorPositionValid )
1075 {
1076 VECTOR2I keyboardPos( controls->GetSettings().m_lastKeyboardCursorPosition );
1077
1078 grid.SetSnap( false );
1079
1080 // Use the keyboard position directly without grid alignment. The position
1081 // was already calculated correctly in CursorControl by adding the grid step
1082 // to the current position. Aligning to grid here would snap to the nearest
1083 // grid point, which causes precision errors when the original position is
1084 // not on a grid point (issue #22805).
1085 m_cursor = keyboardPos;
1086
1087 // Update axis lock based on arrow key press, but skip on refreshPreview
1088 // to avoid double-processing when CursorControl posts refreshPreview after
1089 // handling the arrow key.
1090 if( !evt->IsAction( &ACTIONS::refreshPreview ) )
1091 {
1092 long action = controls->GetSettings().m_lastKeyboardCursorCommand;
1093
1094 if( action == ACTIONS::CURSOR_LEFT || action == ACTIONS::CURSOR_RIGHT )
1095 {
1096 if( axisLock == AXIS_LOCK::HORIZONTAL )
1097 {
1098 // Check if opposite horizontal key pressed to unlock
1099 if( ( lastArrowKeyAction == ACTIONS::CURSOR_LEFT && action == ACTIONS::CURSOR_RIGHT ) ||
1100 ( lastArrowKeyAction == ACTIONS::CURSOR_RIGHT && action == ACTIONS::CURSOR_LEFT ) )
1101 {
1102 axisLock = AXIS_LOCK::NONE;
1103 }
1104 // Same direction axis, keep locked
1105 }
1106 else
1107 {
1108 axisLock = AXIS_LOCK::HORIZONTAL;
1109 }
1110 }
1111 else if( action == ACTIONS::CURSOR_UP || action == ACTIONS::CURSOR_DOWN )
1112 {
1113 if( axisLock == AXIS_LOCK::VERTICAL )
1114 {
1115 // Check if opposite vertical key pressed to unlock
1116 if( ( lastArrowKeyAction == ACTIONS::CURSOR_UP && action == ACTIONS::CURSOR_DOWN ) ||
1117 ( lastArrowKeyAction == ACTIONS::CURSOR_DOWN && action == ACTIONS::CURSOR_UP ) )
1118 {
1119 axisLock = AXIS_LOCK::NONE;
1120 }
1121 // Same direction axis, keep locked
1122 }
1123 else
1124 {
1125 axisLock = AXIS_LOCK::VERTICAL;
1126 }
1127 }
1128
1129 lastArrowKeyAction = action;
1130 }
1131 }
1132 else
1133 {
1134 VECTOR2I mousePos( controls->GetMousePosition() );
1135
1136 m_cursor = grid.BestSnapAnchor( mousePos, layers, selectionGrid, sel_items );
1137 }
1138
1139 if( axisLock == AXIS_LOCK::HORIZONTAL )
1140 m_cursor.y = prevPos.y;
1141 else if( axisLock == AXIS_LOCK::VERTICAL )
1142 m_cursor.x = prevPos.x;
1143
1144 if( !selection.HasReferencePoint() )
1145 originalPos = m_cursor;
1146
1147 if( updateBBox )
1148 {
1149 originalBBox = BOX2I();
1150 bboxMovement = VECTOR2D();
1151
1152 for( EDA_ITEM* item : sel_items )
1153 originalBBox.Merge( item->ViewBBox() );
1154
1155 updateBBox = false;
1156 }
1157
1158 // Constrain selection bounding box to coordinates limits
1159 movement = getSafeMovement( m_cursor - prevPos, originalBBox, bboxMovement );
1160
1161 // Apply constrained movement
1162 m_cursor = prevPos + movement;
1163
1164 controls->ForceCursorPosition( true, m_cursor );
1165 selection.SetReferencePoint( m_cursor );
1166
1167 prevPos = m_cursor;
1168 bboxMovement += movement;
1169
1170 // Drag items to the current cursor position
1171 for( BOARD_ITEM* item : sel_items )
1172 {
1173 // Don't double move child items.
1174 if( !item->GetParent() || !item->GetParent()->IsSelected() )
1175 {
1176 item->Move( movement );
1177
1178 // Images are on non-cached layers and will not be updated automatically in the overlay, so
1179 // explicitly tell the view they've moved.
1180 if( item->Type() == PCB_REFERENCE_IMAGE_T )
1181 view()->Update( item, KIGFX::GEOMETRY );
1182 }
1183
1184 if( item->Type() == PCB_GENERATOR_T && sel_items.size() == 1 )
1185 {
1186 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genUpdateEdit, aCommit,
1187 static_cast<PCB_GENERATOR*>( item ) );
1188 }
1189
1190 if( item->Type() == PCB_FOOTPRINT_T )
1191 redraw3D = true;
1192 }
1193
1194 if( redraw3D && allowRedraw3D )
1195 editFrame->Update3DView( false, true );
1196
1197 if( showCourtyardConflicts && drc_on_move->m_FpInMove.size() )
1198 {
1199 drc_on_move->Run();
1200 drc_on_move->UpdateConflicts( m_toolMgr->GetView(), true );
1201 }
1202
1204 }
1205 else if( !m_dragging && ( aAutoStart || !evt->IsAction( &ACTIONS::refreshPreview ) ) )
1206 {
1207 // Prepare to start dragging
1208 editFrame->HideSolderMask();
1209
1210 m_dragging = true;
1211
1212 for( BOARD_ITEM* item : sel_items )
1213 {
1214 if( item->GetParent() && item->GetParent()->IsSelected() )
1215 continue;
1216
1217 if( !item->IsNew() && !item->IsMoving() )
1218 {
1219 if( item->Type() == PCB_GENERATOR_T && sel_items.size() == 1 )
1220 {
1221 enableLocalRatsnest = false;
1222
1223 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genStartEdit, aCommit,
1224 static_cast<PCB_GENERATOR*>( item ) );
1225 }
1226 else
1227 {
1228 aCommit->Modify( item, nullptr, RECURSE_MODE::RECURSE );
1229 }
1230
1231 item->SetFlags( IS_MOVING );
1232
1233 if( item->Type() == PCB_SHAPE_T )
1234 static_cast<PCB_SHAPE*>( item )->UpdateHatching();
1235
1236 item->RunOnChildren(
1237 [&]( BOARD_ITEM* child )
1238 {
1239 child->SetFlags( IS_MOVING );
1240
1241 if( child->Type() == PCB_SHAPE_T )
1242 static_cast<PCB_SHAPE*>( child )->UpdateHatching();
1243 },
1245 }
1246 }
1247
1248 m_cursor = controls->GetCursorPosition();
1249
1250 if( selection.HasReferencePoint() )
1251 {
1252 // start moving with the reference point attached to the cursor
1253 grid.SetAuxAxes( false );
1254
1255 movement = m_cursor - selection.GetReferencePoint();
1256
1257 // Drag items to the current cursor position
1258 for( EDA_ITEM* item : selection )
1259 {
1260 if( !item->IsBOARD_ITEM() )
1261 continue;
1262
1263 // Don't double move footprint pads, fields, etc.
1264 if( item->GetParent() && item->GetParent()->IsSelected() )
1265 continue;
1266
1267 BOARD_ITEM* boardItem = static_cast<BOARD_ITEM*>( item );
1268 boardItem->Move( movement );
1269
1270 // Images are on non-cached layers and will not be updated automatically in the overlay, so
1271 // explicitly tell the view they've moved.
1272 if( boardItem->Type() == PCB_REFERENCE_IMAGE_T )
1273 view()->Update( boardItem, KIGFX::GEOMETRY );
1274 }
1275
1276 selection.SetReferencePoint( m_cursor );
1277 }
1278 else
1279 {
1280 if( showCourtyardConflicts )
1281 {
1282 std::vector<FOOTPRINT*>& FPs = drc_on_move->m_FpInMove;
1283
1284 for( BOARD_ITEM* item : sel_items )
1285 {
1286 if( item->Type() == PCB_FOOTPRINT_T )
1287 FPs.push_back( static_cast<FOOTPRINT*>( item ) );
1288
1289 item->RunOnChildren(
1290 [&]( BOARD_ITEM* child )
1291 {
1292 if( child->Type() == PCB_FOOTPRINT_T )
1293 FPs.push_back( static_cast<FOOTPRINT*>( child ) );
1294 },
1296 }
1297 }
1298
1299 // Use the mouse position over cursor, as otherwise large grids will allow only
1300 // snapping to items that are closest to grid points
1301 m_cursor = grid.BestDragOrigin( originalMousePos, sel_items, grid.GetSelectionGrid( selection ),
1302 &m_selectionTool->GetFilter() );
1303
1304 // Set the current cursor position to the first dragged item origin, so the
1305 // movement vector could be computed later
1306 if( moveWithReference )
1307 {
1308 selection.SetReferencePoint( pickedReferencePoint );
1309
1310 if( angleSnapMode != LEADER_MODE::DIRECT )
1311 grid.SetSnapLineOrigin( selection.GetReferencePoint() );
1312
1313 controls->ForceCursorPosition( true, pickedReferencePoint );
1314 m_cursor = pickedReferencePoint;
1315 }
1316 else
1317 {
1318 VECTOR2I dragOrigin = m_cursor;
1319
1320 selection.SetReferencePoint( dragOrigin );
1321
1322 if( angleSnapMode != LEADER_MODE::DIRECT )
1323 grid.SetSnapLineOrigin( dragOrigin );
1324
1325 grid.SetAuxAxes( true, dragOrigin );
1326
1327 if( !editFrame->GetMoveWarpsCursor() )
1328 m_cursor = originalCursorPos;
1329 else
1330 m_cursor = dragOrigin;
1331 }
1332
1333 originalPos = selection.GetReferencePoint();
1334 }
1335
1336 // Update variables for bounding box collision calculations
1337 updateBBox = true;
1338
1339 controls->SetCursorPosition( m_cursor, false );
1340
1341 prevPos = m_cursor;
1342 controls->SetAutoPan( true );
1344 }
1345
1346 if( statusPopup )
1347 statusPopup->Move( KIPLATFORM::UI::GetMousePosition() + wxPoint( 20, 20 ) );
1348
1349 if( enableLocalRatsnest )
1350 m_toolMgr->PostAction( PCB_ACTIONS::updateLocalRatsnest, movement );
1351 }
1352 else if( evt->IsCancelInteractive() || evt->IsActivate() )
1353 {
1354 if( m_dragging && evt->IsCancelInteractive() )
1355 evt->SetPassEvent( false );
1356
1357 restore_state = true; // Canceling the tool means that items have to be restored
1358 break; // Finish
1359 }
1360 else if( evt->IsClick( BUT_RIGHT ) )
1361 {
1362 m_selectionTool->GetToolMenu().ShowContextMenu( selection );
1363 }
1364 else if( evt->IsAction( &ACTIONS::undo ) )
1365 {
1366 restore_state = true; // Perform undo locally
1367 break; // Finish
1368 }
1369 else if( evt->IsAction( &ACTIONS::doDelete ) )
1370 {
1371 evt->SetPassEvent();
1372 // Exit on a delete; there will no longer be anything to drag.
1373 break;
1374 }
1375 else if( evt->IsAction( &ACTIONS::duplicate ) && evt != &copy )
1376 {
1377 wxBell();
1378 }
1379 else if( evt->IsAction( &ACTIONS::cut ) )
1380 {
1381 wxBell();
1382 }
1383 else if( evt->IsAction( &PCB_ACTIONS::rotateCw )
1385 || evt->IsAction( &PCB_ACTIONS::flip )
1386 || evt->IsAction( &PCB_ACTIONS::mirrorH )
1387 || evt->IsAction( &PCB_ACTIONS::mirrorV ) )
1388 {
1389 updateBBox = true;
1390 eatFirstMouseUp = false;
1391 evt->SetPassEvent();
1392 }
1393 else if( evt->IsMouseUp( BUT_LEFT ) || evt->IsClick( BUT_LEFT ) || isSkip )
1394 {
1395 // Eat mouse-up/-click events that leaked through from the lock dialog
1396 if( eatFirstMouseUp && !evt->IsAction( &ACTIONS::cursorClick ) )
1397 {
1398 eatFirstMouseUp = false;
1399 continue;
1400 }
1401 else if( moveIndividually && m_dragging )
1402 {
1403 // Put skipped items back where they started
1404 if( isSkip )
1405 orig_items[itemIdx]->SetPosition( originalPos );
1406
1407 view()->Update( orig_items[itemIdx] );
1409
1410 if( ++itemIdx < orig_items.size() )
1411 {
1412 BOARD_ITEM* nextItem = orig_items[itemIdx];
1413
1414 m_selectionTool->ClearSelection();
1415
1416 originalPos = nextItem->GetPosition();
1417 m_selectionTool->AddItemToSel( nextItem );
1418 selection.SetReferencePoint( originalPos );
1419 if( angleSnapMode != LEADER_MODE::DIRECT )
1420 grid.SetSnapLineOrigin( selection.GetReferencePoint() );
1421
1422 sel_items.clear();
1423 sel_items.push_back( nextItem );
1424 updateStatusPopup( nextItem, itemIdx + 1, orig_items.size() );
1425
1426 // Pick up new item
1427 aCommit->Modify( nextItem, nullptr, RECURSE_MODE::RECURSE );
1428 nextItem->Move( controls->GetCursorPosition( true ) - nextItem->GetPosition() );
1429
1430 // Images are on non-cached layers and will not be updated automatically in the overlay, so
1431 // explicitly tell the view they've moved.
1432 if( nextItem->Type() == PCB_REFERENCE_IMAGE_T )
1433 view()->Update( nextItem, KIGFX::GEOMETRY );
1434
1435 continue;
1436 }
1437 }
1438
1439 break; // finish
1440 }
1441 else if( evt->IsDblClick( BUT_LEFT ) )
1442 {
1443 // The first click will move the new item, so put it back
1444 if( moveIndividually )
1445 orig_items[itemIdx]->SetPosition( originalPos );
1446
1447 break; // finish
1448 }
1450 {
1451 angleSnapMode = GetAngleSnapMode();
1452 configureAngleSnap( angleSnapMode );
1453 displayConstraintsMessage( angleSnapMode );
1454 evt->SetPassEvent( true );
1455 }
1456 else if( evt->IsAction( &ACTIONS::increment ) )
1457 {
1458 if( evt->HasParameter() )
1459 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, evt->Parameter<ACTIONS::INCREMENT>() );
1460 else
1461 m_toolMgr->RunSynchronousAction( ACTIONS::increment, aCommit, ACTIONS::INCREMENT { 1, 0 } );
1462 }
1469 || evt->IsAction( &ACTIONS::redo ) )
1470 {
1471 wxBell();
1472 }
1473 else
1474 {
1475 evt->SetPassEvent();
1476 }
1477
1478 } while( ( evt = Wait() ) ); // Assignment (instead of equality test) is intentional
1479
1480 // Clear temporary COURTYARD_CONFLICT flag and ensure the conflict shadow is cleared
1481 if( showCourtyardConflicts )
1482 drc_on_move->ClearConflicts( m_toolMgr->GetView() );
1483
1484 controls->ForceCursorPosition( false );
1485 controls->ShowCursor( false );
1486 controls->SetAutoPan( false );
1487
1488 m_dragging = false;
1489
1490 // Discard reference point when selection is "dropped" onto the board
1491 selection.ClearReferencePoint();
1492
1493 // Unselect all items to clear selection flags and then re-select the originally selected
1494 // items.
1495 m_toolMgr->RunAction( ACTIONS::selectionClear );
1496
1497 if( restore_state )
1498 {
1499 if( sel_items.size() == 1 && sel_items.back()->Type() == PCB_GENERATOR_T )
1500 {
1501 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genCancelEdit, aCommit,
1502 static_cast<PCB_GENERATOR*>( sel_items.back() ) );
1503 }
1504 }
1505 else
1506 {
1507 if( sel_items.size() == 1 && sel_items.back()->Type() == PCB_GENERATOR_T )
1508 {
1509 m_toolMgr->RunSynchronousAction( PCB_ACTIONS::genFinishEdit, aCommit,
1510 static_cast<PCB_GENERATOR*>( sel_items.back() ) );
1511 }
1512
1513 EDA_ITEMS oItems( orig_items.begin(), orig_items.end() );
1514 m_toolMgr->RunAction<EDA_ITEMS*>( ACTIONS::selectItems, &oItems );
1515 }
1516
1517 // Remove the dynamic ratsnest from the screen
1519
1520 editFrame->PopTool( pushedEvent );
1522
1523 return !restore_state;
1524}
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:279
virtual void SetPosition(const VECTOR2I &aPos)
Definition eda_item.h:280
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:149
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:112
bool IsSelected() const
Definition eda_item.h:129
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:355
bool IsMoving() const
Definition eda_item.h:127
bool IsNew() const
Definition eda_item.h:126
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: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:55
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:1573
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: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: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