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 
34 #include <pcbnew.h>
35 #include <pcbnew_id.h>
36 #include <build_version.h>
37 #include <class_board.h>
38 #include <class_drawpanel.h>
39 #include <kicad_string.h>
40 #include <io_mgr.h>
41 #include <macros.h>
42 #include <stdlib.h>
43 #include <pcb_draw_panel_gal.h>
44 #include <action_plugin.h>
45 
47 
49 {
50  if( s_PcbEditFrame )
51  return s_PcbEditFrame->GetBoard();
52  else
53  return NULL;
54 }
55 
56 
58 {
59  s_PcbEditFrame = aPcbEditFrame;
60 }
61 
62 
63 BOARD* LoadBoard( wxString& aFileName )
64 {
65  if( aFileName.EndsWith( wxT( ".kicad_pcb" ) ) )
66  return LoadBoard( aFileName, IO_MGR::KICAD_SEXP );
67 
68  else if( aFileName.EndsWith( wxT( ".brd" ) ) )
69  return LoadBoard( aFileName, IO_MGR::LEGACY );
70 
71  // as fall back for any other kind use the legacy format
72  return LoadBoard( aFileName, IO_MGR::LEGACY );
73 }
74 
75 
76 BOARD* LoadBoard( wxString& aFileName, IO_MGR::PCB_FILE_T aFormat )
77 {
78  BOARD* brd = IO_MGR::Load( aFormat, aFileName );
79 
80  if( brd )
81  {
82  brd->BuildConnectivity();
83  brd->BuildListOfNets();
85  }
86 
87 
88  return brd;
89 }
90 
91 
92 bool SaveBoard( wxString& aFileName, BOARD* aBoard, IO_MGR::PCB_FILE_T aFormat )
93 {
94  aBoard->BuildConnectivity();
97 
98  IO_MGR::Save( aFormat, aFileName, aBoard, NULL );
99 
100  return true;
101 }
102 
103 
104 bool SaveBoard( wxString& aFileName, BOARD* aBoard )
105 {
106  return SaveBoard( aFileName, aBoard, IO_MGR::KICAD_SEXP );
107 }
108 
109 
110 bool ExportSpecctraDSN( wxString& aFullFilename )
111 {
112  if( s_PcbEditFrame )
113  {
114  bool ok = s_PcbEditFrame->ExportSpecctraFile( aFullFilename );
115  return ok;
116  }
117  else
118  {
119  return false;
120  }
121 }
122 
123 
124 bool ImportSpecctraSES( wxString& aFullFilename )
125 {
126  if( s_PcbEditFrame )
127  {
128  bool ok = s_PcbEditFrame->ImportSpecctraSession( aFullFilename );
129  return ok;
130  }
131  else
132  {
133  return false;
134  }
135 }
136 
137 
138 bool ArchiveModulesOnBoard( bool aStoreInNewLib, const wxString& aLibName, wxString* aLibPath )
139 {
140  if( s_PcbEditFrame )
141  {
142  s_PcbEditFrame->ArchiveModulesOnBoard( aStoreInNewLib, aLibName, aLibPath );
143  return true;
144  }
145  else
146  {
147  return false;
148  }
149 }
150 
151 void Refresh()
152 {
153  if( s_PcbEditFrame )
154  {
155  auto board = s_PcbEditFrame->GetBoard();
156  board->BuildConnectivity();
157 
159  {
160  auto gal_canvas = static_cast<PCB_DRAW_PANEL_GAL*>( s_PcbEditFrame->GetGalCanvas() );
161 
162  // Reinit everything: this is the easy way to do that
163  s_PcbEditFrame->UseGalCanvas( true );
164  gal_canvas->Refresh();
165  }
166  else
167  // first argument is erase background, second is a wxRect that
168  // defines a reftresh area (all canvas if null)
169  s_PcbEditFrame->GetCanvas()->Refresh( true, NULL );
170  }
171 }
172 
173 
174 void WindowZoom( int xl, int yl, int width, int height )
175 {
176  EDA_RECT Rect( wxPoint( xl, yl ), wxSize( width, height ) );
177 
178  if( s_PcbEditFrame )
179  s_PcbEditFrame->Window_Zoom( Rect );
180 }
181 
182 
184 {
185  if( s_PcbEditFrame )
187 }
188 
189 
191 {
192  if( s_PcbEditFrame )
193  return s_PcbEditFrame->GetUserUnits();
194 
195  return -1;
196 }
197 
198 
200 {
202 }
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:727
static bool IsActionRunning()
Function IsActionRunning.
void UseGalCanvas(bool aEnable) override
int GetUserUnits()
Returns the currently selected user unit value for the interface.
Class BOARD to handle a board.
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:931
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Function GetDesignSettings.
Definition: class_board.h:534
void ScriptingSetPcbEditFrame(PCB_EDIT_FRAME *aPcbEditFrame)
This file contains miscellaneous commonly used macros and functions.
bool IsActionRunning()
Are we currently in an action plugin?
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use.
Definition: draw_frame.h:924
void SynchronizeNetsAndNetClasses()
Function SynchronizeNetsAndNetClasses copies NETCLASS info to each NET, based on NET membership in a ...
Definition: netclass.cpp:155
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
EDA_UNITS_T GetUserUnits() const override
Return the user units currently in use.
Definition: draw_frame.h:289
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:173
bool ImportSpecctraSES(wxString &aFullFilename)
will import a specctra *.ses file and use it to relocate MODULEs and to replace all vias and tracks i...
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,...
virtual EDA_DRAW_PANEL * GetCanvas() const
Definition: draw_frame.h:399
bool ExportSpecctraDSN(wxString &aFullFilename)
will export the current BOARD to a specctra dsn file.
BOARD * GetBoard()
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:188
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
void ImportSpecctraSession(wxCommandEvent &event)
Function ImportSpecctraSession will import a specctra *.ses file and use it to relocate MODULEs and t...
void ArchiveModulesOnBoard(bool aStoreInNewLib, const wxString &aLibName=wxEmptyString, wxString *aLibPath=NULL)
Function ArchiveModulesOnBoard Save modules in a library:
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
virtual void Window_Zoom(EDA_RECT &Rect)
BOARD * GetBoard() const
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL)
void WindowZoom(int xl, int yl, int width, int height)
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
static PCB_EDIT_FRAME * s_PcbEditFrame
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