From e661545ecff810d1442610669e977919c3e6f1b2 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Thu, 25 Aug 2022 10:05:30 -0700 Subject: [PATCH] Choose the first unused priority value for zones Default the new zone priority value to the lowest available unused priority. Ensures that, by default, a new zone has a unique priority level preventing zones from unintenionally shorting --- pcbnew/tools/zone_create_helper.cpp | 43 ++++++++++++++++++++++------- pcbnew/tools/zone_create_helper.h | 7 +++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/pcbnew/tools/zone_create_helper.cpp b/pcbnew/tools/zone_create_helper.cpp index 0fbf54e20c..99eead02e0 100644 --- a/pcbnew/tools/zone_create_helper.cpp +++ b/pcbnew/tools/zone_create_helper.cpp @@ -54,6 +54,37 @@ ZONE_CREATE_HELPER::~ZONE_CREATE_HELPER() } +void ZONE_CREATE_HELPER::setUniquePriority( ZONE_SETTINGS& aZoneInfo ) +{ + PCB_BASE_EDIT_FRAME* frame = m_tool.getEditFrame(); + BOARD* board = frame->GetBoard(); + + // By default, new zones get the first unused priority + std::set priorities; + + for( ZONE* zone : board->Zones() ) + { + if( zone->GetTeardropAreaType() == TEARDROP_TYPE::TD_NONE + && ( zone->GetLayerSet() & LSET::AllCuMask() ).any() ) + { + priorities.insert( zone->GetAssignedPriority() ); + } + } + + unsigned priority = 0; + + for( unsigned exist_priority : priorities ) + { + if( priority != exist_priority ) + break; + + ++priority; + } + + aZoneInfo.m_ZonePriority = priority; +} + + std::unique_ptr ZONE_CREATE_HELPER::createNewZone( bool aKeepout ) { PCB_BASE_EDIT_FRAME* frame = m_tool.getEditFrame(); @@ -68,20 +99,12 @@ std::unique_ptr ZONE_CREATE_HELPER::createNewZone( bool aKeepout ) zoneInfo.m_NetcodeSelection = highlightedNets.empty() ? -1 : *highlightedNets.begin(); zoneInfo.SetIsRuleArea( m_params.m_keepout ); - // By default, new zones should have the highest available priority if( m_params.m_mode != ZONE_MODE::GRAPHIC_POLYGON && ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() ) { - unsigned priority = 0; - - for( ZONE* zone : board->Zones() ) - { - if( zone->GetTeardropAreaType() == TEARDROP_TYPE::TD_NONE ) - priority = std::max( priority, zone->GetAssignedPriority() ); - } - - zoneInfo.m_ZonePriority = static_cast( priority + 1 ); + setUniquePriority( zoneInfo ); } + // If we don't have a net from highlighting, maybe we can get one from the selection PCB_SELECTION_TOOL* selectionTool = m_tool.GetManager()->GetTool(); diff --git a/pcbnew/tools/zone_create_helper.h b/pcbnew/tools/zone_create_helper.h index aa2d793faa..f6ac419ad0 100644 --- a/pcbnew/tools/zone_create_helper.h +++ b/pcbnew/tools/zone_create_helper.h @@ -117,6 +117,13 @@ public: void commitZone( std::unique_ptr aZone ); private: + + /** + * Choose a new priority for @aZoneInfo. This will be the lowest unused zone priority number + * @param aZoneInfo ZONE_SETTINGS to apply the new priority number to + */ + void setUniquePriority( ZONE_SETTINGS& aZoneInfo ); + DRAWING_TOOL& m_tool; ///< Parameters of the zone to be drawn