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 <collectors.h>
30 #include <config_params.h>
31 #include <confirm.h>
32 #include <dialog_create_array.h>
34 #include <dialog_move_exact.h>
35 #include <fctsys.h>
36 #include <footprint_edit_frame.h>
37 #include <footprint_tree_pane.h>
38 #include <footprint_viewer_frame.h>
39 #include <footprint_wizard_frame.h>
40 #include <fp_lib_table.h>
41 #include <functional>
42 #include <gestfich.h>
43 #include <invoke_pcb_dialog.h>
44 #include <kiface_i.h>
45 #include <kiway.h>
46 #include <kiway_express.h>
47 #include <macros.h>
48 #include <menus_helpers.h>
49 #include <pcb_draw_panel_gal.h>
50 #include <pcb_layer_box_selector.h>
51 #include <pcb_layer_widget.h>
52 #include <pcbnew.h>
53 #include <pcbnew_id.h>
54 #include <pcbnew_settings.h>
55 #include <pgm_base.h>
56 #include <ratsnest_data.h>
58 #include <tool/tool_manager.h>
59 #include <tools/pcb_actions.h>
60 #include <trigo.h>
61 #include <view/view.h>
62 #include <widgets/lib_tree.h>
64 
65 using namespace std::placeholders;
66 
67 
68 void FOOTPRINT_EDIT_FRAME::LoadModuleFromBoard( wxCommandEvent& event )
69 {
70  Load_Module_From_BOARD( NULL );
71 }
72 
73 
75 {
76  bool is_last_fp_from_brd = IsCurrentFPFromBoard();
77 
78  MODULE* module = LoadFootprint( aFPID );
79 
80  if( !module )
81  return;
82 
83  if( !Clear_Pcb( true ) )
84  return;
85 
86  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
87  AddModuleToBoard( module );
88 
89  auto fp = GetBoard()->GetFirstModule();
90 
91  if( fp )
92  {
93  fp->ClearFlags();
94 
95  // if either m_Reference or m_Value are gone, reinstall them -
96  // otherwise you cannot see what you are doing on board
97  TEXTE_MODULE* ref = &fp->Reference();
98  TEXTE_MODULE* val = &fp->Value();
99 
100  if( val && ref )
101  {
102  ref->SetType( TEXTE_MODULE::TEXT_is_REFERENCE ); // just in case ...
103 
104  if( ref->GetLength() == 0 )
105  ref->SetText( wxT( "Ref**" ) );
106 
107  val->SetType( TEXTE_MODULE::TEXT_is_VALUE ); // just in case ...
108 
109  if( val->GetLength() == 0 )
110  val->SetText( wxT( "Val**" ) );
111  }
112  }
113 
114  Zoom_Automatique( false );
115 
116  Update3DView( true );
117 
118  GetScreen()->ClrModify();
119 
120  updateView();
121  GetCanvas()->Refresh();
122 
123  // Update the save items if needed.
124  if( is_last_fp_from_brd )
125  {
126  ReCreateMenuBar();
127  ReCreateHToolbar();
128  }
129 
130  m_treePane->GetLibTree()->ExpandLibId( aFPID );
131  m_treePane->GetLibTree()->CenterLibId( aFPID );
132  m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
133 }
134 
135 
137 {
138  int id = event.GetId();
139  wxPoint pos;
140 
141  wxGetMousePosition( &pos.x, &pos.y );
142  pos.y += 20;
143 
144  switch( id )
145  {
147  {
148  LIB_ID selected = m_treePane->GetLibTree()->GetSelectedLibId();
149  MODULE* module = CreateNewModule( wxEmptyString );
150 
151  if( !module )
152  break;
153 
154  if( !Clear_Pcb( true ) )
155  break;
156 
157  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
158  AddModuleToBoard( module );
159 
160  // Initialize data relative to nets and netclasses (for a new
161  // module the defaults are used)
162  // This is mandatory to handle and draw pads
164  module->SetPosition( wxPoint( 0, 0 ) );
165 
166  if( GetBoard()->GetFirstModule() )
168 
169  Zoom_Automatique( false );
170  GetScreen()->SetModify();
171 
172  // If selected from the library tree then go ahead and save it there
173  if( !selected.GetLibNickname().empty() )
174  {
175  LIB_ID fpid = module->GetFPID();
176  fpid.SetLibNickname( selected.GetLibNickname() );
177  module->SetFPID( fpid );
178  SaveFootprint( module );
179  GetScreen()->ClrModify();
180  }
181 
182  updateView();
183  GetCanvas()->Refresh();
184  Update3DView( true );
185 
186  SyncLibraryTree( false );
187  }
188  break;
189 
191  {
192  LIB_ID selected = m_treePane->GetLibTree()->GetSelectedLibId();
193 
194  if( IsContentModified() )
195  {
196  if( !HandleUnsavedChanges( this, _( "The current footprint has been modified. "
197  "Save changes?" ),
198  [&]() -> bool {
199  return SaveFootprint( GetBoard()->GetFirstModule() );
200  } ) )
201  {
202  break;
203  }
204  }
205 
206  auto wizard = (FOOTPRINT_WIZARD_FRAME*) Kiway().Player( FRAME_FOOTPRINT_WIZARD, true,
207  this );
208 
209  if( wizard->ShowModal( NULL, this ) )
210  {
211  // Creates the new footprint from python script wizard
212  MODULE* module = wizard->GetBuiltFootprint();
213 
214  if( module == NULL ) // i.e. if create module command aborted
215  break;
216 
217  Clear_Pcb( false );
218 
219  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
220  // Add the new object to board
221  AddModuleToBoard( module );
222 
223  // Initialize data relative to nets and netclasses (for a new
224  // module the defaults are used)
225  // This is mandatory to handle and draw pads
227  module->SetPosition( wxPoint( 0, 0 ) );
228  module->ClearFlags();
229 
230  Zoom_Automatique( false );
231  GetScreen()->SetModify();
232 
233  // If selected from the library tree then go ahead and save it there
234  if( !selected.GetLibNickname().empty() )
235  {
236  LIB_ID fpid = module->GetFPID();
237  fpid.SetLibNickname( selected.GetLibNickname() );
238  module->SetFPID( fpid );
239  SaveFootprint( module );
240  GetScreen()->ClrModify();
241  }
242 
243  updateView();
244  GetCanvas()->Refresh();
245  Update3DView( true );
246 
247  SyncLibraryTree( false );
248  }
249 
250  wizard->Destroy();
251  }
252  break;
253 
254  case ID_MODEDIT_SAVE:
255  if( !GetBoard()->GetFirstModule() ) // no loaded footprint
256  break;
257 
258  if( GetTargetFPID() == GetLoadedFPID() )
259  {
260  if( SaveFootprint( GetBoard()->GetFirstModule() ) )
261  {
262  m_toolManager->GetView()->Update( GetBoard()->GetFirstModule() );
263 
264  GetCanvas()->ForceRefresh();
265  GetScreen()->ClrModify();
266  }
267  }
268 
269  m_treePane->GetLibTree()->RefreshLibTree();
270  break;
271 
272  case ID_MODEDIT_SAVE_AS:
273  if( GetTargetFPID().GetLibItemName().empty() )
274  {
275  // Save Library As
276  const wxString& src_libNickname = GetTargetFPID().GetLibNickname();
277  wxString src_libFullName = Prj().PcbFootprintLibs()->GetFullURI( src_libNickname );
278 
279  if( SaveLibraryAs( src_libFullName ) )
280  SyncLibraryTree( true );
281  }
282  else if( GetTargetFPID() == GetLoadedFPID() )
283  {
284  // Save Board Footprint As
285  MODULE* footprint = GetBoard()->GetFirstModule();
286 
287  if( footprint && SaveFootprintAs( footprint ) )
288  {
289  m_footprintNameWhenLoaded = footprint->GetFPID().GetLibItemName();
290  m_toolManager->GetView()->Update( footprint );
291  GetScreen()->ClrModify();
292 
293  GetCanvas()->ForceRefresh();
294  SyncLibraryTree( true );
295  }
296  }
297  else
298  {
299  // Save Selected Footprint As
300  MODULE* footprint = LoadFootprint( GetTargetFPID() );
301 
302  if( footprint && SaveFootprintAs( footprint ) )
303  SyncLibraryTree( true );
304  }
305 
306  m_treePane->GetLibTree()->RefreshLibTree();
307  break;
308 
310  SaveFootprintToBoard( true );
311  break;
312 
314  SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
315 
316  if( GetDisplayOptions().m_ContrastModeDisplay )
317  GetCanvas()->Refresh();
318  break;
319 
320  case ID_MODEDIT_CHECK:
321  // Currently: not implemented
322  break;
323 
324  default:
325  wxLogDebug( wxT( "FOOTPRINT_EDIT_FRAME::Process_Special_Functions error" ) );
326  break;
327  }
328 }
329 
330 
332 {
333 public:
335  {
336  m_nickname = aModule->GetFPID().GetLibNickname().wx_str();
337  m_fpname = aModule->GetFPID().GetLibItemName().wx_str();
338  m_pad_count = aModule->GetPadCount( DO_NOT_INCLUDE_NPTH );
339  m_unique_pad_count = aModule->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
340  m_keywords = aModule->GetKeywords();
341  m_doc = aModule->GetDescription();
342  m_loaded = true;
343  }
344 };
345 
346 
348 {
349  LIB_ID oldFPID = aModule->GetFPID();
350 
351  DIALOG_FOOTPRINT_FP_EDITOR dialog( this, aModule );
352  dialog.ShowModal();
353 
354  // Update library tree
355  BASIC_FOOTPRINT_INFO footprintInfo( aModule );
356  wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
357 
358  if( treeItem.IsOk() ) // Can be not found in tree if the current footprint is imported from file
359  // therefore not yet in tree.
360  {
361  static_cast<LIB_TREE_NODE_LIB_ID*>( treeItem.GetID() )->Update( &footprintInfo );
362  m_treePane->GetLibTree()->RefreshLibTree();
363  }
364 
365  updateTitle(); // in case of a name change...
366 
367  UpdateMsgPanel();
368 }
369 
370 
372 {
373  switch( aItem->Type() )
374  {
375  case PCB_PAD_T:
376  InstallPadOptionsFrame( static_cast<D_PAD*>( aItem ) );
377  break;
378 
379  case PCB_MODULE_T:
380  editFootprintProperties( (MODULE*) aItem );
381  GetCanvas()->Refresh();
382  break;
383 
384  case PCB_MODULE_TEXT_T:
385  InstallTextOptionsFrame( aItem );
386  break;
387 
388  case PCB_MODULE_EDGE_T :
389  InstallGraphicItemPropertiesDialog( aItem );
390  break;
391 
393  {
394  ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( aItem );
395  bool success = false;
396  if( zone )
397  {
398  ZONE_SETTINGS zoneSettings;
399  zoneSettings << *zone;
400  if( zone->GetIsKeepout() )
401  {
402  success = InvokeKeepoutAreaEditor( this, &zoneSettings );
403  }
404  else if( zone->IsOnCopperLayer() )
405  {
406  success = InvokeCopperZonesEditor( this, &zoneSettings );
407  }
408  else
409  {
410  success = InvokeNonCopperZonesEditor( this, &zoneSettings );
411  }
412 
413  if( success )
414  {
415  BOARD_COMMIT commit( this );
416  commit.Modify( zone );
417  commit.Push( _( "Edit Zone" ) );
418  zoneSettings.ExportSetting( *zone );
419  }
420  }
421  }
422  break;
423 
424  default:
425  wxASSERT( 0 );
426  break;
427  }
428 }
429 
430 
432 {
433  return GetColorSettings()->GetColor( LAYER_GRID );
434 }
435 
436 
438 {
440 
441  m_Layers->SelectLayer( aLayer );
442  m_Layers->OnLayerSelected();
443 
444  m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
445  GetCanvas()->SetFocus(); // allow capture of hotkeys
446  GetCanvas()->SetHighContrastLayer( aLayer );
447  GetCanvas()->Refresh();
448 }
449 
450 bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
451 {
452  if( ! Clear_Pcb( true ) )
453  return false; // //this command is aborted
454 
455  GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
456  Import_Module( aFileSet[0] );
457 
458  if( GetBoard()->GetFirstModule() )
460 
461  GetScreen()->ClrModify();
462  Zoom_Automatique( false );
463  GetCanvas()->Refresh();
464 
465  return true;
466 }
467 
468 
470 {
471  const std::string& payload = mail.GetPayload();
472 
473  switch( mail.Command() )
474  {
475  case MAIL_FP_EDIT:
476  if( !payload.empty() )
477  {
478  wxFileName fpFileName( payload );
479  wxString libNickname;
480  wxString msg;
481 
482  FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
483  const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
484 
485  if( !libTableRow )
486  {
487  msg.Printf( _( "The current configuration does not include the footprint library\n"
488  "\"%s\".\nUse Manage Footprint Libraries to edit the configuration." ),
489  fpFileName.GetPath() );
490  DisplayErrorMessage( this, _( "Library not found in footprint library table." ), msg );
491  break;
492  }
493 
494  libNickname = libTableRow->GetNickName();
495 
496  if( !libTable->HasLibrary( libNickname, true ) )
497  {
498  msg.Printf( _( "The library with the nickname \"%s\" is not enabled\n"
499  "in the current configuration. Use Manage Footprint Libraries to\n"
500  "edit the configuration." ), libNickname );
501  DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
502  break;
503  }
504 
505  LIB_ID fpId( libNickname, fpFileName.GetName() );
506 
507  if( m_treePane )
508  {
509  m_treePane->GetLibTree()->SelectLibId( fpId );
510  wxCommandEvent event( COMPONENT_SELECTED );
511  wxPostEvent( m_treePane, event );
512  }
513  }
514 
515  break;
516 
517  default:
518  ;
519  }
520 }
unsigned GetPadCount(INCLUDE_NPTH_T aIncludeNPTH=INCLUDE_NPTH_T(INCLUDE_NPTH)) const
GetPadCount returns the number of pads.
void BuildListOfNets()
Definition: class_board.h:710
FOOTPRINT_WIZARD_FRAME.
ZONE_CONTAINER handles a list of polygons defining a copper zone.
Definition: class_zone.h:60
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:215
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
This file contains miscellaneous commonly used macros and functions.
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:116
#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:341
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:310
Definition of file extensions used in Kicad.
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)
Function SetActiveLayer will change the currently active layer to 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:221
void SetType(TEXT_TYPE aType)
ZONE_SETTINGS handles zones parameters.
Definition: zone_settings.h:58
BOARD * GetBoard()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:461
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:218
int GetLength() const
static TOOL_ACTION layerChanged
Definition: pcb_actions.h:271
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:240
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:216
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:197
bool empty() const
Definition: utf8.h:108
KIWAY Kiway
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:40