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 
54 {
55 }
56 
57 
59 {
60 }
61 
62 
63 // A helper call back function used by autoplace.
64 // It is called by the autoplacer to update the view, when something must be displayed
65 // especially each time a footprint is autoplaced,
67 static int refreshCallback( MODULE* aModule )
68 {
69  if( aModule )
70  fparent->GetCanvas()->GetView()->Update( aModule );
71 
74  wxSafeYield(); // Give a slice of time to refresh the display
75 
76  return 0;
77 }
78 
79 
80 int AUTOPLACE_TOOL::autoplace( std::vector<MODULE*>& aModules, bool aPlaceOffboard )
81 {
82  auto overlay = view()->MakeOverlay();
83 
84  Activate();
85 
86  AR_AUTOPLACER autoplacer( board() );
87 
88  BOARD_COMMIT commit( frame() );
89 
90  autoplacer.SetOverlay( overlay );
91  fparent = frame();
92  std::function<int( MODULE* aModule )> callback = refreshCallback;
93  autoplacer.SetRefreshCallback( callback );
94 
95  std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter(
96  new WX_PROGRESS_REPORTER( frame(), _( "Autoplace Components" ), 1 ) );
97 
98  autoplacer.SetProgressReporter( progressReporter.get() );
99  auto result = autoplacer.AutoplaceModules( aModules, &commit, aPlaceOffboard );
100 
101  if( result == AR_COMPLETED )
102  commit.Push( _( "Autoplace components" ) );
103  else
104  commit.Revert();
105 
106  return 0;
107 }
108 
109 
111 {
112  std::vector<MODULE*> mods;
113 
114  for( auto item : selection() )
115  {
116  if( item->Type() == PCB_MODULE_T )
117  mods.push_back( static_cast<MODULE*>( item ) );
118  }
119 
120  return autoplace( mods, false );
121 }
122 
123 
125 {
126  std::vector<MODULE*> mods;
127 
128  return autoplace( mods, true );
129 }
130 
131 
133 {
136 }
static int refreshCallback(MODULE *aModule)
static PCB_BASE_EDIT_FRAME * fparent
BOARD * board() const
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:427
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags) override
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: pcb_view.cpp:91
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:428
Classes BOARD_ITEM and BOARD_CONNECTED_ITEM.
virtual void Revert() override
Revertes the commit by restoring the modifed items state.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
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:1554
PCB_BASE_EDIT_FRAME * frame() const
class MODULE, a footprint
Definition: typeinfo.h:89
const PCBNEW_SELECTION & selection() const
int autoplace(std::vector< MODULE * > &aModules, bool aPlaceOffboard)
Class TOOL_EVENT.
Definition: tool_event.h:168
KIGFX::PCB_VIEW * view() const
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
#define _(s)
virtual KIGFX::PCB_VIEW * GetView() const override
Function GetView() Returns a pointer to the VIEW instance used in the panel.
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.
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.
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:610