kicad/pcbnew/tools/pcb_editor_control.cpp

1121 lines
34 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014 CERN
2017-03-02 19:46:02 +00:00
* Copyright (C) 2014-2017 KiCad Developers, see AUTHORS.txt for contributors.
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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 <cstdint>
#include <thread>
#include <mutex>
#include "pcb_editor_control.h"
#include "pcb_actions.h"
#include <tool/tool_manager.h>
#include <wx/progdlg.h>
#include "edit_tool.h"
#include "selection_tool.h"
#include "drawing_tool.h"
#include "picker_tool.h"
#include <painter.h>
#include <project.h>
#include <pcbnew_id.h>
#include <wxPcbStruct.h>
#include <class_board.h>
#include <class_zone.h>
#include <pcb_draw_panel_gal.h>
#include <class_module.h>
#include <class_pcb_target.h>
#include <connectivity_data.h>
#include <collectors.h>
2015-06-19 15:32:33 +00:00
#include <zones_functions_for_undo_redo.h>
2016-06-21 15:06:28 +00:00
#include <board_commit.h>
#include <confirm.h>
#include <bitmaps.h>
#include <hotkeys.h>
#include <view/view_group.h>
#include <view/view_controls.h>
2015-06-18 15:51:54 +00:00
#include <origin_viewitem.h>
#include <profile.h>
#include <widgets/progress_reporter.h>
#ifdef USE_OPENMP
#include <omp.h>
#endif /* USE_OPENMP */
#include <tools/tool_event_utils.h>
#include <functional>
using namespace std::placeholders;
2015-06-19 15:32:33 +00:00
// Track & via size control
TOOL_ACTION PCB_ACTIONS::trackWidthInc( "pcbnew.EditorControl.trackWidthInc",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_SWITCH_TRACK_WIDTH_TO_NEXT ),
"", "" );
TOOL_ACTION PCB_ACTIONS::trackWidthDec( "pcbnew.EditorControl.trackWidthDec",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_SWITCH_TRACK_WIDTH_TO_PREVIOUS ),
"", "" );
TOOL_ACTION PCB_ACTIONS::viaSizeInc( "pcbnew.EditorControl.viaSizeInc",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_VIA_SIZE_INC ),
"", "" );
TOOL_ACTION PCB_ACTIONS::viaSizeDec( "pcbnew.EditorControl.viaSizeDec",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_VIA_SIZE_DEC ),
"", "" );
TOOL_ACTION PCB_ACTIONS::trackViaSizeChanged( "pcbnew.EditorControl.trackViaSizeChanged",
AS_GLOBAL, 0,
"", "", NULL, AF_NOTIFY );
TOOL_ACTION PCB_ACTIONS::zoneMerge( "pcbnew.EditorControl.zoneMerge",
AS_GLOBAL, 0,
_( "Merge Zones" ), _( "Merge zones" ) );
TOOL_ACTION PCB_ACTIONS::zoneDuplicate( "pcbnew.EditorControl.zoneDuplicate",
AS_GLOBAL, 0,
_( "Duplicate Zone onto Layer" ), _( "Duplicate zone outline onto a different layer" ),
zone_duplicate_xpm );
TOOL_ACTION PCB_ACTIONS::placeTarget( "pcbnew.EditorControl.placeTarget",
AS_GLOBAL, 0,
_( "Add Layer Alignment Target" ), _( "Add a layer alignment target" ), NULL, AF_ACTIVATE );
TOOL_ACTION PCB_ACTIONS::placeModule( "pcbnew.EditorControl.placeModule",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_ADD_MODULE ),
_( "Add Footprint" ), _( "Add a footprint" ), NULL, AF_ACTIVATE );
TOOL_ACTION PCB_ACTIONS::drillOrigin( "pcbnew.EditorControl.drillOrigin",
AS_GLOBAL, 0,
"", "" );
TOOL_ACTION PCB_ACTIONS::crossProbeSchToPcb( "pcbnew.EditorControl.crossProbSchToPcb",
AS_GLOBAL, 0,
"", "" );
TOOL_ACTION PCB_ACTIONS::toggleLock( "pcbnew.EditorControl.toggleLock",
AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_LOCK_UNLOCK_FOOTPRINT ),
"Toggle Lock", "" );
TOOL_ACTION PCB_ACTIONS::lock( "pcbnew.EditorControl.lock",
AS_GLOBAL, 0,
_( "Lock" ), "" );
TOOL_ACTION PCB_ACTIONS::unlock( "pcbnew.EditorControl.unlock",
AS_GLOBAL, 0,
_( "Unlock" ), "" );
TOOL_ACTION PCB_ACTIONS::appendBoard( "pcbnew.EditorControl.appendBoard",
AS_GLOBAL, 0,
"", "" );
TOOL_ACTION PCB_ACTIONS::highlightNet( "pcbnew.EditorControl.highlightNet",
AS_GLOBAL, 0,
"", "" );
TOOL_ACTION PCB_ACTIONS::highlightNetCursor( "pcbnew.EditorControl.highlightNetCursor",
AS_GLOBAL, 0,
"", "" );
TOOL_ACTION PCB_ACTIONS::showLocalRatsnest( "pcbnew.Control.showLocalRatsnest",
AS_GLOBAL, 0,
"", "" );
class ZONE_CONTEXT_MENU : public CONTEXT_MENU
{
public:
ZONE_CONTEXT_MENU()
{
2015-04-02 14:09:48 +00:00
SetIcon( add_zone_xpm );
SetTitle( _( "Zones" ) );
Add( PCB_ACTIONS::zoneFill );
Add( PCB_ACTIONS::zoneFillAll );
Add( PCB_ACTIONS::zoneUnfill );
Add( PCB_ACTIONS::zoneUnfillAll );
AppendSeparator();
Add( PCB_ACTIONS::zoneMerge );
Add( PCB_ACTIONS::zoneDuplicate );
Add( PCB_ACTIONS::drawZoneCutout );
Add( PCB_ACTIONS::drawSimilarZone );
2015-06-19 15:32:33 +00:00
}
protected:
CONTEXT_MENU* create() const override
{
return new ZONE_CONTEXT_MENU();
}
2015-06-19 15:32:33 +00:00
private:
void update() override
2015-06-19 15:32:33 +00:00
{
SELECTION_TOOL* selTool = getToolManager()->GetTool<SELECTION_TOOL>();
// enable zone actions that act on a single zone
bool singleZoneActionsEnabled = ( SELECTION_CONDITIONS::Count( 1 )
&& SELECTION_CONDITIONS::OnlyType( PCB_ZONE_AREA_T )
)( selTool->GetSelection() );
Enable( getMenuId( PCB_ACTIONS::zoneDuplicate ), singleZoneActionsEnabled );
Enable( getMenuId( PCB_ACTIONS::drawZoneCutout ), singleZoneActionsEnabled );
Enable( getMenuId( PCB_ACTIONS::drawSimilarZone ), singleZoneActionsEnabled );
// enable zone actions that ably to a specific set of zones (as opposed to all of them)
bool nonGlobalActionsEnabled = ( SELECTION_CONDITIONS::MoreThan( 0 ) )( selTool->GetSelection() );
Enable( getMenuId( PCB_ACTIONS::zoneFill ), nonGlobalActionsEnabled );
Enable( getMenuId( PCB_ACTIONS::zoneUnfill ), nonGlobalActionsEnabled );
2015-06-19 15:32:33 +00:00
// lines like this make me really think about a better name for SELECTION_CONDITIONS class
bool mergeEnabled = ( SELECTION_CONDITIONS::MoreThan( 1 ) &&
/*SELECTION_CONDITIONS::OnlyType( PCB_ZONE_AREA_T ) &&*/
PCB_SELECTION_CONDITIONS::SameNet( true ) &&
PCB_SELECTION_CONDITIONS::SameLayer() )( selTool->GetSelection() );
2015-06-19 15:32:33 +00:00
Enable( getMenuId( PCB_ACTIONS::zoneMerge ), mergeEnabled );
}
};
class LOCK_CONTEXT_MENU : public CONTEXT_MENU
{
public:
LOCK_CONTEXT_MENU()
{
SetIcon( locked_xpm );
SetTitle( _( "Locking" ) );
Add( PCB_ACTIONS::lock );
Add( PCB_ACTIONS::unlock );
Add( PCB_ACTIONS::toggleLock );
}
CONTEXT_MENU* create() const override
{
return new LOCK_CONTEXT_MENU();
}
};
PCB_EDITOR_CONTROL::PCB_EDITOR_CONTROL() :
PCB_TOOL( "pcbnew.EditorControl" ),
m_frame( nullptr )
{
2017-03-02 22:57:13 +00:00
m_placeOrigin.reset( new KIGFX::ORIGIN_VIEWITEM( KIGFX::COLOR4D( 0.8, 0.0, 0.0, 1.0 ),
KIGFX::ORIGIN_VIEWITEM::CIRCLE_CROSS ) );
m_probingSchToPcb = false;
m_slowRatsnest = false;
2015-06-18 15:51:54 +00:00
}
PCB_EDITOR_CONTROL::~PCB_EDITOR_CONTROL()
{
}
void PCB_EDITOR_CONTROL::Reset( RESET_REASON aReason )
{
m_frame = getEditFrame<PCB_EDIT_FRAME>();
2015-06-18 15:51:54 +00:00
if( aReason == MODEL_RELOAD || aReason == GAL_SWITCH )
{
m_placeOrigin->SetPosition( getModel<BOARD>()->GetAuxOrigin() );
2017-03-02 22:57:13 +00:00
getView()->Remove( m_placeOrigin.get() );
getView()->Add( m_placeOrigin.get() );
2015-06-18 15:51:54 +00:00
}
}
bool PCB_EDITOR_CONTROL::Init()
{
auto zoneMenu = std::make_shared<ZONE_CONTEXT_MENU>();
zoneMenu->SetTool( this );
auto lockMenu = std::make_shared<LOCK_CONTEXT_MENU>();
lockMenu->SetTool( this );
// Add the PCB control menus to relevant other tools
SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
if( selTool )
{
auto& toolMenu = selTool->GetToolMenu();
auto& menu = toolMenu.GetMenu();
toolMenu.AddSubMenu( zoneMenu );
toolMenu.AddSubMenu( lockMenu );
menu.AddMenu( zoneMenu.get(), false,
SELECTION_CONDITIONS::OnlyType( PCB_ZONE_AREA_T ) );
menu.AddMenu( lockMenu.get(), false,
SELECTION_CONDITIONS::OnlyTypes( GENERAL_COLLECTOR::Tracks ) );
}
DRAWING_TOOL* drawingTool = m_toolMgr->GetTool<DRAWING_TOOL>();
if( drawingTool )
{
auto& toolMenu = drawingTool->GetToolMenu();
auto& menu = toolMenu.GetMenu();
toolMenu.AddSubMenu( zoneMenu );
// Functor to say if the PCB_EDIT_FRAME is in a given mode
// Capture the tool pointer and tool mode by value
auto toolActiveFunctor = [=]( DRAWING_TOOL::MODE aMode )
{
return [=]( const SELECTION& sel )
{
return drawingTool->GetDrawingMode() == aMode;
};
};
menu.AddMenu( zoneMenu.get(), false, toolActiveFunctor( DRAWING_TOOL::MODE::ZONE ) );
}
m_ratsnestTimer.SetOwner( this );
Connect( m_ratsnestTimer.GetId(), wxEVT_TIMER,
wxTimerEventHandler( PCB_EDITOR_CONTROL::ratsnestTimer ), NULL, this );
return true;
}
// Track & via size control
int PCB_EDITOR_CONTROL::TrackWidthInc( const TOOL_EVENT& aEvent )
{
BOARD* board = getModel<BOARD>();
int widthIndex = board->GetDesignSettings().GetTrackWidthIndex() + 1;
if( widthIndex >= (int) board->GetDesignSettings().m_TrackWidthList.size() )
widthIndex = board->GetDesignSettings().m_TrackWidthList.size() - 1;
board->GetDesignSettings().SetTrackWidthIndex( widthIndex );
board->GetDesignSettings().UseCustomTrackViaSize( false );
m_toolMgr->RunAction( PCB_ACTIONS::trackViaSizeChanged );
return 0;
}
int PCB_EDITOR_CONTROL::TrackWidthDec( const TOOL_EVENT& aEvent )
{
BOARD* board = getModel<BOARD>();
int widthIndex = board->GetDesignSettings().GetTrackWidthIndex() - 1;
if( widthIndex < 0 )
widthIndex = 0;
board->GetDesignSettings().SetTrackWidthIndex( widthIndex );
board->GetDesignSettings().UseCustomTrackViaSize( false );
m_toolMgr->RunAction( PCB_ACTIONS::trackViaSizeChanged );
return 0;
}
int PCB_EDITOR_CONTROL::ViaSizeInc( const TOOL_EVENT& aEvent )
{
BOARD* board = getModel<BOARD>();
int sizeIndex = board->GetDesignSettings().GetViaSizeIndex() + 1;
if( sizeIndex >= (int) board->GetDesignSettings().m_ViasDimensionsList.size() )
sizeIndex = board->GetDesignSettings().m_ViasDimensionsList.size() - 1;
board->GetDesignSettings().SetViaSizeIndex( sizeIndex );
board->GetDesignSettings().UseCustomTrackViaSize( false );
m_toolMgr->RunAction( PCB_ACTIONS::trackViaSizeChanged );
return 0;
}
int PCB_EDITOR_CONTROL::ViaSizeDec( const TOOL_EVENT& aEvent )
{
BOARD* board = getModel<BOARD>();
int sizeIndex = board->GetDesignSettings().GetViaSizeIndex() - 1;
if( sizeIndex < 0 )
sizeIndex = 0;
board->GetDesignSettings().SetViaSizeIndex( sizeIndex );
board->GetDesignSettings().UseCustomTrackViaSize( false );
m_toolMgr->RunAction( PCB_ACTIONS::trackViaSizeChanged );
return 0;
}
int PCB_EDITOR_CONTROL::PlaceModule( const TOOL_EVENT& aEvent )
{
MODULE* module = aEvent.Parameter<MODULE*>();
KIGFX::VIEW_CONTROLS* controls = getViewControls();
SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
SELECTION& selection = selTool->GetSelection();
BOARD_COMMIT commit( m_frame );
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
controls->ShowCursor( true );
controls->SetSnapping( true );
Activate();
m_frame->SetToolID( ID_PCB_MODULE_BUTT, wxCURSOR_PENCIL, _( "Add footprint" ) );
// Add all the drawable parts to preview
VECTOR2I cursorPos = controls->GetCursorPosition();
if( module )
{
module->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, module );
}
bool reselect = false;
// Main loop: keep receiving events
while( OPT_TOOL_EVENT evt = Wait() )
{
cursorPos = controls->GetCursorPosition();
if( reselect && module )
m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, module );
if( evt->IsCancel() || evt->IsActivate() )
{
if( module )
{
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
commit.Revert();
module = NULL;
}
else // let's have another chance placing a module
break;
if( evt->IsActivate() ) // now finish unconditionally
break;
}
else if( evt->IsClick( BUT_LEFT ) )
{
if( !module )
{
// Pick the module to be placed
module = m_frame->LoadModuleFromLibrary( wxEmptyString,
m_frame->Prj().PcbFootprintLibs(),
true, NULL );
2016-08-16 14:56:20 +00:00
if( module == NULL )
continue;
// NOTE: Module has been already added in LoadModuleFromLibrary(),
commit.Added( module );
module->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, module );
controls->SetCursorPosition( cursorPos, false );
}
else
{
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
2016-06-21 15:06:28 +00:00
commit.Push( _( "Place a module" ) );
module = NULL; // to indicate that there is no module that we currently modify
}
bool placing = ( module != NULL );
controls->SetAutoPan( placing );
controls->CaptureCursor( placing );
}
else if( module && evt->IsMotion() )
{
module->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
selection.SetReferencePoint( cursorPos );
getView()->Update( &selection );
}
else if( module && evt->IsAction( &PCB_ACTIONS::properties ) )
{
// Calling 'Properties' action clears the selection, so we need to restore it
reselect = true;
}
}
m_frame->SetNoToolSelected();
return 0;
}
int PCB_EDITOR_CONTROL::ToggleLockSelected( const TOOL_EVENT& aEvent )
{
return modifyLockSelected( TOGGLE );
}
int PCB_EDITOR_CONTROL::LockSelected( const TOOL_EVENT& aEvent )
{
return modifyLockSelected( ON );
}
int PCB_EDITOR_CONTROL::UnlockSelected( const TOOL_EVENT& aEvent )
{
return modifyLockSelected( OFF );
}
int PCB_EDITOR_CONTROL::modifyLockSelected( MODIFY_MODE aMode )
{
SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
const SELECTION& selection = selTool->GetSelection();
if( selection.Empty() )
m_toolMgr->RunAction( PCB_ACTIONS::selectionCursor, true );
bool modified = false;
for( auto i : selection )
{
auto item = static_cast<BOARD_ITEM*>( i );
bool prevState = item->IsLocked();
switch( aMode )
{
case ON:
item->SetLocked( true );
break;
case OFF:
item->SetLocked( false );
break;
case TOGGLE:
item->SetLocked( !prevState );
break;
}
// Check if we really modified an item
if( !modified && prevState != item->IsLocked() )
modified = true;
}
if( modified )
m_frame->OnModify();
return 0;
}
int PCB_EDITOR_CONTROL::PlaceTarget( const TOOL_EVENT& aEvent )
{
KIGFX::VIEW* view = getView();
KIGFX::VIEW_CONTROLS* controls = getViewControls();
BOARD* board = getModel<BOARD>();
PCB_TARGET* target = new PCB_TARGET( board );
// Init the new item attributes
target->SetLayer( Edge_Cuts );
target->SetWidth( board->GetDesignSettings().m_EdgeSegmentWidth );
target->SetSize( Millimeter2iu( 5 ) );
VECTOR2I cursorPos = controls->GetCursorPosition();
target->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
// Add a VIEW_GROUP that serves as a preview for the new item
KIGFX::VIEW_GROUP preview( view );
preview.Add( target );
view->Add( &preview );
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
controls->SetSnapping( true );
Activate();
m_frame->SetToolID( ID_PCB_TARGET_BUTT, wxCURSOR_PENCIL, _( "Add layer alignment target" ) );
// Main loop: keep receiving events
while( OPT_TOOL_EVENT evt = Wait() )
{
cursorPos = controls->GetCursorPosition();
if( evt->IsCancel() || evt->IsActivate() )
break;
else if( evt->IsAction( &PCB_ACTIONS::incWidth ) )
{
target->SetWidth( target->GetWidth() + WIDTH_STEP );
view->Update( &preview );
}
else if( evt->IsAction( &PCB_ACTIONS::decWidth ) )
{
int width = target->GetWidth();
if( width > WIDTH_STEP )
{
target->SetWidth( width - WIDTH_STEP );
view->Update( &preview );
}
}
else if( evt->IsClick( BUT_LEFT ) )
{
assert( target->GetSize() > 0 );
assert( target->GetWidth() > 0 );
2016-06-21 15:06:28 +00:00
BOARD_COMMIT commit( m_frame );
commit.Add( target );
commit.Push( _( "Place a layer alignment target" ) );
preview.Remove( target );
// Create next PCB_TARGET
target = new PCB_TARGET( *target );
preview.Add( target );
}
else if( evt->IsMotion() )
{
target->SetPosition( wxPoint( cursorPos.x, cursorPos.y ) );
view->Update( &preview );
}
}
delete target;
controls->SetSnapping( false );
view->Remove( &preview );
m_frame->SetNoToolSelected();
return 0;
}
2016-12-09 11:04:32 +00:00
static bool mergeZones( BOARD_COMMIT& aCommit, std::vector<ZONE_CONTAINER *>& aOriginZones,
std::vector<ZONE_CONTAINER *>& aMergedZones )
{
2016-12-09 11:04:32 +00:00
for( unsigned int i = 1; i < aOriginZones.size(); i++ )
{
aOriginZones[0]->Outline()->BooleanAdd( *aOriginZones[i]->Outline(),
SHAPE_POLY_SET::PM_FAST );
}
aOriginZones[0]->Outline()->Simplify( SHAPE_POLY_SET::PM_FAST );
// We should have one polygon with hole
// We can have 2 polygons with hole, if the 2 initial polygons have only one common corner
// and therefore cannot be merged (they are dectected as intersecting)
// but we should never have more than 2 polys
if( aOriginZones[0]->Outline()->OutlineCount() > 1 )
{
2016-12-09 11:04:32 +00:00
wxLogMessage( wxT( "BOARD::CombineAreas error: more than 2 polys after merging" ) );
return false;
}
2016-12-09 11:04:32 +00:00
for( unsigned int i = 1; i < aOriginZones.size(); i++ )
{
aCommit.Remove( aOriginZones[i] );
}
2016-12-09 11:04:32 +00:00
aCommit.Modify( aOriginZones[0] );
aMergedZones.push_back( aOriginZones[0] );
aOriginZones[0]->SetLocalFlags( 1 );
aOriginZones[0]->Hatch();
aOriginZones[0]->CacheTriangulation();
return true;
}
2015-06-19 15:32:33 +00:00
int PCB_EDITOR_CONTROL::ZoneMerge( const TOOL_EVENT& aEvent )
{
const SELECTION& selection = m_toolMgr->GetTool<SELECTION_TOOL>()->GetSelection();
2015-06-19 15:32:33 +00:00
BOARD* board = getModel<BOARD>();
2016-06-21 15:06:28 +00:00
BOARD_COMMIT commit( m_frame );
2015-06-19 15:32:33 +00:00
if( selection.Size() < 2 )
return 0;
int netcode = -1;
2016-12-09 11:04:32 +00:00
ZONE_CONTAINER* firstZone = nullptr;
std::vector<ZONE_CONTAINER*> toMerge, merged;
2016-12-09 11:04:32 +00:00
for( auto item : selection )
2015-06-19 15:32:33 +00:00
{
auto curr_area = dynamic_cast<ZONE_CONTAINER*>( item );
2015-06-19 15:32:33 +00:00
if( !curr_area )
continue;
if( !firstZone )
firstZone = curr_area;
2015-06-19 15:32:33 +00:00
netcode = curr_area->GetNetCode();
2015-06-19 15:32:33 +00:00
2017-03-22 13:43:10 +00:00
if( firstZone )
{
2017-06-23 11:56:28 +00:00
if( firstZone->GetNetCode() != netcode )
continue;
2015-06-19 15:32:33 +00:00
2017-06-23 11:56:28 +00:00
if( curr_area->GetPriority() != firstZone->GetPriority() )
continue;
2015-06-19 15:32:33 +00:00
2017-06-23 11:56:28 +00:00
if( curr_area->GetIsKeepout() != firstZone->GetIsKeepout() )
continue;
2015-06-19 15:32:33 +00:00
2017-06-23 11:56:28 +00:00
if( curr_area->GetLayer() != firstZone->GetLayer() )
continue;
2015-06-19 15:32:33 +00:00
2017-06-23 11:56:28 +00:00
if( !board->TestAreaIntersection( curr_area, firstZone ) )
continue;
2015-06-19 15:32:33 +00:00
2017-06-23 11:56:28 +00:00
toMerge.push_back( curr_area );
}
2017-03-22 13:43:10 +00:00
else
{
toMerge.push_back( curr_area );
}
2015-06-19 15:32:33 +00:00
}
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
2016-06-21 15:06:28 +00:00
2016-12-09 11:04:32 +00:00
if( mergeZones( commit, toMerge, merged ) )
2015-06-19 15:32:33 +00:00
{
commit.Push( _( "Merge zones" ) );
2016-12-09 11:04:32 +00:00
for( auto item : merged )
m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, item );
2015-06-19 15:32:33 +00:00
}
return 0;
}
int PCB_EDITOR_CONTROL::ZoneDuplicate( const TOOL_EVENT& aEvent )
{
auto selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
const auto& selection = selTool->GetSelection();
// because this pops up the zone editor, it would be confusing
// to handle multiple zones, so just handle single selections
// containing exactly one zone
if( selection.Size() != 1 )
return 0;
auto oldZone = dyn_cast<ZONE_CONTAINER*>( selection[0] );
if( !oldZone )
return 0;
auto newZone = std::make_unique<ZONE_CONTAINER>( *oldZone );
newZone->ClearSelected();
newZone->UnFill();
ZONE_SETTINGS zoneSettings;
zoneSettings << *oldZone;
bool success = false;
if( oldZone->GetIsKeepout() )
success = InvokeKeepoutAreaEditor( m_frame, &zoneSettings );
else if( oldZone->IsOnCopperLayer() )
success = InvokeCopperZonesEditor( m_frame, &zoneSettings );
else
success = InvokeNonCopperZonesEditor( m_frame, oldZone, &zoneSettings );
// If the new zone is on the same layer as the the initial zone,
// do nothing
if( success )
{
if( oldZone->GetIsKeepout() && ( oldZone->GetLayerSet() == zoneSettings.m_Layers ) )
{
DisplayError(
m_frame, _( "The duplicated keepout zone cannot be on the same layers as the original zone." ) );
success = false;
}
else if( !oldZone->GetIsKeepout() && ( oldZone->GetLayer() == zoneSettings.m_CurrentZone_Layer ) )
{
DisplayError(
m_frame, _( "The duplicated zone cannot be on the same layer as the original zone." ) );
success = false;
}
}
// duplicate the zone
if( success )
{
BOARD_COMMIT commit( m_frame );
zoneSettings.ExportSetting( *newZone );
commit.Add( newZone.release() );
commit.Push( _( "Duplicate zone" ) );
}
return 0;
}
int PCB_EDITOR_CONTROL::CrossProbePcbToSch( const TOOL_EVENT& aEvent )
2015-02-14 20:52:22 +00:00
{
if( m_probingSchToPcb )
{
m_probingSchToPcb = false;
return 0;
}
2015-02-14 20:52:22 +00:00
SELECTION_TOOL* selTool = m_toolMgr->GetTool<SELECTION_TOOL>();
const SELECTION& selection = selTool->GetSelection();
if( selection.Size() == 1 )
m_frame->SendMessageToEESCHEMA( static_cast<BOARD_ITEM*>( selection.Front() ) );
2015-02-14 20:52:22 +00:00
return 0;
}
int PCB_EDITOR_CONTROL::CrossProbeSchToPcb( const TOOL_EVENT& aEvent )
{
BOARD_ITEM* item = aEvent.Parameter<BOARD_ITEM*>();
if( item )
{
m_probingSchToPcb = true;
getView()->SetCenter( VECTOR2D( item->GetPosition() ) );
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
// If it is a pad and the net highlighting tool is enabled, highlight the net
if( item->Type() == PCB_PAD_T && m_frame->GetToolId() == ID_PCB_HIGHLIGHT_BUTT )
{
int net = static_cast<D_PAD*>( item )->GetNetCode();
m_toolMgr->RunAction( PCB_ACTIONS::highlightNet, false, net );
}
else
// Otherwise simply select the corresponding item
{
m_toolMgr->RunAction( PCB_ACTIONS::selectItem, true, item );
}
}
return 0;
}
bool PCB_EDITOR_CONTROL::DoSetDrillOrigin( KIGFX::VIEW* aView, PCB_BASE_FRAME* aFrame,
BOARD_ITEM* originViewItem, const VECTOR2D& aPosition )
{
aFrame->SetAuxOrigin( wxPoint( aPosition.x, aPosition.y ) );
originViewItem->SetPosition( wxPoint( aPosition.x, aPosition.y ) );
2015-06-18 15:51:54 +00:00
aView->MarkDirty();
return true;
}
bool PCB_EDITOR_CONTROL::SetDrillOrigin( KIGFX::VIEW* aView, PCB_BASE_FRAME* aFrame,
BOARD_ITEM* originViewItem, const VECTOR2D& aPosition )
{
aFrame->SaveCopyInUndoList( originViewItem, UR_DRILLORIGIN );
DoSetDrillOrigin( aView, aFrame, originViewItem, aPosition );
}
int PCB_EDITOR_CONTROL::DrillOrigin( const TOOL_EVENT& aEvent )
{
2015-07-24 08:58:47 +00:00
Activate();
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
m_frame->SetToolID( ID_PCB_PLACE_OFFSET_COORD_BUTT, wxCURSOR_HAND, _( "Adjust zero" ) );
picker->SetClickHandler( std::bind( SetDrillOrigin, getView(), m_frame, m_placeOrigin.get(), _1 ) );
picker->Activate();
2015-07-24 08:58:47 +00:00
Wait();
return 0;
}
/**
* Function highlightNet()
* Looks for a BOARD_CONNECTED_ITEM in a given spot, and if one is found - it enables
* highlight for its net.
2017-07-08 19:13:32 +00:00
* @param aToolMgr is the TOOL_MANAGER currently in use.
2017-06-22 07:01:29 +00:00
* @param aPosition is the point where an item is expected (world coordinates).
*/
static bool highlightNet( TOOL_MANAGER* aToolMgr, const VECTOR2D& aPosition )
{
auto render = aToolMgr->GetView()->GetPainter()->GetSettings();
auto frame = static_cast<PCB_EDIT_FRAME*>( aToolMgr->GetEditFrame() );
auto guide = frame->GetCollectorsGuide();
BOARD* board = static_cast<BOARD*>( aToolMgr->GetModel() );
GENERAL_COLLECTOR collector;
int net = -1;
// Find a connected item for which we are going to highlight a net
collector.Collect( board, GENERAL_COLLECTOR::PadsTracksOrZones,
wxPoint( aPosition.x, aPosition.y ), guide );
2017-03-02 19:46:02 +00:00
for( int i = 0; i < collector.GetCount(); i++ )
{
if( collector[i]->Type() == PCB_PAD_T )
{
frame->SendMessageToEESCHEMA( static_cast<BOARD_CONNECTED_ITEM*>( collector[i] ) );
break;
}
}
bool enableHighlight = ( collector.GetCount() > 0 );
// Obtain net code for the clicked item
if( enableHighlight )
net = static_cast<BOARD_CONNECTED_ITEM*>( collector[0] )->GetNetCode();
// Toggle highlight when the same net was picked
if( net > 0 && net == render->GetHighlightNetCode() )
enableHighlight = !render->IsHighlightEnabled();
if( enableHighlight != render->IsHighlightEnabled() || net != render->GetHighlightNetCode() )
{
render->SetHighlight( enableHighlight, net );
aToolMgr->GetView()->UpdateAllLayersColor();
}
// Store the highlighted netcode in the current board (for dialogs for instance)
if( enableHighlight && net >= 0 )
{
board->SetHighLightNet( net );
NETINFO_ITEM* netinfo = board->FindNet( net );
if( netinfo )
{
MSG_PANEL_ITEMS items;
netinfo->GetMsgPanelInfo( items );
frame->SetMsgPanel( items );
frame->SendCrossProbeNetName( netinfo->GetNetname() );
}
}
else
{
board->ResetHighLight();
frame->SetMsgPanel( board );
frame->SendCrossProbeNetName( "" );
}
return true;
}
int PCB_EDITOR_CONTROL::HighlightNet( const TOOL_EVENT& aEvent )
{
int netcode = aEvent.Parameter<intptr_t>();
if( netcode > 0 )
{
KIGFX::RENDER_SETTINGS* render = m_toolMgr->GetView()->GetPainter()->GetSettings();
render->SetHighlight( true, netcode );
m_toolMgr->GetView()->UpdateAllLayersColor();
}
else
{
// No net code specified, pick the net code belonging to the item under the cursor
highlightNet( m_toolMgr, getViewControls()->GetMousePosition() );
}
return 0;
}
int PCB_EDITOR_CONTROL::HighlightNetCursor( const TOOL_EVENT& aEvent )
{
2015-07-24 08:58:47 +00:00
Activate();
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
m_frame->SetToolID( ID_PCB_HIGHLIGHT_BUTT, wxCURSOR_HAND, _( "Highlight net" ) );
picker->SetClickHandler( std::bind( highlightNet, m_toolMgr, _1 ) );
2015-07-24 08:58:47 +00:00
picker->SetSnapping( false );
picker->Activate();
2015-07-24 08:58:47 +00:00
Wait();
return 0;
}
2017-06-23 11:56:28 +00:00
static bool showLocalRatsnest( TOOL_MANAGER* aToolMgr, const VECTOR2D& aPosition )
2017-03-22 13:43:10 +00:00
{
auto selectionTool = aToolMgr->GetTool<SELECTION_TOOL>();
aToolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
aToolMgr->RunAction( PCB_ACTIONS::selectionCursor, true, EDIT_TOOL::FootprintFilter );
const SELECTION& selection = selectionTool->GetSelection();
if( selection.Empty() )
return true;
2017-06-23 11:56:28 +00:00
for( auto item : selection )
{
2017-06-23 11:56:28 +00:00
if( item->Type() == PCB_MODULE_T )
{
2017-06-23 11:56:28 +00:00
for( auto pad : static_cast<MODULE *> (item)->Pads() )
{
pad->SetLocalRatsnestVisible( true );
}
}
}
2017-03-22 13:43:10 +00:00
return true;
}
2017-06-23 11:56:28 +00:00
2017-03-22 13:43:10 +00:00
int PCB_EDITOR_CONTROL::ShowLocalRatsnest( const TOOL_EVENT& aEvent )
{
Activate();
auto picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
m_frame->SetToolID( ID_PCB_SHOW_1_RATSNEST_BUTT, wxCURSOR_PENCIL, _( "Pick Components for Local Ratsnest" ) );
picker->SetClickHandler( std::bind( showLocalRatsnest, m_toolMgr, _1 ) );
2017-03-22 13:43:10 +00:00
picker->SetSnapping( false );
picker->Activate();
Wait();
return 0;
}
2017-06-23 11:56:28 +00:00
int PCB_EDITOR_CONTROL::UpdateSelectionRatsnest( const TOOL_EVENT& aEvent )
{
auto selectionTool = m_toolMgr->GetTool<SELECTION_TOOL>();
auto& selection = selectionTool->GetSelection();
auto connectivity = getModel<BOARD>()->GetConnectivity();
if( selection.Empty() )
{
connectivity->ClearDynamicRatsnest();
}
else if( m_slowRatsnest )
{
// Compute ratsnest only when user stops dragging for a moment
connectivity->HideDynamicRatsnest();
m_ratsnestTimer.Start( 20 );
}
else
{
// Check how much time doest it take to calculate ratsnest
PROF_COUNTER counter;
calculateSelectionRatsnest();
counter.Stop();
// If it is too slow, then switch to 'slow ratsnest' mode when
// ratsnest is calculated when user stops dragging items for a moment
if( counter.msecs() > 25 )
{
m_slowRatsnest = true;
connectivity->HideDynamicRatsnest();
}
}
return 0;
}
int PCB_EDITOR_CONTROL::HideSelectionRatsnest( const TOOL_EVENT& aEvent )
{
getModel<BOARD>()->GetConnectivity()->ClearDynamicRatsnest();
m_slowRatsnest = false;
2017-04-24 20:26:11 +00:00
return 0;
}
void PCB_EDITOR_CONTROL::ratsnestTimer( wxTimerEvent& aEvent )
{
m_ratsnestTimer.Stop();
calculateSelectionRatsnest();
static_cast<PCB_DRAW_PANEL_GAL*>( m_frame->GetGalCanvas() )->RedrawRatsnest();
m_frame->GetGalCanvas()->Refresh();
}
void PCB_EDITOR_CONTROL::calculateSelectionRatsnest()
{
auto selectionTool = m_toolMgr->GetTool<SELECTION_TOOL>();
auto& selection = selectionTool->GetSelection();
auto connectivity = board()->GetConnectivity();
std::vector<BOARD_ITEM*> items;
items.reserve( selection.Size() );
for( auto item : selection )
items.push_back( static_cast<BOARD_ITEM*>( item ) );
connectivity->ComputeDynamicRatsnest( items );
}
void PCB_EDITOR_CONTROL::setTransitions()
{
// Track & via size control
Go( &PCB_EDITOR_CONTROL::TrackWidthInc, PCB_ACTIONS::trackWidthInc.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::TrackWidthDec, PCB_ACTIONS::trackWidthDec.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::ViaSizeInc, PCB_ACTIONS::viaSizeInc.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::ViaSizeDec, PCB_ACTIONS::viaSizeDec.MakeEvent() );
// Zone actions
Go( &PCB_EDITOR_CONTROL::ZoneMerge, PCB_ACTIONS::zoneMerge.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::ZoneDuplicate, PCB_ACTIONS::zoneDuplicate.MakeEvent() );
2015-02-14 20:52:22 +00:00
// Placing tools
Go( &PCB_EDITOR_CONTROL::PlaceTarget, PCB_ACTIONS::placeTarget.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::PlaceModule, PCB_ACTIONS::placeModule.MakeEvent() );
// Other
Go( &PCB_EDITOR_CONTROL::ToggleLockSelected, PCB_ACTIONS::toggleLock.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::LockSelected, PCB_ACTIONS::lock.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::UnlockSelected, PCB_ACTIONS::unlock.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::CrossProbePcbToSch, SELECTION_TOOL::SelectedEvent );
Go( &PCB_EDITOR_CONTROL::CrossProbeSchToPcb, PCB_ACTIONS::crossProbeSchToPcb.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::DrillOrigin, PCB_ACTIONS::drillOrigin.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::HighlightNet, PCB_ACTIONS::highlightNet.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::HighlightNetCursor, PCB_ACTIONS::highlightNetCursor.MakeEvent() );
2017-06-23 11:56:28 +00:00
Go( &PCB_EDITOR_CONTROL::ShowLocalRatsnest, PCB_ACTIONS::showLocalRatsnest.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::UpdateSelectionRatsnest, PCB_ACTIONS::selectionModified.MakeEvent() );
Go( &PCB_EDITOR_CONTROL::HideSelectionRatsnest, SELECTION_TOOL::ClearedEvent );
}
const int PCB_EDITOR_CONTROL::WIDTH_STEP = 100000;