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 // A helper call back function used by autoplace.
72 // It is called by the autoplacer to update the view, when something must be displayed
73 // especially each time a footprint is autoplaced,
75 static int refreshCallback( MODULE* aModule )
76 {
77  if( aModule )
78  {
79  fparent->GetGalCanvas()->GetView()->Update( aModule );
80  }
81 
84  wxSafeYield(); // Give a slice of time to refresh the display
85 
86  return 0;
87 }
88 
89 
90 int AUTOPLACE_TOOL::autoplace( std::vector<MODULE*>& aModules, bool aPlaceOffboard )
91 {
92  auto overlay = view()->MakeOverlay();
93 
94  Activate();
95 
96  AR_AUTOPLACER autoplacer( board() );
97 
98  BOARD_COMMIT commit( frame() );
99 
100  autoplacer.SetOverlay( overlay );
101  fparent = frame();
102  std::function<int( MODULE* aModule )> callback = refreshCallback;
103  autoplacer.SetRefreshCallback( callback );
104 
105  std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter(
106  new WX_PROGRESS_REPORTER( frame(), _( "Autoplace Components" ), 1 ) );
107 
108  autoplacer.SetProgressReporter( progressReporter.get() );
109  auto result = autoplacer.AutoplaceModules( aModules, &commit, aPlaceOffboard );
110 
111  if( result == AR_COMPLETED )
112  commit.Push( _( "Autoplace components" ) );
113  else
114  commit.Revert();
115 
116  return 0;
117 }
118 
119 
121 {
122  std::vector<MODULE*> mods;
123 
124  for( auto item : selection() )
125  {
126  if( item->Type() == PCB_MODULE_T )
127  mods.push_back( static_cast<MODULE*>( item ) );
128  }
129 
130  return autoplace( mods, false );
131 }
132 
133 
135 {
136  std::vector<MODULE*> mods;
137 
138  return autoplace( mods, true );
139 }
140 
141 
143 {
146 }
static int refreshCallback(MODULE *aModule)
static PCB_BASE_EDIT_FRAME * fparent
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:398
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:399
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:935
const SELECTION & selection() const
Definition: pcb_tool.cpp:251
KIGFX::PCB_VIEW * view() const
Definition: pcb_tool.h:137
AR_RESULT AutoplaceModules(std::vector< MODULE * > aModules, BOARD_COMMIT *aCommit, bool aPlaceOffboardModules=false)
void SetOverlay(std::shared_ptr< KIGFX::VIEW_OVERLAY > aOverlay)
Set a VIEW overlay to draw items during a autoplace session.
Definition: ar_autoplacer.h:76
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:1553
class MODULE, a footprint
Definition: typeinfo.h:89
int autoplace(std::vector< MODULE * > &aModules, bool aPlaceOffboard)
BOARD * board() const
Definition: pcb_tool.h:140
Class TOOL_EVENT.
Definition: tool_event.h:168
void SetRefreshCallback(std::function< int(MODULE *aModule)> aCallback)
a callback function to redraw on screen the view after changes, for instance after moving a footprint
Definition: ar_autoplacer.h:85
All active tools
Definition: tool_event.h:144
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1539
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
int autoplaceOffboard(const TOOL_EVENT &aEvent)
Common, abstract interface for edit frames.
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
Class TOOL_ACTION.
Definition: tool_action.h:46
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
Definition: ar_autoplacer.h:90
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
PCB_EDIT_FRAME * frame() const
Definition: pcb_tool.h:139
void Activate()
Function Activate() Runs the tool.
Module description (excepted pads)
int autoplaceSelected(const TOOL_EVENT &aEvent)
void MarkDirty()
Function MarkDirty() Forces redraw of view on the next rendering.
Definition: view.h:621