KiCad PCB EDA Suite
autoplacer_tool.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 Kicad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
25 #include <board_commit.h>
26 #include <class_draw_panel_gal.h>
27 #include <confirm.h>
31 #include <tool/tool_manager.h>
32 #include <view/view.h>
33 #include <view/view_controls.h>
34 
35 // For frame ToolID values
36 #include <pcbnew_id.h>
37 
38 // For action icons
39 #include <bitmaps.h>
40 
41 #include <class_board_item.h>
42 #include <class_module.h>
43 #include <tools/pcb_actions.h>
44 #include <tools/selection_tool.h>
45 #include <tools/tool_event_utils.h>
46 
48 
49 #include "ar_autoplacer.h"
50 #include "autoplacer_tool.h"
51 
52 
53 TOOL_ACTION PCB_ACTIONS::autoplaceSelectedComponents( "pcbnew.Autoplacer.autoplaceSelected",
54  AS_GLOBAL, 0, _( "Auto-place selected components" ),
55  _( "Performs automatic placement of selected components" ) );
56 
57 TOOL_ACTION PCB_ACTIONS::autoplaceOffboardComponents( "pcbnew.Autoplacer.autoplaceOffboard",
58  AS_GLOBAL, 0, _( "Auto-place off-board components" ),
59  _( "Performs automatic placement of components outside board area" ) );
60 
61 AUTOPLACE_TOOL::AUTOPLACE_TOOL() : PCB_TOOL( "pcbnew.Autoplacer" )
62 {
63 }
64 
65 
67 {
68 }
69 
70 
71 int AUTOPLACE_TOOL::autoplace( std::vector<MODULE*>& aModules, bool aPlaceOffboard )
72 {
73  auto overlay = view()->MakeOverlay();
74 
75  Activate();
76 
77  AR_AUTOPLACER autoplacer( board() );
78 
79  BOARD_COMMIT commit( frame() );
80 
81  autoplacer.SetOverlay( overlay );
82 
83  std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter(
84  new WX_PROGRESS_REPORTER( frame(), _( "Autoplace Components" ), 1 ) );
85 
86  autoplacer.SetProgressReporter( progressReporter.get() );
87  auto result = autoplacer.AutoplaceModules( aModules, &commit, aPlaceOffboard );
88 
89  if( result == AR_COMPLETED )
90  commit.Push( _( "Autoplace components" ) );
91  else
92  commit.Revert();
93 
94  return 0;
95 }
96 
97 
99 {
100  std::vector<MODULE*> mods;
101 
102  for( auto item : selection() )
103  {
104  if( item->Type() == PCB_MODULE_T )
105  mods.push_back( static_cast<MODULE*>( item ) );
106  }
107 
108  return autoplace( mods, false );
109 }
110 
111 
113 {
114  std::vector<MODULE*> mods;
115 
116  return autoplace( mods, true );
117 }
118 
119 
121 {
124 }
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:389
void setTransitions() override
Bind handlers to corresponding TOOL_ACTIONs
This file is part of the common library.
VIEW_CONTROLS class definition.
static TOOL_ACTION autoplaceSelectedComponents
Definition: pcb_actions.h:390
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
AR_RESULT AutoplaceModules(std::vector< MODULE * > aModules, BOARD_COMMIT *aCommit, bool aPlaceOffboardModules=false)
void SetOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Definition: ar_autoplacer.h:73
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition: view.cpp:1544
class MODULE, a footprint
Definition: typeinfo.h:89
int autoplace(std::vector< MODULE * > &aModules, bool aPlaceOffboard)
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:139
Class TOOL_EVENT.
Definition: tool_event.h:168
const SELECTION & selection() const
Definition: pcb_tool.cpp:245
All active tools
Definition: tool_event.h:144
int autoplaceOffboard(const TOOL_EVENT &aEvent)
KIGFX::PCB_VIEW * view() const
Definition: pcb_tool.h:137
Class TOOL_ACTION.
Definition: tool_action.h:46
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
Definition: ar_autoplacer.h:83
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
void Activate()
Function Activate() Runs the tool.
Module description (excepted pads)
int autoplaceSelected(const TOOL_EVENT &aEvent)
BOARD * board() const
Definition: pcb_tool.h:140