KiCad PCB EDA Suite
pcbnew_scripting_helpers.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) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
5  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <Python.h>
31 #undef HAVE_CLOCK_GETTIME // macro is defined in Python.h and causes redefine warning
32 
33 #include <action_plugin.h>
34 #include <build_version.h>
35 #include <class_board.h>
36 #include <cstdlib>
37 #include <fp_lib_table.h>
38 #include <io_mgr.h>
39 #include <kicad_string.h>
40 #include <macros.h>
41 #include <pcb_draw_panel_gal.h>
42 #include <pcbnew.h>
44 #include <project.h>
47 
49 
51 
53 {
54  if( s_PcbEditFrame )
55  return s_PcbEditFrame->GetBoard();
56  else
57  return NULL;
58 }
59 
60 
62 {
63  s_PcbEditFrame = aPcbEditFrame;
64 }
65 
66 
67 BOARD* LoadBoard( wxString& aFileName )
68 {
69  if( aFileName.EndsWith( wxT( ".kicad_pcb" ) ) )
70  return LoadBoard( aFileName, IO_MGR::KICAD_SEXP );
71 
72  else if( aFileName.EndsWith( wxT( ".brd" ) ) )
73  return LoadBoard( aFileName, IO_MGR::LEGACY );
74 
75  // as fall back for any other kind use the legacy format
76  return LoadBoard( aFileName, IO_MGR::LEGACY );
77 }
78 
79 
81 {
82  if( !s_SettingsManager )
83  s_SettingsManager = new SETTINGS_MANAGER( true );
84 
85  return s_SettingsManager;
86 }
87 
88 
90 {
91  PROJECT* project = GetSettingsManager()->GetProject( "" );
92 
93  if( !project )
94  {
96  project = GetSettingsManager()->GetProject( "" );
97  }
98 
99  return project;
100 }
101 
102 
103 BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat )
104 {
105  wxFileName pro = aFileName;
106  pro.SetExt( ProjectFileExtension );
107  pro.MakeAbsolute();
108  wxString projectPath = pro.GetFullPath();
109 
110  PROJECT* project = GetSettingsManager()->GetProject( projectPath );
111 
112  if( !project )
113  {
114  GetSettingsManager()->LoadProject( projectPath );
115  GetSettingsManager()->GetProject( projectPath );
116  }
117 
118  // Board cannot be loaded without a project, so create the default project
119  if( !project )
120  project = GetDefaultProject();
121 
122  BOARD* brd = IO_MGR::Load( aFormat, aFileName );
123 
124  if( brd )
125  {
126  brd->SetProject( project );
127  brd->BuildConnectivity();
128  brd->BuildListOfNets();
130  }
131 
132  return brd;
133 }
134 
135 
137 {
138  BOARD* brd = new BOARD();
139 
140  brd->SetProject( GetDefaultProject() );
141 
142  return brd;
143 }
144 
145 
146 bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat )
147 {
148  aBoard->BuildConnectivity();
151 
152  IO_MGR::Save( aFormat, aFileName, aBoard, NULL );
153 
154  wxFileName pro = aFileName;
155  pro.SetExt( ProjectFileExtension );
156  pro.MakeAbsolute();
157  wxString projectPath = pro.GetFullPath();
158 
159  GetSettingsManager()->SaveProject( pro.GetFullPath() );
160 
161  return true;
162 }
163 
164 
165 bool SaveBoard( wxString& aFileName, BOARD* aBoard )
166 {
167  return SaveBoard( aFileName, aBoard, IO_MGR::KICAD_SEXP );
168 }
169 
170 
172 {
173  BOARD* board = GetBoard();
174 
175  if( !board )
176  return nullptr;
177 
178  PROJECT* project = board->GetProject();
179 
180  if( !project )
181  return nullptr;
182 
183  return project->PcbFootprintLibs();
184 }
185 
186 
187 wxArrayString GetFootprintLibraries()
188 {
189  wxArrayString footprintLibraryNames;
190 
192 
193  if( !tbl )
194  return footprintLibraryNames;
195 
196  for( const wxString& name : tbl->GetLogicalLibs() )
197  footprintLibraryNames.Add( name );
198 
199  return footprintLibraryNames;
200 }
201 
202 
203 wxArrayString GetFootprints( const wxString& aNickName )
204 {
205  wxArrayString footprintNames;
206 
208 
209  if( !tbl )
210  return footprintNames;
211 
212  tbl->FootprintEnumerate( footprintNames, aNickName, true );
213 
214  return footprintNames;
215 }
216 
217 
218 bool ExportSpecctraDSN( wxString& aFullFilename )
219 {
220  if( s_PcbEditFrame )
221  {
222  bool ok = s_PcbEditFrame->ExportSpecctraFile( aFullFilename );
223  return ok;
224  }
225  else
226  {
227  return false;
228  }
229 }
230 
231 
232 bool ImportSpecctraSES( wxString& aFullFilename )
233 {
234  if( s_PcbEditFrame )
235  {
236  bool ok = s_PcbEditFrame->ImportSpecctraSession( aFullFilename );
237  return ok;
238  }
239  else
240  {
241  return false;
242  }
243 }
244 
245 
246 bool ArchiveModulesOnBoard( bool aStoreInNewLib, const wxString& aLibName, wxString* aLibPath )
247 {
248  if( s_PcbEditFrame )
249  {
250  s_PcbEditFrame->ArchiveModulesOnBoard( aStoreInNewLib, aLibName, aLibPath );
251  return true;
252  }
253  else
254  {
255  return false;
256  }
257 }
258 
259 void Refresh()
260 {
261  if( s_PcbEditFrame )
262  {
263  auto board = s_PcbEditFrame->GetBoard();
264  board->BuildConnectivity();
265 
266  // Re-init everything: this is the easy way to do that
269  }
270 }
271 
272 
274 {
275  if( s_PcbEditFrame )
277 }
278 
279 
281 {
282  if( s_PcbEditFrame )
283  return static_cast<int>( s_PcbEditFrame->GetUserUnits() );
284 
285  return -1;
286 }
287 
288 
290 {
292 }
void UpdateUserInterface()
Update the layer manager and other widgets from the board setup (layer and items visibility,...
bool ArchiveModulesOnBoard(bool aStoreInNewLib, const wxString &aLibName, wxString *aLibPath)
Function ArchiveModulesOnBoard Save modules in a library:
BOARD * LoadBoard(wxString &aFileName)
void BuildListOfNets()
Definition: class_board.h:718
static bool IsActionRunning()
Function IsActionRunning.
PROJECT holds project specific data.
Definition: project.h:61
int GetUserUnits()
Returns the currently selected user unit value for the interface.
const std::string ProjectFileExtension
void ActivateGalCanvas() override
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
wxArrayString GetFootprints(const wxString &aNickName)
will get the names of all of the footprints available in a footprint library
PROJECT * GetProject() const
Definition: class_board.h:376
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:551
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPcbEditFrame)
This file contains miscellaneous commonly used macros and functions.
bool IsActionRunning()
Are we currently in an action plugin?
wxArrayString GetFootprintLibraries()
will get the nicknames of all of the footprint libraries configured in pcbnew in both the project and...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aNickname, bool aBestEfforts)
Return a list of footprint names contained within the library given by aNickname.
void SynchronizeNetsAndNetClasses()
Function SynchronizeNetsAndNetClasses copies NETCLASS info to each NET, based on NET membership in a ...
#define NULL
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
static const char Default[]
the name of the default NETCLASS
Definition: netclass.h:80
static SETTINGS_MANAGER * s_SettingsManager
static BOARD * Load(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aAppendToMe=NULL, const PROPERTIES *aProperties=NULL)
Function Load finds the requested PLUGIN and if found, calls the PLUGIN->Load(..) funtion on it using...
Definition: io_mgr.cpp:176
bool ImportSpecctraSES(wxString &aFullFilename)
will import a specctra *.ses file and use it to relocate MODULEs and to replace all vias and tracks i...
Definition of file extensions used in Kicad.
void BuildConnectivity()
Builds or rebuilds the board connectivity database for the board, especially the list of connected it...
void UpdateUserInterface()
Update the layer manager and other widgets from the board setup (layer and items visibility,...
FP_LIB_TABLE * GetFootprintLibraryTable()
bool ExportSpecctraDSN(wxString &aFullFilename)
will export the current BOARD to a specctra dsn file.
SETTINGS_MANAGER * GetSettingsManager()
PROJECT * GetDefaultProject()
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
BOARD * GetBoard()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:284
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
void SetProject(PROJECT *aProject)
Links a board to a given project.
static void Save(PCB_FILE_T aFileType, const wxString &aFileName, BOARD *aBoard, const PROPERTIES *aProperties=NULL)
Function Save will write either a full aBoard to a storage file in a format that this implementation ...
Definition: io_mgr.cpp:191
bool ImportSpecctraSession(const wxString &aFullFilename)
Function ImportSpecctraSession will import a specctra *.ses file and use it to relocate MODULEs and t...
bool SaveBoard(wxString &aFileName, BOARD *aBoard, IO_MGR::PCB_FILE_T aFormat)
Class PCBNEW_ACTION_PLUGINS.
bool SetCurrentNetClass(const wxString &aNetClassName)
Function SetCurrentNetClass Must be called after a netclass selection (or after a netclass parameter ...
Legacy Pcbnew file formats prior to s-expression.
Definition: io_mgr.h:56
const char * name
Definition: DXF_plotter.cpp:60
void ArchiveModulesOnBoard(bool aStoreInNewLib, const wxString &aLibName=wxEmptyString, wxString *aLibPath=NULL)
Function ArchiveModulesOnBoard Save modules in a library:
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
bool SaveProject(const wxString &aFullPath=wxEmptyString)
Saves a loaded project.
PCB_EDIT_FRAME is the main frame for Pcbnew.
BOARD * CreateEmptyBoard()
Constructs a default BOARD with a tempoary (no filename) project.
BOARD * GetBoard() const
PCB_FILE_T
Enum PCB_FILE_T is a set of file types that the IO_MGR knows about, and for which there has been a pl...
Definition: io_mgr.h:54
PROJECT * GetProject(const wxString &aFullPath) const
Retrieves a loaded project by name.
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
static PCB_EDIT_FRAME * s_PcbEditFrame
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
bool ExportSpecctraFile(const wxString &aFullFilename)
Function ExportSpecctraFile will export the current BOARD to a specctra dsn file.
S-expression Pcbnew file format.
Definition: io_mgr.h:57