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 <colors_selection.h>
46 #include <wx/stdpaths.h>
47 
48 #include <wx/file.h>
49 #include <wx/snglinst.h>
50 #include <wx/dir.h>
51 #include <gestfich.h>
52 
53 #include <pcbnew.h>
54 #include <hotkeys.h>
56 #include <class_board.h>
57 #include <class_draw_panel_gal.h>
58 #include <fp_lib_table.h>
59 #include <module_editor_frame.h>
60 #include <modview_frame.h>
61 #include <footprint_wizard_frame.h>
63 #include <footprint_info_impl.h>
64 #include <gl_context_mgr.h>
65 extern bool IsWxPythonLoaded();
66 
67 // Colors for layers and items
69 
70 bool g_Drc_On = true;
72 bool g_Raccord_45_Auto = true;
74 bool g_Track_45_Only_Allowed = true; // True to allow horiz, vert. and 45deg only tracks
75 bool g_Segments_45_Only; // True to allow horiz, vert. and 45deg only graphic segments
77 
82 
83 wxPoint g_Offset_Module; // module offset used when moving a footprint
84 
85 /* Name of the document footprint list
86  * usually located in share/modules/footprints_doc
87  * this is of the responsibility to users to create this file
88  * if they want to have a list of footprints
89  */
90 wxString g_DocModulesFileName = wxT( "footprints_doc/footprints.pdf" );
91 
92 /*
93  * Used in track creation, a list of track segments currently being created,
94  * with the newest track at the end of the list, sorted by new-ness. e.g. use
95  * TRACK->Back() to get the next older track, TRACK->Next() to get the next
96  * newer track.
97  */
99 
100 
101 namespace PCB {
102 
103 static struct IFACE : public KIFACE_I
104 {
105  // Of course all are virtual overloads, implementations of the KIFACE.
106 
107  IFACE( const char* aName, KIWAY::FACE_T aType ) :
108  KIFACE_I( aName, aType )
109  {}
110 
111  bool OnKifaceStart( PGM_BASE* aProgram, int aCtlBits ) override;
112 
113  void OnKifaceEnd() override;
114 
115  wxWindow* CreateWindow( wxWindow* aParent, int aClassId, KIWAY* aKiway, int aCtlBits = 0 ) override
116  {
117  wxWindow* frame = NULL;
118 
119  switch( aClassId )
120  {
121  case FRAME_PCB:
122  frame = dynamic_cast< wxWindow* >( new PCB_EDIT_FRAME( aKiway, aParent ) );
123 
124 #if defined( KICAD_SCRIPTING )
125  // give the scripting helpers access to our frame
127 #endif
128 
129  if( Kiface().IsSingle() )
130  {
131  // only run this under single_top, not under a project manager.
133  }
134 
135  break;
136 
138  frame = dynamic_cast< wxWindow* >( new FOOTPRINT_EDIT_FRAME( aKiway, aParent ) );
139  break;
140 
143  frame = dynamic_cast< wxWindow* >( new FOOTPRINT_VIEWER_FRAME( aKiway, aParent,
144  FRAME_T( aClassId ) ) );
145  break;
146 
148  frame = dynamic_cast< wxWindow* >( new FOOTPRINT_WIZARD_FRAME( aKiway, aParent,
149  FRAME_T( aClassId ) ) );
150  break;
151 
153  frame = dynamic_cast< wxWindow* >( FOOTPRINT_PREVIEW_PANEL::New( aKiway, aParent ) );
154  break;
155 
156  default:
157  break;
158  }
159 
160  return frame;
161  }
162 
174  void* IfaceOrAddress( int aDataId ) override
175  {
176  switch( aDataId )
177  {
179  return (void*) static_cast<FOOTPRINT_LIST*>( new FOOTPRINT_LIST_IMPL() );
180 
182  return (void*) new FP_LIB_TABLE( &GFootprintTable );
183 
184  default:
185  return nullptr;
186  }
187  }
188 
189 } kiface( "pcbnew", KIWAY::FACE_PCB );
190 
191 } // namespace
192 
193 using namespace PCB;
194 
195 
197 
198 
199 KIFACE_I& Kiface() { return kiface; }
200 
201 
202 // KIFACE_GETTER's actual spelling is a substitution macro found in kiway.h.
203 // KIFACE_GETTER will not have name mangling due to declaration in kiway.h.
204 MY_API( KIFACE* ) KIFACE_GETTER( int* aKIFACEversion, int aKiwayVersion, PGM_BASE* aProgram )
205 {
206  process = aProgram;
207  return &kiface;
208 }
209 
210 #if defined( BUILD_KIWAY_DLL )
211 PGM_BASE& Pgm()
212 {
213  wxASSERT( process ); // KIFACE_GETTER has already been called.
214  return *process;
215 }
216 #endif
217 
218 
219 #if defined( KICAD_SCRIPTING )
220 static bool scriptingSetup()
221 {
222 
223 #if defined( __WINDOWS__ )
224  // If our python.exe (in kicad/bin) exists, force our kicad python environment
225  wxString kipython = FindKicadFile( "python.exe" );
226 
227  // we need only the path:
228  wxFileName fn( kipython );
229  kipython = fn.GetPath();
230 
231  // If our python install is existing inside kicad, use it
232  // Note: this is usefull only when an other python version is installed
233  if( wxDirExists( kipython ) )
234  {
235  // clear any PYTHONPATH and PYTHONHOME env var definition: the default
236  // values work fine inside Kicad:
237  wxSetEnv( wxT( "PYTHONPATH" ), wxEmptyString );
238  wxSetEnv( wxT( "PYTHONHOME" ), wxEmptyString );
239 
240  // Add our python executable path in first position:
241  wxString ppath;
242  wxGetEnv( wxT( "PATH" ), &ppath );
243 
244  kipython << wxT( ";" ) << ppath;
245  wxSetEnv( wxT( "PATH" ), kipython );
246  }
247 
248 #elif defined( __WXMAC__ )
249 
250  // Add default paths to PYTHONPATH
251  wxString pypath;
252 
253  // Bundle scripting folder (<kicad.app>/Contents/SharedSupport/scripting)
254  pypath += GetOSXKicadDataDir() + wxT( "/scripting" );
255 
256  // $(KICAD_PATH)/scripting/plugins is always added in kicadplugins.i
257  if( wxGetenv("KICAD_PATH") != NULL )
258  {
259  pypath += wxT( ":" ) + wxString( wxGetenv("KICAD_PATH") );
260  }
261 
262  // Bundle wxPython folder (<kicad.app>/Contents/Frameworks/python/site-packages)
263  pypath += wxT( ":" ) + Pgm().GetExecutablePath() +
264  wxT( "Contents/Frameworks/python/site-packages" );
265 
266  // Original content of $PYTHONPATH
267  if( wxGetenv( wxT( "PYTHONPATH" ) ) != NULL )
268  {
269  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
270  }
271 
272  // set $PYTHONPATH
273  wxSetEnv( "PYTHONPATH", pypath );
274 
275 #else
276  // Linux-specific setup
277  wxString pypath;
278 
279  pypath = Pgm().GetExecutablePath() + wxT( "../lib/python2.7/dist-packages" );
280 
281  if( !wxIsEmpty( wxGetenv( wxT( "PYTHONPATH" ) ) ) )
282  pypath = wxString( wxGetenv( wxT( "PYTHONPATH" ) ) ) + wxT( ":" ) + pypath;
283 
284  wxSetEnv( wxT( "PYTHONPATH" ), pypath );
285 
286 #endif
287 
289  {
290  wxLogError( "pcbnewInitPythonScripting() failed." );
291  return false;
292  }
293 
294  return true;
295 }
296 #endif // KICAD_SCRIPTING
297 
298 
300 {
301 #if defined(KICAD_SCRIPTING)
302  //Reload plugin list: reload Python plugins if they are newer than
303  // the already loaded, and load new plugins
304  char cmd[1024];
305 
306  snprintf( cmd, sizeof(cmd),
307  "pcbnew.LoadPlugins(\"%s\")", TO_UTF8( PyScriptingPath() ) );
308 
309  PyLOCK lock;
310 
311  // ReRun the Python method pcbnew.LoadPlugins
312  // (already called when starting Pcbnew)
313  PyRun_SimpleString( cmd );
314 #endif
315 }
316 
317 
322 
323 
324 bool IFACE::OnKifaceStart( PGM_BASE* aProgram, int aCtlBits )
325 {
326  // This is process level, not project level, initialization of the DSO.
327 
328  // Do nothing in here pertinent to a project!
329 
330  start_common( aCtlBits );
331 
332  // Must be called before creating the main frame in order to
333  // display the real hotkeys in menus or tool tips
335 
336  try
337  {
338  // The global table is not related to a specific project. All projects
339  // will use the same global table. So the KIFACE::OnKifaceStart() contract
340  // of avoiding anything project specific is not violated here.
341 
342  if( !FP_LIB_TABLE::LoadGlobalTable( GFootprintTable ) )
343  {
344  DisplayInfoMessage( NULL, _(
345  "You have run Pcbnew for the first time using the "
346  "new footprint library table method for finding footprints.\n"
347  "Pcbnew has either copied the default "
348  "table or created an empty table in the kicad configuration folder.\n"
349  "You must first configure the library "
350  "table to include all footprint libraries you want to use.\n"
351  "See the \"Footprint Library Table\" section of "
352  "the CvPcb or Pcbnew documentation for more information." ) );
353  }
354  }
355  catch( const IO_ERROR& ioe )
356  {
357  wxString msg = wxString::Format( _(
358  "An error occurred attempting to load the global footprint library "
359  "table:\n\n%s" ),
360  GetChars( ioe.What() )
361  );
362  DisplayError( NULL, msg );
363  return false;
364  }
365 
366 #if defined(KICAD_SCRIPTING)
367  scriptingSetup();
368 #endif
369 
370  return true;
371 }
372 
373 
375 {
376  // This function deletes OpenGL contexts used (if any) by wxGLCanvas objects.
377  // It can be called only when closing the application, because it deletes an OpenGL context
378  // which can still be in usage. Destroying OpenGL contexts earlier may crash the application.
380 
381  end_common();
382 #if defined( KICAD_SCRIPTING_WXPYTHON )
383  // Restore the thread state and tell Python to cleanup after itself.
384  // wxPython will do its own cleanup as part of that process.
385  // This should only be called if python was setup correctly.
386 
387  if( IsWxPythonLoaded() )
389 #endif
390 }
bool g_Segments_45_Only
Definition: pcbnew.cpp:75
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
bool g_Drc_On
Definition: pcbnew.cpp:70
Definition of class FOOTPRINT_EDIT_FRAME.
wxPoint g_Offset_Module
Definition: pcbnew.cpp:83
PCB_LAYER_ID g_Route_Layer_TOP
Definition: pcbnew.cpp:78
PCB_LAYER_ID g_Route_Layer_BOTTOM
Definition: pcbnew.cpp:79
#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:73
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:324
bool g_AutoDeleteOldTrack
Definition: pcbnew.cpp:71
This file is part of the common library.
DLIST< TRACK > g_CurrentTrackList
Definition: pcbnew.cpp:98
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:196
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:72
Pcbnew hotkeys.
FP_LIB_TABLE GFootprintTable
The global footprint library table.
Definition: pcbnew.cpp:321
This file contains miscellaneous commonly used macros and functions.
IFACE(const char *aName, KIWAY::FACE_T aType)
Definition: pcbnew.cpp:107
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:374
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:115
bool IsWxPythonLoaded()
PCB_LAYER_ID
A quick note on layer IDs:
int aKiwayVersion
Definition: pcbnew.cpp:204
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:90
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:146
COLORS_DESIGN_SETTINGS g_ColorsSettings
Definition: pcbnew.cpp:68
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:174
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:299
void DisplayInfoMessage(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:89
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
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.
int g_MagneticTrackOption
Definition: pcbnew.cpp:81
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
bool g_Track_45_Only_Allowed
Definition: pcbnew.cpp:74
Class KIFACE is used by a participant in the KIWAY alchemy.
Definition: kiway.h:150
Class COLORS_DESIGN_SETTINGS is a list of color settings for designs in Eeschema, Pcbnew and GerbView...
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: pcbnew.cpp:199
int g_MagneticPadOption
Definition: pcbnew.cpp:80
#define PCB_EDIT_FRAME_NAME
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
Definition: wxPcbStruct.h:78
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:69
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
bool g_TwoSegmentTrackBuild
Definition: pcbnew.cpp:76
int PGM_BASE * aProgram
Definition: pcbnew.cpp:205