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 <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>
|
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 )
|
|
|
|
{
|
2019-10-26 15:49:29 +00:00
|
|
|
auto& frame = *m_tool.getEditFrame<PCB_BASE_EDIT_FRAME>();
|
2017-02-26 17:45:52 +00:00
|
|
|
auto& board = *m_tool.getModel<BOARD>();
|
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();
|
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 );
|
2019-10-20 16:27:44 +00:00
|
|
|
zoneInfo.m_Zone_45_Only = ( m_params.m_leaderMode == POLYGON_GEOM_MANAGER::LEADER_MODE::DEG45 );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
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
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-10-26 15:49:29 +00:00
|
|
|
// The new zone is a ZONE_CONTAINER if created in the board editor
|
|
|
|
// and a MODULE_ZONE_CONTAINER if created in the footprint editor
|
|
|
|
wxASSERT( !m_tool.m_editModules || ( parent->Type() == PCB_MODULE_T ) );
|
|
|
|
|
|
|
|
auto newZone = m_tool.m_editModules ?
|
|
|
|
std::make_unique<MODULE_ZONE_CONTAINER>( parent ) :
|
|
|
|
std::make_unique<ZONE_CONTAINER>( parent );
|
2017-02-26 17:45:52 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-03-12 13:42:32 +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:
|
|
|
|
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
|
|
|
|
2020-03-12 13:42:32 +00:00
|
|
|
// Add holes (if any) to thez new zone outline:
|
|
|
|
for (int hole = 0; hole < originalOutline.HoleCount( outline ) ; hole++ )
|
|
|
|
newZoneOutline->AddHole( originalOutline.CHole( outline, hole ) );
|
2018-07-31 20:33:53 +00:00
|
|
|
|
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 )
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2019-07-12 21:42:30 +00:00
|
|
|
ZONE_FILLER filler( m_tool.getModel<BOARD>() );
|
2017-11-30 16:22:45 +00:00
|
|
|
filler.Fill( { aZone.get() } );
|
|
|
|
}
|
2017-10-19 21:14:01 +00:00
|
|
|
|
2019-06-27 11:47:24 +00:00
|
|
|
bCommit.Add( aZone.get() );
|
2017-10-19 21:14:01 +00:00
|
|
|
bCommit.Push( _( "Add a zone" ) );
|
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
|
|
|
{
|
2018-07-26 13:29:37 +00:00
|
|
|
BOARD_COMMIT bCommit( &m_tool );
|
2019-07-12 21:42:30 +00:00
|
|
|
BOARD_ITEM_CONTAINER* parent = m_tool.m_frame->GetModel();
|
2020-04-23 23:13:24 +00:00
|
|
|
LSET graphicPolygonsLayers = LSET::AllLayersMask();
|
2017-10-19 21:14:01 +00:00
|
|
|
|
2020-04-23 23:13:24 +00:00
|
|
|
graphicPolygonsLayers.reset( Edge_Cuts ).reset( F_CrtYd ).reset( B_CrtYd );
|
|
|
|
|
|
|
|
if( graphicPolygonsLayers.Contains( m_tool.getDrawingLayer() ) )
|
2018-12-10 14:08:03 +00:00
|
|
|
{
|
2019-07-12 21:42:30 +00:00
|
|
|
auto poly = m_tool.m_editModules ? new EDGE_MODULE( (MODULE *) parent )
|
|
|
|
: new DRAWSEGMENT();
|
2018-12-10 14:08:03 +00:00
|
|
|
poly->SetShape ( S_POLYGON );
|
|
|
|
poly->SetLayer( m_tool.getDrawingLayer() );
|
|
|
|
poly->SetPolyShape ( *aZone->Outline() );
|
|
|
|
bCommit.Add( poly );
|
2019-06-27 11:47:24 +00:00
|
|
|
m_tool.GetManager()->RunAction( PCB_ACTIONS::selectItem, true, poly );
|
2018-12-10 14:08:03 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2019-07-12 21:42:30 +00:00
|
|
|
auto new_seg = m_tool.m_editModules ? new EDGE_MODULE( (MODULE *) parent )
|
|
|
|
: new DRAWSEGMENT();
|
2018-12-10 14:08:03 +00:00
|
|
|
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
|
2019-07-12 21:42:30 +00:00
|
|
|
// the user's choice here can affect things like the colour of the preview
|
2017-02-26 17:45:52 +00:00
|
|
|
if( !m_zone )
|
|
|
|
{
|
2019-06-27 11:47:24 +00:00
|
|
|
m_tool.GetManager()->RunAction( PCB_ACTIONS::selectionClear, true );
|
|
|
|
|
2017-02-26 17:45:52 +00:00
|
|
|
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
|
|
|
|
2019-07-12 21:42:30 +00:00
|
|
|
aMgr.SetLeaderMode( m_zone->GetHV45() ? 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 )
|
|
|
|
{
|
|
|
|
// 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 )
|
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 )
|
|
|
|
{
|
2019-12-05 15:40:22 +00:00
|
|
|
const auto& pts = aMgr.GetLeaderLinePoints();
|
2019-10-24 15:01:14 +00:00
|
|
|
for( int i = 1; i < pts.PointCount(); i++ )
|
|
|
|
outline->Append( pts.CPoint( i ) );
|
2019-08-26 13:30:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:55:59 +00:00
|
|
|
outline->Outline( 0 ).SetClosed( true );
|
|
|
|
outline->RemoveNullSegments();
|
2019-10-24 15:01:14 +00:00
|
|
|
outline->Simplify( SHAPE_POLY_SET::PM_FAST );
|
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 );
|
|
|
|
}
|