2017-02-26 17:45:52 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
2019-01-23 14:39:08 +00:00
|
|
|
*
|
2022-12-09 16:11:56 +00:00
|
|
|
* Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-02-26 17:45:52 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
Fix issues with zone filling connectivity locking
Two issues found with the locking system used to prevent access to
stale connectivity data during the zone fill process:
1) a std::mutex has undefined behavior if you try to use it to guard
against access from the same thread. Because of the use of wx event
loops (and coroutines) it is entirely possible, and in some situations
inevitable, that the same thread will try to redraw the ratsnest in the
middle of zone refilling.
2) The mutex was only guarding the ZONE_FILLER::Fill method, but the callers
of that method also do connectivity updates as part of the COMMIT::Push.
Redrawing the ratsnest after the Fill but before the Push will result in
stale connectivity pointers to zone filled areas.
Fixed (1) by switching to a trivial spinlock implementation. Spinlocks would
generally not be desirable if the contention for the connectivity data crossed
thread boundaries, but at the moment I believe it's guaranteed that the reads
and writes to connectivity that are guarded by this lock happen from the main
UI thread. The writes are also quite rare compared to reads, and reads are
generally fast, so I'm not really worried about the UI thread spinning for any
real amount of time.
Fixed (2) by moving the locking location up to the call sites of
ZONE_FILLER::Fill.
This issue was quite difficult to reproduce, but I found a fairly reliable way:
It only happens (for me) on Windows, MSYS2 build, with wxWidgets 3.0
It also only happens if I restrict PcbNew to use 2 CPU cores.
With those conditions, I can reproduce the issue described in #6471 by
repeatedly editing a zone properties and changing its net. The crash is
especially easy to trigger if you press some keys (such as 'e' for edit)
while the progress dialog is displayed. It's easiest to do this in a debug
build as the slower KiCad is running, the bigger the window is to trigger this
bug.
Fixes https://gitlab.com/kicad/code/kicad/-/issues/6471
Fixes https://gitlab.com/kicad/code/kicad/-/issues/7048
2021-01-18 17:24:07 +00:00
|
|
|
#include <core/spinlock.h>
|
|
|
|
#include <connectivity/connectivity_data.h>
|
2017-02-26 17:45:52 +00:00
|
|
|
#include <tools/zone_create_helper.h>
|
|
|
|
#include <tool/tool_manager.h>
|
2020-11-11 23:05:59 +00:00
|
|
|
#include <zone.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <pcb_shape.h>
|
2021-06-03 18:05:43 +00:00
|
|
|
#include <footprint.h>
|
2020-10-04 23:34:59 +00:00
|
|
|
#include <fp_shape.h>
|
2017-02-26 17:45:52 +00:00
|
|
|
#include <board_commit.h>
|
2021-06-06 19:03:10 +00:00
|
|
|
#include <board_design_settings.h>
|
2017-02-26 17:45:52 +00:00
|
|
|
#include <pcb_painter.h>
|
2021-03-17 18:57:07 +00:00
|
|
|
#include <pcbnew_settings.h>
|
2017-02-26 17:45:52 +00:00
|
|
|
#include <tools/pcb_actions.h>
|
2020-12-16 13:31:32 +00:00
|
|
|
#include <tools/pcb_selection_tool.h>
|
2017-11-30 16:22:45 +00:00
|
|
|
#include <zone_filler.h>
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2019-01-23 14:39:08 +00:00
|
|
|
ZONE_CREATE_HELPER::ZONE_CREATE_HELPER( DRAWING_TOOL& aTool, PARAMS& aParams ):
|
2017-02-26 17:45:52 +00:00
|
|
|
m_tool( aTool ),
|
|
|
|
m_params( aParams ),
|
|
|
|
m_parentView( *aTool.getView() )
|
|
|
|
{
|
|
|
|
m_parentView.Add( &m_previewItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZONE_CREATE_HELPER::~ZONE_CREATE_HELPER()
|
|
|
|
{
|
|
|
|
// remove the preview from the view
|
|
|
|
m_parentView.SetVisible( &m_previewItem, false );
|
|
|
|
m_parentView.Remove( &m_previewItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-25 17:05:30 +00:00
|
|
|
void ZONE_CREATE_HELPER::setUniquePriority( ZONE_SETTINGS& aZoneInfo )
|
|
|
|
{
|
|
|
|
PCB_BASE_EDIT_FRAME* frame = m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
|
|
|
|
BOARD* board = frame->GetBoard();
|
|
|
|
|
|
|
|
// By default, new zones get the first unused priority
|
|
|
|
std::set<unsigned> priorities;
|
|
|
|
|
|
|
|
for( ZONE* zone : board->Zones() )
|
|
|
|
{
|
|
|
|
if( zone->GetTeardropAreaType() == TEARDROP_TYPE::TD_NONE
|
2022-08-25 17:12:58 +00:00
|
|
|
&& ( zone->GetLayerSet() & LSET::AllCuMask() ).any()
|
|
|
|
&& !zone->GetIsRuleArea() )
|
2022-08-25 17:05:30 +00:00
|
|
|
{
|
|
|
|
priorities.insert( zone->GetAssignedPriority() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned priority = 0;
|
|
|
|
|
|
|
|
for( unsigned exist_priority : priorities )
|
|
|
|
{
|
|
|
|
if( priority != exist_priority )
|
|
|
|
break;
|
|
|
|
|
|
|
|
++priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
aZoneInfo.m_ZonePriority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
std::unique_ptr<ZONE> ZONE_CREATE_HELPER::createNewZone( bool aKeepout )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2020-08-09 11:22:09 +00:00
|
|
|
PCB_BASE_EDIT_FRAME* frame = m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
|
|
|
|
BOARD* board = frame->GetBoard();
|
2020-03-12 13:42:32 +00:00
|
|
|
BOARD_ITEM_CONTAINER* parent = m_tool.m_frame->GetModel();
|
2018-11-11 13:09:02 +00:00
|
|
|
KIGFX::VIEW_CONTROLS* controls = m_tool.GetManager()->GetViewControls();
|
2020-08-09 11:22:09 +00:00
|
|
|
std::set<int> highlightedNets = board->GetHighLightNetCodes();
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
// Get the current default settings for zones
|
2020-08-09 11:22:09 +00:00
|
|
|
ZONE_SETTINGS zoneInfo = frame->GetZoneSettings();
|
2020-06-24 02:19:08 +00:00
|
|
|
zoneInfo.m_Layers.reset().set( m_params.m_layer ); // TODO(JE) multilayer defaults?
|
2020-08-09 11:22:09 +00:00
|
|
|
zoneInfo.m_NetcodeSelection = highlightedNets.empty() ? -1 : *highlightedNets.begin();
|
2020-09-21 23:32:07 +00:00
|
|
|
zoneInfo.SetIsRuleArea( m_params.m_keepout );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2022-08-24 23:34:53 +00:00
|
|
|
if( m_params.m_mode != ZONE_MODE::GRAPHIC_POLYGON
|
|
|
|
&& ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() )
|
|
|
|
{
|
2022-08-25 17:05:30 +00:00
|
|
|
setUniquePriority( zoneInfo );
|
2022-08-24 23:34:53 +00:00
|
|
|
}
|
2022-08-25 17:05:30 +00:00
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// If we don't have a net from highlighting, maybe we can get one from the selection
|
2020-12-16 13:31:32 +00:00
|
|
|
PCB_SELECTION_TOOL* selectionTool = m_tool.GetManager()->GetTool<PCB_SELECTION_TOOL>();
|
2020-06-23 21:16:34 +00:00
|
|
|
|
|
|
|
if( selectionTool && !selectionTool->GetSelection().Empty()
|
|
|
|
&& zoneInfo.m_NetcodeSelection == -1 )
|
|
|
|
{
|
|
|
|
EDA_ITEM* item = *selectionTool->GetSelection().GetItems().begin();
|
|
|
|
|
|
|
|
if( BOARD_CONNECTED_ITEM* bci = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
|
|
|
|
zoneInfo.m_NetcodeSelection = bci->GetNetCode();
|
|
|
|
}
|
|
|
|
|
2019-06-24 15:27:05 +00:00
|
|
|
if( m_params.m_mode != ZONE_MODE::GRAPHIC_POLYGON )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2017-10-19 21:14:01 +00:00
|
|
|
// Show options dialog
|
2018-05-25 14:56:04 +00:00
|
|
|
int dialogResult;
|
2017-10-19 21:14:01 +00:00
|
|
|
|
|
|
|
if( m_params.m_keepout )
|
2020-09-21 23:32:07 +00:00
|
|
|
dialogResult = InvokeRuleAreaEditor( frame, &zoneInfo );
|
2023-08-27 09:52:02 +00:00
|
|
|
else if( ( zoneInfo.m_Layers & LSET::AllCuMask() ).any() )
|
|
|
|
dialogResult = InvokeCopperZonesEditor( frame, &zoneInfo );
|
2017-02-26 17:45:52 +00:00
|
|
|
else
|
2023-08-27 09:52:02 +00:00
|
|
|
dialogResult = InvokeNonCopperZonesEditor( frame, &zoneInfo );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2018-05-25 14:56:04 +00:00
|
|
|
if( dialogResult == wxID_CANCEL )
|
2017-10-19 21:14:01 +00:00
|
|
|
return nullptr;
|
2018-11-11 13:09:02 +00:00
|
|
|
|
2022-05-16 22:41:30 +00:00
|
|
|
controls->WarpMouseCursor( controls->GetCursorPosition(), true );
|
2023-08-27 09:52:02 +00:00
|
|
|
frame->GetCanvas()->SetFocus();
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
// The new zone is a ZONE if created in the board editor and a FP_ZONE if created in the
|
|
|
|
// footprint editor
|
2020-11-13 12:21:02 +00:00
|
|
|
wxASSERT( !m_tool.m_isFootprintEditor || ( parent->Type() == PCB_FOOTPRINT_T ) );
|
2019-10-26 15:49:29 +00:00
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
std::unique_ptr<ZONE> newZone = m_tool.m_isFootprintEditor ?
|
|
|
|
std::make_unique<FP_ZONE>( parent ) :
|
|
|
|
std::make_unique<ZONE>( parent );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
// Apply the selected settings
|
|
|
|
zoneInfo.ExportSetting( *newZone );
|
|
|
|
|
|
|
|
return newZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
std::unique_ptr<ZONE> ZONE_CREATE_HELPER::createZoneFromExisting( const ZONE& aSrcZone )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2020-11-11 23:05:59 +00:00
|
|
|
BOARD* board = m_tool.getModel<BOARD>();
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
std::unique_ptr<ZONE> newZone = std::make_unique<ZONE>( board );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
ZONE_SETTINGS zoneSettings;
|
|
|
|
zoneSettings << aSrcZone;
|
|
|
|
|
|
|
|
zoneSettings.ExportSetting( *newZone );
|
|
|
|
|
|
|
|
return newZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-06 09:27:41 +00:00
|
|
|
void ZONE_CREATE_HELPER::performZoneCutout( ZONE& aZone, const ZONE& aCutout )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2018-07-26 14:04:31 +00:00
|
|
|
BOARD_COMMIT commit( &m_tool );
|
2020-11-11 23:05:59 +00:00
|
|
|
std::vector<ZONE*> newZones;
|
2018-07-26 14:04:31 +00:00
|
|
|
|
2019-01-23 14:39:08 +00:00
|
|
|
// Clear the selection before removing the old zone
|
|
|
|
auto toolMgr = m_tool.GetManager();
|
|
|
|
toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
|
|
|
|
|
2018-07-26 14:04:31 +00:00
|
|
|
SHAPE_POLY_SET originalOutline( *aZone.Outline() );
|
2018-07-26 13:29:37 +00:00
|
|
|
originalOutline.BooleanSubtract( *aCutout.Outline(), SHAPE_POLY_SET::PM_FAST );
|
2018-07-26 14:04:31 +00:00
|
|
|
|
2023-08-27 09:21:47 +00:00
|
|
|
// After substracting the hole, originalOutline can have more than one main outline.
|
|
|
|
// But a zone can have only one main outline, so create as many zones as originalOutline
|
|
|
|
// contains main outlines:
|
2020-03-12 13:42:32 +00:00
|
|
|
for( int outline = 0; outline < originalOutline.OutlineCount(); outline++ )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2018-07-26 13:29:37 +00:00
|
|
|
auto newZoneOutline = new SHAPE_POLY_SET;
|
2020-03-12 13:42:32 +00:00
|
|
|
newZoneOutline->AddOutline( originalOutline.Outline( outline ) );
|
2018-07-26 14:04:31 +00:00
|
|
|
|
2023-08-27 09:21:47 +00:00
|
|
|
// Add holes (if any) to the new zone outline:
|
2020-03-12 13:42:32 +00:00
|
|
|
for (int hole = 0; hole < originalOutline.HoleCount( outline ) ; hole++ )
|
|
|
|
newZoneOutline->AddHole( originalOutline.CHole( outline, hole ) );
|
2018-07-31 20:33:53 +00:00
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
auto newZone = new ZONE( aZone );
|
2018-07-26 13:29:37 +00:00
|
|
|
newZone->SetOutline( newZoneOutline );
|
|
|
|
newZone->SetLocalFlags( 1 );
|
2020-08-07 14:04:34 +00:00
|
|
|
newZone->HatchBorder();
|
2021-03-19 19:07:16 +00:00
|
|
|
newZone->UnFill();
|
2018-07-26 13:29:37 +00:00
|
|
|
newZones.push_back( newZone );
|
|
|
|
commit.Add( newZone );
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 13:29:37 +00:00
|
|
|
commit.Remove( &aZone );
|
2020-08-12 16:39:57 +00:00
|
|
|
commit.Push( _( "Add a zone cutout" ) );
|
2018-07-31 20:33:53 +00:00
|
|
|
|
2019-01-23 14:39:08 +00:00
|
|
|
// Select the new zone and set it as the source for the next cutout
|
2020-10-08 19:21:24 +00:00
|
|
|
if( newZones.empty() )
|
|
|
|
{
|
|
|
|
m_params.m_sourceZone = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_params.m_sourceZone = newZones[0];
|
|
|
|
toolMgr->RunAction( PCB_ACTIONS::selectItem, true, newZones[0] );
|
|
|
|
}
|
2019-01-23 14:39:08 +00:00
|
|
|
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE> aZone )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2017-10-19 21:14:01 +00:00
|
|
|
switch ( m_params.m_mode )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2019-06-24 15:27:05 +00:00
|
|
|
case ZONE_MODE::CUTOUT:
|
2019-07-12 21:42:30 +00:00
|
|
|
// For cutouts, subtract from the source
|
2017-10-19 21:14:01 +00:00
|
|
|
performZoneCutout( *m_params.m_sourceZone, *aZone );
|
|
|
|
break;
|
|
|
|
|
2019-06-24 15:27:05 +00:00
|
|
|
case ZONE_MODE::ADD:
|
|
|
|
case ZONE_MODE::SIMILAR:
|
2018-07-26 13:29:37 +00:00
|
|
|
{
|
2021-03-12 13:36:06 +00:00
|
|
|
BOARD_COMMIT commit( &m_tool );
|
2018-07-26 13:29:37 +00:00
|
|
|
|
2020-08-07 14:04:34 +00:00
|
|
|
aZone->HatchBorder();
|
Fix issues with zone filling connectivity locking
Two issues found with the locking system used to prevent access to
stale connectivity data during the zone fill process:
1) a std::mutex has undefined behavior if you try to use it to guard
against access from the same thread. Because of the use of wx event
loops (and coroutines) it is entirely possible, and in some situations
inevitable, that the same thread will try to redraw the ratsnest in the
middle of zone refilling.
2) The mutex was only guarding the ZONE_FILLER::Fill method, but the callers
of that method also do connectivity updates as part of the COMMIT::Push.
Redrawing the ratsnest after the Fill but before the Push will result in
stale connectivity pointers to zone filled areas.
Fixed (1) by switching to a trivial spinlock implementation. Spinlocks would
generally not be desirable if the contention for the connectivity data crossed
thread boundaries, but at the moment I believe it's guaranteed that the reads
and writes to connectivity that are guarded by this lock happen from the main
UI thread. The writes are also quite rare compared to reads, and reads are
generally fast, so I'm not really worried about the UI thread spinning for any
real amount of time.
Fixed (2) by moving the locking location up to the call sites of
ZONE_FILLER::Fill.
This issue was quite difficult to reproduce, but I found a fairly reliable way:
It only happens (for me) on Windows, MSYS2 build, with wxWidgets 3.0
It also only happens if I restrict PcbNew to use 2 CPU cores.
With those conditions, I can reproduce the issue described in #6471 by
repeatedly editing a zone properties and changing its net. The crash is
especially easy to trigger if you press some keys (such as 'e' for edit)
while the progress dialog is displayed. It's easiest to do this in a debug
build as the slower KiCad is running, the bigger the window is to trigger this
bug.
Fixes https://gitlab.com/kicad/code/kicad/-/issues/6471
Fixes https://gitlab.com/kicad/code/kicad/-/issues/7048
2021-01-18 17:24:07 +00:00
|
|
|
|
2021-03-19 19:07:16 +00:00
|
|
|
commit.Add( aZone.get() );
|
2021-03-12 13:36:06 +00:00
|
|
|
commit.Push( _( "Add a zone" ) );
|
2022-02-28 22:05:43 +00:00
|
|
|
|
2019-06-27 11:47:24 +00:00
|
|
|
m_tool.GetManager()->RunAction( PCB_ACTIONS::selectItem, true, aZone.release() );
|
2017-10-19 21:14:01 +00:00
|
|
|
break;
|
2018-07-26 13:29:37 +00:00
|
|
|
}
|
2017-10-19 21:14:01 +00:00
|
|
|
|
2019-06-24 15:27:05 +00:00
|
|
|
case ZONE_MODE::GRAPHIC_POLYGON:
|
2017-10-19 21:14:01 +00:00
|
|
|
{
|
2021-03-12 13:36:06 +00:00
|
|
|
BOARD_COMMIT commit( &m_tool );
|
|
|
|
BOARD* board = m_tool.getModel<BOARD>();
|
|
|
|
PCB_LAYER_ID layer = m_params.m_layer;
|
|
|
|
PCB_SHAPE* poly;
|
2017-10-19 21:14:01 +00:00
|
|
|
|
2021-03-12 13:36:06 +00:00
|
|
|
if( m_tool.m_isFootprintEditor )
|
|
|
|
poly = new FP_SHAPE( static_cast<FOOTPRINT*>( m_tool.m_frame->GetModel() ) );
|
|
|
|
else
|
|
|
|
poly = new PCB_SHAPE();
|
2020-04-23 23:13:24 +00:00
|
|
|
|
2021-07-21 18:31:25 +00:00
|
|
|
poly->SetShape( SHAPE_T::POLY );
|
2023-08-27 09:21:47 +00:00
|
|
|
poly->SetFilled( layer != Edge_Cuts && layer != F_CrtYd && layer != B_CrtYd );
|
2021-03-12 13:36:06 +00:00
|
|
|
|
2021-07-17 19:56:18 +00:00
|
|
|
poly->SetStroke( STROKE_PARAMS( board->GetDesignSettings().GetLineThickness( layer ),
|
|
|
|
PLOT_DASH_TYPE::SOLID ) );
|
2021-03-12 13:36:06 +00:00
|
|
|
poly->SetLayer( layer );
|
2020-12-30 22:04:37 +00:00
|
|
|
poly->SetPolyShape( *aZone->Outline() );
|
2021-03-12 13:36:06 +00:00
|
|
|
|
|
|
|
commit.Add( poly );
|
|
|
|
commit.Push( _( "Add a graphical polygon" ) );
|
2017-04-07 09:57:02 +00:00
|
|
|
|
2023-08-27 09:21:47 +00:00
|
|
|
m_tool.GetManager()->RunAction<EDA_ITEM*>( PCB_ACTIONS::selectItem, poly );
|
2017-10-19 21:14:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
2017-11-02 20:41:29 +00:00
|
|
|
}
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
|
2018-02-20 14:51:40 +00:00
|
|
|
bool ZONE_CREATE_HELPER::OnFirstPoint( POLYGON_GEOM_MANAGER& aMgr )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
|
|
|
// if we don't have a zone, create one
|
|
|
|
if( !m_zone )
|
|
|
|
{
|
|
|
|
if( m_params.m_sourceZone )
|
|
|
|
m_zone = createZoneFromExisting( *m_params.m_sourceZone );
|
|
|
|
else
|
|
|
|
m_zone = createNewZone( m_params.m_keepout );
|
|
|
|
|
|
|
|
if( m_zone )
|
|
|
|
{
|
2020-06-23 21:16:34 +00:00
|
|
|
m_tool.GetManager()->RunAction( PCB_ACTIONS::selectionClear, true );
|
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
// set up properties from zone
|
2017-02-26 17:45:52 +00:00
|
|
|
const auto& settings = *m_parentView.GetPainter()->GetSettings();
|
2022-02-28 21:40:36 +00:00
|
|
|
COLOR4D color = settings.GetColor( nullptr, m_zone->GetFirstLayer() );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2018-07-26 13:29:37 +00:00
|
|
|
m_previewItem.SetStrokeColor( COLOR4D::WHITE );
|
2017-02-26 17:45:52 +00:00
|
|
|
m_previewItem.SetFillColor( color.WithAlpha( 0.2 ) );
|
|
|
|
|
|
|
|
m_parentView.SetVisible( &m_previewItem, true );
|
2018-02-20 14:51:40 +00:00
|
|
|
|
2022-05-29 20:28:10 +00:00
|
|
|
aMgr.SetLeaderMode( m_tool.Is45Limited() ? POLYGON_GEOM_MANAGER::LEADER_MODE::DEG45
|
|
|
|
: POLYGON_GEOM_MANAGER::LEADER_MODE::DIRECT );
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 21:42:30 +00:00
|
|
|
return m_zone != nullptr;
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ZONE_CREATE_HELPER::OnGeometryChange( const POLYGON_GEOM_MANAGER& aMgr )
|
|
|
|
{
|
2023-08-27 09:52:02 +00:00
|
|
|
// Handle a cancel-interactive
|
|
|
|
if( m_zone && !aMgr.IsPolygonInProgress() )
|
|
|
|
{
|
|
|
|
m_zone = nullptr;
|
|
|
|
m_parentView.SetVisible( &m_previewItem, false );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-26 17:45:52 +00:00
|
|
|
// send the points to the preview item
|
2022-12-09 16:11:56 +00:00
|
|
|
m_previewItem.SetPoints( aMgr.GetLockedInPoints(), aMgr.GetLeaderLinePoints(),
|
|
|
|
aMgr.GetLoopLinePoints() );
|
2017-02-26 17:45:52 +00:00
|
|
|
m_parentView.Update( &m_previewItem, KIGFX::GEOMETRY );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ZONE_CREATE_HELPER::OnComplete( const POLYGON_GEOM_MANAGER& aMgr )
|
|
|
|
{
|
|
|
|
auto& finalPoints = aMgr.GetLockedInPoints();
|
|
|
|
|
2018-02-19 10:20:52 +00:00
|
|
|
if( finalPoints.PointCount() < 3 )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
|
|
|
// just scrap the zone in progress
|
|
|
|
m_zone = nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-27 09:21:47 +00:00
|
|
|
// if m_params.m_mode == DRAWING_TOOL::ZONE_MODE::CUTOUT, m_zone will be merged to the
|
|
|
|
// existing zone as a new hole.
|
2017-03-07 12:06:00 +00:00
|
|
|
m_zone->Outline()->NewOutline();
|
2018-03-02 09:55:59 +00:00
|
|
|
auto* outline = m_zone->Outline();
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2018-02-19 10:20:52 +00:00
|
|
|
for( int i = 0; i < finalPoints.PointCount(); ++i )
|
2018-03-02 09:55:59 +00:00
|
|
|
outline->Append( finalPoints.CPoint( i ) );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2023-08-27 09:21:47 +00:00
|
|
|
// In DEG45 mode, we may have intermediate points in the leader that should be included
|
|
|
|
// as they are shown in the preview. These typically maintain the 45 constraint
|
2019-08-26 13:30:03 +00:00
|
|
|
if( aMgr.GetLeaderMode() == POLYGON_GEOM_MANAGER::LEADER_MODE::DEG45 )
|
|
|
|
{
|
2022-12-09 16:11:56 +00:00
|
|
|
const SHAPE_LINE_CHAIN leaderPts = aMgr.GetLeaderLinePoints();
|
|
|
|
for( int i = 1; i < leaderPts.PointCount(); i++ )
|
|
|
|
outline->Append( leaderPts.CPoint( i ) );
|
|
|
|
|
|
|
|
const SHAPE_LINE_CHAIN loopPts = aMgr.GetLoopLinePoints();
|
2022-12-10 03:21:05 +00:00
|
|
|
for( int i = 1; i < loopPts.PointCount() - 1; i++ )
|
2022-12-09 16:11:56 +00:00
|
|
|
outline->Append( loopPts.CPoint( i ) );
|
2019-08-26 13:30:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 03:21:05 +00:00
|
|
|
SHAPE_LINE_CHAIN& chain = outline->Outline( 0 );
|
|
|
|
|
|
|
|
chain.SetClosed( true );
|
|
|
|
chain.Simplify( true );
|
|
|
|
|
|
|
|
// Remove the start point if it lies on the line between neighbouring points.
|
|
|
|
// Simplify doesn't handle that currently.
|
|
|
|
if( chain.PointCount() >= 3 )
|
|
|
|
{
|
|
|
|
SEG seg( chain.CPoint( -1 ), chain.CPoint( 1 ) );
|
|
|
|
|
|
|
|
if( seg.LineDistance( chain.CPoint( 0 ) ) <= 1 )
|
|
|
|
chain.Remove( 0 );
|
|
|
|
}
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
// hand the zone over to the committer
|
|
|
|
commitZone( std::move( m_zone ) );
|
2019-01-23 14:39:08 +00:00
|
|
|
m_zone = nullptr;
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_parentView.SetVisible( &m_previewItem, false );
|
|
|
|
}
|