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 
65 extern bool IsWxPythonLoaded();
66 
67 // Colors for layers and items
68 
72 bool g_Raccord_45_Auto = true;
73 
74 wxPoint g_Offset_Module; // module offset used when moving a footprint
75 
76 /* Name of the document footprint list
77  * usually located in share/modules/footprints_doc
78  * this is of the responsibility to users to create this file
79  * if they want to have a list of footprints
80  */
81 wxString g_DocModulesFileName = wxT( "footprints_doc/footprints.pdf" );
82 
83 /*
84  * Used in track creation, a list of track segments currently being created,
85  * with the newest track at the end of the list, sorted by new-ness. e.g. use
86  * TRACK->Back() to get the next older track, TRACK->Next() to get the next
87  * newer track.
88  */
90 
91 
92 
93 namespace PCB {
94 
95 static struct IFACE : public KIFACE_I
96 {
97  // Of course all are virtual overloads, implementations of the KIFACE.
98 
99  IFACE( const char* aName, KIWAY::FACE_T aType ) :
100  KIFACE_I( aName, aType )
101  {}
102 
103  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
104 
105  void OnKifaceEnd() override;
106 
107  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
108  {
109  switch( aClassId )
110  {
111  case FRAME_PCB:
112  {
113  auto frame = new PCB_EDIT_FRAME( aKiway, aParent );
114 
115 #if defined( KICAD_SCRIPTING )
116  // give the scripting helpers access to our frame
117  ScriptingSetPcbEditFrame( frame );
118 #endif
119 
120  if( Kiface().IsSingle() )
121  {
122  // only run this under single_top, not under a project manager.
123  frame->CreateServer( KICAD_PCB_PORT_SERVICE_NUMBER );
124  }
125 
126  return frame;
127  }
128 
130  return new FOOTPRINT_EDIT_FRAME( aKiway, aParent );
131 
134  return new FOOTPRINT_VIEWER_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
135 
137  return new FOOTPRINT_WIZARD_FRAME( aKiway, aParent, FRAME_T( aClassId ) );
138 
140  return dynamic_cast< wxWindow* >( FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent ) );
141 
142  default:
143  return nullptr;
144  }
145  }
146 
158  void* IfaceOrAddress( int aDataId ) override
159  {
160  switch( aDataId )
161  {
162  // Return a pointer to the global instance of the footprint list.
164  return (void*) &GFootprintList;
165 
166  // Return a new FP_LIB_TABLE with the global table installed as a fallback.
168  return (void*) new FP_LIB_TABLE( &GFootprintTable );
169 
170  // Return a pointer to the global instance of the global footprint table.
172  return (void*) &GFootprintTable;
173 
174  default:
175  return nullptr;
176  }
177  }
178 
179 } kiface( "pcbnew", KIWAY::FACE_PCB );
180 
181 } // namespace
182 
183 using namespace PCB;
184 
185 
187 
188 
189 KIFACE_I& Kiface() { return kiface; }
190 
191 
192 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
193 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
194 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
195 {
196  process = aProgram;
197  return &kiface;
198 }
199 
200 #if defined( BUILD_KIWAY_DLL )
201 PGM_BASE& Pgm()
202 {
203  wxASSERT( process ); // KIFACE_GETTER has already been called.
204  return *process;
205 }
206 #endif
207 
208 
209 #if defined( KICAD_SCRIPTING )
210 static bool scriptingSetup()
211 {
212 
213 #if defined( __WINDOWS__ )
214  // If our python.exe (in kicad/bin) exists, force our kicad python environment
215  wxString kipython = FindKicadFile( "python.exe" );
216 
217  // we need only the path:
218  wxFileName fn( kipython );
219  kipython = fn.GetPath();
220 
221  // If our python install is existing inside kicad, use it
222  // Note: this is usefull only when another python version is installed
223  if( wxDirExists( kipython ) )
224  {
225  // clear any PYTHONPATH and PYTHONHOME env var definition: the default
226  // values work fine inside Kicad:
227  wxSetEnv( wxT( "PYTHONPATH" ), wxEmptyString );
228  wxSetEnv( wxT( "PYTHONHOME" ), wxEmptyString );
229 
230  // Add our python executable path in first position:
231  wxString ppath;
232  wxGetEnv( wxT( "PATH" ), &ppath );
233 
234  kipython << wxT( ";" ) << ppath;
235  wxSetEnv( wxT( "PATH" ), kipython );
236  }
237 
238 #elif defined( __WXMAC__ )
239 
240  // Add default paths to PYTHONPATH
241  wxString pypath;
242 
243  // Bundle scripting folder (<kicad.app>/Contents/SharedSupport/scripting)
244  pypath += GetOSXKicadDataDir() + wxT( "/scripting" );
245 
246  // $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i
247  if( wxGetenv("KICAD_PATH") != NULL )
248  {
249  pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") );
250  }
251 
252  // Bundle wxPython folder (<kicad.app>/Contents/Frameworks/python/site-packages)
253  pypath += wxT( ":" ) + Pgm().GetExecutablePath() +
254  wxT( "Contents/Frameworks/python/site-packages" );
255 
256  // Original content of $PYTHONPATH
257  if( wxGetenv( wxT( "PYTHONPATH" ) ) != NULL )
258  {
259  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
260  }
261 
262  // set $PYTHONPATH
263  wxSetEnv( "PYTHONPATH", pypath );
264 
265 #else
266  // Linux-specific setup
267  wxString pypath;
268 
269  pypath = Pgm().GetExecutablePath() + wxT( "../lib/python2.7/dist-packages" );
270 
271  if( !wxIsEmpty( wxGetenv( wxT( "PYTHONPATH" ) ) ) )
272  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
273 
274  wxSetEnv( wxT( "PYTHONPATH" ), pypath );
275 
276 #endif
277 
279  {
280  wxLogError( "pcbnewInitPythonScripting() failed." );
281  return false;
282  }
283 
284  return true;
285 }
286 #endif // KICAD_SCRIPTING
287 
288 
290 {
291 #if defined(KICAD_SCRIPTING)
292  //Reload plugin list: reload Python plugins if they are newer than
293  // the already loaded, and load new plugins
294  char cmd[1024];
295 
296  snprintf( cmd, sizeof(cmd),
297  "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) );
298 
299  PyLOCK lock;
300 
301  // ReRun the Python method pcbnew.LoadPlugins
302  // (already called when starting Pcbnew)
303  PyRun_SimpleString( cmd );
304 #endif
305 }
306 
307 
312 
317 
318 
319 
320 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
321 {
322  // This is process level, not project level, initialization of the DSO.
323 
324  // Do nothing in here pertinent to a project!
325 
326  start_common( aCtlBits );
327 
328  // Must be called before creating the main frame in order to
329  // display the real hotkeys in menus or tool tips
331 
332  try
333  {
334  // The global table is not related to a specific project. All projects
335  // will use the same global table. So the KIFACE::OnKifaceStart() contract
336  // of avoiding anything project specific is not violated here.
337 
338  if( !FP_LIB_TABLE::LoadGlobalTable( GFootprintTable ) )
339  {
340  DisplayInfoMessage( NULL, _(
341  "You have run Pcbnew for the first time using the "
342  "new footprint library table method for finding footprints.\n"
343  "Pcbnew has either copied the default "
344  "table or created an empty table in the kicad configuration folder.\n"
345  "You must first configure the library "
346  "table to include all footprint libraries you want to use.\n"
347  "See the \"Footprint Library Table\" section of "
348  "the CvPcb or Pcbnew documentation for more information." ) );
349  }
350  }
351  catch( const IO_ERROR& ioe )
352  {
353  // if we are here, a incorrect global footprint library table was found.
354  // Incorrect global footprint library table is not a fatal error:
355  // the user just has to edit the (partially) loaded table.
356 
357  wxString msg = _(
358  "An error occurred attempting to load the global footprint library table:\n"
359  "Please edit this global footprint library table in Preferences menu"
360  );
361 
362  DisplayErrorMessage( NULL, msg, ioe.What() );
363  }
364 
365 #if defined(KICAD_SCRIPTING)
366  scriptingSetup();
367 #endif
368 
369  return true;
370 }
371 
372 
374 {
375  // This function deletes OpenGL contexts used (if any) by wxGLCanvas objects.
376  // It can be called only when closing the application, because it deletes an OpenGL context
377  // which can still be in usage. Destroying OpenGL contexts earlier may crash the application.
379 
380  end_common();
381 #if defined( KICAD_SCRIPTING_WXPYTHON )
382  // Restore the thread state and tell Python to cleanup after itself.
383  // wxPython will do its own cleanup as part of that process.
384  // This should only be called if python was setup correctly.
385 
386  if( IsWxPythonLoaded() )
388 #endif
389 }
void DeleteAll()
Function DeleteAll destroys all managed OpenGL contexts.
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:74
PCB_LAYER_ID g_Route_Layer_TOP
Definition: pcbnew.cpp:69
PCB_LAYER_ID g_Route_Layer_BOTTOM
Definition: pcbnew.cpp:70
#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:71
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:199
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.
Definition: pcbnew.cpp:320
This file is part of the common library.
DLIST< TRACK > g_CurrentTrackList
Definition: pcbnew.cpp:89
VTBL_ENTRY const wxString & GetExecutablePath() const
Definition: pgm_base.h:173
Class PGM_BASE keeps program (whole process) data for KiCad programs.
Definition: pgm_base.h:107
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:186
Component library viewer main window.
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
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:72
Pcbnew hotkeys.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:311
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:99
void OnKifaceEnd() override
Function OnKifaceEnd is called just once just before the DSO is to be unloaded.
Definition: pcbnew.cpp:373
static GL_CONTEXT_MANAGER & Get()
Function Get returns the GL_CONTEXT_MANAGER instance (singleton).
#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:107
bool IsWxPythonLoaded()
PCB_LAYER_ID
A quick note on layer IDs:
int aKiwayVersion
Definition: pcbnew.cpp:194
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:81
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:158
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:289
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.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: pcbnew.cpp:189
FOOTPRINT_LIST_IMPL GFootprintList
The global footprint info table.
Definition: pcbnew.cpp:316
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:216
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
int PGM_BASE * aProgram
Definition: pcbnew.cpp:195
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39