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
|
|
|
*
|
|
|
|
* Copyright (C) 2017-2019 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <tools/zone_create_helper.h>
|
|
|
|
|
|
|
|
#include <view/view.h>
|
|
|
|
#include <tool/tool_manager.h>
|
|
|
|
#include <class_zone.h>
|
2017-10-19 21:14:01 +00:00
|
|
|
#include <class_drawsegment.h>
|
|
|
|
#include <class_edge_mod.h>
|
|
|
|
#include <class_pad.h>
|
2017-02-26 17:45:52 +00:00
|
|
|
#include <board_commit.h>
|
|
|
|
#include <pcb_painter.h>
|
|
|
|
|
|
|
|
#include <tools/pcb_actions.h>
|
|
|
|
#include <tools/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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createNewZone( bool aKeepout )
|
|
|
|
{
|
|
|
|
auto& frame = *m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
|
|
|
|
auto& board = *m_tool.getModel<BOARD>();
|
2018-11-11 13:09:02 +00:00
|
|
|
KIGFX::VIEW_CONTROLS* controls = m_tool.GetManager()->GetViewControls();
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
// Get the current default settings for zones
|
|
|
|
ZONE_SETTINGS zoneInfo = frame.GetZoneSettings();
|
2018-10-03 12:57:02 +00:00
|
|
|
zoneInfo.m_CurrentZone_Layer = m_params.m_layer;
|
2017-02-26 17:45:52 +00:00
|
|
|
zoneInfo.m_NetcodeSelection = board.GetHighLightNetCode();
|
|
|
|
zoneInfo.SetIsKeepout( m_params.m_keepout );
|
|
|
|
|
2018-03-02 09:55:59 +00:00
|
|
|
if( m_params.m_mode != DRAWING_TOOL::ZONE_MODE::GRAPHIC_POLYGON )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2017-10-19 21:14:01 +00:00
|
|
|
// Get the current default settings for zones
|
|
|
|
|
|
|
|
// 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 )
|
|
|
|
dialogResult = InvokeKeepoutAreaEditor( &frame, &zoneInfo );
|
2017-02-26 17:45:52 +00:00
|
|
|
else
|
2018-02-24 13:31:25 +00:00
|
|
|
{
|
|
|
|
if( IsCopperLayer( zoneInfo.m_CurrentZone_Layer ) )
|
|
|
|
dialogResult = InvokeCopperZonesEditor( &frame, &zoneInfo );
|
|
|
|
else
|
2018-05-25 14:56:04 +00:00
|
|
|
dialogResult = InvokeNonCopperZonesEditor( &frame, &zoneInfo );
|
2018-02-24 13:31:25 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
controls->WarpCursor( controls->GetCursorPosition(), true );
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
|
|
|
|
|
|
|
|
// Apply the selected settings
|
|
|
|
zoneInfo.ExportSetting( *newZone );
|
|
|
|
|
|
|
|
return newZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ZONE_CONTAINER> ZONE_CREATE_HELPER::createZoneFromExisting(
|
|
|
|
const ZONE_CONTAINER& aSrcZone )
|
|
|
|
{
|
|
|
|
auto& board = *m_tool.getModel<BOARD>();
|
|
|
|
|
|
|
|
auto newZone = std::make_unique<ZONE_CONTAINER>( &board );
|
|
|
|
|
|
|
|
ZONE_SETTINGS zoneSettings;
|
|
|
|
zoneSettings << aSrcZone;
|
|
|
|
|
|
|
|
zoneSettings.ExportSetting( *newZone );
|
|
|
|
|
|
|
|
return newZone;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-25 14:56:04 +00:00
|
|
|
void ZONE_CREATE_HELPER::performZoneCutout( ZONE_CONTAINER& aZone, ZONE_CONTAINER& aCutout )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2018-07-26 14:04:31 +00:00
|
|
|
BOARD_COMMIT commit( &m_tool );
|
2017-04-07 09:57:02 +00:00
|
|
|
BOARD* board = m_tool.getModel<BOARD>();
|
2018-07-26 13:29:37 +00:00
|
|
|
std::vector<ZONE_CONTAINER*> 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
|
|
|
|
2018-07-26 13:29:37 +00:00
|
|
|
for( int i = 0; i < originalOutline.OutlineCount(); i++ )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2018-07-26 13:29:37 +00:00
|
|
|
auto newZoneOutline = new SHAPE_POLY_SET;
|
2018-07-26 14:04:31 +00:00
|
|
|
newZoneOutline->AddOutline( originalOutline.Outline( i ) );
|
|
|
|
|
2018-07-31 20:33:53 +00:00
|
|
|
for (int j = 0; j < originalOutline.HoleCount(i) ; j++ )
|
|
|
|
{
|
|
|
|
newZoneOutline->AddHole( originalOutline.CHole(i, j), i );
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:29:37 +00:00
|
|
|
auto newZone = new ZONE_CONTAINER( aZone );
|
|
|
|
newZone->SetOutline( newZoneOutline );
|
|
|
|
newZone->SetLocalFlags( 1 );
|
2018-07-26 14:04:31 +00:00
|
|
|
newZone->Hatch();
|
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 );
|
2018-07-26 14:04:31 +00:00
|
|
|
commit.Push( _( "Add a zone cutout" ) );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
2018-07-26 13:29:37 +00:00
|
|
|
ZONE_FILLER filler( board );
|
2018-07-26 14:04:31 +00:00
|
|
|
filler.Fill( newZones );
|
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
|
2018-07-31 20:33:53 +00:00
|
|
|
toolMgr->RunAction( PCB_ACTIONS::selectItem, true, newZones[0] );
|
2019-01-23 14:39:08 +00:00
|
|
|
m_params.m_sourceZone = newZones[0];
|
|
|
|
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ZONE_CREATE_HELPER::commitZone( std::unique_ptr<ZONE_CONTAINER> aZone )
|
|
|
|
{
|
|
|
|
auto& frame = *m_tool.getEditFrame<PCB_EDIT_FRAME>();
|
2017-11-30 16:22:45 +00:00
|
|
|
auto board = m_tool.getModel<BOARD>();
|
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
|
|
|
{
|
2017-10-19 21:14:01 +00:00
|
|
|
case DRAWING_TOOL::ZONE_MODE::CUTOUT:
|
|
|
|
// For cutouts, subtract from the source
|
|
|
|
performZoneCutout( *m_params.m_sourceZone, *aZone );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DRAWING_TOOL::ZONE_MODE::ADD:
|
|
|
|
case DRAWING_TOOL::ZONE_MODE::SIMILAR:
|
2018-07-26 13:29:37 +00:00
|
|
|
{
|
|
|
|
BOARD_COMMIT bCommit( &m_tool );
|
|
|
|
|
2017-10-19 21:14:01 +00:00
|
|
|
aZone->Hatch();
|
|
|
|
|
|
|
|
if( !m_params.m_keepout )
|
2017-11-30 16:22:45 +00:00
|
|
|
{
|
|
|
|
ZONE_FILLER filler( board );
|
|
|
|
filler.Fill( { aZone.get() } );
|
|
|
|
}
|
2017-10-19 21:14:01 +00:00
|
|
|
|
|
|
|
bCommit.Add( aZone.release() );
|
|
|
|
bCommit.Push( _( "Add a zone" ) );
|
|
|
|
break;
|
2018-07-26 13:29:37 +00:00
|
|
|
}
|
2017-10-19 21:14:01 +00:00
|
|
|
|
|
|
|
case DRAWING_TOOL::ZONE_MODE::GRAPHIC_POLYGON:
|
|
|
|
{
|
2018-07-26 13:29:37 +00:00
|
|
|
BOARD_COMMIT bCommit( &m_tool );
|
2017-10-19 21:14:01 +00:00
|
|
|
BOARD_ITEM_CONTAINER* parent = frame.GetModel();
|
|
|
|
|
2018-12-10 14:08:03 +00:00
|
|
|
if( m_tool.getDrawingLayer() != Edge_Cuts )
|
|
|
|
{
|
|
|
|
auto poly = m_tool.m_editModules ?
|
|
|
|
new EDGE_MODULE( (MODULE *) parent ) :
|
|
|
|
new DRAWSEGMENT();
|
|
|
|
poly->SetShape ( S_POLYGON );
|
|
|
|
poly->SetLayer( m_tool.getDrawingLayer() );
|
|
|
|
poly->SetPolyShape ( *aZone->Outline() );
|
|
|
|
bCommit.Add( poly );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto outline = aZone->Outline();
|
|
|
|
|
2019-07-14 12:46:59 +00:00
|
|
|
for( auto seg = outline->CIterateSegments( 0 ); seg; seg++ )
|
2018-12-10 14:08:03 +00:00
|
|
|
{
|
|
|
|
auto new_seg = m_tool.m_editModules ?
|
|
|
|
new EDGE_MODULE( (MODULE *) parent ) :
|
|
|
|
new DRAWSEGMENT();
|
|
|
|
new_seg->SetShape( S_SEGMENT );
|
|
|
|
new_seg->SetLayer( m_tool.getDrawingLayer() );
|
|
|
|
new_seg->SetStart( wxPoint( seg.Get().A.x, seg.Get().A.y ) );
|
|
|
|
new_seg->SetEnd( wxPoint( seg.Get().B.x, seg.Get().B.y ) );
|
|
|
|
bCommit.Add( new_seg );
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 09:57:02 +00:00
|
|
|
|
2017-10-19 21:14:01 +00:00
|
|
|
bCommit.Push( _( "Add a graphical polygon" ) );
|
2017-04-07 09:57:02 +00:00
|
|
|
|
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
|
|
|
|
// the user's choice here can affect things like the colour
|
|
|
|
// of the preview
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
// set up poperties from zone
|
|
|
|
const auto& settings = *m_parentView.GetPainter()->GetSettings();
|
|
|
|
COLOR4D color = settings.GetColor( nullptr, m_zone->GetLayer() );
|
|
|
|
|
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
|
|
|
|
|
|
|
aMgr.SetLeaderMode( m_zone.get()->GetHV45()
|
|
|
|
? POLYGON_GEOM_MANAGER::LEADER_MODE::DEG45
|
|
|
|
: POLYGON_GEOM_MANAGER::LEADER_MODE::DIRECT );
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_zone )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ZONE_CREATE_HELPER::OnGeometryChange( const POLYGON_GEOM_MANAGER& aMgr )
|
|
|
|
{
|
|
|
|
// send the points to the preview item
|
|
|
|
m_previewItem.SetPoints( aMgr.GetLockedInPoints(), aMgr.GetLeaderLinePoints() );
|
|
|
|
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
|
|
|
|
{
|
2017-04-07 09:57:02 +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 )
|
2017-02-26 17:45:52 +00:00
|
|
|
{
|
2018-03-02 09:55:59 +00:00
|
|
|
outline->Append( finalPoints.CPoint( i ) );
|
2017-02-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 13:30:03 +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
|
|
|
|
if( aMgr.GetLeaderMode() == POLYGON_GEOM_MANAGER::LEADER_MODE::DEG45 )
|
|
|
|
{
|
|
|
|
auto pts = aMgr.GetLeaderLinePoints();
|
|
|
|
|
|
|
|
if( outline->TotalVertices() > 0 )
|
|
|
|
outline->RemoveVertex( outline->TotalVertices() - 1 );
|
|
|
|
|
|
|
|
// The first 2 points of the leader are the continuation of the previous segment
|
|
|
|
// The third point is where it intersects with the extension from the 0-th segment
|
|
|
|
for( int i = 2; i < pts.PointCount(); i++ )
|
|
|
|
outline->Append( pts.CPoint( i ) );
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:55:59 +00:00
|
|
|
outline->Outline( 0 ).SetClosed( true );
|
|
|
|
outline->RemoveNullSegments();
|
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 );
|
|
|
|
}
|