KiCad PCB EDA Suite
pcbnew/pcbnew.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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #ifdef KICAD_SCRIPTING
32  #include <python_scripting.h>
34 #endif
35 #include <fctsys.h>
36 #include <pgm_base.h>
37 #include <kiface_i.h>
38 #include <kiface_ids.h>
39 #include <confirm.h>
40 #include <macros.h>
41 #include <pcb_edit_frame.h>
42 #include <eda_dde.h>
43 #include <wx/file.h>
44 #include <wx/snglinst.h>
45 #include <gestfich.h>
46 #include <pcbnew.h>
47 #include <pcbnew_settings.h>
49 #include <class_board.h>
50 #include <class_draw_panel_gal.h>
51 #include <fp_lib_table.h>
52 #include <footprint_edit_frame.h>
53 #include <footprint_viewer_frame.h>
54 #include <footprint_wizard_frame.h>
56 #include <footprint_info_impl.h>
57 #include <dialog_configure_paths.h>
58 #include "invoke_pcb_dialog.h"
60 
61 extern bool IsWxPythonLoaded();
62 
63 namespace PCB {
64 
65 static struct IFACE : public KIFACE_I
66 {
67  // Of course all are virtual overloads, implementations of the KIFACE.
68 
69  IFACE( const char* aName, KIWAY::FACE_T aType ) :
70  KIFACE_I( aName, aType )
71  {}
72 
73  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
74 
75  void OnKifaceEnd() override;
76 
77  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
78  {
79  switch( aClassId )
80  {
81  case FRAME_PCB_EDITOR:
82  {
83  auto frame = new PCB_EDIT_FRAME( aKiway, aParent );
84 
85 #if defined( KICAD_SCRIPTING )
86  // give the scripting helpers access to our frame
87  ScriptingSetPcbEditFrame( frame );
88 #endif
89 
90  if( Kiface().IsSingle() )
91  {
92  // only run this under single_top, not under a project manager.
93  frame->CreateServer( KICAD_PCB_PORT_SERVICE_NUMBER );
94  }
95 
96  return frame;
97  }
98 
100  return new FOOTPRINT_EDIT_FRAME( aKiway, aParent,
102 
105  return new FOOTPRINT_VIEWER_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
106 
108  return new FOOTPRINT_WIZARD_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
109 
111  return dynamic_cast< wxWindow* >( FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent ) );
112 
114  {
115  DIALOG_CONFIGURE_PATHS dlg( aParent, aKiway->Prj().Get3DFilenameResolver() );
116 
117  // The dialog's constructor probably failed to set its Kiway because the
118  // dynamic_cast fails when aParent was allocated by a separate compiliation
119  // module. So set it directly.
120  dlg.SetKiway( &dlg, aKiway );
121 
122  if( dlg.ShowModal() == wxID_OK )
123  aKiway->CommonSettingsChanged( true, false );
124 
125  // Dialog has completed; nothing to return.
126  return nullptr;
127  }
128 
130  InvokePcbLibTableEditor( aKiway, aParent );
131  // Dialog has completed; nothing to return.
132  return nullptr;
133 
134  default:
135  return nullptr;
136  }
137  }
138 
147  void* IfaceOrAddress( int aDataId ) override
148  {
149  switch( aDataId )
150  {
151  // Return a pointer to the global instance of the footprint list.
153  return (void*) &GFootprintList;
154 
155  // Return a new FP_LIB_TABLE with the global table installed as a fallback.
157  return (void*) new FP_LIB_TABLE( &GFootprintTable );
158 
159  // Return a pointer to the global instance of the global footprint table.
161  return (void*) &GFootprintTable;
162 
163  default:
164  return nullptr;
165  }
166  }
167 
174  void SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
175  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
176  const wxString& aSrcFilePath, wxString& aErrors ) override;
177 
178 } kiface( "pcbnew", KIWAY::FACE_PCB );
179 
180 } // namespace
181 
182 using namespace PCB;
183 
184 
186 
187 
188 KIFACE_I& Kiface() { return kiface; }
189 
190 
191 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
192 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
193 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
194 {
195  process = aProgram;
196  return &kiface;
197 }
198 
199 #if defined( BUILD_KIWAY_DLL )
200 PGM_BASE& Pgm()
201 {
202  wxASSERT( process ); // KIFACE_GETTER has already been called.
203  return *process;
204 }
205 
206 // Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
208 {
209  return process;
210 }
211 
212 #endif
213 
214 
215 #if defined( KICAD_SCRIPTING )
216 static bool scriptingSetup()
217 {
218 
219 #if defined( __WINDOWS__ )
220  // If our python.exe (in kicad/bin) exists, force our kicad python environment
221  wxString kipython = FindKicadFile( "python.exe" );
222 
223  // we need only the path:
224  wxFileName fn( kipython );
225  kipython = fn.GetPath();
226 
227  // If our python install is existing inside kicad, use it
228  // Note: this is useful only when another python version is installed
229  if( wxDirExists( kipython ) )
230  {
231  // clear any PYTHONPATH and PYTHONHOME env var definition: the default
232  // values work fine inside Kicad:
233  wxSetEnv( wxT( "PYTHONPATH" ), wxEmptyString );
234  wxSetEnv( wxT( "PYTHONHOME" ), wxEmptyString );
235 
236  // Add our python executable path in first position:
237  wxString ppath;
238  wxGetEnv( wxT( "PATH" ), &ppath );
239 
240  kipython << wxT( ";" ) << ppath;
241  wxSetEnv( wxT( "PATH" ), kipython );
242  }
243 
244 #elif defined( __WXMAC__ )
245 
246  // Add default paths to PYTHONPATH
247  wxString pypath;
248 
249  // Bundle scripting folder (<kicad.app>/Contents/SharedSupport/scripting)
250  pypath += GetOSXKicadDataDir() + wxT( "/scripting" );
251 
252  // $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i
253  if( wxGetenv("KICAD_PATH") != NULL )
254  {
255  pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") );
256  }
257 
258  // Bundle wxPython folder (<kicad.app>/Contents/Frameworks/python/site-packages)
259  pypath += wxT( ":" ) + Pgm().GetExecutablePath() +
260  wxT( "Contents/Frameworks/python/site-packages" );
261 
262  // Original content of $PYTHONPATH
263  if( wxGetenv( wxT( "PYTHONPATH" ) ) != NULL )
264  {
265  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
266  }
267 
268  // set $PYTHONPATH
269  wxSetEnv( "PYTHONPATH", pypath );
270 
271 #else
272  wxString pypath;
273 
274  // PYTHON_DEST is the scripts install dir as determined by the build system.
275  pypath = Pgm().GetExecutablePath() + wxT( "../" PYTHON_DEST );
276 
277  if( !wxIsEmpty( wxGetenv( wxT( "PYTHONPATH" ) ) ) )
278  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
279 
280  wxSetEnv( wxT( "PYTHONPATH" ), pypath );
281 
282 #endif
283 
285  {
286  wxLogError( "pcbnewInitPythonScripting() failed." );
287  return false;
288  }
289 
290  return true;
291 }
292 #endif // KICAD_SCRIPTING
293 
294 
296 {
297 #if defined( KICAD_SCRIPTING )
298  // Reload plugin list: reload Python plugins if they are newer than the already loaded,
299  // and load new plugins
300  char cmd[1024];
301 
302  snprintf( cmd, sizeof( cmd ), "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) );
303 
304  PyLOCK lock;
305 
306  // ReRun the Python method pcbnew.LoadPlugins (already called when starting Pcbnew)
307  int retv = PyRun_SimpleString( cmd );
308 
309  if( retv != 0 )
310  wxLogError( "Python error %d occurred running command:\n\n`%s`", retv, cmd );
311 #endif
312 }
313 
314 
319 
324 
325 
326 
327 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
328 {
329  // This is process-level-initialization, not project-level-initialization of the DSO.
330  // Do nothing in here pertinent to a project!
333 
334  start_common( aCtlBits );
335 
336  wxFileName fn = FP_LIB_TABLE::GetGlobalTableFileName();
337 
338  if( !fn.FileExists() )
339  {
341 
342  fpDialog.ShowModal();
343  }
344  else
345  {
346  try
347  {
348  // The global table is not related to a specific project. All projects
349  // will use the same global table. So the KIFACE::OnKifaceStart() contract
350  // of avoiding anything project specific is not violated here.
352  return false;
353  }
354  catch( const IO_ERROR& ioe )
355  {
356  // if we are here, a incorrect global footprint library table was found.
357  // Incorrect global symbol library table is not a fatal error:
358  // the user just has to edit the (partially) loaded table.
359  wxString msg = _(
360  "An error occurred attempting to load the global footprint library table.\n"
361  "Please edit this global footprint library table in Preferences menu."
362  );
363 
364  DisplayErrorMessage( NULL, msg, ioe.What() );
365  }
366  }
367 
368 #if defined( KICAD_SCRIPTING )
369  scriptingSetup();
370 #endif
371 
372  return true;
373 }
374 
375 
377 {
378 #if defined( KICAD_SCRIPTING_WXPYTHON )
379  // Restore the thread state and tell Python to cleanup after itself.
380  // wxPython will do its own cleanup as part of that process.
381  // This should only be called if python was setup correctly.
382 
383  if( IsWxPythonLoaded() )
385 #endif
386 
387  end_common();
388 }
389 
390 
391 void IFACE::SaveFileAs( const wxString& aProjectBasePath, const wxString& aSrcProjectName,
392  const wxString& aNewProjectBasePath, const wxString& aNewProjectName,
393  const wxString& aSrcFilePath, wxString& aErrors )
394 {
395  wxFileName destFile( aSrcFilePath );
396  wxString destPath = destFile.GetPathWithSep();
397  wxUniChar pathSep = wxFileName::GetPathSeparator();
398  wxString ext = destFile.GetExt();
399 
400  if( destPath.StartsWith( aProjectBasePath + pathSep ) )
401  destPath.Replace( aProjectBasePath, aNewProjectBasePath, false );
403  wxString srcProjectFootprintLib = pathSep + aSrcProjectName + ".pretty" + pathSep;
404  wxString newProjectFootprintLib = pathSep + aNewProjectName + ".pretty" + pathSep;
405 
406  destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
407 
408  destFile.SetPath( destPath );
409 
410  if( ext == "kicad_pcb" || ext == "kicad_pcb-bak" )
411  {
412  if( destFile.GetName() == aSrcProjectName )
413  destFile.SetName( aNewProjectName );
414 
415  CopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
416  }
417  else if( ext == "brd" )
418  {
419  if( destFile.GetName() == aSrcProjectName )
420  destFile.SetName( aNewProjectName );
421 
422  CopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
423  }
424  else if( ext == "mod" || ext == "kicad_mod" )
425  {
426  // Footprints are not project-specific. Keep their source names.
427  CopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
428  }
429  else if( ext == "cmp" )
430  {
431  // JEY TODO
432  }
433  else if( ext == "rpt" )
434  {
435  // DRC must be the "gold standard". Since we can't gaurantee that there aren't
436  // any non-deterministic cases in the save-as algorithm, we don't want to certify
437  // the result with the source's DRC report. Therefore copy it under the old
438  // name.
439  CopyFile( aSrcFilePath, destFile.GetFullPath(), aErrors );
440  }
441  else if( destFile.GetName() == "fp-lib-table" )
442  {
443  try
444  {
445  FP_LIB_TABLE fpLibTable;
446  fpLibTable.Load( aSrcFilePath );
447 
448  for( unsigned i = 0; i < fpLibTable.GetCount(); i++ )
449  {
450  LIB_TABLE_ROW& row = fpLibTable.At( i );
451  wxString uri = row.GetFullURI();
452 
453  uri.Replace( "/" + aSrcProjectName + ".pretty", "/" + aNewProjectName + ".pretty" );
454 
455  row.SetFullURI( uri );
456  }
457 
458  fpLibTable.Save( destFile.GetFullPath() );
459  }
460  catch( ... )
461  {
462  wxString msg;
463 
464  if( !aErrors.empty() )
465  aErrors += "\n";
466 
467  msg.Printf( _( "Cannot copy file \"%s\"." ), destFile.GetFullPath() );
468  aErrors += msg;
469  }
470  }
471  else
472  {
473  wxFAIL_MSG( "Unexpected filetype for Pcbnew::SaveFileAs()" );
474  }
475 }
476 
FP_LIB_TABLE GFootprintTable
The global footprint library table.
FOOTPRINT_WIZARD_FRAME.
KIFACE_I is a KIFACE (I)mplementation, with some features useful for DSOs which implement a KIFACE.
Definition: kiface_i.h:37
void SetKiway(wxWindow *aDest, KIWAY *aKiway)
Function SetKiway.
#define KICAD_PCB_PORT_SERVICE_NUMBER
< Pcbnew listens on this port for commands from Eeschema
Definition: eda_dde.h:39
void SaveFileAs(const wxString &aProjectBasePath, const wxString &aSrcProjectName, const wxString &aNewProjectBasePath, const wxString &aNewProjectName, const wxString &aSrcFilePath, wxString &aErrors) override
Function SaveFileAs Saving a file under a different name is delegated to the various KIFACEs because ...
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
DDE server & client.
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_i.cpp:89
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
VTBL_ENTRY SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:175
bool pcbnewInitPythonScripting(const char *aUserScriptingPath)
Initialize the python environment and publish the Pcbnew interface inside it.
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.
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Function OnKifaceStart is called just once shortly after the DSO is loaded.
This file is part of the common library.
PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:137
FRAME_T
Enum FRAME_T is the set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:34
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:172
unsigned GetCount() const
Get the number of rows contained in the table.
Component library viewer main window.
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
void CopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Function CopyFile.
Definition: gestfich.cpp:363
APP_SETTINGS_BASE * KifaceSettings() const
Definition: kiface_i.h:103
Return the global FP_LIB_TABLE.
Definition: kiface_ids.h:53
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPcbEditFrame)
void pcbnewFinishPythonScripting()
not specified: a GAL engine must be set by the client
FOOTPRINT_LIST_IMPL GFootprintList
The global footprint info table.
This file contains miscellaneous commonly used macros and functions.
IFACE(const char *aName, KIWAY::FACE_T aType)
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
void InvokePcbLibTableEditor(KIWAY *aKiway, wxWindow *aCaller)
Function InvokePcbLibTableEditor shows the modal DIALOG_FP_LIB_TABLE for purposes of editing the glob...
void OnKifaceEnd() override
Function OnKifaceEnd is called just once just before the DSO is to be unloaded.
static PGM_BASE * process
wxString PyScriptingPath()
Find the Python scripting path.
wxWindow * CreateWindow(wxWindow *aParent, int aClassId, KIWAY *aKiway, int aCtlBits=0) override
Function CreateWindow creates a wxWindow for the current project.
int PGM_BASE * aProgram
#define NULL
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:117
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
static bool LoadGlobalTable(FP_LIB_TABLE &aTable)
Function LoadGlobalTable loads the global footprint library table into aTable.
void end_common()
Common things to do for a top program module, during OnKifaceEnd();.
Definition: kiface_i.cpp:99
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent)
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
pcbnew DSO
Definition: kiway.h:282
Return a new FP_LIB_TABLE with the global table installed as a fallback.
Definition: kiface_ids.h:46
#define KIFACE_GETTER
Definition: kiway.h:112
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:112
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
PCB::IFACE KIFACE_I kiface("pcbnew", KIWAY::FACE_PCB)
FACE_T
Known KIFACE implementations.
Definition: kiway.h:279
JSON_SETTINGS * RegisterSettings(JSON_SETTINGS *aSettings, bool aLoadNow=true)
Takes ownership of the pointer passed in.
int aKiwayVersion
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
see class PGM_BASE
LIB_TABLE_ROW & At(unsigned aIndex)
Get the 'n'th LIB_TABLE_ROW object.
#define _(s)
Definition: 3d_actions.cpp:33
KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:150
PCB_EDIT_FRAME is the main frame for Pcbnew.
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
#define TO_UTF8(wxstring)
VTBL_ENTRY void CommonSettingsChanged(bool aEnvVarsChanged, bool aTextVarsChanged)
Function CommonSettingsChanged Calls CommonSettingsChanged() on all KIWAY_PLAYERs.
Definition: kiway.cpp:476
void InitSettings(APP_SETTINGS_BASE *aSettings)
Definition: kiface_i.h:105
void PythonPluginsReloadBase()
Helper function PythonPluginsReloadBase Reload Python plugins if they are newer than the already load...
bool IsWxPythonLoaded()
PGM_BASE * PgmOrNull()
similat to PGM_BASE& Pgm(), but return a reference that can be nullptr when running a shared lib from...
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
static wxString GetGlobalTableFileName()
Function GetGlobalTableFileName.
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39