KiCad PCB EDA Suite
Loading...
Searching...
No Matches
multichannel_tool.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24
25#include <board_commit.h>
26#include <tools/pcb_actions.h>
27#include <tools/pcb_selection.h>
28
31
32#include "multichannel_tool.h"
33
34#include <pcbexpr_evaluator.h>
35
36#include <zone.h>
39#include <pcb_group.h>
40#include <footprint.h>
41#include <pad.h>
42#include <pcb_text.h>
46#include <algorithm>
48#include <pcb_track.h>
49#include <tool/tool_manager.h>
51#include <chrono>
52#include <core/profile.h>
53#include <thread_pool.h>
55#include <string_utils.h>
56#include <wx/log.h>
57#include <wx/richmsgdlg.h>
58#include <pgm_base.h>
59
60
61#define MULTICHANNEL_EXTRA_DEBUG
62
63static const wxString traceMultichannelTool = wxT( "MULTICHANNEL_TOOL" );
64
65
66static wxString FormatComponentList( const std::set<FOOTPRINT*>& aComponents )
67{
68 std::vector<wxString> refs;
69
70 for( FOOTPRINT* fp : aComponents )
71 {
72 if( !fp )
73 continue;
74
75 refs.push_back( fp->GetReferenceAsString() );
76 }
77
78 std::sort( refs.begin(), refs.end(),
79 []( const wxString& aLhs, const wxString& aRhs )
80 {
81 return aLhs.CmpNoCase( aRhs ) < 0;
82 } );
83
84 if( refs.empty() )
85 return _( "(none)" );
86
87 wxString result;
88 wxString line;
89 size_t componentsOnLine = 0;
90
91 for( const wxString& ref : refs )
92 {
93 if( componentsOnLine == 10 )
94 {
95 if( !result.IsEmpty() )
96 result += wxT( "\n" );
97
98 result += line;
99 line.clear();
100 componentsOnLine = 0;
101 }
102
103 AccumulateDescription( line, ref );
104 componentsOnLine++;
105 }
106
107 if( !line.IsEmpty() )
108 {
109 if( !result.IsEmpty() )
110 result += wxT( "\n" );
111
112 result += line;
113 }
114
115 return result;
116}
117
118
119static void ShowTopologyMismatchReasons( wxWindow* aParent, const wxString& aSummary,
120 const std::vector<wxString>& aReasons )
121{
122 if( !aParent || aReasons.empty() )
123 return;
124
125 wxString reasonText;
126
127 for( size_t idx = 0; idx < aReasons.size(); ++idx )
128 {
129 if( idx > 0 )
130 reasonText += wxT( "\n" );
131
132 reasonText += aReasons[idx];
133 }
134
135 wxRichMessageDialog dlg( aParent, aSummary, _( "Topology mismatch" ), wxICON_ERROR | wxOK );
136 dlg.ShowDetailedText( reasonText );
137 dlg.ShowModal();
138}
139
140
142{
143}
144
145
150
151void MULTICHANNEL_TOOL::ShowMismatchDetails( wxWindow* aParent, const wxString& aSummary,
152 const std::vector<wxString>& aReasons ) const
153{
154 wxWindow* parent = aParent ? aParent : frame();
155 ShowTopologyMismatchReasons( parent, aSummary, aReasons );
156}
157
158
164
165
167 std::set<FOOTPRINT*>& aComponents )
168{
169 if( !aRuleArea || !aRuleArea->m_zone )
170 return false;
171
172 // When we're copying the layout of a design block, we are provided an exact list of items
173 // rather than querying the board for items that are inside the area.
175 {
176 // Get all board connected items that are from the design bloc
177 for( EDA_ITEM* item : aRuleArea->m_designBlockItems )
178 {
179 if( item->Type() == PCB_FOOTPRINT_T )
180 aComponents.insert( static_cast<FOOTPRINT*>( item ) );
181 }
182
183 return (int) aComponents.size();
184 }
185
186
188 PCBEXPR_UCODE ucode;
189 PCBEXPR_CONTEXT ctx, preflightCtx;
190
191 auto reportError =
192 [&]( const wxString& aMessage, int aOffset )
193 {
194 wxLogTrace( traceMultichannelTool, wxT( "ERROR: %s"), aMessage );
195 };
196
197 ctx.SetErrorCallback( reportError );
198 preflightCtx.SetErrorCallback( reportError );
199 compiler.SetErrorCallback( reportError );
200 //compiler.SetDebugReporter( m_reporter );
201
202 wxLogTrace( traceMultichannelTool, wxT( "rule area '%s'" ), aRuleArea->m_zone->GetZoneName() );
203
204 wxString ruleText;
205
206 switch( aRuleArea->m_zone->GetPlacementAreaSourceType() )
207 {
209 ruleText = wxT( "A.memberOfSheetOrChildren('" ) + aRuleArea->m_zone->GetPlacementAreaSource() + wxT( "')" );
210 break;
212 ruleText = wxT( "A.hasComponentClass('" ) + aRuleArea->m_zone->GetPlacementAreaSource() + wxT( "')" );
213 break;
215 ruleText = wxT( "A.memberOfGroup('" ) + aRuleArea->m_zone->GetPlacementAreaSource() + wxT( "')" );
216 break;
218 // For design blocks, handled above outside the rules system
219 break;
220 }
221
222 auto ok = compiler.Compile( ruleText, &ucode, &preflightCtx );
223
224 if( !ok )
225 return false;
226
227 for( FOOTPRINT* fp : board()->Footprints() )
228 {
229 ctx.SetItems( fp, fp );
230 LIBEVAL::VALUE* val = ucode.Run( &ctx );
231
232 if( val->AsDouble() != 0.0 )
233 {
234 wxLogTrace( traceMultichannelTool, wxT( " - %s [sheet %s]" ),
235 fp->GetReference(),
236 fp->GetSheetname() );
237
238 aComponents.insert( fp );
239 }
240 }
241
242 return true;
243}
244
245
246bool MULTICHANNEL_TOOL::findOtherItemsInRuleArea( RULE_AREA* aRuleArea, std::set<BOARD_ITEM*>& aItems )
247{
248 if( !aRuleArea || !aRuleArea->m_zone )
249 return false;
250
251 // When we're copying the layout of a design block, we are provided an exact list of items
252 // rather than querying the board for items that are inside the area.
254 {
255 // Get all board items that aren't footprints. Connected items are usually handled by the
256 // routing path, except zones which are copied via "other items".
257 for( EDA_ITEM* item : aRuleArea->m_designBlockItems )
258 {
259 if( item->Type() == PCB_FOOTPRINT_T )
260 continue;
261
262 if( BOARD_ITEM* boardItem = dynamic_cast<BOARD_ITEM*>( item ) )
263 {
264 if( !boardItem->IsConnected() || boardItem->Type() == PCB_ZONE_T )
265 aItems.insert( boardItem );
266 }
267 }
268
269 return aItems.size() > 0;
270 }
271
273 PCBEXPR_UCODE ucode;
274 PCBEXPR_CONTEXT ctx, preflightCtx;
275
276 auto reportError =
277 [&]( const wxString& aMessage, int aOffset )
278 {
279 wxLogTrace( traceMultichannelTool, wxT( "ERROR: %s"), aMessage );
280 };
281
282 ctx.SetErrorCallback( reportError );
283 preflightCtx.SetErrorCallback( reportError );
284 compiler.SetErrorCallback( reportError );
285
286 // Use the zone's UUID to identify it uniquely. Using the zone name could match other zones
287 // with the same name (e.g., a copper fill zone with the same name as a rule area).
288 wxString ruleText = wxString::Format( wxT( "A.enclosedByArea('%s')" ),
289 aRuleArea->m_zone->m_Uuid.AsString() );
290
291 if( !compiler.Compile( ruleText, &ucode, &preflightCtx ) )
292 return false;
293
294 auto testAndAdd =
295 [&]( BOARD_ITEM* aItem )
296 {
297 ctx.SetItems( aItem, aItem );
298 auto val = ucode.Run( &ctx );
299
300 if( val->AsDouble() != 0.0 )
301 aItems.insert( aItem );
302 };
303
304 for( ZONE* zone : board()->Zones() )
305 {
306 if( zone == aRuleArea->m_zone )
307 continue;
308
309 testAndAdd( zone );
310 }
311
312 for( BOARD_ITEM* drawing : board()->Drawings() )
313 {
314 if( !drawing->IsConnected() )
315 testAndAdd( drawing );
316 }
317
318 return true;
319}
320
321
322std::set<FOOTPRINT*> MULTICHANNEL_TOOL::queryComponentsInSheet( wxString aSheetName ) const
323{
324 std::set<FOOTPRINT*> rv;
325
326 if( aSheetName.EndsWith( wxT( "/" ) ) )
327 aSheetName.RemoveLast();
328
329 wxString childPrefix = aSheetName + wxT( "/" );
330
331 for( FOOTPRINT* fp : board()->Footprints() )
332 {
333 auto sn = fp->GetSheetname();
334
335 if( sn.EndsWith( wxT( "/" ) ) )
336 sn.RemoveLast();
337
338 if( sn == aSheetName || sn.StartsWith( childPrefix ) )
339 rv.insert( fp );
340 }
341
342 return rv;
343}
344
345
346std::set<FOOTPRINT*>
347MULTICHANNEL_TOOL::queryComponentsInComponentClass( const wxString& aComponentClassName ) const
348{
349 std::set<FOOTPRINT*> rv;
350
351 for( FOOTPRINT* fp : board()->Footprints() )
352 {
353 if( fp->GetComponentClass()->ContainsClassName( aComponentClassName ) )
354 rv.insert( fp );
355 }
356
357 return rv;
358}
359
360
361std::set<FOOTPRINT*> MULTICHANNEL_TOOL::queryComponentsInGroup( const wxString& aGroupName ) const
362{
363 std::set<FOOTPRINT*> rv;
364
365 for( PCB_GROUP* group : board()->Groups() )
366 {
367 if( group->GetName() == aGroupName )
368 {
369 for( EDA_ITEM* item : group->GetItems() )
370 {
371 if( item->Type() == PCB_FOOTPRINT_T )
372 rv.insert( static_cast<FOOTPRINT*>( item ) );
373 }
374 }
375 }
376
377 return rv;
378}
379
380
381std::set<BOARD_ITEM*> MULTICHANNEL_TOOL::queryBoardItemsInGroup( const wxString& aGroupName ) const
382{
383 std::set<BOARD_ITEM*> rv;
384
385 for( PCB_GROUP* group : board()->Groups() )
386 {
387 if( group->GetName() != aGroupName )
388 continue;
389
390 for( EDA_ITEM* item : group->GetItems() )
391 {
392 if( item->IsBOARD_ITEM() )
393 rv.insert( static_cast<BOARD_ITEM*>( item ) );
394 }
395 }
396
397 return rv;
398}
399
400
401const SHAPE_LINE_CHAIN MULTICHANNEL_TOOL::buildRAOutline( std::set<FOOTPRINT*>& aFootprints, int aMargin )
402{
403 std::vector<VECTOR2I> bbCorners;
404 bbCorners.reserve( aFootprints.size() * 4 );
405
406 for( FOOTPRINT* fp : aFootprints )
407 {
408 const BOX2I bb = fp->GetBoundingBox( false ).GetInflated( aMargin );
409 KIGEOM::CollectBoxCorners( bb, bbCorners );
410 }
411
412 std::vector<VECTOR2I> hullVertices;
413 BuildConvexHull( hullVertices, bbCorners );
414
415 SHAPE_LINE_CHAIN hull( hullVertices );
416
417 // Make the newly computed convex hull use only 90 degree segments
418 return KIGEOM::RectifyPolygon( hull );
419}
420
421const SHAPE_LINE_CHAIN MULTICHANNEL_TOOL::buildRAOutline( const std::set<BOARD_ITEM*>& aItems, int aMargin )
422{
423 std::vector<VECTOR2I> bbCorners;
424 bbCorners.reserve( aItems.size() * 4 );
425
426 for( BOARD_ITEM* item : aItems )
427 {
428 BOX2I bb = item->GetBoundingBox();
429
430 if( item->Type() == PCB_FOOTPRINT_T )
431 bb = static_cast<FOOTPRINT*>( item )->GetBoundingBox( false );
432
433 KIGEOM::CollectBoxCorners( bb.GetInflated( aMargin ), bbCorners );
434 }
435
436 std::vector<VECTOR2I> hullVertices;
437 BuildConvexHull( hullVertices, bbCorners );
438
439 SHAPE_LINE_CHAIN hull( hullVertices );
440
441 // Make the newly computed convex hull use only 90 degree segments
442 return KIGEOM::RectifyPolygon( hull );
443}
444
445
447{
448 using PathAndName = std::pair<wxString, wxString>;
449 std::set<PathAndName> uniqueSheets;
450 std::set<wxString> uniqueComponentClasses;
451 std::set<wxString> uniqueGroups;
452
453 m_areas.m_areas.clear();
454
455 for( const FOOTPRINT* fp : board()->Footprints() )
456 {
457 uniqueSheets.insert( PathAndName( fp->GetSheetname(), fp->GetSheetfile() ) );
458
459 const COMPONENT_CLASS* compClass = fp->GetComponentClass();
460
461 for( const COMPONENT_CLASS* singleClass : compClass->GetConstituentClasses() )
462 uniqueComponentClasses.insert( singleClass->GetName() );
463
464 if( fp->GetParentGroup() && !fp->GetParentGroup()->GetName().IsEmpty() )
465 uniqueGroups.insert( fp->GetParentGroup()->GetName() );
466 }
467
468 for( const PathAndName& sheet : uniqueSheets )
469 {
470 RULE_AREA ent;
471
473 ent.m_generateEnabled = false;
474 ent.m_sheetPath = sheet.first;
475 ent.m_sheetName = sheet.second;
477 m_areas.m_areas.push_back( ent );
478
479 wxLogTrace( traceMultichannelTool, wxT("found sheet '%s' @ '%s' s %d\n"),
480 ent.m_sheetName,
481 ent.m_sheetPath,
482 (int) m_areas.m_areas.size() );
483 }
484
485 for( const wxString& compClass : uniqueComponentClasses )
486 {
487 RULE_AREA ent;
488
490 ent.m_generateEnabled = false;
491 ent.m_componentClass = compClass;
493 m_areas.m_areas.push_back( ent );
494
495 wxLogTrace( traceMultichannelTool, wxT( "found component class '%s' s %d\n" ),
497 static_cast<int>( m_areas.m_areas.size() ) );
498 }
499
500 for( const wxString& groupName : uniqueGroups )
501 {
502 RULE_AREA ent;
503
505 ent.m_generateEnabled = false;
506 ent.m_groupName = groupName;
508 m_areas.m_areas.push_back( ent );
509
510 wxLogTrace( traceMultichannelTool, wxT( "found group '%s' s %d\n" ),
512 static_cast<int>( m_areas.m_areas.size() ) );
513 }
514}
515
516
518{
519 m_areas.m_areas.clear();
520
521 for( ZONE* zone : board()->Zones() )
522 {
523 if( !zone->GetIsRuleArea() )
524 continue;
525
526 if( !zone->GetPlacementAreaEnabled() )
527 continue;
528
529 RULE_AREA area;
530
531 area.m_existsAlready = true;
532 area.m_zone = zone;
533 area.m_ruleName = zone->GetZoneName();
534 area.m_center = zone->Outline()->COutline( 0 ).Centre();
535
537
538 m_areas.m_areas.push_back( area );
539
540 wxLogTrace( traceMultichannelTool, wxT( "RA '%s', %d footprints\n" ), area.m_ruleName,
541 (int) area.m_components.size() );
542 }
543
544 wxLogTrace( traceMultichannelTool, wxT( "Total RAs found: %d\n" ), (int) m_areas.m_areas.size() );
545}
546
547
549{
550 for( RULE_AREA& ra : m_areas.m_areas )
551 {
552 if( ra.m_ruleName == aName )
553 return &ra;
554 }
555
556 return nullptr;
557}
558
559
561{
563}
564
565
567{
568 std::vector<ZONE*> refRAs;
569
570 auto isSelectedItemAnRA =
571 []( EDA_ITEM* aItem ) -> ZONE*
572 {
573 if( !aItem || aItem->Type() != PCB_ZONE_T )
574 return nullptr;
575
576 ZONE* zone = static_cast<ZONE*>( aItem );
577
578 if( !zone->GetIsRuleArea() )
579 return nullptr;
580
581 if( !zone->GetPlacementAreaEnabled() )
582 return nullptr;
583
584 return zone;
585 };
586
587 for( EDA_ITEM* item : selection() )
588 {
589 if( ZONE* zone = isSelectedItemAnRA( item ) )
590 {
591 refRAs.push_back( zone );
592 }
593 else if( item->Type() == PCB_GROUP_T )
594 {
595 PCB_GROUP *group = static_cast<PCB_GROUP*>( item );
596
597 for( EDA_ITEM* grpItem : group->GetItems() )
598 {
599 if( ZONE* grpZone = isSelectedItemAnRA( grpItem ) )
600 refRAs.push_back( grpZone );
601 }
602 }
603 }
604
605 if( refRAs.size() != 1 )
606 {
609 this,
610 _( "Select a reference Rule Area to copy from..." ),
611 [&]( EDA_ITEM* aItem )
612 {
613 return isSelectedItemAnRA( aItem ) != nullptr;
614 }
615 } );
616
617 return 0;
618 }
619
621
622 int status = CheckRACompatibility( refRAs.front() );
623
624 if( status < 0 )
625 return status;
626
627 if( m_areas.m_areas.size() <= 1 )
628 {
629 frame()->ShowInfoBarError( _( "No Rule Areas to repeat layout to have been found." ), true );
630 return 0;
631 }
632
634 int ret = dialog.ShowModal();
635
636 if( ret != wxID_OK )
637 return 0;
638
639 return RepeatLayout( aEvent, refRAs.front() );
640}
641
642
644{
645 m_areas.m_refRA = nullptr;
646
647 for( RULE_AREA& ra : m_areas.m_areas )
648 {
649 if( ra.m_zone == aRefZone )
650 {
651 m_areas.m_refRA = &ra;
652 break;
653 }
654 }
655
656 if( !m_areas.m_refRA )
657 return -1;
658
659 m_areas.m_compatMap.clear();
660
661 std::vector<RULE_AREA*> targets;
662
663 for( RULE_AREA& ra : m_areas.m_areas )
664 {
665 if( ra.m_zone == m_areas.m_refRA->m_zone )
666 continue;
667
668 targets.push_back( &ra );
669 m_areas.m_compatMap[&ra] = RULE_AREA_COMPAT_DATA();
670 }
671
672 if( targets.empty() )
673 return 0;
674
675 int total = static_cast<int>( targets.size() );
676 std::atomic<int> completed( 0 );
677 std::atomic<bool> cancelled( false );
678 std::atomic<int> matchedComponents( 0 );
679 std::atomic<int> totalComponents( 0 );
680 RULE_AREA* refRA = m_areas.m_refRA;
681
683 isoParams.m_cancelled = &cancelled;
684 isoParams.m_matchedComponents = &matchedComponents;
685 isoParams.m_totalComponents = &totalComponents;
686
687 // Process RA resolutions sequentially on a single background thread.
688 // Each resolveConnectionTopology call internally parallelizes its MRV scan
689 // across the thread pool, creating many short-lived tasks that fully utilize
690 // all available cores. Running the outer loop sequentially avoids thread
691 // pool starvation from nested parallelism.
693
694 auto future = tp.submit_task(
695 [this, refRA, &targets, &completed, &cancelled, &matchedComponents, &isoParams]()
696 {
697 for( RULE_AREA* target : targets )
698 {
699 if( cancelled.load( std::memory_order_relaxed ) )
700 break;
701
702 matchedComponents.store( 0, std::memory_order_relaxed );
703
704 RULE_AREA_COMPAT_DATA& compatData = m_areas.m_compatMap[target];
705 resolveConnectionTopology( refRA, target, compatData, isoParams );
706 completed.fetch_add( 1, std::memory_order_relaxed );
707 }
708 } );
709
710 if( Pgm().IsGUI() )
711 {
712 std::unique_ptr<WX_PROGRESS_REPORTER> reporter;
713 auto startTime = std::chrono::steady_clock::now();
714 double highWaterMark = 0.0;
715
716 while( future.wait_for( std::chrono::milliseconds( 100 ) ) != std::future_status::ready )
717 {
718 if( !reporter )
719 {
720 auto elapsed = std::chrono::steady_clock::now() - startTime;
721
722 if( elapsed > std::chrono::seconds( 1 ) )
723 {
724 reporter = std::make_unique<WX_PROGRESS_REPORTER>(
725 frame(), _( "Checking Rule Area compatibility..." ), 1, PR_CAN_ABORT );
726 }
727 else
728 {
729 // Flush background-thread log messages so timing traces appear promptly
730 wxLog::FlushActive();
731 }
732 }
733
734 if( reporter )
735 {
736 int done = completed.load( std::memory_order_relaxed );
737 int matched = matchedComponents.load( std::memory_order_relaxed );
738 int compTotal = totalComponents.load( std::memory_order_relaxed );
739
740 double fraction = ( compTotal > 0 )
741 ? static_cast<double>( matched ) / compTotal
742 : 0.0;
743 double progress = static_cast<double>( done + fraction ) / total;
744
745 if( progress > highWaterMark )
746 highWaterMark = progress;
747
748 reporter->SetCurrentProgress( highWaterMark );
749 reporter->Report( wxString::Format(
750 _( "Resolving topology %d of %d (%d/%d components)" ),
751 done + 1, total, matched, compTotal ) );
752
753 if( !reporter->KeepRefreshing() )
754 cancelled.store( true, std::memory_order_relaxed );
755 }
756 }
757 }
758 else
759 {
760 future.wait();
761 }
762
763 if( cancelled.load( std::memory_order_relaxed ) )
764 {
765 m_areas.m_compatMap.clear();
766 return -1;
767 }
768
769 return 0;
770}
771
772
773int MULTICHANNEL_TOOL::RepeatLayout( const TOOL_EVENT& aEvent, RULE_AREA& aRefArea, RULE_AREA& aTargetArea,
774 REPEAT_LAYOUT_OPTIONS& aOptions )
775{
776 wxCHECK_MSG( aRefArea.m_zone, -1, wxT( "Reference Rule Area has no zone." ) );
777 wxCHECK_MSG( aTargetArea.m_zone, -1, wxT( "Target Rule Area has no zone." ) );
778
780
781 if( !resolveConnectionTopology( &aRefArea, &aTargetArea, compat ) )
782 {
783 if( Pgm().IsGUI() )
784 {
785 wxString summary = wxString::Format( _( "Rule Area topologies do not match: %s" ), compat.m_errorMsg );
787 }
788
789 return -1;
790 }
791
792 BOARD_COMMIT commit( GetManager(), true, false );
793
794 // If no anchor is provided, pick the first matched pair to avoid center-alignment shifting
795 // the whole group. This keeps Apply Design Block Layout from moving the group to wherever
796 // the source design block happened to be placed.
797 if( aTargetArea.m_sourceType == PLACEMENT_SOURCE_T::GROUP_PLACEMENT && !aOptions.m_anchorFp )
798 {
799 if( !compat.m_matchingComponents.empty() )
800 aOptions.m_anchorFp = compat.m_matchingComponents.begin()->first;
801 }
802
803 if( !copyRuleAreaContents( &aRefArea, &aTargetArea, &commit, aOptions, compat ) )
804 {
805 auto errMsg = wxString::Format( _( "Copy Rule Area contents failed between rule areas '%s' and '%s'." ),
806 aRefArea.m_zone->GetZoneName(), aTargetArea.m_zone->GetZoneName() );
807
808 commit.Revert();
809
810 if( Pgm().IsGUI() )
811 frame()->ShowInfoBarError( errMsg, true );
812
813 return -1;
814 }
815
817 {
818 if( aTargetArea.m_components.size() == 0 || !( *aTargetArea.m_components.begin() )->GetParentGroup() )
819 {
820 commit.Revert();
821
822 if( Pgm().IsGUI() )
823 frame()->ShowInfoBarError( _( "Target group does not have a group." ), true );
824
825 return -1;
826 }
827
828 EDA_GROUP* group = ( *aTargetArea.m_components.begin() )->GetParentGroup();
829
830 commit.Modify( group->AsEdaItem(), nullptr, RECURSE_MODE::NO_RECURSE );
831
832 for( BOARD_ITEM* item : compat.m_groupableItems )
833 {
834 commit.Modify( item );
835 group->AddItem( item );
836 }
837 }
838
839 commit.Push( _( "Repeat layout" ) );
840
841 return 0;
842}
843
844
845int MULTICHANNEL_TOOL::RepeatLayout( const TOOL_EVENT& aEvent, ZONE* aRefZone )
846{
847 int totalCopied = 0;
848
849 BOARD_COMMIT commit( GetManager(), true, false );
850
851 for( auto& [targetArea, compatData] : m_areas.m_compatMap )
852 {
853 if( !compatData.m_doCopy )
854 {
855 wxLogTrace( traceMultichannelTool, wxT( "skipping copy to RA '%s' (disabled in dialog)\n" ),
856 targetArea->m_ruleName );
857 continue;
858 }
859
860 if( !compatData.m_isOk )
861 continue;
862
863 if( !copyRuleAreaContents( m_areas.m_refRA, targetArea, &commit, m_areas.m_options, compatData ) )
864 {
865 auto errMsg = wxString::Format( _( "Copy Rule Area contents failed between rule areas '%s' and '%s'." ),
866 m_areas.m_refRA->m_zone->GetZoneName(),
867 targetArea->m_zone->GetZoneName() );
868
869 commit.Revert();
870
871 if( Pgm().IsGUI() )
872 frame()->ShowInfoBarError( errMsg, true );
873
874 return -1;
875 }
876
877 totalCopied++;
878 wxSafeYield();
879 }
880
881 if( m_areas.m_options.m_groupItems )
882 {
883 for( const auto& [targetArea, compatData] : m_areas.m_compatMap )
884 {
885 if( compatData.m_groupableItems.size() < 2 )
886 continue;
887
888 pruneExistingGroups( commit, compatData.m_affectedItems );
889
890 PCB_GROUP* group = new PCB_GROUP( board() );
891
892 commit.Add( group );
893
894 for( BOARD_ITEM* item : compatData.m_groupableItems )
895 {
896 commit.Modify( item );
897 group->AddItem( item );
898 }
899 }
900 }
901
902 commit.Push( _( "Repeat layout" ) );
903
904 if( Pgm().IsGUI() )
905 frame()->ShowInfoBarMsg( wxString::Format( _( "Copied to %d Rule Areas." ), totalCopied ), true );
906
907 return 0;
908}
909
910
911wxString MULTICHANNEL_TOOL::stripComponentIndex( const wxString& aRef ) const
912{
913 wxString rv;
914
915 // fixme: i'm pretty sure this can be written in a simpler way, but I really suck at figuring
916 // out which wx's built in functions would do it for me. And I hate regexps :-)
917 for( auto k : aRef )
918 {
919 if( !k.IsAscii() )
920 break;
921
922 char c;
923 k.GetAsChar( &c );
924
925 if( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) || ( c == '_' ) )
926 rv.Append( k );
927 else
928 break;
929 }
930
931 return rv;
932}
933
934
935int MULTICHANNEL_TOOL::findRoutingInRuleArea( RULE_AREA* aRuleArea, std::set<BOARD_CONNECTED_ITEM*>& aOutput,
936 std::shared_ptr<CONNECTIVITY_DATA> aConnectivity,
937 const SHAPE_POLY_SET& aRAPoly, const REPEAT_LAYOUT_OPTIONS& aOpts ) const
938{
939 if( !aRuleArea || !aRuleArea->m_zone )
940 return 0;
941
942 // The user also will consider tracks and vias that are inside the source area but
943 // not connected to any of the source pads to count as "routing" (e.g. stitching vias)
944
945 int count = 0;
946
947 // When we're copying the layout of a design block, we are provided an exact list of items
948 // rather than querying the board for items that are inside the area.
950 {
951 // Get all board connected items that are from the design block, except pads,
952 // which shouldn't be copied
953 for( EDA_ITEM* item : aRuleArea->m_designBlockItems )
954 {
955 // Include any connected items except pads.
956 if( item->Type() == PCB_PAD_T )
957 continue;
958
959 if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
960 {
961 // Zones are handled by the "copy other items" path, we need this check here
962 // because design blocks explicitly include them as part of the block contents,
963 // but other RA types grab them by querying the board for items enclosed by the RA polygon
964 if( bci->Type() == PCB_ZONE_T )
965 continue;
966
967 if( bci->IsConnected() )
968 aOutput.insert( bci );
969 }
970 }
971
972 return (int) aOutput.size();
973 }
974
976 PCBEXPR_UCODE ucode;
977 PCBEXPR_CONTEXT ctx, preflightCtx;
978
979 auto reportError =
980 [&]( const wxString& aMessage, int aOffset )
981 {
982 wxLogTrace( traceMultichannelTool, wxT( "ERROR: %s" ), aMessage );
983 };
984
985 ctx.SetErrorCallback( reportError );
986 preflightCtx.SetErrorCallback( reportError );
987 compiler.SetErrorCallback( reportError );
988
989 // Use the zone's UUID to identify it uniquely. Using the zone name could match other zones
990 // with the same name (e.g., a copper fill zone with the same name as a rule area).
991 wxString ruleText = wxString::Format( wxT( "A.enclosedByArea('%s')" ),
992 aRuleArea->m_zone->m_Uuid.AsString() );
993
994 auto testAndAdd =
995 [&]( BOARD_CONNECTED_ITEM* aItem )
996 {
997 if( aOutput.contains( aItem ) )
998 return;
999
1000 ctx.SetItems( aItem, aItem );
1001 LIBEVAL::VALUE* val = ucode.Run( &ctx );
1002
1003 if( val->AsDouble() != 0.0 )
1004 {
1005 aOutput.insert( aItem );
1006 count++;
1007 }
1008 };
1009
1010 if( compiler.Compile( ruleText, &ucode, &preflightCtx ) )
1011 {
1012 for( PCB_TRACK* track : board()->Tracks() )
1013 testAndAdd( track );
1014
1015 for( BOARD_ITEM* drawing : board()->Drawings() )
1016 {
1017 if( drawing->IsConnected() )
1018 testAndAdd( static_cast<BOARD_CONNECTED_ITEM*>( drawing ) );
1019 }
1020 }
1021
1022 return count;
1023}
1024
1025
1027 BOARD_COMMIT* aCommit, REPEAT_LAYOUT_OPTIONS aOpts,
1028 RULE_AREA_COMPAT_DATA& aCompatData )
1029{
1030 // copy RA shapes first
1031 SHAPE_LINE_CHAIN refOutline = aRefArea->m_zone->Outline()->COutline( 0 );
1032 SHAPE_LINE_CHAIN targetOutline = aTargetArea->m_zone->Outline()->COutline( 0 );
1033
1034 FOOTPRINT* targetAnchorFp = nullptr;
1035 VECTOR2I disp = aTargetArea->m_center - aRefArea->m_center;
1036 EDA_ANGLE rot = EDA_ANGLE( 0 );
1037
1038 if( aOpts.m_anchorFp )
1039 {
1040 for( const auto& [refFP, targetFP] : aCompatData.m_matchingComponents )
1041 {
1042 if( refFP->GetReference() == aOpts.m_anchorFp->GetReference() )
1043 targetAnchorFp = targetFP;
1044 }
1045
1046 // If the dialog-selected anchor reference doesn't exist in the target area (e.g. refs don't match),
1047 // fall back to the first matched pair to avoid center-alignment shifting the whole group.
1048 if( !targetAnchorFp && !aCompatData.m_matchingComponents.empty() )
1049 targetAnchorFp = aCompatData.m_matchingComponents.begin()->second;
1050
1051 if( targetAnchorFp )
1052 {
1053 VECTOR2I oldpos = aOpts.m_anchorFp->GetPosition();
1054 rot = EDA_ANGLE( targetAnchorFp->GetOrientationDegrees() - aOpts.m_anchorFp->GetOrientationDegrees() );
1055 aOpts.m_anchorFp->Rotate( VECTOR2( 0, 0 ), EDA_ANGLE( rot ) );
1056 oldpos = aOpts.m_anchorFp->GetPosition();
1057 VECTOR2I newpos = targetAnchorFp->GetPosition();
1058 disp = newpos - oldpos;
1059 aOpts.m_anchorFp->Rotate( VECTOR2( 0, 0 ), EDA_ANGLE( -rot ) );
1060 }
1061 }
1062
1063 SHAPE_POLY_SET refPoly;
1064 refPoly.AddOutline( refOutline );
1065 refPoly.CacheTriangulation( false );
1066
1067 SHAPE_POLY_SET targetPoly;
1068
1069 SHAPE_LINE_CHAIN newTargetOutline( refOutline );
1070 newTargetOutline.Rotate( rot, VECTOR2( 0, 0 ) );
1071 newTargetOutline.Move( disp );
1072 targetPoly.AddOutline( newTargetOutline );
1073 targetPoly.CacheTriangulation( false );
1074
1075 std::shared_ptr<CONNECTIVITY_DATA> connectivity = board()->GetConnectivity();
1076 std::map<EDA_GROUP*, EDA_GROUP*> groupMap;
1077
1078 // For Apply Design Block Layout, grouping is handled later by RepeatLayout() using the
1079 // existing target group. Do not clone groups here or we end up with duplicates.
1080 const bool preserveGroups = aTargetArea->m_sourceType != PLACEMENT_SOURCE_T::GROUP_PLACEMENT;
1081
1082 auto fixupParentGroup =
1083 [&]( BOARD_ITEM* sourceItem, BOARD_ITEM* destItem )
1084 {
1085 if( !preserveGroups )
1086 return;
1087
1088 if( EDA_GROUP* parentGroup = sourceItem->GetParentGroup() )
1089 {
1090 if( !groupMap.contains( parentGroup ) )
1091 {
1092 PCB_GROUP* newGroup = static_cast<PCB_GROUP*>(
1093 static_cast<PCB_GROUP*>( parentGroup->AsEdaItem() )->Duplicate( false ) );
1094 newGroup->GetItems().clear();
1095 groupMap[parentGroup] = newGroup;
1096 aCommit->Add( newGroup );
1097 }
1098
1099 groupMap[parentGroup]->AddItem( destItem );
1100 }
1101 };
1102
1103 // Only stage changes for a target Rule Area zone if it actually belongs to the board.
1104 // In some workflows (e.g. ApplyDesignBlockLayout), the target area is a temporary zone
1105 // and is not added to the BOARD.
1106 bool targetZoneOnBoard = false;
1107
1108 if( aTargetArea->m_zone )
1109 {
1110 for( ZONE* z : board()->Zones() )
1111 {
1112 if( z == aTargetArea->m_zone )
1113 {
1114 targetZoneOnBoard = true;
1115 break;
1116 }
1117 }
1118 }
1119
1120 if( targetZoneOnBoard )
1121 {
1122 aCommit->Modify( aTargetArea->m_zone );
1123 aCompatData.m_affectedItems.insert( aTargetArea->m_zone );
1124 aCompatData.m_groupableItems.insert( aTargetArea->m_zone );
1125 }
1126
1127 if( aOpts.m_copyRouting )
1128 {
1129 std::set<BOARD_CONNECTED_ITEM*> refRouting;
1130 std::set<BOARD_CONNECTED_ITEM*> targetRouting;
1131
1132 wxLogTrace( traceMultichannelTool, wxT( "copying routing: %d fps\n" ),
1133 (int) aCompatData.m_matchingComponents.size() );
1134
1135 std::set<int> refc;
1136 std::set<int> targc;
1137
1138 for( const auto& [refFP, targetFP] : aCompatData.m_matchingComponents )
1139 {
1140 for( PAD* pad : refFP->Pads() )
1141 refc.insert( pad->GetNetCode() );
1142
1143 for( PAD* pad : targetFP->Pads() )
1144 targc.insert( pad->GetNetCode() );
1145 }
1146
1147 findRoutingInRuleArea( aTargetArea, targetRouting, connectivity, targetPoly, aOpts );
1148 findRoutingInRuleArea( aRefArea, refRouting, connectivity, refPoly, aOpts );
1149
1150 for( BOARD_CONNECTED_ITEM* item : targetRouting )
1151 {
1152 // Never remove pads as part of routing copy.
1153 if( item->Type() == PCB_PAD_T )
1154 continue;
1155
1156 if( item->IsLocked() && !aOpts.m_includeLockedItems )
1157 continue;
1158
1159 if( aOpts.m_connectedRoutingOnly && !targc.contains( item->GetNetCode() ) )
1160 continue;
1161
1162 // item already removed
1163 if( aCommit->GetStatus( item ) != 0 )
1164 continue;
1165
1166 if( !aTargetArea->m_zone->GetLayerSet().Contains( item->GetLayer() ) )
1167 {
1168 continue;
1169 }
1170
1171 aCompatData.m_affectedItems.insert( item );
1172 aCommit->Remove( item );
1173 }
1174
1175 for( BOARD_CONNECTED_ITEM* item : refRouting )
1176 {
1177 // Never copy pads as part of routing copy.
1178 if( item->Type() == PCB_PAD_T )
1179 continue;
1180
1181 if( item->IsLocked() && !aOpts.m_includeLockedItems )
1182 continue;
1183
1184 if( aOpts.m_connectedRoutingOnly && !refc.contains( item->GetNetCode() ) )
1185 continue;
1186
1187 if( !aRefArea->m_zone->GetLayerSet().Contains( item->GetLayer() ) )
1188 continue;
1189
1190 if( !aTargetArea->m_zone->GetLayerSet().Contains( item->GetLayer() ) )
1191 continue;
1192
1193 BOARD_CONNECTED_ITEM* copied = static_cast<BOARD_CONNECTED_ITEM*>( item->Duplicate( false ) );
1194
1195 fixupNet( item, copied, aCompatData.m_matchingComponents );
1196 fixupParentGroup( item, copied );
1197
1198 copied->Rotate( VECTOR2( 0, 0 ), rot );
1199 copied->Move( disp );
1200 aCompatData.m_groupableItems.insert( copied );
1201 aCommit->Add( copied );
1202 }
1203 }
1204
1205 if( aOpts.m_copyOtherItems )
1206 {
1207 std::set<BOARD_ITEM*> sourceItems;
1208 std::set<BOARD_ITEM*> targetItems;
1209
1210 findOtherItemsInRuleArea( aRefArea, sourceItems );
1211 findOtherItemsInRuleArea( aTargetArea, targetItems );
1212
1213 for( BOARD_ITEM* item : targetItems )
1214 {
1215 if( item->Type() == PCB_TEXT_T && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
1216 continue;
1217
1218 if( item->IsLocked() && !aOpts.m_includeLockedItems )
1219 continue;
1220
1221 // item already removed
1222 if( aCommit->GetStatus( item ) != 0 )
1223 continue;
1224
1225 if( item->Type() == PCB_ZONE_T )
1226 {
1227 ZONE* zone = static_cast<ZONE*>( item );
1228
1229 // Check all zone layers are included in the target rule area.
1230 if( aTargetArea->m_zone->GetLayerSet().ContainsAll( zone->GetLayerSet() ) )
1231 {
1232 aCompatData.m_affectedItems.insert( zone );
1233 aCommit->Remove( zone );
1234 }
1235 }
1236 else
1237 {
1238 if( aTargetArea->m_zone->GetLayerSet().Contains( item->GetLayer() ) )
1239 {
1240 aCompatData.m_affectedItems.insert( item );
1241 aCommit->Remove( item );
1242 }
1243 }
1244 }
1245
1246 for( BOARD_ITEM* item : sourceItems )
1247 {
1248 if( item->Type() == PCB_TEXT_T && item->GetParent() && item->GetParent()->Type() == PCB_FOOTPRINT_T )
1249 continue;
1250
1251 if( item->IsLocked() && !aOpts.m_includeLockedItems )
1252 continue;
1253
1254 BOARD_ITEM* copied = nullptr;
1255
1256 if( item->Type() == PCB_ZONE_T )
1257 {
1258 ZONE* zone = static_cast<ZONE*>( item );
1259 LSET allowedLayers = aRefArea->m_zone->GetLayerSet() & aTargetArea->m_zone->GetLayerSet();
1260
1261 // Check all zone layers are included in both source and target rule areas.
1262 if( !allowedLayers.ContainsAll( zone->GetLayerSet() ) )
1263 continue;
1264
1265 ZONE* targetZone = static_cast<ZONE*>( item->Duplicate( false ) );
1266 fixupNet( zone, targetZone, aCompatData.m_matchingComponents );
1267
1268 copied = targetZone;
1269 }
1270 else
1271 {
1272 if( !aRefArea->m_zone->GetLayerSet().Contains( item->GetLayer() ) )
1273 continue;
1274
1275 if( !aTargetArea->m_zone->GetLayerSet().Contains( item->GetLayer() ) )
1276 continue;
1277
1278 copied = static_cast<BOARD_ITEM*>( item->Clone() );
1279 }
1280
1281 if( copied )
1282 {
1283 fixupParentGroup( item, copied );
1284
1285 copied->ClearFlags();
1286 copied->Rotate( VECTOR2( 0, 0 ), rot );
1287 copied->Move( disp );
1288 aCompatData.m_groupableItems.insert( copied );
1289 aCommit->Add( copied );
1290 }
1291 }
1292 }
1293
1294 if( aOpts.m_copyPlacement )
1295 {
1296 for( const auto& [refFP, targetFP] : aCompatData.m_matchingComponents )
1297 {
1298 if( !aRefArea->m_zone->GetLayerSet().Contains( refFP->GetLayer() ) )
1299 {
1300 wxLogTrace( traceMultichannelTool, wxT( "discard ref:%s (ref layer)\n" ),
1301 refFP->GetReference() );
1302 continue;
1303 }
1304 if( !aTargetArea->m_zone->GetLayerSet().Contains( refFP->GetLayer() ) )
1305 {
1306 wxLogTrace( traceMultichannelTool, wxT( "discard ref:%s (target layer)\n" ),
1307 refFP->GetReference() );
1308 continue;
1309 }
1310
1311 // For regular Rule Area repeat, ignore source footprints outside the reference area.
1312 // For Design Block apply, use the exact source item set collected from the block.
1314 && !refFP->GetEffectiveShape( refFP->GetLayer() )->Collide( &refPoly, 0 ) )
1315 {
1316 continue;
1317 }
1318
1319 if( targetFP->IsLocked() && !aOpts.m_includeLockedItems )
1320 continue;
1321
1322 aCommit->Modify( targetFP );
1323
1324 targetFP->SetLayerAndFlip( refFP->GetLayer() );
1325 targetFP->SetOrientation( refFP->GetOrientation() );
1326 targetFP->SetPosition( refFP->GetPosition() );
1327 targetFP->Rotate( VECTOR2( 0, 0 ), rot );
1328 targetFP->Move( disp );
1329
1330 for( PCB_FIELD* refField : refFP->GetFields() )
1331 {
1332 wxCHECK2( refField, continue );
1333
1334 PCB_FIELD* targetField = targetFP->GetField( refField->GetName() );
1335
1336 if( !targetField )
1337 continue;
1338
1339 targetField->SetLayerSet( refField->GetLayerSet() );
1340 targetField->SetVisible( refField->IsVisible() );
1341 targetField->SetAttributes( refField->GetAttributes() );
1342 targetField->SetPosition( refField->GetPosition() );
1343 targetField->Rotate( VECTOR2( 0, 0 ), rot );
1344 targetField->Move( disp );
1345 targetField->SetIsKnockout( refField->IsKnockout() );
1346 }
1347
1348 // Copy non-field text items (user-added text on the footprint)
1349 for( BOARD_ITEM* refItem : refFP->GraphicalItems() )
1350 {
1351 if( refItem->Type() != PCB_TEXT_T )
1352 continue;
1353
1354 PCB_TEXT* refText = static_cast<PCB_TEXT*>( refItem );
1355
1356 for( BOARD_ITEM* targetItem : targetFP->GraphicalItems() )
1357 {
1358 if( targetItem->Type() != PCB_TEXT_T )
1359 continue;
1360
1361 PCB_TEXT* targetText = static_cast<PCB_TEXT*>( targetItem );
1362
1363 // Match text items by their text content
1364 if( targetText->GetText() == refText->GetText() )
1365 {
1366 targetText->SetLayer( refText->GetLayer() );
1367 targetText->SetVisible( refText->IsVisible() );
1368 targetText->SetAttributes( refText->GetAttributes() );
1369 targetText->SetPosition( refText->GetPosition() );
1370 targetText->Rotate( VECTOR2( 0, 0 ), rot );
1371 targetText->Move( disp );
1372 targetText->SetIsKnockout( refText->IsKnockout() );
1373 break;
1374 }
1375 }
1376 }
1377
1378 // Copy 3D model settings
1379 targetFP->Models() = refFP->Models();
1380
1381 aCompatData.m_affectedItems.insert( targetFP );
1382 aCompatData.m_groupableItems.insert( targetFP );
1383 }
1384 }
1385
1386 aTargetArea->m_zone->RemoveAllContours();
1387 aTargetArea->m_zone->AddPolygon( newTargetOutline );
1388 aTargetArea->m_zone->UnHatchBorder();
1389 aTargetArea->m_zone->HatchBorder();
1390
1391 return true;
1392}
1393
1399 TMATCH::COMPONENT_MATCHES& aComponentMatches )
1400{
1401 auto connectivity = board()->GetConnectivity();
1402 const std::vector<BOARD_CONNECTED_ITEM*> refConnectedPads = connectivity->GetNetItems( aRef->GetNetCode(),
1403 { PCB_PAD_T } );
1404
1405 for( const BOARD_CONNECTED_ITEM* refConItem : refConnectedPads )
1406 {
1407 if( refConItem->Type() != PCB_PAD_T )
1408 continue;
1409
1410 const PAD* refPad = static_cast<const PAD*>( refConItem );
1411 FOOTPRINT* sourceFootprint = refPad->GetParentFootprint();
1412
1413 if( aComponentMatches.contains( sourceFootprint ) )
1414 {
1415 const FOOTPRINT* targetFootprint = aComponentMatches[sourceFootprint];
1416 std::vector<const PAD*> targetFpPads = targetFootprint->GetPads( refPad->GetNumber() );
1417
1418 if( !targetFpPads.empty() )
1419 {
1420 int targetNetCode = targetFpPads[0]->GetNet()->GetNetCode();
1421 aTarget->SetNetCode( targetNetCode );
1422
1423 break;
1424 }
1425 }
1426 }
1427}
1428
1429
1431 RULE_AREA_COMPAT_DATA& aMatches,
1432 const TMATCH::ISOMORPHISM_PARAMS& aParams )
1433{
1434 using namespace TMATCH;
1435
1436 PROF_TIMER timerBuild;
1437 std::unique_ptr<CONNECTION_GRAPH> cgRef( CONNECTION_GRAPH::BuildFromFootprintSet( aRefArea->m_components,
1438 aTargetArea->m_components ) );
1439 std::unique_ptr<CONNECTION_GRAPH> cgTarget( CONNECTION_GRAPH::BuildFromFootprintSet( aTargetArea->m_components,
1440 aRefArea->m_components ) );
1441 timerBuild.Stop();
1442
1443 wxLogTrace( traceMultichannelTool, wxT( "Graph construction: %s (%d + %d components)" ),
1444 timerBuild.to_string(),
1445 (int) aRefArea->m_components.size(),
1446 (int) aTargetArea->m_components.size() );
1447
1448 std::vector<TMATCH::TOPOLOGY_MISMATCH_REASON> mismatchReasons;
1449
1450 PROF_TIMER timerIso;
1451 bool status = cgRef->FindIsomorphism( cgTarget.get(), aMatches.m_matchingComponents,
1452 mismatchReasons, aParams );
1453 timerIso.Stop();
1454
1455 wxLogTrace( traceMultichannelTool, wxT( "FindIsomorphism: %s, result=%d" ),
1456 timerIso.to_string(), status ? 1 : 0 );
1457
1458 aMatches.m_isOk = status;
1459
1460 if( status )
1461 {
1462 aMatches.m_errorMsg = _( "OK" );
1463 aMatches.m_mismatchReasons.clear();
1464 return true;
1465 }
1466
1467 aMatches.m_mismatchReasons.clear();
1468
1469 for( const auto& reason : mismatchReasons )
1470 {
1471 if( reason.m_reason.IsEmpty() )
1472 continue;
1473
1474 if( !reason.m_reference.IsEmpty() && !reason.m_candidate.IsEmpty() )
1475 {
1476 aMatches.m_mismatchReasons.push_back( wxString::Format( wxT( "%s -> %s: %s" ),
1477 reason.m_reference,
1478 reason.m_candidate,
1479 reason.m_reason ) );
1480 }
1481 else if( !reason.m_reference.IsEmpty() )
1482 {
1483 aMatches.m_mismatchReasons.push_back( wxString::Format( wxT( "%s: %s" ),
1484 reason.m_reference,
1485 reason.m_reason ) );
1486 }
1487 else
1488 aMatches.m_mismatchReasons.push_back( reason.m_reason );
1489 }
1490
1491 if( aMatches.m_mismatchReasons.empty() )
1492 aMatches.m_mismatchReasons.push_back( _( "Topology mismatch" ) );
1493
1494 // Component count mismatch
1495 if( aRefArea->m_components.size() != aTargetArea->m_components.size() )
1496 {
1497 aMatches.m_mismatchReasons.push_back(
1498 wxString::Format( _( "Reference area total components: %d" ), (int) aRefArea->m_components.size() ) );
1499 aMatches.m_mismatchReasons.push_back( wxString::Format( _( "Reference area components:\n%s" ),
1500 FormatComponentList( aRefArea->m_components ) ) );
1501 aMatches.m_mismatchReasons.push_back(
1502 wxString::Format( _( "Target area total components: %d" ), (int) aTargetArea->m_components.size() ) );
1503 aMatches.m_mismatchReasons.push_back( wxString::Format( _( "Target area components:\n%s" ),
1504 FormatComponentList( aTargetArea->m_components ) ) );
1505 }
1506
1507 aMatches.m_errorMsg = aMatches.m_mismatchReasons.front();
1508
1509 return status;
1510}
1511
1512
1514 const std::unordered_set<BOARD_ITEM*>& aItemsToRemove )
1515{
1516 // Note: groups are only collections, not "real" hierarchy. A group's members are still parented
1517 // by the board (and therefore nested groups are still in the board's list of groups).
1518 for( PCB_GROUP* group : board()->Groups() )
1519 {
1520 std::vector<EDA_ITEM*> pruneList;
1521
1522 for( EDA_ITEM* refItem : group->GetItems() )
1523 {
1524 for( BOARD_ITEM* testItem : aItemsToRemove )
1525 {
1526 if( refItem->m_Uuid == testItem->m_Uuid )
1527 pruneList.push_back( refItem );
1528 }
1529 }
1530
1531 if( !pruneList.empty() )
1532 {
1533 aCommit.Modify( group );
1534
1535 for( EDA_ITEM* item : pruneList )
1536 group->RemoveItem( item );
1537
1538 if( group->GetItems().size() < 2 )
1539 aCommit.Remove( group );
1540 }
1541 }
1542
1543 return false;
1544}
1545
1546
1548{
1549 if( Pgm().IsGUI() )
1550 {
1552
1553 if( m_areas.m_areas.size() <= 1 )
1554 {
1555 frame()->ShowInfoBarError( _( "Cannot auto-generate any placement areas because the "
1556 "schematic has only one or no hierarchical sheets, "
1557 "groups, or component classes." ),
1558 true );
1559 return 0;
1560 }
1561
1563 int ret = dialog.ShowModal();
1564
1565 if( ret != wxID_OK )
1566 return 0;
1567 }
1568
1569 for( ZONE* zone : board()->Zones() )
1570 {
1571 if( !zone->GetIsRuleArea() )
1572 continue;
1573
1574 if( !zone->GetPlacementAreaEnabled() )
1575 continue;
1576
1577 std::set<FOOTPRINT*> components;
1578 RULE_AREA zoneRA;
1579 zoneRA.m_zone = zone;
1580 zoneRA.m_sourceType = zone->GetPlacementAreaSourceType();
1581 findComponentsInRuleArea( &zoneRA, components );
1582
1583 if( components.empty() )
1584 continue;
1585
1586 for( RULE_AREA& ra : m_areas.m_areas )
1587 {
1588 if( components == ra.m_components )
1589 {
1590 if( zone->GetPlacementAreaSourceType() == PLACEMENT_SOURCE_T::SHEETNAME )
1591 {
1592 wxLogTrace( traceMultichannelTool,
1593 wxT( "Placement rule area for sheet '%s' already exists as '%s'\n" ),
1594 ra.m_sheetPath, zone->GetZoneName() );
1595 }
1596 else if( zone->GetPlacementAreaSourceType() == PLACEMENT_SOURCE_T::COMPONENT_CLASS )
1597 {
1598 wxLogTrace( traceMultichannelTool,
1599 wxT( "Placement rule area for component class '%s' already exists as '%s'\n" ),
1600 ra.m_componentClass, zone->GetZoneName() );
1601 }
1602 else
1603 {
1604 wxLogTrace( traceMultichannelTool,
1605 wxT( "Placement rule area for group '%s' already exists as '%s'\n" ),
1606 ra.m_groupName, zone->GetZoneName() );
1607 }
1608
1609 ra.m_oldZone = zone;
1610 ra.m_existsAlready = true;
1611 }
1612 }
1613 }
1614
1615 wxLogTrace( traceMultichannelTool, wxT( "%d placement areas found\n" ), (int) m_areas.m_areas.size() );
1616
1617 BOARD_COMMIT commit( GetManager(), true, false );
1618
1619 for( RULE_AREA& ra : m_areas.m_areas )
1620 {
1621 if( !ra.m_generateEnabled )
1622 continue;
1623
1624 if( ra.m_existsAlready && !m_areas.m_replaceExisting )
1625 continue;
1626
1627 if( ra.m_components.empty() )
1628 continue;
1629
1630 SHAPE_LINE_CHAIN raOutline;
1631
1632 // Groups are a way for the user to more explicitly provide a list of items to include in
1633 // the multichannel tool, as opposed to inferring them based on sheet structure or component classes.
1634 // So for group-based RAs, we build the RA outline based everything in the group, not just components.
1636 {
1637 std::set<BOARD_ITEM*> groupItems = queryBoardItemsInGroup( ra.m_groupName );
1638
1639 if( groupItems.empty() )
1640 {
1641 wxLogTrace( traceMultichannelTool,
1642 wxT( "Skipping placement rule area generation for source group '%s': group has no board items." ),
1643 ra.m_groupName );
1644 continue;
1645 }
1646
1647 raOutline = buildRAOutline( groupItems, 100000 );
1648 }
1649 else
1650 {
1651 raOutline = buildRAOutline( ra.m_components, 100000 );
1652 }
1653
1654 std::unique_ptr<ZONE> newZone( new ZONE( board() ) );
1655
1657 newZone->SetZoneName( wxString::Format( wxT( "auto-placement-area-%s" ), ra.m_sheetPath ) );
1659 newZone->SetZoneName( wxString::Format( wxT( "auto-placement-area-%s" ), ra.m_componentClass ) );
1660 else
1661 newZone->SetZoneName( wxString::Format( wxT( "auto-placement-area-%s" ), ra.m_groupName ) );
1662
1663 wxLogTrace( traceMultichannelTool, wxT( "Generated rule area '%s' (%d components)\n" ),
1664 newZone->GetZoneName(),
1665 (int) ra.m_components.size() );
1666
1667 newZone->SetIsRuleArea( true );
1668 newZone->SetLayerSet( LSET::AllCuMask() );
1669 newZone->SetPlacementAreaEnabled( true );
1670 newZone->SetDoNotAllowZoneFills( false );
1671 newZone->SetDoNotAllowVias( false );
1672 newZone->SetDoNotAllowTracks( false );
1673 newZone->SetDoNotAllowPads( false );
1674 newZone->SetDoNotAllowFootprints( false );
1675
1677 {
1678 newZone->SetPlacementAreaSourceType( PLACEMENT_SOURCE_T::SHEETNAME );
1679 newZone->SetPlacementAreaSource( ra.m_sheetPath );
1680 }
1682 {
1683 newZone->SetPlacementAreaSourceType( PLACEMENT_SOURCE_T::COMPONENT_CLASS );
1684 newZone->SetPlacementAreaSource( ra.m_componentClass );
1685 }
1686 else
1687 {
1688 newZone->SetPlacementAreaSourceType( PLACEMENT_SOURCE_T::GROUP_PLACEMENT );
1689 newZone->SetPlacementAreaSource( ra.m_groupName );
1690 }
1691
1692 newZone->AddPolygon( raOutline );
1693 newZone->SetHatchStyle( ZONE_BORDER_DISPLAY_STYLE::NO_HATCH );
1694
1695 if( ra.m_existsAlready )
1696 {
1697 commit.Remove( ra.m_oldZone );
1698 }
1699
1700 ra.m_zone = newZone.release();
1701 commit.Add( ra.m_zone );
1702 }
1703
1704 // fixme: handle corner cases where the items belonging to a Rule Area already
1705 // belong to other groups.
1706
1707 if( m_areas.m_options.m_groupItems )
1708 {
1709 for( RULE_AREA& ra : m_areas.m_areas )
1710 {
1711 if( !ra.m_generateEnabled )
1712 continue;
1713
1714 if( ra.m_existsAlready && !m_areas.m_replaceExisting )
1715 continue;
1716
1717 // A group needs at least 2 items (zone + at least 1 component)
1718 if( ra.m_components.empty() )
1719 continue;
1720
1721 std::unordered_set<BOARD_ITEM*> toPrune;
1722
1723 std::copy( ra.m_components.begin(), ra.m_components.end(), std::inserter( toPrune, toPrune.begin() ) );
1724
1725 if( ra.m_existsAlready )
1726 toPrune.insert( ra.m_zone );
1727
1728 pruneExistingGroups( commit, toPrune );
1729
1730 PCB_GROUP* group = new PCB_GROUP( board() );
1731
1732 commit.Add( group );
1733
1734 commit.Modify( ra.m_zone );
1735 group->AddItem( ra.m_zone );
1736
1737 for( FOOTPRINT* fp : ra.m_components )
1738 {
1739 commit.Modify( fp );
1740 group->AddItem( fp );
1741 }
1742 }
1743 }
1744
1745 commit.Push( _( "Auto-generate placement rule areas" ) );
1746
1747 return true;
1748}
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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 SetLayerSet(const LSET &aLayers)
Definition board_item.h:265
virtual bool IsKnockout() const
Definition board_item.h:324
virtual void SetIsKnockout(bool aKnockout)
Definition board_item.h:325
virtual void SetLayer(PCB_LAYER_ID aLayer)
Set the layer this item is on.
Definition board_item.h:285
FOOTPRINT * GetParentFootprint() const
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Definition board.h:563
constexpr BOX2< Vec > GetInflated(coord_type aDx, coord_type aDy) const
Get a new rectangle that is this one, inflated by aDx and aDy.
Definition box2.h:638
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition commit.h:72
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:90
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
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:78
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:171
A lightweight representation of a component class.
const std::vector< COMPONENT_CLASS * > & GetConstituentClasses() const
Fetches a vector of the constituent classes for this (effective) class.
const std::vector< BOARD_CONNECTED_ITEM * > GetNetItems(int aNetCode, const std::vector< KICAD_T > &aTypes) const
Function GetNetItems() Returns the list of items that belong to a certain net.
int ShowModal() override
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
std::unordered_set< EDA_ITEM * > & GetItems()
Definition eda_group.h:54
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:99
const KIID m_Uuid
Definition eda_item.h:527
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:117
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:111
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:98
virtual bool IsVisible() const
Definition eda_text.h:187
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
Definition eda_text.cpp:447
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:400
const TEXT_ATTRIBUTES & GetAttributes() const
Definition eda_text.h:231
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
double GetOrientationDegrees() const
Definition footprint.h:346
std::vector< const PAD * > GetPads(const wxString &aPadNumber, const PAD *aIgnore=nullptr) const
const wxString & GetReference() const
Definition footprint.h:751
VECTOR2I GetPosition() const override
Definition footprint.h:327
wxString AsString() const
Definition kiid.cpp:244
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
bool Compile(const wxString &aString, UCODE *aCode, CONTEXT *aPreflightContext)
void SetErrorCallback(std::function< void(const wxString &aMessage, int aOffset)> aCallback)
VALUE * Run(CONTEXT *ctx)
virtual double AsDouble() const
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
bool ContainsAll(const LSET &aLayers) const
See if this layer set contains all layers in another set.
Definition lset.h:85
static LSET AllCuMask(int aCuLayerCount)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
Definition lset.cpp:599
bool Contains(PCB_LAYER_ID aLayer) const
See if the layer set contains a PCB layer.
Definition lset.h:63
int CheckRACompatibility(ZONE *aRefZone)
std::set< FOOTPRINT * > queryComponentsInSheet(wxString aSheetName) const
bool findOtherItemsInRuleArea(RULE_AREA *aRuleArea, std::set< BOARD_ITEM * > &aItems)
int repeatLayout(const TOOL_EVENT &aEvent)
bool findComponentsInRuleArea(RULE_AREA *aRuleArea, std::set< FOOTPRINT * > &aComponents)
void UpdatePickedItem(const EDA_ITEM *aItem) override
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
const SHAPE_LINE_CHAIN buildRAOutline(std::set< FOOTPRINT * > &aFootprints, int aMargin)
bool resolveConnectionTopology(RULE_AREA *aRefArea, RULE_AREA *aTargetArea, RULE_AREA_COMPAT_DATA &aMatches, const TMATCH::ISOMORPHISM_PARAMS &aParams={})
int findRoutingInRuleArea(RULE_AREA *aRuleArea, std::set< BOARD_CONNECTED_ITEM * > &aOutput, std::shared_ptr< CONNECTIVITY_DATA > aConnectivity, const SHAPE_POLY_SET &aRAPoly, const REPEAT_LAYOUT_OPTIONS &aOpts) const
wxString stripComponentIndex(const wxString &aRef) const
RULE_AREAS_DATA m_areas
bool pruneExistingGroups(COMMIT &aCommit, const std::unordered_set< BOARD_ITEM * > &aItemsToCheck)
void ShowMismatchDetails(wxWindow *aParent, const wxString &aSummary, const std::vector< wxString > &aReasons) const
int RepeatLayout(const TOOL_EVENT &aEvent, ZONE *aRefZone)
int AutogenerateRuleAreas(const TOOL_EVENT &aEvent)
void fixupNet(BOARD_CONNECTED_ITEM *aRef, BOARD_CONNECTED_ITEM *aTarget, TMATCH::COMPONENT_MATCHES &aComponentMatches)
Attempts to make sure copied items are assigned the right net.
bool copyRuleAreaContents(RULE_AREA *aRefArea, RULE_AREA *aTargetArea, BOARD_COMMIT *aCommit, REPEAT_LAYOUT_OPTIONS aOpts, RULE_AREA_COMPAT_DATA &aCompatData)
std::set< FOOTPRINT * > queryComponentsInComponentClass(const wxString &aComponentClassName) const
RULE_AREA * findRAByName(const wxString &aName)
std::set< FOOTPRINT * > queryComponentsInGroup(const wxString &aGroupName) const
std::set< BOARD_ITEM * > queryBoardItemsInGroup(const wxString &aGroupName) const
Definition pad.h:55
const wxString & GetNumber() const
Definition pad.h:137
void SetItems(BOARD_ITEM *a, BOARD_ITEM *b=nullptr)
static TOOL_ACTION repeatLayout
static TOOL_ACTION generatePlacementRuleAreas
static TOOL_ACTION selectItemInteractively
Selection of reference points/items.
A set of BOARD_ITEMs (i.e., without duplicates).
Definition pcb_group.h:53
virtual VECTOR2I GetPosition() const override
Definition pcb_text.h:82
virtual void SetPosition(const VECTOR2I &aPos) override
Definition pcb_text.h:84
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition pcb_text.h:86
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
Definition pcb_text.cpp:420
T * frame() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
BOARD * board() const
const PCB_SELECTION & selection() const
A small class to help profiling.
Definition profile.h:49
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:88
std::string to_string()
Definition profile.h:155
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void Move(const VECTOR2I &aVector) override
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
Represent a set of closed polygons.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
virtual void CacheTriangulation(bool aPartition=true, bool aSimplify=false)
Build a polygon triangulation, needed to draw a polygon on OpenGL and in some other calculations.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
static std::unique_ptr< CONNECTION_GRAPH > BuildFromFootprintSet(const std::set< FOOTPRINT * > &aFps, const std::set< FOOTPRINT * > &aOtherChannelFps={})
TOOL_MANAGER * GetManager() const
Return the instance of TOOL_MANAGER that takes care of the tool.
Definition tool_base.h:146
TOOL_MANAGER * m_toolMgr
Definition tool_base.h:220
Generic, UI-independent tool event.
Definition tool_event.h:171
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
Define a general 2D-vector/point.
Definition vector2d.h:71
Handle a list of polygons defining a copper zone.
Definition zone.h:73
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition zone.h:719
void AddPolygon(std::vector< VECTOR2I > &aPolygon)
Add a polygon to the zone outline.
Definition zone.cpp:1166
wxString GetPlacementAreaSource() const
Definition zone.h:724
void HatchBorder()
Compute the hatch lines depending on the hatch parameters and stores it in the zone's attribute m_bor...
Definition zone.cpp:1293
PLACEMENT_SOURCE_T GetPlacementAreaSourceType() const
Definition zone.h:726
SHAPE_POLY_SET * Outline()
Definition zone.h:340
const wxString & GetZoneName() const
Definition zone.h:163
bool GetPlacementAreaEnabled() const
Definition zone.h:721
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition zone.h:136
void UnHatchBorder()
Clear the zone's hatch.
Definition zone.cpp:1287
void RemoveAllContours(void)
Definition zone.h:559
void BuildConvexHull(std::vector< VECTOR2I > &aResult, const std::vector< VECTOR2I > &aPoly)
Calculate the convex hull of a list of points in counter-clockwise order.
#define _(s)
@ NO_RECURSE
Definition eda_item.h:53
static wxString FormatComponentList(const std::set< FOOTPRINT * > &aComponents)
static const wxString traceMultichannelTool
static void ShowTopologyMismatchReasons(wxWindow *aParent, const wxString &aSummary, const std::vector< wxString > &aReasons)
SHAPE_LINE_CHAIN RectifyPolygon(const SHAPE_LINE_CHAIN &aPoly)
void CollectBoxCorners(const BOX2I &aBox, std::vector< VECTOR2I > &aCorners)
Add the 4 corners of a BOX2I to a vector.
std::map< FOOTPRINT *, FOOTPRINT * > COMPONENT_MATCHES
Definition topo_match.h:184
Class to handle a set of BOARD_ITEMs.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
Utility functions for working with shapes.
void AccumulateDescription(wxString &aDesc, const wxString &aItem)
Utility to build comma separated lists in messages.
std::vector< wxString > m_mismatchReasons
std::unordered_set< BOARD_ITEM * > m_affectedItems
Filled in by copyRuleAreaContents with items that were affected by the copy operation.
TMATCH::COMPONENT_MATCHES m_matchingComponents
std::unordered_set< BOARD_ITEM * > m_groupableItems
Filled in by copyRuleAreaContents with affected items that can be grouped together.
VECTOR2I m_center
std::unordered_set< EDA_ITEM * > m_designBlockItems
wxString m_sheetName
PLACEMENT_SOURCE_T m_sourceType
wxString m_componentClass
std::set< FOOTPRINT * > m_components
wxString m_ruleName
wxString m_groupName
wxString m_sheetPath
std::atomic< bool > * m_cancelled
Definition topo_match.h:49
std::atomic< int > * m_matchedComponents
Definition topo_match.h:50
std::atomic< int > * m_totalComponents
Definition topo_match.h:51
bool copied
wxString result
Test unit parsing edge cases and error handling.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
static thread_pool * tp
BS::priority_thread_pool thread_pool
Definition thread_pool.h:31
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
Definition typeinfo.h:111
@ PCB_ZONE_T
class ZONE, a copper pour area
Definition typeinfo.h:108
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
Definition typeinfo.h:92
@ 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
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
#define PR_CAN_ABORT