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 )
208 aItems.insert(
static_cast<BOARD_ITEM*
>( item ) );
211 return (
int) aItems.size();
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 )
267 testAndAdd( drawing );
272 bool addGroup =
true;
274 group->RunOnChildren(
277 if( aItem->
IsType( { PCB_ZONE_T, PCB_SHAPE_T, PCB_BARCODE_T, PCB_DIMENSION_T } ) )
289 aItems.insert(
group );
292 if( restoreBlankName )
301 std::set<FOOTPRINT*> rv;
303 if( aSheetName.EndsWith( wxT(
"/" ) ) )
304 aSheetName.RemoveLast();
308 auto sn = fp->GetSheetname();
309 if( sn.EndsWith( wxT(
"/" ) ) )
312 if( sn == aSheetName )
323 std::set<FOOTPRINT*> rv;
327 if( fp->GetComponentClass()->ContainsClassName( aComponentClassName ) )
337 std::set<FOOTPRINT*> rv;
341 if(
group->GetName() == aGroupName )
346 rv.insert(
static_cast<FOOTPRINT*
>( item ) );
358 std::vector<VECTOR2I> bbCorners;
359 bbCorners.reserve( aFootprints.size() * 4 );
363 const BOX2I bb = fp->GetBoundingBox(
false ).GetInflated( aMargin );
367 std::vector<VECTOR2I> hullVertices;
379 using PathAndName = std::pair<wxString, wxString>;
380 std::set<PathAndName> uniqueSheets;
381 std::set<wxString> uniqueComponentClasses;
382 std::set<wxString> uniqueGroups;
388 uniqueSheets.insert( PathAndName( fp->GetSheetname(), fp->GetSheetfile() ) );
393 uniqueComponentClasses.insert( singleClass->GetName() );
395 if( fp->GetParentGroup() && !fp->GetParentGroup()->GetName().IsEmpty() )
396 uniqueGroups.insert( fp->GetParentGroup()->GetName() );
399 for(
const PathAndName& sheet : uniqueSheets )
408 m_areas.m_areas.push_back( ent );
413 (
int)
m_areas.m_areas.size() );
416 for(
const wxString& compClass : uniqueComponentClasses )
424 m_areas.m_areas.push_back( ent );
428 static_cast<int>(
m_areas.m_areas.size() ) );
431 for(
const wxString& groupName : uniqueGroups )
439 m_areas.m_areas.push_back( ent );
443 static_cast<int>(
m_areas.m_areas.size() ) );
454 if( !zone->GetIsRuleArea() )
457 if( !zone->GetPlacementAreaEnabled() )
465 area.
m_center = zone->Outline()->COutline( 0 ).Centre();
469 m_areas.m_areas.push_back( area );
499 std::vector<ZONE*> refRAs;
501 auto isSelectedItemAnRA =
507 ZONE* zone =
static_cast<ZONE*
>( aItem );
520 if(
auto zone = isSelectedItemAnRA( item ) )
522 refRAs.push_back(zone);
530 if(
auto grpZone = isSelectedItemAnRA( grpItem ) )
531 refRAs.push_back( grpZone );
536 if( refRAs.size() != 1 )
541 _(
"Select a reference Rule Area to copy from..." ),
544 return isSelectedItemAnRA( aItem ) !=
nullptr;
558 if(
m_areas.m_areas.size() <= 1 )
560 frame()->ShowInfoBarError(
_(
"No Rule Areas to repeat layout to have been found." ),
true );
580 if( ra.
m_zone == aRefZone )
614 wxString summary = wxString::Format(
_(
"Rule Area topologies do not match: %s" ), compat.
m_errorMsg );
624 auto errMsg = wxString::Format(
_(
"Copy Rule Area contents failed between rule areas '%s' and '%s'." ),
631 frame()->ShowInfoBarError( errMsg,
true );
645 frame()->ShowInfoBarError(
_(
"Target group does not have a group." ),
true );
658 group->AddItem( item );
662 commit.
Push(
_(
"Repeat layout" ) );
673 for(
auto& targetArea :
m_areas.m_compatMap )
675 if( !targetArea.second.m_doCopy )
678 targetArea.first->m_ruleName );
682 if( !targetArea.second.m_isOk )
687 auto errMsg = wxString::Format(
688 _(
"Copy Rule Area contents failed between rule areas '%s' and '%s'." ),
689 m_areas.m_refRA->m_zone->GetZoneName(),
690 targetArea.first->m_zone->GetZoneName() );
695 frame()->ShowInfoBarError( errMsg,
true );
703 if(
m_areas.m_options.m_groupItems )
705 for(
const auto& [targetArea, compatData] :
m_areas.m_compatMap )
713 for(
BOARD_ITEM* item : compatData.m_groupableItems )
716 group->AddItem( item );
721 commit.
Push(
_(
"Repeat layout" ) );
724 frame()->ShowInfoBarMsg( wxString::Format(
_(
"Copied to %d Rule Areas." ), totalCopied ),
true );
744 if( ( c >=
'a' && c <=
'z' ) || ( c >=
'A' && c <=
'Z' ) || ( c ==
'_' ) )
755 std::shared_ptr<CONNECTIVITY_DATA> aConnectivity,
758 if( !aRuleArea || !aRuleArea->
m_zone )
779 aOutput.insert( bci );
782 return (
int) aOutput.size();
789 auto reportError = [&](
const wxString& aMessage,
int aOffset )
798 bool restoreBlankName =
false;
802 restoreBlankName =
true;
806 wxString ruleText = wxString::Format( wxT(
"A.enclosedByArea('%s')" ), aRuleArea->
m_zone->
GetZoneName() );
811 if( aOutput.contains( aItem ) )
819 aOutput.insert( aItem );
824 if( compiler.
Compile( ruleText, &ucode, &preflightCtx ) )
830 if( restoreBlankName )
854 targetAnchorFp = targetFP;
864 disp = newpos - oldpos;
877 newTargetOutline.
Move( disp );
886 bool targetZoneOnBoard =
false;
892 if( z == aTargetArea->
m_zone )
894 targetZoneOnBoard =
true;
900 if( targetZoneOnBoard )
909 std::set<BOARD_CONNECTED_ITEM*> refRouting;
910 std::set<BOARD_CONNECTED_ITEM*> targetRouting;
920 for(
PAD*
pad : refFP->Pads() )
921 refc.insert(
pad->GetNetCode() );
923 for(
PAD*
pad : targetFP->Pads() )
924 targc.insert(
pad->GetNetCode() );
984 std::set<BOARD_ITEM*> sourceItems;
985 std::set<BOARD_ITEM*> targetItems;
1012 ZONE* zone =
static_cast<ZONE*
>( item );
1015 bool layerMismatch =
false;
1021 layerMismatch =
true;
1024 if( !layerMismatch )
1057 ZONE* zone =
static_cast<ZONE*
>( item );
1060 bool layerMismatch =
false;
1068 layerMismatch =
true;
1075 ZONE* targetZone =
static_cast<ZONE*
>( item->Duplicate(
false ) );
1099 refFP->GetReference() );
1105 refFP->GetReference() );
1110 if( !refFP->GetEffectiveShape( refFP->GetLayer() )->Collide( &refPoly, 0 ) )
1116 aCommit->
Modify( targetFP );
1118 targetFP->SetLayerAndFlip( refFP->GetLayer() );
1119 targetFP->SetOrientation( refFP->GetOrientation() );
1120 targetFP->SetPosition( refFP->GetPosition() );
1121 targetFP->Rotate(
VECTOR2( 0, 0 ), rot );
1122 targetFP->Move( disp );
1123 for(
PCB_FIELD* refField : refFP->GetFields() )
1125 if( !refField->IsVisible() )
1128 PCB_FIELD* targetField = targetFP->GetField( refField->GetName() );
1129 wxCHECK2( targetField,
continue );
1132 targetField->
SetPosition( refField->GetPosition() );
1134 targetField->
Move( disp );
1159 const std::vector<BOARD_CONNECTED_ITEM*> refConnectedPads = connectivity->
GetNetItems( aRef->
GetNetCode(),
1167 const PAD* refPad =
static_cast<const PAD*
>( refConItem );
1170 if( aComponentMatches.contains( sourceFootprint ) )
1172 const FOOTPRINT* targetFootprint = aComponentMatches[sourceFootprint];
1173 std::vector<const PAD*> targetFpPads = targetFootprint->
GetPads( refPad->
GetNumber() );
1175 if( !targetFpPads.empty() )
1177 int targetNetCode = targetFpPads[0]->GetNet()->GetNetCode();
1195 std::vector<TMATCH::TOPOLOGY_MISMATCH_REASON> mismatchReasons;
1196 bool status = cgRef->FindIsomorphism( cgTarget.get(), aMatches.
m_matchingComponents, mismatchReasons );
1198 aMatches.
m_isOk = status;
1209 for(
const auto& reason : mismatchReasons )
1211 if( reason.m_reason.IsEmpty() )
1214 if( !reason.m_reference.IsEmpty() && !reason.m_candidate.IsEmpty() )
1219 reason.m_reason ) );
1221 else if( !reason.m_reference.IsEmpty() )
1225 reason.m_reason ) );
1241 const std::unordered_set<BOARD_ITEM*>& aItemsToRemove )
1247 std::vector<EDA_ITEM*> pruneList;
1253 if( refItem->m_Uuid == testItem->m_Uuid )
1254 pruneList.push_back( refItem );
1258 if( !pruneList.empty() )
1263 group->RemoveItem( item );
1265 if(
group->GetItems().empty() )
1280 if(
m_areas.m_areas.size() <= 1 )
1282 frame()->ShowInfoBarError(
_(
"Cannot auto-generate any placement areas because the "
1283 "schematic has only one or no hierarchical sheets, "
1284 "groups, or component classes." ),
1292 if( ret != wxID_OK )
1298 if( !zone->GetIsRuleArea() )
1301 if( !zone->GetPlacementAreaEnabled() )
1304 std::set<FOOTPRINT*> components;
1307 zoneRA.
m_sourceType = zone->GetPlacementAreaSourceType();
1310 if( components.empty() )
1320 wxT(
"Placement rule area for sheet '%s' already exists as '%s'\n" ),
1326 wxT(
"Placement rule area for component class '%s' already exists as '%s'\n" ),
1332 wxT(
"Placement rule area for group '%s' already exists as '%s'\n" ),
1359 std::unique_ptr<ZONE> newZone(
new ZONE(
board() ) );
1362 newZone->SetZoneName( wxString::Format( wxT(
"auto-placement-area-%s" ), ra.
m_sheetPath ) );
1364 newZone->SetZoneName( wxString::Format( wxT(
"auto-placement-area-%s" ), ra.
m_componentClass ) );
1366 newZone->SetZoneName( wxString::Format( wxT(
"auto-placement-area-%s" ), ra.
m_groupName ) );
1369 newZone->GetZoneName(),
1372 newZone->SetIsRuleArea(
true );
1374 newZone->SetPlacementAreaEnabled(
true );
1375 newZone->SetDoNotAllowZoneFills(
false );
1376 newZone->SetDoNotAllowVias(
false );
1377 newZone->SetDoNotAllowTracks(
false );
1378 newZone->SetDoNotAllowPads(
false );
1379 newZone->SetDoNotAllowFootprints(
false );
1384 newZone->SetPlacementAreaSource( ra.
m_sheetPath );
1394 newZone->SetPlacementAreaSource( ra.
m_groupName );
1397 newZone->AddPolygon( raOutline );
1405 ra.
m_zone = newZone.release();
1412 if(
m_areas.m_options.m_groupItems )
1422 std::unordered_set<BOARD_ITEM*> toPrune;
1427 toPrune.insert( ra.
m_zone );
1441 group->AddItem( fp );
1446 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 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.
KICAD_T Type() const
Returns the type of object.
virtual bool IsType(const std::vector< KICAD_T > &aScanTypes) const
Check whether the item is one of the listed types.
void SetAttributes(const EDA_TEXT &aSrc, bool aSetPosition=true)
Set the text attributes from another instance.
bool IsBOARD_ITEM() const
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