KiCad PCB EDA Suite
Loading...
Searching...
No Matches
autoplace_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 The KiCad Developers, see AUTHORS.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>
27#include <tool/tool_manager.h>
28#include <tools/pcb_actions.h>
30#include <widgets/wx_infobar.h>
32
33#include "ar_autoplacer.h"
34#include "autoplace_tool.h"
35
36
38{
39}
40
41
43{
44}
45
46
47// A helper call back function used by autoplace.
48// It is called by the autoplacer to update the view, when something must be displayed
49// especially each time a footprint is autoplaced,
51
52static int refreshCallback( FOOTPRINT* aFootprint )
53{
54 if( aFootprint )
55 fparent->GetCanvas()->GetView()->Update( aFootprint );
56
59 wxSafeYield(); // Give a slice of time to refresh the display
60
61 return 0;
62}
63
64
65int AUTOPLACE_TOOL::autoplace( std::vector<FOOTPRINT*>& aFootprints )
66{
68
69 if( bbox.GetWidth() == 0 || bbox.GetHeight() == 0 )
70 {
71 wxString msg = wxString::Format( _( "Board edges must be defined on the %s layer." ),
73
74 frame()->GetInfoBar()->RemoveAllButtons();
75 frame()->GetInfoBar()->ShowMessageFor( msg, 5000, wxICON_ERROR );
76 return 0;
77 }
78
79 int locked_count = std::count_if( aFootprints.begin(), aFootprints.end(),
80 [](FOOTPRINT* fp) { return fp->IsLocked(); } );
81
82
83 PCBNEW_SETTINGS* settings = frame()->GetPcbNewSettings();
84
85 if( locked_count > 0 && !settings->m_LockingOptions.m_sessionSkipPrompts )
86 {
87 DIALOG_LOCKED_ITEMS_QUERY dlg( frame(), locked_count, settings->m_LockingOptions );
88
89 switch( dlg.ShowModal() )
90 {
91 case wxID_OK:
92 // Remove locked items from aFootprints
93 aFootprints.erase( std::remove_if( aFootprints.begin(), aFootprints.end(),
94 []( FOOTPRINT* fp )
95 {
96 return fp->IsLocked();
97 } ),
98 aFootprints.end() );
99 break;
100
101 case wxID_CANCEL:
102 // cancel operation
103 return 0;
104
105 case wxID_APPLY:
106 // Proceed with all items (do nothing)
107 break;
108 }
109 }
110
111 Activate();
112
113 AR_AUTOPLACER autoplacer( board() );
114 BOARD_COMMIT commit( frame() );
115
116 std::shared_ptr<KIGFX::VIEW_OVERLAY> overlay = view()->MakeOverlay();
117 autoplacer.SetOverlay( overlay );
118
119 fparent = frame();
120 std::function<int( FOOTPRINT* aFootprint )> callback = refreshCallback;
121 autoplacer.SetRefreshCallback( callback );
122
123 std::unique_ptr<WX_PROGRESS_REPORTER> progressReporter =
124 std::make_unique<WX_PROGRESS_REPORTER>( frame(), _( "Autoplace Components" ), 1 );
125
126 autoplacer.SetProgressReporter( progressReporter.get() );
127 auto result = autoplacer.AutoplaceFootprints( aFootprints, &commit, false );
128
129 if( result == AR_COMPLETED )
130 commit.Push( _( "Autoplace Components" ) );
131 else
132 commit.Revert();
133
134 return 0;
135}
136
137
139{
140 std::vector<FOOTPRINT*> footprints;
141
142 for( EDA_ITEM* item : selection() )
143 {
144 if( item->Type() == PCB_FOOTPRINT_T )
145 {
146 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
147 footprints.push_back( footprint );
148 }
149 }
150
151 return autoplace( footprints );
152}
153
154
156{
157 SHAPE_POLY_SET boardShape;
158 board()->GetBoardPolygonOutlines( boardShape );
159
160 std::vector<FOOTPRINT*> footprints;
161
162 for( FOOTPRINT* footprint : board()->Footprints() )
163 {
164 if( !boardShape.Contains( footprint->GetPosition() ) )
165 footprints.push_back( footprint );
166 }
167
168 return autoplace( footprints );
169}
170
171
173{
176}
@ AR_COMPLETED
Definition: ar_autoplacer.h:51
static int refreshCallback(FOOTPRINT *aFootprint)
static PCB_BASE_EDIT_FRAME * fparent
AR_RESULT AutoplaceFootprints(std::vector< FOOTPRINT * > &aFootprints, 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:69
void SetRefreshCallback(std::function< int(FOOTPRINT *aFootprint)> aCallback)
Callback to redraw on screen the view after changes, for instance after moving a footprint.
Definition: ar_autoplacer.h:77
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
Definition: ar_autoplacer.h:82
int autoplaceOffboard(const TOOL_EVENT &aEvent)
~AUTOPLACE_TOOL()
Bind handlers to corresponding TOOL_ACTIONs.
int autoplaceSelected(const TOOL_EVENT &aEvent)
int autoplace(std::vector< FOOTPRINT * > &aFootprints)
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
virtual void Revert() override
Revert the commit by restoring the modified items state.
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, OUTLINE_ERROR_HANDLER *aErrorHandler=nullptr, bool aAllowUseArcsInPolygons=false, bool aIncludeNPTHAsOutlines=false)
Extract the board outlines and build a closed polygon from lines, arcs and circle items on edge cut l...
Definition: board.cpp:2536
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:937
constexpr size_type GetWidth() const
Definition: box2.h:214
constexpr size_type GetHeight() const
Definition: box2.h:215
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:89
VECTOR2I GetPosition() const override
Definition: footprint.h:222
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:91
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition: view.cpp:1686
void MarkDirty()
Force redraw of view on the next rendering.
Definition: view.h:655
LOCKING_OPTIONS m_LockingOptions
static TOOL_ACTION autoplaceOffboardComponents
Definition: pcb_actions.h:577
static TOOL_ACTION autoplaceSelectedComponents
Definition: pcb_actions.h:578
Common, abstract interface for edit frames.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
T * frame() const
KIGFX::PCB_VIEW * view() const
BOARD * board() const
const PCB_SELECTION & selection() const
FOOTPRINT * footprint() const
Represent a set of closed polygons.
bool Contains(const VECTOR2I &aP, int aSubpolyIndex=-1, int aAccuracy=0, bool aUseBBoxCaches=false) const
Return true if a given subpolygon contains the point aP.
Generic, UI-independent tool event.
Definition: tool_event.h:168
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
void Activate()
Run the tool.
#define _(s)
wxString LayerName(int aLayer)
Returns the default display name for a given layer.
Definition: layer_id.cpp:31
@ Edge_Cuts
Definition: layer_ids.h:112
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
Definition: playground.cpp:46
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition: typeinfo.h:86