KiCad PCB EDA Suite
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-2017 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 <make_unique.h>
42 #include <class_drawpanel.h>
43 #include <pcb_edit_frame.h>
44 #include <eda_dde.h>
45 #include <wx/stdpaths.h>
46 
47 #include <wx/file.h>
48 #include <wx/snglinst.h>
49 #include <wx/dir.h>
50 #include <gestfich.h>
51 
52 #include <pcbnew.h>
53 #include <hotkeys.h>
55 #include <class_board.h>
56 #include <class_draw_panel_gal.h>
57 #include <fp_lib_table.h>
58 #include <footprint_edit_frame.h>
59 #include <footprint_viewer_frame.h>
60 #include <footprint_wizard_frame.h>
62 #include <footprint_info_impl.h>
63 #include <gl_context_mgr.h>
64 #include "invoke_pcb_dialog.h"
65 
66 extern bool IsWxPythonLoaded();
67 
68 // Colors for layers and items
69 
73 bool g_Raccord_45_Auto = true;
74 
75 wxPoint g_Offset_Module; // module offset used when moving a footprint
76 
77 /* Name of the document footprint list
78  * usually located in share/modules/footprints_doc
79  * this is of the responsibility to users to create this file
80  * if they want to have a list of footprints
81  */
82 wxString g_DocModulesFileName = wxT( "footprints_doc/footprints.pdf" );
83 
84 /*
85  * Used in track creation, a list of track segments currently being created,
86  * with the newest track at the end of the list, sorted by new-ness. e.g. use
87  * TRACK->Back() to get the next older track, TRACK->Next() to get the next
88  * newer track.
89  */
91 
92 
93 
94 namespace PCB {
95 
96 static struct IFACE : public KIFACE_I
97 {
98  // Of course all are virtual overloads, implementations of the KIFACE.
99 
100  IFACE( const char* aName, KIWAY::FACE_T aType ) :
101  KIFACE_I( aName, aType )
102  {}
103 
104  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
105 
106  void OnKifaceEnd() override;
107 
108  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
109  {
110  switch( aClassId )
111  {
112  case FRAME_PCB:
113  {
114  auto frame = new PCB_EDIT_FRAME( aKiway, aParent );
115 
116 #if defined( KICAD_SCRIPTING )
117  // give the scripting helpers access to our frame
118  ScriptingSetPcbEditFrame( frame );
119 #endif
120 
121  if( Kiface().IsSingle() )
122  {
123  // only run this under single_top, not under a project manager.
124  frame->CreateServer( KICAD_PCB_PORT_SERVICE_NUMBER );
125  }
126 
127  return frame;
128  }
129 
133  {
135  auto pcbFrame = static_cast<PCB_EDIT_FRAME*>( wxWindow::FindWindowByName( wxT( "Pcbnew" ) ) );
136 
137  if( pcbFrame )
138  backend = pcbFrame->GetGalCanvas()->GetBackend();
139  else
141 
142  switch( aClassId )
143  {
145  return new FOOTPRINT_EDIT_FRAME( aKiway, aParent, backend );
148  return new FOOTPRINT_VIEWER_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
149  }
150  }
151 
153  return new FOOTPRINT_WIZARD_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
154 
156  return dynamic_cast< wxWindow* >( FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent ) );
157 
159  InvokePcbLibTableEditor( aKiway, aParent );
160 
161  // Dialog has completed; nothing to return.
162  return nullptr;
163 
164  default:
165  return nullptr;
166  }
167  }
168 
180  void* IfaceOrAddress( int aDataId ) override
181  {
182  switch( aDataId )
183  {
184  // Return a pointer to the global instance of the footprint list.
186  return (void*) &GFootprintList;
187 
188  // Return a new FP_LIB_TABLE with the global table installed as a fallback.
190  return (void*) new FP_LIB_TABLE( &GFootprintTable );
191 
192  // Return a pointer to the global instance of the global footprint table.
194  return (void*) &GFootprintTable;
195 
196  default:
197  return nullptr;
198  }
199  }
200 
201 } kiface( "pcbnew", KIWAY::FACE_PCB );
202 
203 } // namespace
204 
205 using namespace PCB;
206 
207 
209 
210 
211 KIFACE_I& Kiface() { return kiface; }
212 
213 
214 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
215 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
216 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
217 {
218  process = aProgram;
219  return &kiface;
220 }
221 
222 #if defined( BUILD_KIWAY_DLL )
223 PGM_BASE& Pgm()
224 {
225  wxASSERT( process ); // KIFACE_GETTER has already been called.
226  return *process;
227 }
228 #endif
229 
230 
231 #if defined( KICAD_SCRIPTING )
232 static bool scriptingSetup()
233 {
234 
235 #if defined( __WINDOWS__ )
236  // If our python.exe (in kicad/bin) exists, force our kicad python environment
237  wxString kipython = FindKicadFile( "python.exe" );
238 
239  // we need only the path:
240  wxFileName fn( kipython );
241  kipython = fn.GetPath();
242 
243  // If our python install is existing inside kicad, use it
244  // Note: this is usefull only when another python version is installed
245  if( wxDirExists( kipython ) )
246  {
247  // clear any PYTHONPATH and PYTHONHOME env var definition: the default
248  // values work fine inside Kicad:
249  wxSetEnv( wxT( "PYTHONPATH" ), wxEmptyString );
250  wxSetEnv( wxT( "PYTHONHOME" ), wxEmptyString );
251 
252  // Add our python executable path in first position:
253  wxString ppath;
254  wxGetEnv( wxT( "PATH" ), &ppath );
255 
256  kipython << wxT( ";" ) << ppath;
257  wxSetEnv( wxT( "PATH" ), kipython );
258  }
259 
260 #elif defined( __WXMAC__ )
261 
262  // Add default paths to PYTHONPATH
263  wxString pypath;
264 
265  // Bundle scripting folder (<kicad.app>/Contents/SharedSupport/scripting)
266  pypath += GetOSXKicadDataDir() + wxT( "/scripting" );
267 
268  // $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i
269  if( wxGetenv("KICAD_PATH") != NULL )
270  {
271  pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") );
272  }
273 
274  // Bundle wxPython folder (<kicad.app>/Contents/Frameworks/python/site-packages)
275  pypath += wxT( ":" ) + Pgm().GetExecutablePath() +
276  wxT( "Contents/Frameworks/python/site-packages" );
277 
278  // Original content of $PYTHONPATH
279  if( wxGetenv( wxT( "PYTHONPATH" ) ) != NULL )
280  {
281  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
282  }
283 
284  // set $PYTHONPATH
285  wxSetEnv( "PYTHONPATH", pypath );
286 
287 #else
288  wxString pypath;
289 
290  // PYTHON_DEST is the scripts install dir as determined by the build system.
291  pypath = Pgm().GetExecutablePath() + wxT( "../" PYTHON_DEST );
292 
293  if( !wxIsEmpty( wxGetenv( wxT( "PYTHONPATH" ) ) ) )
294  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
295 
296  wxSetEnv( wxT( "PYTHONPATH" ), pypath );
297 
298 #endif
299 
301  {
302  wxLogError( "pcbnewInitPythonScripting() failed." );
303  return false;
304  }
305 
306  return true;
307 }
308 #endif // KICAD_SCRIPTING
309 
310 
312 {
313 #if defined(KICAD_SCRIPTING)
314  //Reload plugin list: reload Python plugins if they are newer than
315  // the already loaded, and load new plugins
316  char cmd[1024];
317 
318  snprintf( cmd, sizeof(cmd),
319  "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) );
320 
321  PyLOCK lock;
322 
323  // ReRun the Python method pcbnew.LoadPlugins
324  // (already called when starting Pcbnew)
325  PyRun_SimpleString( cmd );
326 #endif
327 }
328 
329 
334 
339 
340 
341 
342 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
343 {
344  // This is process level, not project level, initialization of the DSO.
345 
346  // Do nothing in here pertinent to a project!
347 
348  start_common( aCtlBits );
349 
350  // Must be called before creating the main frame in order to
351  // display the real hotkeys in menus or tool tips
353 
354  try
355  {
356  // The global table is not related to a specific project. All projects
357  // will use the same global table. So the KIFACE::OnKifaceStart() contract
358  // of avoiding anything project specific is not violated here.
359 
360  if( !FP_LIB_TABLE::LoadGlobalTable( GFootprintTable ) )
361  {
362  DisplayInfoMessage( NULL, _(
363  "You have run Pcbnew for the first time using the "
364  "new footprint library table method for finding footprints.\n"
365  "Pcbnew has either copied the default "
366  "table or created an empty table in the kicad configuration folder.\n"
367  "You must first configure the library "
368  "table to include all footprint libraries you want to use.\n"
369  "See the \"Footprint Library Table\" section of "
370  "the CvPcb or Pcbnew documentation for more information." ) );
371  }
372  }
373  catch( const IO_ERROR& ioe )
374  {
375  // if we are here, a incorrect global footprint library table was found.
376  // Incorrect global footprint library table is not a fatal error:
377  // the user just has to edit the (partially) loaded table.
378 
379  wxString msg = _(
380  "An error occurred attempting to load the global footprint library table:\n"
381  "Please edit this global footprint library table in Preferences menu"
382  );
383 
384  DisplayErrorMessage( NULL, msg, ioe.What() );
385  }
386 
387 #if defined(KICAD_SCRIPTING)
388  scriptingSetup();
389 #endif
390 
391  return true;
392 }
393 
394 
396 {
397 #if defined( KICAD_SCRIPTING_WXPYTHON )
398  // Restore the thread state and tell Python to cleanup after itself.
399  // wxPython will do its own cleanup as part of that process.
400  // This should only be called if python was setup correctly.
401 
402  if( IsWxPythonLoaded() )
404 #endif
405 
407 }
Class FOOTPRINT_WIZARD_FRAME.
Class KIFACE_I is a KIFACE (I)mplementation, with some features useful for DSOs which implement a KIF...
Definition: kiface_i.h:37
wxPoint g_Offset_Module
Definition: pcbnew.cpp:75
PCB_LAYER_ID g_Route_Layer_TOP
Definition: pcbnew.cpp:70
PCB_LAYER_ID g_Route_Layer_BOTTOM
Definition: pcbnew.cpp:71
#define KICAD_PCB_PORT_SERVICE_NUMBER
< Pcbnew listens on this port for commands from Eeschema
Definition: eda_dde.h:39
DDE server & client.
bool start_common(int aCtlBits)
Common things to do for a top program module, during OnKifaceStart().
Definition: kiface_i.cpp:90
bool g_Alternate_Track_Posture
Definition: pcbnew.cpp:72
bool pcbnewInitPythonScripting(const char *aUserScriptingPath)
Function pcbnewInitPythonScripting Initializes the Python engine inside pcbnew.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:259
This file is part of the common library TODO brief description.
GAL_TYPE GetBackend() const
Function GetBackend Returns the type of backend currently used by GAL canvas.
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Function OnKifaceStart is called just once shortly after the DSO is loaded.
Definition: pcbnew.cpp:342
This file is part of the common library.
DLIST< TRACK > g_CurrentTrackList
Definition: pcbnew.cpp:90
VTBL_ENTRY const wxString & GetExecutablePath() const
Definition: pgm_base.h:215
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:149
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
Class BOARD to handle a board.
static PGM_BASE * process
Definition: pcbnew.cpp:208
Component library viewer main window.
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
static EDA_DRAW_PANEL_GAL::GAL_TYPE LoadCanvasTypeSetting()
Returns the canvas type stored in the application settings.
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:66
Return the global FP_LIB_TABLE.
Definition: kiface_ids.h:53
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPcbEditFrame)
void pcbnewFinishPythonScripting()
Definition of class FOOTPRINT_EDIT_FRAME.
bool g_Raccord_45_Auto
Definition: pcbnew.cpp:73
Pcbnew hotkeys.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:333
This file contains miscellaneous commonly used macros and functions.
#define PCB_EDIT_FRAME_NAME
Definition: draw_frame.h:56
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: pcbnew.cpp:100
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.
Definition: pcbnew.cpp:395
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
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.
Definition: pcbnew.cpp:108
bool IsWxPythonLoaded()
PCB_LAYER_ID
A quick note on layer IDs:
int aKiwayVersion
Definition: pcbnew.cpp:216
KIFACE * KIFACE_GETTER(int *aKIFACEversion, int aKIWAYversion, PGM_BASE *aProgram)
No name mangling. Each KIFACE (DSO/DLL) will implement this once.
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:100
The common library.
static FOOTPRINT_PREVIEW_PANEL * New(KIWAY *aKiway, wxWindow *aParent)
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:258
struct EDA_HOTKEY_CONFIG g_Board_Editor_Hotkeys_Descr[]
pcbnew DSO
Definition: kiway.h:267
Return a new FP_LIB_TABLE with the global table installed as a fallback.
Definition: kiface_ids.h:46
wxString g_DocModulesFileName
Definition: pcbnew.cpp:82
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:146
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
Definition: pcbnew.cpp:180
PCB::IFACE KIFACE_I kiface("pcbnew", KIWAY::FACE_PCB)
FACE_T
Known KIFACE implementations.
Definition: kiway.h:264
int ReadHotkeyConfig(const wxString &aAppname, struct EDA_HOTKEY_CONFIG *aDescList)
Function ReadHotkeyConfig Read configuration data and fill the current hotkey list with hotkeys...
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:115
void PythonPluginsReloadBase()
Helper function PythonPluginsReloadBase Reload Python plugins if they are newer than the already load...
Definition: pcbnew.cpp:311
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
see class PGM_BASE
Implementation of std::make_unique for pre C++14 compilation environments.
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:151
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:921
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: pcbnew.cpp:211
FOOTPRINT_LIST_IMPL GFootprintList
The global footprint info table.
Definition: pcbnew.cpp:338
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:277
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
int PGM_BASE * aProgram
Definition: pcbnew.cpp:217
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39