KiCad PCB EDA Suite
footprint_editor_utils.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) 1992-2021 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 #include <board_commit.h>
25 #include <confirm.h>
27 #include <footprint_edit_frame.h>
28 #include <footprint_tree_pane.h>
29 #include <fp_lib_table.h>
30 #include <functional>
31 #include <kiway_express.h>
32 #include <pcb_layer_box_selector.h>
33 #include <pcbnew_id.h>
34 #include <ratsnest/ratsnest_data.h>
36 #include <tool/tool_manager.h>
37 #include <tools/pcb_actions.h>
39 #include <widgets/lib_tree.h>
40 
41 using namespace std::placeholders;
42 
43 
44 void FOOTPRINT_EDIT_FRAME::LoadFootprintFromBoard( wxCommandEvent& event )
45 {
46  LoadFootprintFromBoard( nullptr );
47 }
48 
49 
51 {
52  bool is_last_fp_from_brd = IsCurrentFPFromBoard();
53 
54  FOOTPRINT* footprint = LoadFootprint( aFPID );
55 
56  if( !footprint )
57  return;
58 
59  if( !Clear_Pcb( true ) )
60  return;
61 
62  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
63  AddFootprintToBoard( footprint );
64 
65  footprint->ClearFlags();
66 
67  // if either m_Reference or m_Value are gone, reinstall them -
68  // otherwise you cannot see what you are doing on board
69  FP_TEXT* ref = &footprint->Reference();
70  FP_TEXT* val = &footprint->Value();
71 
72  if( val && ref )
73  {
74  ref->SetType( FP_TEXT::TEXT_is_REFERENCE ); // just in case ...
75 
76  if( ref->GetLength() == 0 )
77  ref->SetText( wxT( "Ref**" ) );
78 
79  val->SetType( FP_TEXT::TEXT_is_VALUE ); // just in case ...
80 
81  if( val->GetLength() == 0 )
82  val->SetText( wxT( "Val**" ) );
83  }
84 
85  if( m_zoomSelectBox->GetSelection() == 0 )
86  Zoom_Automatique( false );
87 
88  Update3DView( true, true );
89 
90  GetScreen()->SetContentModified( false );
91 
92  UpdateView();
93  GetCanvas()->Refresh();
94 
95  // Update the save items if needed.
96  if( is_last_fp_from_brd )
97  {
98  ReCreateMenuBar();
99  ReCreateHToolbar();
100  }
101 
102  m_treePane->GetLibTree()->ExpandLibId( aFPID );
103  m_treePane->GetLibTree()->CenterLibId( aFPID );
104  m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
105 }
106 
107 
108 void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
109 {
110  SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
111 
112  if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
113  GetCanvas()->Refresh();
114 }
115 
116 
117 void FOOTPRINT_EDIT_FRAME::SaveFootprintToBoard( wxCommandEvent& event )
118 {
119  SaveFootprintToBoard( true );
120 }
121 
122 
124 {
125 public:
127  {
128  m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
129  m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
130  m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
131  m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
132  m_keywords = aFootprint->GetKeywords();
133  m_doc = aFootprint->GetDescription();
134  m_loaded = true;
135  }
136 };
137 
138 
140 {
141  LIB_ID oldFPID = aFootprint->GetFPID();
142 
143  DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
144  dialog.ShowModal();
145 
146  // Update library tree
147  BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
148  wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
149 
150  if( treeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
151  // from file therefore not yet in tree.
152  {
153  static_cast<LIB_TREE_NODE_LIB_ID*>( treeItem.GetID() )->Update( &footprintInfo );
154  m_treePane->GetLibTree()->RefreshLibTree();
155  }
156 
157  UpdateTitle(); // in case of a name change...
158 
159  UpdateMsgPanel();
160 }
161 
162 
164 {
165  switch( aItem->Type() )
166  {
167  case PCB_PAD_T:
168  ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
169  break;
170 
171  case PCB_FOOTPRINT_T:
172  editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
173  GetCanvas()->Refresh();
174  break;
175 
176  case PCB_FP_TEXT_T:
177  ShowTextPropertiesDialog( aItem );
178  break;
179 
180  case PCB_FP_SHAPE_T :
181  ShowGraphicItemPropertiesDialog( aItem );
182  break;
183 
184  case PCB_FP_ZONE_T:
185  {
186  ZONE* zone = static_cast<ZONE*>( aItem );
187  bool success = false;
188  ZONE_SETTINGS zoneSettings;
189 
190  zoneSettings << *static_cast<ZONE*>( aItem );
191 
192  if( zone->GetIsRuleArea() )
193  {
194  success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
195  }
196  else if( zone->IsOnCopperLayer() )
197  {
198  success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
199  }
200  else
201  {
202  success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
203  }
204 
205  if( success )
206  {
207  BOARD_COMMIT commit( this );
208  commit.Modify( zone );
209  commit.Push( _( "Edit Zone" ) );
210  zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
211  }
212 
213  break;
214  }
215 
216  case PCB_GROUP_T:
217  m_toolManager->RunAction( PCB_ACTIONS::groupProperties, true, aItem );
218  break;
219 
220  default:
221  wxFAIL_MSG( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type "
222  + aItem->GetClass() );
223  break;
224  }
225 }
226 
227 
229 {
230  return GetColorSettings()->GetColor( LAYER_GRID );
231 }
232 
233 
235 {
237 
238  m_appearancePanel->OnLayerChanged();
239 
240  m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
241  GetCanvas()->SetFocus(); // allow capture of hotkeys
242  GetCanvas()->SetHighContrastLayer( aLayer );
243  GetCanvas()->Refresh();
244 }
245 
246 bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
247 {
248  if( !Clear_Pcb( true ) )
249  return false; // this command is aborted
250 
251  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
252  ImportFootprint( aFileSet[ 0 ] );
253 
254  if( GetBoard()->GetFirstFootprint() )
256 
257  GetScreen()->SetContentModified( false );
258  Zoom_Automatique( false );
259  GetCanvas()->Refresh();
260 
261  return true;
262 }
263 
264 
266 {
267  const std::string& payload = mail.GetPayload();
268 
269  switch( mail.Command() )
270  {
271  case MAIL_FP_EDIT:
272  if( !payload.empty() )
273  {
274  wxFileName fpFileName( payload );
275  wxString libNickname;
276  wxString msg;
277 
278  FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
279  const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
280 
281  if( !libTableRow )
282  {
283  msg.Printf( _( "The current configuration does not include a library named '%s'.\n"
284  "Use Manage Footprint Libraries to edit the configuration." ),
285  fpFileName.GetPath() );
286  DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
287  msg );
288  break;
289  }
290 
291  libNickname = libTableRow->GetNickName();
292 
293  if( !libTable->HasLibrary( libNickname, true ) )
294  {
295  msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
296  "Use Manage Footprint Libraries to edit the configuration." ),
297  libNickname );
298  DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
299  break;
300  }
301 
302  LIB_ID fpId( libNickname, fpFileName.GetName() );
303 
304  if( m_treePane )
305  {
306  m_treePane->GetLibTree()->SelectLibId( fpId );
307  wxCommandEvent event( SYMBOL_SELECTED );
308  wxPostEvent( m_treePane, event );
309  }
310  }
311 
312  break;
313 
314  default:
315  ;
316  }
317 }
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
const wxString & GetDescription() const
Definition: footprint.h:188
Carry a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:38
void OnEditItemRequest(BOARD_ITEM *aItem) override
Install the corresponding dialog editor for the given item.
void LoadFootprintFromLibrary(LIB_ID aFPID)
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
void SelectLayer(wxCommandEvent &event)
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
class FP_TEXT, text in a footprint
Definition: typeinfo.h:92
void editFootprintProperties(FOOTPRINT *aFootprint)
Run the Footprint Properties dialog and handle changes made in it.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:265
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of pads.
Definition: footprint.cpp:1018
void SetActiveLayer(PCB_LAYER_ID aLayer) override
bool GetIsRuleArea() const
Accessors to parameters used in Rule Area zones:
Definition: zone.h:730
This file is part of the common library.
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:80
COLOR4D GetGridColor() override
class PCB_GROUP, a set of BOARD_ITEMs
Definition: typeinfo.h:108
PROJECT & Prj()
Definition: kicad.cpp:403
int InvokeNonCopperZonesEditor(PCB_BASE_FRAME *aParent, ZONE_SETTINGS *aSettings)
Function InvokeNonCopperZonesEditor invokes up a modal dialog window for non-copper zone editing.
Class that computes missing connections on a PCB.
bool OpenProjectFiles(const std::vector< wxString > &aFileSet, int aCtl=0) override
Load a KiCad board (.kicad_pcb) from aFileName.
class FP_SHAPE, a footprint edge
Definition: typeinfo.h:93
class PAD, a pad in a footprint
Definition: typeinfo.h:89
void SaveFootprintToBoard(wxCommandEvent &event)
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
void SetType(TEXT_TYPE aType)
Definition: fp_text.h:140
FP_TEXT & Value()
read/write accessors:
Definition: footprint.h:457
FP_TEXT & Reference()
Definition: footprint.h:458
const wxString & GetNickName() const
PCB_LAYER_ID
A quick note on layer IDs:
FOOTPRINT * GetFirstFootprint() const
Get the first footprint on the board or nullptr.
Definition: board.h:317
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:114
VECTOR2< double > VECTOR2D
Definition: vector2d.h:622
void ExportSetting(ZONE &aTarget, bool aFullExport=true) const
Function ExportSetting copy settings to a given zone.
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
Inactive layers are shown normally (no high-contrast mode)
#define _(s)
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:90
void ClearFlags(EDA_ITEM_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: eda_item.h:154
const LIB_ID & GetFPID() const
Definition: footprint.h:185
Handle a list of polygons defining a copper zone.
Definition: zone.h:57
int GetLength() const
Definition: fp_text.cpp:184
virtual void SetActiveLayer(PCB_LAYER_ID aLayer)
const wxString & GetKeywords() const
Definition: footprint.h:191
int InvokeRuleAreaEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeRuleAreaEditor invokes up a modal dialog window for copper zone editing.
class FOOTPRINT, a footprint
Definition: typeinfo.h:88
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:67
BOARD * GetBoard()
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:283
BASIC_FOOTPRINT_INFO(FOOTPRINT *aFootprint)
void LoadFootprintFromBoard(wxCommandEvent &event)
Called from the main toolbar to load a footprint from board mainly to edit it.
static TOOL_ACTION layerChanged
Definition: pcb_actions.h:292
virtual wxString GetClass() const =0
Return the class name.
class ZONE, managed by a footprint
Definition: typeinfo.h:94
wxString wx_str() const
Definition: utf8.cpp:46
bool IsOnCopperLayer() const override
Definition: zone.cpp:224
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
Return the number of unique non-blank pads.
Definition: footprint.cpp:1037
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
int InvokeCopperZonesEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeCopperZonesEditor invokes up a modal dialog window for copper zone editing.
MAIL_T Command()
Returns the MAIL_T associated with this mail.
Definition: kiway_express.h:49
std::string & GetPayload()
Return the payload, which can be any text but it typically self identifying s-expression.
Definition: kiway_express.h:57
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:905
void KiwayMailIn(KIWAY_EXPRESS &mail) override
Receive KIWAY_EXPRESS messages from other players.
static TOOL_ACTION groupProperties
Definition: pcb_actions.h:219
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:113
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103