2012-06-08 09:56:42 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
|
2022-02-25 23:15:10 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2012-06-08 09:56:42 +00:00
|
|
|
*
|
|
|
|
* Some code comes from FreePCB.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-09-14 22:45:14 +00:00
|
|
|
#include <kiface_base.h>
|
2020-12-23 14:24:03 +00:00
|
|
|
#include <confirm.h>
|
|
|
|
#include <pcb_edit_frame.h>
|
2021-03-17 18:57:07 +00:00
|
|
|
#include <pcbnew_settings.h>
|
2020-12-23 14:24:03 +00:00
|
|
|
#include <board_commit.h>
|
2020-11-11 23:05:59 +00:00
|
|
|
#include <zone.h>
|
2020-12-23 14:24:03 +00:00
|
|
|
#include <zones.h>
|
|
|
|
#include <zones_functions_for_undo_redo.h>
|
|
|
|
#include <connectivity/connectivity_data.h>
|
2021-08-14 20:05:21 +00:00
|
|
|
#include <widgets/wx_progress_reporters.h>
|
2020-12-23 14:24:03 +00:00
|
|
|
#include <zone_filler.h>
|
|
|
|
|
|
|
|
|
|
|
|
void PCB_EDIT_FRAME::Edit_Zone_Params( ZONE* aZone )
|
|
|
|
{
|
|
|
|
int dialogResult;
|
|
|
|
ZONE_SETTINGS zoneInfo = GetZoneSettings();
|
|
|
|
PICKED_ITEMS_LIST pickedList; // zones for undo/redo command
|
|
|
|
PICKED_ITEMS_LIST deletedList; // zones that have been deleted when combined
|
|
|
|
BOARD_COMMIT commit( this );
|
|
|
|
|
|
|
|
// Save initial zones configuration, for undo/redo, before adding new zone
|
|
|
|
// note the net name and the layer can be changed, so we must save all zones
|
|
|
|
deletedList.ClearListAndDeleteItems();
|
|
|
|
pickedList.ClearListAndDeleteItems();
|
|
|
|
SaveCopyOfZones( pickedList, GetBoard(), -1, UNDEFINED_LAYER );
|
|
|
|
|
|
|
|
if( aZone->GetIsRuleArea() )
|
|
|
|
{
|
|
|
|
// edit a rule area on a copper layer
|
|
|
|
zoneInfo << *aZone;
|
|
|
|
dialogResult = InvokeRuleAreaEditor( this, &zoneInfo );
|
|
|
|
}
|
|
|
|
else if( IsCopperLayer( aZone->GetLayer() ) )
|
|
|
|
{
|
|
|
|
// edit a zone on a copper layer
|
|
|
|
zoneInfo << *aZone;
|
|
|
|
dialogResult = InvokeCopperZonesEditor( this, &zoneInfo );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zoneInfo << *aZone;
|
|
|
|
dialogResult = InvokeNonCopperZonesEditor( this, &zoneInfo );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( dialogResult == wxID_CANCEL )
|
|
|
|
{
|
|
|
|
deletedList.ClearListAndDeleteItems();
|
|
|
|
pickedList.ClearListAndDeleteItems();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetZoneSettings( zoneInfo );
|
|
|
|
OnModify();
|
|
|
|
|
|
|
|
if( dialogResult == ZONE_EXPORT_VALUES )
|
|
|
|
{
|
|
|
|
UpdateCopyOfZonesList( pickedList, deletedList, GetBoard() );
|
|
|
|
commit.Stage( pickedList );
|
|
|
|
commit.Push( _( "Modify zone properties" ) );
|
|
|
|
pickedList.ClearItemsList(); // s_ItemsListPicker is no more owner of picked items
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxBusyCursor dummy;
|
|
|
|
|
|
|
|
// Undraw old zone outlines
|
|
|
|
for( ZONE* zone : GetBoard()->Zones() )
|
|
|
|
GetCanvas()->GetView()->Update( zone );
|
|
|
|
|
|
|
|
zoneInfo.ExportSetting( *aZone );
|
|
|
|
|
|
|
|
NETINFO_ITEM* net = GetBoard()->FindNet( zoneInfo.m_NetcodeSelection );
|
|
|
|
|
|
|
|
if( net ) // net == NULL should not occur
|
|
|
|
aZone->SetNetCode( net->GetNetCode() );
|
|
|
|
|
|
|
|
UpdateCopyOfZonesList( pickedList, deletedList, GetBoard() );
|
|
|
|
|
2021-03-25 20:02:28 +00:00
|
|
|
// refill zones with the new properties applied
|
|
|
|
std::vector<ZONE*> zones_to_refill;
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < pickedList.GetCount(); ++i )
|
2020-12-23 14:24:03 +00:00
|
|
|
{
|
2021-03-25 20:02:28 +00:00
|
|
|
ZONE* zone = dyn_cast<ZONE*>( pickedList.GetPickedItem( i ) );
|
2020-12-23 14:24:03 +00:00
|
|
|
|
2021-03-25 20:02:28 +00:00
|
|
|
if( zone == nullptr )
|
2020-12-23 14:24:03 +00:00
|
|
|
{
|
2022-02-04 22:44:59 +00:00
|
|
|
wxASSERT_MSG( false, wxT( "Expected a zone after zone properties edit" ) );
|
2021-03-25 20:02:28 +00:00
|
|
|
continue;
|
2021-03-17 18:57:07 +00:00
|
|
|
}
|
2020-12-23 14:24:03 +00:00
|
|
|
|
2021-03-25 20:02:28 +00:00
|
|
|
// aZone won't be filled if the layer set was modified, but it needs to be updated
|
|
|
|
if( zone->IsFilled() || zone == aZone )
|
|
|
|
zones_to_refill.push_back( zone );
|
|
|
|
}
|
|
|
|
|
|
|
|
commit.Stage( pickedList );
|
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-25 20:02:28 +00:00
|
|
|
// Only auto-refill zones here if in user preferences
|
2022-02-25 23:15:10 +00:00
|
|
|
if( Settings().m_AutoRefillZones && zones_to_refill.size() )
|
2021-03-25 20:02:28 +00:00
|
|
|
{
|
2022-02-25 23:15:10 +00:00
|
|
|
ZONE_FILLER filler( GetBoard(), &commit );
|
|
|
|
wxString title = wxString::Format( _( "Refill %d Zones" ), (int) zones_to_refill.size() );
|
|
|
|
|
|
|
|
std::unique_ptr<WX_PROGRESS_REPORTER> reporter;
|
|
|
|
reporter = std::make_unique<WX_PROGRESS_REPORTER>( this, title, 4 );
|
|
|
|
filler.SetProgressReporter( reporter.get() );
|
|
|
|
|
|
|
|
filler.Fill( zones_to_refill );
|
2020-12-23 14:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
commit.Push( _( "Modify zone properties" ) );
|
2022-02-25 23:15:10 +00:00
|
|
|
|
|
|
|
if( Settings().m_AutoRefillZones && zones_to_refill.size() )
|
|
|
|
GetBoard()->GetConnectivity()->Build( GetBoard() );
|
|
|
|
|
2020-12-23 14:24:03 +00:00
|
|
|
GetBoard()->GetConnectivity()->RecalculateRatsnest();
|
|
|
|
|
|
|
|
pickedList.ClearItemsList(); // s_ItemsListPicker is no longer owner of picked items
|
|
|
|
}
|
2008-01-04 12:32:10 +00:00
|
|
|
|
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
bool BOARD::TestZoneIntersection( ZONE* aZone1, ZONE* aZone2 )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
|
|
|
// see if areas are on same layer
|
2020-11-11 23:05:59 +00:00
|
|
|
if( aZone1->GetLayer() != aZone2->GetLayer() )
|
2012-08-02 13:23:53 +00:00
|
|
|
return false;
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2020-11-11 23:05:59 +00:00
|
|
|
SHAPE_POLY_SET* poly1 = aZone1->Outline();
|
|
|
|
SHAPE_POLY_SET* poly2 = aZone2->Outline();
|
2008-01-04 12:32:10 +00:00
|
|
|
|
|
|
|
// test bounding rects
|
2017-03-07 12:06:00 +00:00
|
|
|
BOX2I b1 = poly1->BBox();
|
|
|
|
BOX2I b2 = poly2->BBox();
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2014-11-08 12:25:29 +00:00
|
|
|
if( ! b1.Intersects( b2 ) )
|
2012-08-02 13:23:53 +00:00
|
|
|
return false;
|
2008-01-04 12:32:10 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Now test for intersecting segments
|
|
|
|
for( auto segIterator1 = poly1->IterateSegmentsWithHoles(); segIterator1; segIterator1++ )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
// Build segment
|
|
|
|
SEG firstSegment = *segIterator1;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
for( auto segIterator2 = poly2->IterateSegmentsWithHoles(); segIterator2; segIterator2++ )
|
2008-01-04 12:32:10 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
// Build second segment
|
|
|
|
SEG secondSegment = *segIterator2;
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// Check whether the two segments built collide
|
|
|
|
if( firstSegment.Collide( secondSegment, 0 ) )
|
|
|
|
return true;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
// If a contour is inside another contour, no segments intersects, but the zones
|
|
|
|
// can be combined if a corner is inside an outline (only one corner is enough)
|
|
|
|
for( auto iter = poly2->IterateWithHoles(); iter; iter++ )
|
2008-01-20 19:55:22 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
if( poly1->Contains( *iter ) )
|
2012-08-04 09:43:27 +00:00
|
|
|
return true;
|
2012-08-02 13:23:53 +00:00
|
|
|
}
|
2008-01-20 19:55:22 +00:00
|
|
|
|
2017-03-07 12:06:00 +00:00
|
|
|
for( auto iter = poly1->IterateWithHoles(); iter; iter++ )
|
2012-08-02 13:23:53 +00:00
|
|
|
{
|
2017-03-07 12:06:00 +00:00
|
|
|
if( poly2->Contains( *iter ) )
|
2012-08-04 09:43:27 +00:00
|
|
|
return true;
|
2008-01-20 19:55:22 +00:00
|
|
|
}
|
2011-09-23 13:57:12 +00:00
|
|
|
|
2012-08-04 09:43:27 +00:00
|
|
|
return false;
|
2008-01-04 12:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|