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 <wxPcbStruct.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 <module_editor_frame.h>
59 #include <modview_frame.h>
60 #include <footprint_wizard_frame.h>
62 #include <footprint_info_impl.h>
63 #include <gl_context_mgr.h>
64 extern bool IsWxPythonLoaded();
65 
66 // Colors for layers and items
67 
71 bool g_Raccord_45_Auto = true;
72 
73 wxPoint g_Offset_Module; // module offset used when moving a footprint
74 
75 /* Name of the document footprint list
76  * usually located in share/modules/footprints_doc
77  * this is of the responsibility to users to create this file
78  * if they want to have a list of footprints
79  */
80 wxString g_DocModulesFileName = wxT( "footprints_doc/footprints.pdf" );
81 
82 /*
83  * Used in track creation, a list of track segments currently being created,
84  * with the newest track at the end of the list, sorted by new-ness. e.g. use
85  * TRACK->Back() to get the next older track, TRACK->Next() to get the next
86  * newer track.
87  */
89 
90 
91 namespace PCB {
92 
93 static struct IFACE : public KIFACE_I
94 {
95  // Of course all are virtual overloads, implementations of the KIFACE.
96 
97  IFACE( const char* aName, KIWAY::FACE_T aType ) :
98  KIFACE_I( aName, aType )
99  {}
100 
101  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
102 
103  void OnKifaceEnd() override;
104 
105  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
106  {
107  wxWindow* frame = NULL;
108 
109  switch( aClassId )
110  {
111  case FRAME_PCB:
112  frame = dynamic_cast< wxWindow* >( new PCB_EDIT_FRAME( aKiway, aParent ) );
113 
114 #if defined( KICAD_SCRIPTING )
115  // give the scripting helpers access to our frame
117 #endif
118 
119  if( Kiface().IsSingle() )
120  {
121  // only run this under single_top, not under a project manager.
123  }
124 
125  break;
126 
128  frame = dynamic_cast< wxWindow* >( new FOOTPRINT_EDIT_FRAME( aKiway, aParent ) );
129  break;
130 
133  frame = dynamic_cast< wxWindow* >( new FOOTPRINT_VIEWER_FRAME( aKiway, aParent,
134  FRAME_T( aClassId ) ) );
135  break;
136 
138  frame = dynamic_cast< wxWindow* >( new FOOTPRINT_WIZARD_FRAME( aKiway, aParent,
139  FRAME_T( aClassId ) ) );
140  break;
141 
143  frame = dynamic_cast< wxWindow* >( FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent ) );
144  break;
145 
146  default:
147  break;
148  }
149 
150  return frame;
151  }
152 
164  void* IfaceOrAddress( int aDataId ) override
165  {
166  switch( aDataId )
167  {
169  return (void*) static_cast<FOOTPRINT_LIST*>( new FOOTPRINT_LIST_IMPL() );
170 
172  return (void*) new FP_LIB_TABLE( &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 an other 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 
313 
314 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
315 {
316  // This is process level, not project level, initialization of the DSO.
317 
318  // Do nothing in here pertinent to a project!
319 
320  start_common( aCtlBits );
321 
322  // Must be called before creating the main frame in order to
323  // display the real hotkeys in menus or tool tips
325 
326  try
327  {
328  // The global table is not related to a specific project. All projects
329  // will use the same global table. So the KIFACE::OnKifaceStart() contract
330  // of avoiding anything project specific is not violated here.
331 
332  if( !FP_LIB_TABLE::LoadGlobalTable( GFootprintTable ) )
333  {
334  DisplayInfoMessage( NULL, _(
335  "You have run Pcbnew for the first time using the "
336  "new footprint library table method for finding footprints.\n"
337  "Pcbnew has either copied the default "
338  "table or created an empty table in the kicad configuration folder.\n"
339  "You must first configure the library "
340  "table to include all footprint libraries you want to use.\n"
341  "See the \"Footprint Library Table\" section of "
342  "the CvPcb or Pcbnew documentation for more information." ) );
343  }
344  }
345  catch( const IO_ERROR& ioe )
346  {
347  // if we are here, a incorrect global footprint library table was found.
348  // Incorrect global footprint library table is not a fatal error:
349  // the user just has to edit the (partially) loaded table.
350 
351  wxString msg = _(
352  "An error occurred attempting to load the global footprint library table:\n"
353  "Please edit this global footprint library table in Preferences menu"
354  );
355 
356  DisplayErrorMessage( NULL, msg, ioe.What() );
357  }
358 
359 #if defined(KICAD_SCRIPTING)
360  scriptingSetup();
361 #endif
362 
363  return true;
364 }
365 
366 
368 {
369  // This function deletes OpenGL contexts used (if any) by wxGLCanvas objects.
370  // It can be called only when closing the application, because it deletes an OpenGL context
371  // which can still be in usage. Destroying OpenGL contexts earlier may crash the application.
373 
374  end_common();
375 #if defined( KICAD_SCRIPTING_WXPYTHON )
376  // Restore the thread state and tell Python to cleanup after itself.
377  // wxPython will do its own cleanup as part of that process.
378  // This should only be called if python was setup correctly.
379 
380  if( IsWxPythonLoaded() )
382 #endif
383 }
void ReadHotkeyConfig(const wxString &Appname, struct EDA_HOTKEY_CONFIG *aDescList)
Function ReadHotkeyConfig Read hotkey configuration for a given app, possibly before the frame for th...
void DeleteAll()
Function DeleteAll destroys all managed OpenGL contexts.
Return a new instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39
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
Definition of class FOOTPRINT_EDIT_FRAME.
wxPoint g_Offset_Module
Definition: pcbnew.cpp:73
PCB_LAYER_ID g_Route_Layer_TOP
Definition: pcbnew.cpp:68
PCB_LAYER_ID g_Route_Layer_BOTTOM
Definition: pcbnew.cpp:69
#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:94
bool g_Alternate_Track_Posture
Definition: pcbnew.cpp:70
bool pcbnewInitPythonScripting(const char *aUserScriptingPath)
Function pcbnewInitPythonScripting Initializes the Python engine inside pcbnew.
This file is part of the common library TODO brief description.
wxSocketServer * CreateServer(wxWindow *window, int service, bool local)
Definition: eda_dde.cpp:49
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPCBEdaFrame)
bool OnKifaceStart(PGM_BASE *aProgram, int aCtlBits) override
Function OnKifaceStart is called just once shortly after the DSO is loaded.
Definition: pcbnew.cpp:314
This file is part of the common library.
DLIST< TRACK > g_CurrentTrackList
Definition: pcbnew.cpp:88
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.
Definition: modview_frame.h:44
MY_API(KIFACE *) KIFACE_GETTER(int *aKIFACEversion
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
void pcbnewFinishPythonScripting()
bool g_Raccord_45_Auto
Definition: pcbnew.cpp:71
Pcbnew hotkeys.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:311
This file contains miscellaneous commonly used macros and functions.
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: pcbnew.cpp:97
struct EDA_HOTKEY_CONFIG g_Board_Editor_Hokeys_Descr[]
void OnKifaceEnd() override
Function OnKifaceEnd is called just once just before the DSO is to be unloaded.
Definition: pcbnew.cpp:367
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:105
bool IsWxPythonLoaded()
PCB_LAYER_ID
A quick note on layer IDs:
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:85
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:104
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:257
pcbnew DSO
Definition: kiway.h:266
wxString g_DocModulesFileName
Definition: pcbnew.cpp:80
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:102
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:146
Return a new FP_LIB_TABLE copying the global table.
Definition: kiface_ids.h:46
void * IfaceOrAddress(int aDataId) override
Function IfaceOrAddress return a pointer to the requested object.
Definition: pcbnew.cpp:164
PCB::IFACE KIFACE_I kiface("pcbnew", KIWAY::FACE_PCB)
FACE_T
Known KIFACE implementations.
Definition: kiway.h:263
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
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
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:150
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: pcbnew.cpp:189
#define PCB_EDIT_FRAME_NAME
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
Definition: wxPcbStruct.h:78
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