52#include <wx/richmsgdlg.h>
56#define MULTICHANNEL_EXTRA_DEBUG
62 const std::vector<wxString>& aReasons )
64 if( !aParent || aReasons.empty() )
69 for(
size_t idx = 0; idx < aReasons.size(); ++idx )
72 reasonText += wxT(
"\n" );
74 reasonText += aReasons[idx];
77 wxRichMessageDialog dlg( aParent, aSummary,
_(
"Topology mismatch" ), wxICON_ERROR | wxOK );
78 dlg.ShowDetailedText( reasonText );
94 const std::vector<wxString>& aReasons )
const
96 wxWindow* parent = aParent ? aParent :
frame();
109 std::set<FOOTPRINT*>& aComponents )
111 if( !aRuleArea || !aRuleArea->
m_zone )
122 aComponents.insert(
static_cast<FOOTPRINT*
>( item ) );
125 return (
int) aComponents.size();
134 [&](
const wxString& aMessage,
int aOffset )
164 auto ok = compiler.
Compile( ruleText, &ucode, &preflightCtx );
178 fp->GetSheetname() );
180 aComponents.insert( fp );
190 if( !aRuleArea || !aRuleArea->
m_zone )
206 if( !boardItem->IsConnected() )
207 aItems.insert( boardItem );
211 return aItems.size() > 0;
214 std::vector<BOARD_ITEM*>
result;
221 [&](
const wxString& aMessage,
int aOffset )
230 bool restoreBlankName =
false;
234 restoreBlankName =
true;
238 wxString ruleText = wxString::Format( wxT(
"A.enclosedByArea('%s')" ), aRuleArea->
m_zone->
GetZoneName() );
240 if( !compiler.
Compile( ruleText, &ucode, &preflightCtx ) )
242 if( restoreBlankName )
252 auto val = ucode.
Run( &ctx );
254 if( val->AsDouble() != 0.0 )
255 aItems.insert( aItem );
260 if( zone == aRuleArea->
m_zone )
268 if( !drawing->IsConnected() )
269 testAndAdd( drawing );
272 if( restoreBlankName )
281 std::set<FOOTPRINT*> rv;
283 if( aSheetName.EndsWith( wxT(
"/" ) ) )
284 aSheetName.RemoveLast();
288 auto sn = fp->GetSheetname();
289 if( sn.EndsWith( wxT(
"/" ) ) )
292 if( sn == aSheetName )
303 std::set<FOOTPRINT*> rv;
307 if( fp->GetComponentClass()->ContainsClassName( aComponentClassName ) )
317 std::set<FOOTPRINT*> rv;
321 if(
group->GetName() == aGroupName )
326 rv.insert(
static_cast<FOOTPRINT*
>( item ) );
337 std::vector<VECTOR2I> bbCorners;
338 bbCorners.reserve( aFootprints.size() * 4 );
342 const BOX2I bb = fp->GetBoundingBox(
false ).GetInflated( aMargin );
346 std::vector<VECTOR2I> hullVertices;
358 using PathAndName = std::pair<wxString, wxString>;
359 std::set<PathAndName> uniqueSheets;
360 std::set<wxString> uniqueComponentClasses;
361 std::set<wxString> uniqueGroups;
367 uniqueSheets.insert( PathAndName( fp->GetSheetname(), fp->GetSheetfile() ) );
372 uniqueComponentClasses.insert( singleClass->GetName() );
374 if( fp->GetParentGroup() && !fp->GetParentGroup()->GetName().IsEmpty() )
375 uniqueGroups.insert( fp->GetParentGroup()->GetName() );
378 for(
const PathAndName& sheet : uniqueSheets )
387 m_areas.m_areas.push_back( ent );
392 (
int)
m_areas.m_areas.size() );
395 for(
const wxString& compClass : uniqueComponentClasses )
403 m_areas.m_areas.push_back( ent );
407 static_cast<int>(
m_areas.m_areas.size() ) );
410 for(
const wxString& groupName : uniqueGroups )
418 m_areas.m_areas.push_back( ent );
422 static_cast<int>(
m_areas.m_areas.size() ) );
433 if( !zone->GetIsRuleArea() )
436 if( !zone->GetPlacementAreaEnabled() )
444 area.
m_center = zone->Outline()->COutline( 0 ).Centre();
448 m_areas.m_areas.push_back( area );
478 std::vector<ZONE*> refRAs;
480 auto isSelectedItemAnRA =
486 ZONE* zone =
static_cast<ZONE*
>( aItem );
499 if(
ZONE* zone = isSelectedItemAnRA( item ) )
501 refRAs.push_back( zone );
509 if(
ZONE* grpZone = isSelectedItemAnRA( grpItem ) )
510 refRAs.push_back( grpZone );
515 if( refRAs.size() != 1 )
520 _(
"Select a reference Rule Area to copy from..." ),
523 return isSelectedItemAnRA( aItem ) !=
nullptr;
537 if(
m_areas.m_areas.size() <= 1 )
539 frame()->ShowInfoBarError(
_(
"No Rule Areas to repeat layout to have been found." ),
true );
559 if( ra.
m_zone == aRefZone )
593 wxString summary = wxString::Format(
_(
"Rule Area topologies do not match: %s" ), compat.
m_errorMsg );
604 auto errMsg = wxString::Format(
_(
"Copy Rule Area contents failed between rule areas '%s' and '%s'." ),
605 m_areas.m_refRA->m_zone->GetZoneName(),
611 frame()->ShowInfoBarError( errMsg,
true );
623 frame()->ShowInfoBarError(
_(
"Target group does not have a group." ),
true );
635 group->AddItem( item );
639 commit.
Push(
_(
"Repeat layout" ) );
651 for(
auto& [targetArea, compatData] :
m_areas.m_compatMap )
653 if( !compatData.m_doCopy )
656 targetArea->m_ruleName );
660 if( !compatData.m_isOk )
665 auto errMsg = wxString::Format(
_(
"Copy Rule Area contents failed between rule areas '%s' and '%s'." ),
666 m_areas.m_refRA->m_zone->GetZoneName(),
667 targetArea->m_zone->GetZoneName() );
672 frame()->ShowInfoBarError( errMsg,
true );
681 if(
m_areas.m_options.m_groupItems )
683 for(
const auto& [targetArea, compatData] :
m_areas.m_compatMap )
691 for(
BOARD_ITEM* item : compatData.m_groupableItems )
694 group->AddItem( item );
699 commit.
Push(
_(
"Repeat layout" ) );
702 frame()->ShowInfoBarMsg( wxString::Format(
_(
"Copied to %d Rule Areas." ), totalCopied ),
true );
722 if( ( c >=
'a' && c <=
'z' ) || ( c >=
'A' && c <=
'Z' ) || ( c ==
'_' ) )
733 std::shared_ptr<CONNECTIVITY_DATA> aConnectivity,
736 if( !aRuleArea || !aRuleArea->
m_zone )
758 if( bci->IsConnected() )
759 aOutput.insert( bci );
763 return (
int) aOutput.size();
771 [&](
const wxString& aMessage,
int aOffset )
780 bool restoreBlankName =
false;
784 restoreBlankName =
true;
788 wxString ruleText = wxString::Format( wxT(
"A.enclosedByArea('%s')" ), aRuleArea->
m_zone->
GetZoneName() );
793 if( aOutput.contains( aItem ) )
801 aOutput.insert( aItem );
806 if( compiler.
Compile( ruleText, &ucode, &preflightCtx ) )
813 if( drawing->IsConnected() )
818 if( restoreBlankName )
842 targetAnchorFp = targetFP;
852 disp = newpos - oldpos;
865 newTargetOutline.
Move( disp );
870 std::map<EDA_GROUP*, EDA_GROUP*> groupMap;
872 auto fixupParentGroup =
877 if( !groupMap.contains( parentGroup ) )
880 groupMap[parentGroup] =
static_cast<PCB_GROUP*
>( newGroup );
881 aCommit->
Add( newGroup );
884 destItem->SetParentGroup( groupMap[parentGroup] );
891 bool targetZoneOnBoard =
false;
897 if( z == aTargetArea->
m_zone )
899 targetZoneOnBoard =
true;
905 if( targetZoneOnBoard )
914 std::set<BOARD_CONNECTED_ITEM*> refRouting;
915 std::set<BOARD_CONNECTED_ITEM*> targetRouting;
925 for(
PAD*
pad : refFP->Pads() )
926 refc.insert(
pad->GetNetCode() );
928 for(
PAD*
pad : targetFP->Pads() )
929 targc.insert(
pad->GetNetCode() );
979 fixupParentGroup( item,
copied );
990 std::set<BOARD_ITEM*> sourceItems;
991 std::set<BOARD_ITEM*> targetItems;
1010 ZONE* zone =
static_cast<ZONE*
>( item );
1013 bool layerMismatch =
false;
1019 layerMismatch =
true;
1022 if( !layerMismatch )
1050 ZONE* zone =
static_cast<ZONE*
>( item );
1053 bool layerMismatch =
false;
1061 layerMismatch =
true;
1068 ZONE* targetZone =
static_cast<ZONE*
>( item->Duplicate(
false ) );
1086 fixupParentGroup( item,
copied );
1104 refFP->GetReference() );
1110 refFP->GetReference() );
1115 if( !refFP->GetEffectiveShape( refFP->GetLayer() )->Collide( &refPoly, 0 ) )
1121 aCommit->
Modify( targetFP );
1123 targetFP->SetLayerAndFlip( refFP->GetLayer() );
1124 targetFP->SetOrientation( refFP->GetOrientation() );
1125 targetFP->SetPosition( refFP->GetPosition() );
1126 targetFP->Rotate(
VECTOR2( 0, 0 ), rot );
1127 targetFP->Move( disp );
1129 for(
PCB_FIELD* refField : refFP->GetFields() )
1131 PCB_FIELD* targetField = targetFP->GetField( refField->GetName() );
1132 wxCHECK2( targetField,
continue );
1134 targetField->
SetLayerSet( refField->GetLayerSet() );
1135 targetField->
SetVisible( refField->IsVisible() );
1137 targetField->
SetPosition( refField->GetPosition() );
1139 targetField->
Move( disp );
1164 const std::vector<BOARD_CONNECTED_ITEM*> refConnectedPads = connectivity->
GetNetItems( aRef->
GetNetCode(),
1172 const PAD* refPad =
static_cast<const PAD*
>( refConItem );
1175 if( aComponentMatches.contains( sourceFootprint ) )
1177 const FOOTPRINT* targetFootprint = aComponentMatches[sourceFootprint];
1178 std::vector<const PAD*> targetFpPads = targetFootprint->
GetPads( refPad->
GetNumber() );
1180 if( !targetFpPads.empty() )
1182 int targetNetCode = targetFpPads[0]->GetNet()->GetNetCode();
1200 std::vector<TMATCH::TOPOLOGY_MISMATCH_REASON> mismatchReasons;
1201 bool status = cgRef->FindIsomorphism( cgTarget.get(), aMatches.
m_matchingComponents, mismatchReasons );
1203 aMatches.
m_isOk = status;
1214 for(
const auto& reason : mismatchReasons )
1216 if( reason.m_reason.IsEmpty() )
1219 if( !reason.m_reference.IsEmpty() && !reason.m_candidate.IsEmpty() )
1224 reason.m_reason ) );
1226 else if( !reason.m_reference.IsEmpty() )
1230 reason.m_reason ) );
1246 const std::unordered_set<BOARD_ITEM*>& aItemsToRemove )
1252 std::vector<EDA_ITEM*> pruneList;
1258 if( refItem->m_Uuid == testItem->m_Uuid )
1259 pruneList.push_back( refItem );
1263 if( !pruneList.empty() )
1268 group->RemoveItem( item );
1270 if(
group->GetItems().empty() )
1285 if(
m_areas.m_areas.size() <= 1 )
1287 frame()->ShowInfoBarError(
_(
"Cannot auto-generate any placement areas because the "
1288 "schematic has only one or no hierarchical sheets, "
1289 "groups, or component classes." ),
1297 if( ret != wxID_OK )
1303 if( !zone->GetIsRuleArea() )
1306 if( !zone->GetPlacementAreaEnabled() )
1309 std::set<FOOTPRINT*> components;
1312 zoneRA.
m_sourceType = zone->GetPlacementAreaSourceType();
1315 if( components.empty() )
1325 wxT(
"Placement rule area for sheet '%s' already exists as '%s'\n" ),
1331 wxT(
"Placement rule area for component class '%s' already exists as '%s'\n" ),
1337 wxT(
"Placement rule area for group '%s' already exists as '%s'\n" ),
1364 std::unique_ptr<ZONE> newZone(
new ZONE(
board() ) );
1367 newZone->SetZoneName( wxString::Format( wxT(
"auto-placement-area-%s" ), ra.
m_sheetPath ) );
1369 newZone->SetZoneName( wxString::Format( wxT(
"auto-placement-area-%s" ), ra.
m_componentClass ) );
1371 newZone->SetZoneName( wxString::Format( wxT(
"auto-placement-area-%s" ), ra.
m_groupName ) );
1374 newZone->GetZoneName(),
1377 newZone->SetIsRuleArea(
true );
1379 newZone->SetPlacementAreaEnabled(
true );
1380 newZone->SetDoNotAllowZoneFills(
false );
1381 newZone->SetDoNotAllowVias(
false );
1382 newZone->SetDoNotAllowTracks(
false );
1383 newZone->SetDoNotAllowPads(
false );
1384 newZone->SetDoNotAllowFootprints(
false );
1389 newZone->SetPlacementAreaSource( ra.
m_sheetPath );
1399 newZone->SetPlacementAreaSource( ra.
m_groupName );
1402 newZone->AddPolygon( raOutline );
1410 ra.
m_zone = newZone.release();
1417 if(
m_areas.m_options.m_groupItems )
1427 std::unordered_set<BOARD_ITEM*> toPrune;
1432 toPrune.insert( ra.
m_zone );
1446 group->AddItem( fp );
1451 commit.
Push(
_(
"Auto-generate placement rule areas" ) );
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...
virtual BOARD_ITEM * Duplicate(bool addToParentGroup, BOARD_COMMIT *aCommit=nullptr) const
Create a copy of this BOARD_ITEM.
virtual void SetLayerSet(const LSET &aLayers)
virtual void SetIsKnockout(bool aKnockout)
FOOTPRINT * GetParentFootprint() const
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Return a list of missing connections between components/tracks.
Represent a set of changes (additions, deletions or modifications) of a data model (e....
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
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.
A set of EDA_ITEMs (i.e., without duplicates).
A base class for most all the KiCad significant classes used in schematics and boards.
virtual EDA_GROUP * GetParentGroup() const
KICAD_T Type() const
Returns the type of object.
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
virtual void SetVisible(bool aVisible)
wxString AsString() const
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.
static LSET AllCuMask(int aCuLayerCount)
Return a mask holding the requested number of Cu PCB_LAYER_IDs.
bool Contains(PCB_LAYER_ID aLayer) const
See if the layer set contains a PCB layer.
const wxString & GetNumber() const
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).
virtual void SetPosition(const VECTOR2I &aPos) override
void Move(const VECTOR2I &aMoveVector) override
Move this object.
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate this object.
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)
Define a general 2D-vector/point.
Handle a list of polygons defining a copper zone.
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
void AddPolygon(std::vector< VECTOR2I > &aPolygon)
Add a polygon to the zone outline.
wxString GetPlacementAreaSource() const
void HatchBorder()
Compute the hatch lines depending on the hatch parameters and stores it in the zone's attribute m_bor...
PLACEMENT_SOURCE_T GetPlacementAreaSourceType() const
SHAPE_POLY_SET * Outline()
const wxString & GetZoneName() const
bool GetPlacementAreaEnabled() const
virtual LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
void SetZoneName(const wxString &aName)
void UnHatchBorder()
Clear the zone's hatch.
void RemoveAllContours(void)
void BuildConvexHull(std::vector< VECTOR2I > &aResult, const std::vector< VECTOR2I > &aPoly)
Calculate the convex hull of a list of points in counter-clockwise order.
PCB_LAYER_ID
A quick note on layer IDs:
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
Class to handle a set of BOARD_ITEMs.
PGM_BASE & Pgm()
The global program "get" accessor.
Utility functions for working with shapes.
bool m_connectedRoutingOnly
bool m_includeLockedItems
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.
std::unordered_set< EDA_ITEM * > m_designBlockItems
PLACEMENT_SOURCE_T m_sourceType
wxString m_componentClass
std::set< FOOTPRINT * > m_components
wxString result
Test unit parsing edge cases and error handling.
@ PCB_GROUP_T
class PCB_GROUP, a set of BOARD_ITEMs
@ PCB_ZONE_T
class ZONE, a copper pour area
@ PCB_TEXT_T
class PCB_TEXT, text on a layer
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
@ PCB_PAD_T
class PAD, a pad in a footprint
VECTOR2< int32_t > VECTOR2I