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