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>
26#include <tools/pcb_selection.h>
27#include <tool/tool_manager.h>
28#include <tools/pcb_actions.h>
29#include <widgets/wx_infobar.h>
31
32#include "ar_autoplacer.h"
33#include "autoplace_tool.h"
34
35
37{
38}
39
40
44
45
46// A helper call back function used by autoplace.
47// It is called by the autoplacer to update the view, when something must be displayed
48// especially each time a footprint is autoplaced,
50
51static int refreshCallback( FOOTPRINT* aFootprint )
52{
53 if( aFootprint )
54 fparent->GetCanvas()->GetView()->Update( aFootprint );
55
56 fparent->GetCanvas()->GetView()->MarkDirty();
57 fparent->GetCanvas()->Refresh();
58 wxSafeYield(); // Give a slice of time to refresh the display
59
60 return 0;
61}
62
63
64int AUTOPLACE_TOOL::autoplace( std::vector<FOOTPRINT*>& aFootprints )
65{
67
68 if( bbox.GetWidth() == 0 || bbox.GetHeight() == 0 )
69 {
70 wxString msg = wxString::Format( _( "Board edges must be defined on the %s layer." ),
72
73 frame()->GetInfoBar()->RemoveAllButtons();
74 frame()->GetInfoBar()->ShowMessageFor( msg, 5000, wxICON_ERROR );
75 return 0;
76 }
77
78 if( !frame()->GetOverrideLocks() )
79 {
80 aFootprints.erase( std::remove_if( aFootprints.begin(), aFootprints.end(),
81 []( FOOTPRINT* fp )
82 {
83 return fp->IsLocked();
84 } ),
85 aFootprints.end() );
86 }
87
88 Activate();
89
90 AR_AUTOPLACER autoplacer( board() );
91 BOARD_COMMIT commit( frame() );
92
93 std::shared_ptr<KIGFX::VIEW_OVERLAY> overlay = view()->MakeOverlay();
94 autoplacer.SetOverlay( overlay );
95
96 fparent = frame();
97 std::function<int( FOOTPRINT* aFootprint )> callback = refreshCallback;
98 autoplacer.SetRefreshCallback( callback );
99
100 WX_PROGRESS_REPORTER progressReporter( frame(), _( "Autoplace Footprints" ), 1, PR_CAN_ABORT );
101 autoplacer.SetProgressReporter( &progressReporter );
102
103 auto result = autoplacer.AutoplaceFootprints( aFootprints, &commit, false );
104
105 if( result == AR_COMPLETED )
106 commit.Push( _( "Autoplace Footprints" ) );
107 else
108 commit.Revert();
109
110 return 0;
111}
112
113
115{
116 std::vector<FOOTPRINT*> footprints;
117
118 for( EDA_ITEM* item : selection() )
119 {
120 if( item->Type() == PCB_FOOTPRINT_T )
121 {
122 FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
123 footprints.push_back( footprint );
124 }
125 }
126
127 return autoplace( footprints );
128}
129
130
132{
133 SHAPE_POLY_SET boardShape;
134 board()->GetBoardPolygonOutlines( boardShape, true );
135
136 std::vector<FOOTPRINT*> footprints;
137
138 for( FOOTPRINT* footprint : board()->Footprints() )
139 {
140 if( !boardShape.Contains( footprint->GetPosition() ) )
141 footprints.push_back( footprint );
142 }
143
144 return autoplace( footprints );
145}
146
147
@ AR_COMPLETED
static int refreshCallback(FOOTPRINT *aFootprint)
static PCB_BASE_EDIT_FRAME * fparent
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
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.
void SetRefreshCallback(std::function< int(FOOTPRINT *aFootprint)> aCallback)
Callback to redraw on screen the view after changes, for instance after moving a footprint.
void SetProgressReporter(PROGRESS_REPORTER *aReporter)
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.
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition board.h:1064
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, bool aInferOutlineIfNecessary, 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:3045
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr size_type GetHeight() const
Definition box2.h:215
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:99
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition view.cpp:1713
static TOOL_ACTION autoplaceOffboardComponents
static TOOL_ACTION autoplaceSelectedComponents
Common, abstract interface for edit frames.
T * frame() const
KIGFX::PCB_VIEW * view() const
PCB_TOOL_BASE(TOOL_ID aId, const std::string &aName)
Constructor.
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:171
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.
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
#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
wxString result
Test unit parsing edge cases and error handling.
@ PCB_FOOTPRINT_T
class FOOTPRINT, a footprint
Definition typeinfo.h:86
#define PR_CAN_ABORT