KiCad PCB EDA Suite
cvpcb_association_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) 2019 Ian McInerney <Ian.S.McInerney@ieee.org>
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <confirm.h>
22 #include <cstdint>
23 #include <functional>
24 #include <kiface_i.h>
25 #include <kiway_express.h>
26 #include <lib_id.h>
27 #include <tool/actions.h>
28 #include <tool/tool_manager.h>
29 #include <wx/clipbrd.h>
30 
31 #include <cvpcb_association.h>
32 #include <cvpcb_mainframe.h>
35 #include <listboxes.h>
36 #include <tools/cvpcb_actions.h>
38 
39 using namespace std::placeholders;
40 
41 
43  TOOL_INTERACTIVE( "cvpcb.Association" ),
44  m_frame( nullptr )
45 {
46 }
47 
48 
50 {
51  COMPONENT* comp;
52  LIB_ID fpid;
53  switch( m_frame->GetFocusedControl() )
54  {
57  break;
58 
60  // Get the selection
61  comp = m_frame->GetSelectedComponent();
62 
63  if( !comp )
64  return 0;
65 
66  // Get the fpid and save it to the clipboard
67  fpid = comp->GetFPID();
68  break;
69 
70  default:
71  // Do nothing
72  break;
73  }
74 
75  // if no valid fpid, then skip
76  if( !fpid.IsValid() )
77  return 0;
78 
79  if( wxTheClipboard->Open() )
80  {
81  if( !wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) ) )
82  wxLogDebug( "Failed to copy data to clipboard" );
83 
84  wxTheClipboard->Flush();
85  wxTheClipboard->Close();
86  }
87 
88  return 0;
89 }
90 
91 
93 {
94  // Only cut when in the component frame
96  return 0;
97 
98  // Get the selection, but only use the first one
100  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
101 
102  if( idx.empty() || !comp )
103  return 0;
104 
105  // Get the fpid
106  LIB_ID fpid;
107  fpid = comp->GetFPID();
108 
109  // if no valid fpid, then skip
110  if( !fpid.IsValid() )
111  return 0;
112 
113  // Save it to the clipboard
114  if( wxTheClipboard->Open() )
115  {
116  if( !wxTheClipboard->SetData( new wxTextDataObject( fpid.GetUniStringLibId() ) ) )
117  {
118  wxLogDebug( "Failed to cut data to clipboard" );
119  wxTheClipboard->Close();
120  return 0;
121  }
122 
123  wxTheClipboard->Flush();
124  wxTheClipboard->Close();
125  }
126 
127  // Remove the association
128  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( idx.front(), "" ) );
129 
130  return 0;
131 }
132 
133 
135 {
136  // Get the selection
137  std::vector<unsigned int> idx = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
138 
139  if( idx.empty() )
140  return 0;
141 
142  // Get the clipboard data and ensure it is valid
143  LIB_ID fpid;
144  wxTextDataObject data;
145 
146  if( wxTheClipboard->Open() )
147  {
148  wxTheClipboard->GetData( data );
149  wxTheClipboard->Close();
150  }
151 
152  if( fpid.Parse( data.GetText(), LIB_ID::ID_PCB ) >= 0 )
153  return 0;
154 
155  // Assign the fpid to the selections
156  bool firstAssoc = true;
157  for( auto i : idx )
158  {
159  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, fpid ), firstAssoc );
160  firstAssoc = false;
161  }
162 
163  return 0;
164 }
165 
167 {
168  m_frame = getEditFrame<CVPCB_MAINFRAME>();
169 }
170 
171 
173 {
175 
176  return 0;
177 }
178 
179 
181 {
183 
184  return 0;
185 }
186 
187 
189 {
190  // Get the currently selected footprint
191  LIB_ID fpid;
192  wxString fp = m_frame->GetSelectedFootprint();
193  fpid.Parse( fp, LIB_ID::ID_PCB );
194 
195  // Ignore the action if the footprint is empty (nothing selected)
196  if( fpid.empty() )
197  return 0;
198 
199  // Test for validity of the requested footprint
200  if( !fpid.IsValid() )
201  {
202  wxString msg =
203  wxString::Format( _( "\"%s\" is not a valid footprint." ), fpid.Format().wx_str() );
205  }
206 
207  // Get all the components that are selected and associate them with the current footprint
208  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
209 
210  bool firstAssoc = true;
211  for( auto i : sel )
212  {
213  CVPCB_ASSOCIATION newfp( i, fpid );
214  m_frame->AssociateFootprint( newfp, firstAssoc );
215  firstAssoc = false;
216  }
217 
218  // Move to the next not associated component
220 
221  return 0;
222 }
223 
224 
226 {
228 
229  return 0;
230 }
231 
232 
234 {
235  // Get all the components that are selected
236  std::vector<unsigned int> sel = m_frame->GetComponentIndices( CVPCB_MAINFRAME::SEL_COMPONENTS );
237 
238  // Delete the association
239  bool firstAssoc = true;
240  for( auto i : sel )
241  {
242  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
243  firstAssoc = false;
244  }
245 
246  return 0;
247 }
248 
249 
251 {
252  if( IsOK( m_frame, _( "Delete all associations?" ) ) )
253  {
254  // Remove all selections to avoid issues when setting the fpids
255  m_frame->SetSelectedComponent( -1, true );
256  std::vector<unsigned int> idx =
258 
259  bool firstAssoc = true;
260  for( auto i : idx )
261  {
262  m_frame->AssociateFootprint( CVPCB_ASSOCIATION( i, LIB_ID() ), firstAssoc );
263  firstAssoc = false;
264  }
265 
266  // Remove all selections after setting the fpids and select the first component
267  m_frame->SetSelectedComponent( -1, true );
269  }
270 
271  // Update the status display
273 
274  return 0;
275 }
276 
277 
279 {
280  // Association
283 
284  // Deletion
287 
288  // Helpers
291 
292  // Clipboard
296 }
void AutomaticFootprintMatching()
Called by the automatic association button Read *.equ files to try to find corresponding footprint fo...
int Associate(const TOOL_EVENT &aEvent)
Associate the selected footprint with the currently selected components.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:258
static TOOL_ACTION gotoNextNA
Definition: cvpcb_actions.h:56
This file is part of the common library.
int PasteAssoc(const TOOL_EVENT &aEvent)
Paste the clipboard onto the current selection.
void UndoAssociation()
Undo the most recent associations that were performed.
TOOL_MANAGER * m_toolMgr
Definition: tool_base.h:219
void AssociateFootprint(const CVPCB_ASSOCIATION &aAssociation, bool aNewEntry=true, bool aAddUndoItem=true)
Associate a footprint with a specific component in the list.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
void RedoAssociation()
Redo the most recently undone association.
int CutAssoc(const TOOL_EVENT &aEvent)
Cut the selected associations to the clipboard.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
bool IsValid() const
Definition: lib_id.h:171
void setTransitions() override
This method is meant to be overridden in order to specify handlers for events.
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Function Go()
bool empty() const
Definition: lib_id.h:186
int AutoAssociate(const TOOL_EVENT &aEvent)
Perform automatic footprint association.
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
int DeleteAll(const TOOL_EVENT &aEvent)
Delete all associations.
void Reset(RESET_REASON aReason) override
Function Reset() Brings the tool to a known, initial state.
static TOOL_ACTION copy
Definition: actions.h:70
static TOOL_ACTION deleteAll
Definition: cvpcb_actions.h:65
void SetSelectedComponent(int aIndex, bool aSkipUpdate=false)
Set the currently selected component in the components listbox.
Class TOOL_EVENT.
Definition: tool_event.h:168
int Redo(const TOOL_EVENT &aEvent)
Redo the footprint associations most recently done.
int DeleteAssoc(const TOOL_EVENT &aEvent)
Delete the selected associations.
static TOOL_ACTION cut
Definition: actions.h:69
static TOOL_ACTION deleteAssoc
Definition: cvpcb_actions.h:66
#define _(s)
static TOOL_ACTION autoAssociate
Footprint Association actions.
Definition: cvpcb_actions.h:63
UTF8 Format() const
Definition: lib_id.cpp:237
Class COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:83
wxString GetSelectedFootprint()
static TOOL_ACTION redo
Definition: actions.h:68
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
size_t i
Definition: json11.cpp:597
COMPONENT * GetSelectedComponent()
Get the selected component from the component listbox.
wxString wx_str() const
Definition: utf8.cpp:51
int Undo(const TOOL_EVENT &aEvent)
Undo the footprint associations most recently done.
RESET_REASON
Determines the reason of reset for a tool
Definition: tool_base.h:79
A class to define a footprint association to be made in cvpcb.
CVPCB_MAINFRAME::CONTROL_TYPE GetFocusedControl()
Find out which control currently has focus.
int CopyAssoc(const TOOL_EVENT &aEvent)
Copy the selected associations to the clipboard.
static TOOL_ACTION undo
Definition: actions.h:67
std::vector< unsigned int > GetComponentIndices(CVPCB_MAINFRAME::CRITERIA aCriteria=CVPCB_MAINFRAME::ALL_COMPONENTS)
Get the indices for all the components meeting the specified criteria in the components listbox.
wxString GetUniStringLibId() const
Definition: lib_id.h:148
static TOOL_ACTION associate
Definition: cvpcb_actions.h:64
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:294
void DisplayStatus()
Function DisplayStatus updates the information displayed on the status bar at bottom of the main fram...
static TOOL_ACTION paste
Definition: actions.h:71