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-2019 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 
25 #include <board_commit.h>
26 #include <class_board.h>
27 #include <class_edge_mod.h>
28 #include <class_module.h>
29 #include <confirm.h>
30 #include <dialog_create_array.h>
32 #include <footprint_edit_frame.h>
33 #include <footprint_tree_pane.h>
34 #include <footprint_viewer_frame.h>
35 #include <footprint_wizard_frame.h>
36 #include <fp_lib_table.h>
37 #include <functional>
38 #include <gestfich.h>
39 #include <kiway.h>
40 #include <kiway_express.h>
41 #include <pcb_layer_box_selector.h>
42 #include <pcb_layer_widget.h>
43 #include <pcbnew_id.h>
44 #include <ratsnest/ratsnest_data.h>
45 #include <pgm_base.h>
47 #include <tool/tool_manager.h>
48 #include <tools/pcb_actions.h>
49 #include <trigo.h>
50 #include <widgets/lib_tree.h>
51 
52 using namespace std::placeholders;
53 
54 
55 void FOOTPRINT_EDIT_FRAME::LoadModuleFromBoard( wxCommandEvent& event )
56 {
57  Load_Module_From_BOARD( NULL );
58 }
59 
60 
62 {
63  bool is_last_fp_from_brd = IsCurrentFPFromBoard();
64 
65  MODULE* module = LoadFootprint( aFPID );
66 
67  if( !module )
68  return;
69 
70  if( !Clear_Pcb( true ) )
71  return;
72 
73  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
74  AddModuleToBoard( module );
75 
76  auto fp = GetBoard()->GetFirstModule();
77 
78  if( fp )
79  {
80  fp->ClearFlags();
81 
82  // if either m_Reference or m_Value are gone, reinstall them -
83  // otherwise you cannot see what you are doing on board
84  TEXTE_MODULE* ref = &fp->Reference();
85  TEXTE_MODULE* val = &fp->Value();
86 
87  if( val && ref )
88  {
89  ref->SetType( TEXTE_MODULE::TEXT_is_REFERENCE ); // just in case ...
90 
91  if( ref->GetLength() == 0 )
92  ref->SetText( wxT( "Ref**" ) );
93 
94  val->SetType( TEXTE_MODULE::TEXT_is_VALUE ); // just in case ...
95 
96  if( val->GetLength() == 0 )
97  val->SetText( wxT( "Val**" ) );
98  }
99  }
100 
101  Zoom_Automatique( false );
102 
103  Update3DView( true );
104 
105  GetScreen()->ClrModify();
106 
107  updateView();
108  GetCanvas()->Refresh();
109 
110  // Update the save items if needed.
111  if( is_last_fp_from_brd )
112  {
113  ReCreateMenuBar();
114  ReCreateHToolbar();
115  }
116 
117  m_treePane->GetLibTree()->ExpandLibId( aFPID );
118  m_treePane->GetLibTree()->CenterLibId( aFPID );
119  m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
120 }
121 
122 
124 {
125  int id = event.GetId();
126  wxPoint pos;
127 
128  wxGetMousePosition( &pos.x, &pos.y );
129  pos.y += 20;
130 
131  switch( id )
132  {
134  {
135  LIB_ID selected = m_treePane->GetLibTree()->GetSelectedLibId();
136  MODULE* module = CreateNewModule( wxEmptyString );
137 
138  if( !module )
139  break;
140 
141  if( !Clear_Pcb( true ) )
142  break;
143 
144  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
145  AddModuleToBoard( module );
146 
147  // Initialize data relative to nets and netclasses (for a new
148  // module the defaults are used)
149  // This is mandatory to handle and draw pads
151  module->SetPosition( wxPoint( 0, 0 ) );
152 
153  if( GetBoard()->GetFirstModule() )
155 
156  Zoom_Automatique( false );
157  GetScreen()->SetModify();
158 
159  // If selected from the library tree then go ahead and save it there
160  if( !selected.GetLibNickname().empty() )
161  {
162  LIB_ID fpid = module->GetFPID();
163  fpid.SetLibNickname( selected.GetLibNickname() );
164  module->SetFPID( fpid );
165  SaveFootprint( module );
166  GetScreen()->ClrModify();
167  }
168 
169  updateView();
170  GetCanvas()->Refresh();
171  Update3DView( true );
172 
173  SyncLibraryTree( false );
174  }
175  break;
176 
178  {
179  LIB_ID selected = m_treePane->GetLibTree()->GetSelectedLibId();
180 
181  if( IsContentModified() )
182  {
183  if( !HandleUnsavedChanges( this, _( "The current footprint has been modified. "
184  "Save changes?" ),
185  [&]() -> bool {
186  return SaveFootprint( GetBoard()->GetFirstModule() );
187  } ) )
188  {
189  break;
190  }
191  }
192 
193  auto wizard = (FOOTPRINT_WIZARD_FRAME*) Kiway().Player( FRAME_FOOTPRINT_WIZARD, true,
194  this );
195 
196  if( wizard->ShowModal( NULL, this ) )
197  {
198  // Creates the new footprint from python script wizard
199  MODULE* module = wizard->GetBuiltFootprint();
200 
201  if( module == NULL ) // i.e. if create module command aborted
202  break;
203 
204  Clear_Pcb( false );
205 
206  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
207  // Add the new object to board
208  AddModuleToBoard( module );
209 
210  // Initialize data relative to nets and netclasses (for a new
211  // module the defaults are used)
212  // This is mandatory to handle and draw pads
214  module->SetPosition( wxPoint( 0, 0 ) );
215  module->ClearFlags();
216 
217  Zoom_Automatique( false );
218  GetScreen()->SetModify();
219 
220  // If selected from the library tree then go ahead and save it there
221  if( !selected.GetLibNickname().empty() )
222  {
223  LIB_ID fpid = module->GetFPID();
224  fpid.SetLibNickname( selected.GetLibNickname() );
225  module->SetFPID( fpid );
226  SaveFootprint( module );
227  GetScreen()->ClrModify();
228  }
229 
230  updateView();
231  GetCanvas()->Refresh();
232  Update3DView( true );
233 
234  SyncLibraryTree( false );
235  }
236 
237  wizard->Destroy();
238  }
239  break;
240 
241  case ID_MODEDIT_SAVE:
242  if( !GetBoard()->GetFirstModule() ) // no loaded footprint
243  break;
244 
245  if( GetTargetFPID() == GetLoadedFPID() )
246  {
247  if( SaveFootprint( GetBoard()->GetFirstModule() ) )
248  {
249  m_toolManager->GetView()->Update( GetBoard()->GetFirstModule() );
250 
251  GetCanvas()->ForceRefresh();
252  GetScreen()->ClrModify();
253  }
254  }
255 
256  m_treePane->GetLibTree()->RefreshLibTree();
257  break;
258 
259  case ID_MODEDIT_SAVE_AS:
260  if( GetTargetFPID().GetLibItemName().empty() )
261  {
262  // Save Library As
263  const wxString& src_libNickname = GetTargetFPID().GetLibNickname();
264  wxString src_libFullName = Prj().PcbFootprintLibs()->GetFullURI( src_libNickname );
265 
266  if( SaveLibraryAs( src_libFullName ) )
267  SyncLibraryTree( true );
268  }
269  else if( GetTargetFPID() == GetLoadedFPID() )
270  {
271  // Save Board Footprint As
272  MODULE* footprint = GetBoard()->GetFirstModule();
273 
274  if( footprint && SaveFootprintAs( footprint ) )
275  {
276  m_footprintNameWhenLoaded = footprint->GetFPID().GetLibItemName();
277  m_toolManager->GetView()->Update( footprint );
278  GetScreen()->ClrModify();
279 
280  GetCanvas()->ForceRefresh();
281  SyncLibraryTree( true );
282  }
283  }
284  else
285  {
286  // Save Selected Footprint As
287  MODULE* footprint = LoadFootprint( GetTargetFPID() );
288 
289  if( footprint && SaveFootprintAs( footprint ) )
290  SyncLibraryTree( true );
291  }
292 
293  m_treePane->GetLibTree()->RefreshLibTree();
294  break;
295 
297  SaveFootprintToBoard( true );
298  break;
299 
301  SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
302 
303  if( GetDisplayOptions().m_ContrastModeDisplay !=
305  {
306  GetCanvas()->Refresh();
307  }
308  break;
309 
310  case ID_MODEDIT_CHECK:
311  // Currently: not implemented
312  break;
313 
314  default:
315  wxLogDebug( wxT( "FOOTPRINT_EDIT_FRAME::Process_Special_Functions error" ) );
316  break;
317  }
318 }
319 
320 
322 {
323 public:
325  {
326  m_nickname = aModule->GetFPID().GetLibNickname().wx_str();
327  m_fpname = aModule->GetFPID().GetLibItemName().wx_str();
328  m_pad_count = aModule->GetPadCount( DO_NOT_INCLUDE_NPTH );
329  m_unique_pad_count = aModule->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
330  m_keywords = aModule->GetKeywords();
331  m_doc = aModule->GetDescription();
332  m_loaded = true;
333  }
334 };
335 
336 
338 {
339  LIB_ID oldFPID = aModule->GetFPID();
340 
341  DIALOG_FOOTPRINT_FP_EDITOR dialog( this, aModule );
342  dialog.ShowModal();
343 
344  // Update library tree
345  BASIC_FOOTPRINT_INFO footprintInfo( aModule );
346  wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
347 
348  if( treeItem.IsOk() ) // Can be not found in tree if the current footprint is imported from file
349  // therefore not yet in tree.
350  {
351  static_cast<LIB_TREE_NODE_LIB_ID*>( treeItem.GetID() )->Update( &footprintInfo );
352  m_treePane->GetLibTree()->RefreshLibTree();
353  }
354 
355  updateTitle(); // in case of a name change...
356 
357  UpdateMsgPanel();
358 }
359 
360 
362 {
363  switch( aItem->Type() )
364  {
365  case PCB_PAD_T:
366  InstallPadOptionsFrame( static_cast<D_PAD*>( aItem ) );
367  break;
368 
369  case PCB_MODULE_T:
370  editFootprintProperties( (MODULE*) aItem );
371  GetCanvas()->Refresh();
372  break;
373 
374  case PCB_MODULE_TEXT_T:
375  InstallTextOptionsFrame( aItem );
376  break;
377 
378  case PCB_MODULE_EDGE_T :
379  InstallGraphicItemPropertiesDialog( aItem );
380  break;
381 
383  {
384  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( aItem );
385  bool success = false;
386  if( zone )
387  {
388  ZONE_SETTINGS zoneSettings;
389  zoneSettings << *zone;
390  if( zone->GetIsKeepout() )
391  {
392  success = InvokeKeepoutAreaEditor( this, &zoneSettings );
393  }
394  else if( zone->IsOnCopperLayer() )
395  {
396  success = InvokeCopperZonesEditor( this, &zoneSettings );
397  }
398  else
399  {
400  success = InvokeNonCopperZonesEditor( this, &zoneSettings );
401  }
402 
403  if( success )
404  {
405  BOARD_COMMIT commit( this );
406  commit.Modify( zone );
407  commit.Push( _( "Edit Zone" ) );
408  zoneSettings.ExportSetting( *zone );
409  }
410  }
411  }
412  break;
413 
414  default:
415  wxASSERT( 0 );
416  break;
417  }
418 }
419 
420 
422 {
423  return GetColorSettings()->GetColor( LAYER_GRID );
424 }
425 
426 
428 {
430 
431  m_Layers->SelectLayer( aLayer );
432  m_Layers->OnLayerSelected();
433 
434  m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
435  GetCanvas()->SetFocus(); // allow capture of hotkeys
436  GetCanvas()->SetHighContrastLayer( aLayer );
437  GetCanvas()->Refresh();
438 }
439 
440 bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
441 {
442  if( ! Clear_Pcb( true ) )
443  return false; // //this command is aborted
444 
445  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
446  Import_Module( aFileSet[0] );
447 
448  if( GetBoard()->GetFirstModule() )
450 
451  GetScreen()->ClrModify();
452  Zoom_Automatique( false );
453  GetCanvas()->Refresh();
454 
455  return true;
456 }
457 
458 
460 {
461  const std::string& payload = mail.GetPayload();
462 
463  switch( mail.Command() )
464  {
465  case MAIL_FP_EDIT:
466  if( !payload.empty() )
467  {
468  wxFileName fpFileName( payload );
469  wxString libNickname;
470  wxString msg;
471 
472  FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
473  const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
474 
475  if( !libTableRow )
476  {
477  msg.Printf( _( "The current configuration does not include the footprint library\n"
478  "\"%s\".\nUse Manage Footprint Libraries to edit the configuration." ),
479  fpFileName.GetPath() );
480  DisplayErrorMessage( this, _( "Library not found in footprint library table." ), msg );
481  break;
482  }
483 
484  libNickname = libTableRow->GetNickName();
485 
486  if( !libTable->HasLibrary( libNickname, true ) )
487  {
488  msg.Printf( _( "The library with the nickname \"%s\" is not enabled\n"
489  "in the current configuration. Use Manage Footprint Libraries to\n"
490  "edit the configuration." ), libNickname );
491  DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
492  break;
493  }
494 
495  LIB_ID fpId( libNickname, fpFileName.GetName() );
496 
497  if( m_treePane )
498  {
499  m_treePane->GetLibTree()->SelectLibId( fpId );
500  wxCommandEvent event( COMPONENT_SELECTED );
501  wxPostEvent( m_treePane, event );
502  }
503  }
504 
505  break;
506 
507  default:
508  ;
509  }
510 }
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
GetPadCount returns the number of pads.
void BuildListOfNets()
Definition: class_board.h:720
FOOTPRINT_WIZARD_FRAME.
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:61
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Display a dialog with Save, Cancel and Discard Changes buttons.
Definition: confirm.cpp:201
COMMIT & Modify(EDA_ITEM *aItem)
Modifies a given item in the model.
Definition: commit.h:103
KIWAY_EXPRESS carries a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:39
void OnEditItemRequest(BOARD_ITEM *aItem) override
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
unsigned GetUniquePadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
GetUniquePadCount returns the number of unique pads.
int InvokeKeepoutAreaEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeKeepoutAreaEditor invokes up a modal dialog window for copper zone editing.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
This file is part of the common library TODO brief description.
void SetActiveLayer(PCB_LAYER_ID aLayer) override
This file is part of the common library.
BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
COLOR4D GetGridColor() override
PROJECT & Prj()
Definition: kicad.cpp:317
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.
void LoadModuleFromLibrary(LIB_ID aFPID)
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
const LIB_ID & GetFPID() const
Definition: class_module.h:225
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
class EDGE_MODULE, a footprint edge
Definition: typeinfo.h:94
class MODULE, a footprint
Definition: typeinfo.h:89
const wxString & GetNickName() const
PCB_LAYER_ID
A quick note on layer IDs:
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:121
#define NULL
VECTOR2< double > VECTOR2D
Definition: vector2d.h:593
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:342
void editFootprintProperties(MODULE *aFootprint)
Run the Footprint Properties dialog and handle changes made in it.
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
MODULE * GetFirstModule() const
Gets the first module in the list (used in footprint viewer/editor) or NULL if none.
Definition: class_board.h:330
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
void SetPosition(const wxPoint &aPos) override
virtual void SetActiveLayer(PCB_LAYER_ID aLayer)
void ExportSetting(ZONE_CONTAINER &aTarget, bool aFullExport=true) const
Function ExportSetting copy settings to a given zone.
const wxString & GetKeywords() const
Definition: class_module.h:231
void SetType(TEXT_TYPE aType)
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:67
BOARD * GetBoard()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:285
int SetLibNickname(const UTF8 &aNickname)
Override the logical library name portion of the LIB_ID to aNickname.
Definition: lib_id.cpp:193
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void Process_Special_Functions(wxCommandEvent &event)
const wxString & GetDescription() const
Definition: class_module.h:228
int GetLength() const
static TOOL_ACTION layerChanged
Definition: pcb_actions.h:272
see class PGM_BASE
void LoadModuleFromBoard(wxCommandEvent &event)
Called from the main toolbar to load a footprint from board mainly to edit it.
Declaration of the eda_3d_viewer class.
#define _(s)
Definition: 3d_actions.cpp:33
wxString wx_str() const
Definition: utf8.cpp:51
static bool empty(const wxTextEntryBase *aCtrl)
BASIC_FOOTPRINT_INFO(MODULE *aModule)
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Executes the changes.
int InvokeCopperZonesEditor(PCB_BASE_FRAME *aCaller, ZONE_SETTINGS *aSettings)
Function InvokeCopperZonesEditor invokes up a modal dialog window for copper zone editing.
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:233
class ZONE_CONTAINER, managed by a footprint
Definition: typeinfo.h:95
MAIL_T Command()
Function Command returns the MAIL_T associated with this mail.
Definition: kiway_express.h:52
EDGE_MODULE class definition.
std::string & GetPayload()
Function Payload returns the payload, which can be any text but it typicall self identifying s-expres...
Definition: kiway_express.h:62
void SetFPID(const LIB_ID &aFPID)
Definition: class_module.h:226
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:849
void KiwayMailIn(KIWAY_EXPRESS &mail) override
Function KiwayMailIn receives KIWAY_EXPRESS messages from other players.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193
bool empty() const
Definition: utf8.h:108
KIWAY Kiway
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99