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