KiCad PCB EDA Suite
pcbnew/netlist.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KiCad, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
8  * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
9  * Copyright (C) 2013-2016 Wayne Stambaugh <stambaughw@verizon.net>
10  * Copyright (C) 1992-2016 KiCad Developers, see change_log.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 
30 #include <functional>
31 using namespace std::placeholders;
32 
33 #include <fctsys.h>
34 #include <pgm_base.h>
35 #include <class_drawpanel.h>
36 #include <class_draw_panel_gal.h>
37 #include <confirm.h>
38 #include <dialog_helpers.h>
39 #include <wxPcbStruct.h>
40 #include <pcb_netlist.h>
41 #include <netlist_reader.h>
42 #include <reporter.h>
44 #include <lib_id.h>
45 #include <fp_lib_table.h>
46 
47 #include <class_board.h>
48 #include <class_module.h>
49 #include <ratsnest_data.h>
50 #include <pcbnew.h>
51 #include <io_mgr.h>
52 
53 #include <tool/tool_manager.h>
54 #include <tools/pcb_actions.h>
55 #include <view/view.h>
56 
57 
58 void PCB_EDIT_FRAME::ReadPcbNetlist( const wxString& aNetlistFileName,
59  const wxString& aCmpFileName,
60  REPORTER* aReporter,
61  bool aChangeFootprints,
62  bool aDeleteUnconnectedTracks,
63  bool aDeleteExtraFootprints,
64  bool aSelectByTimeStamp,
65  bool aDeleteSinglePadNets,
66  bool aIsDryRun )
67 {
68  wxString msg;
69  NETLIST netlist;
70  KIGFX::VIEW* view = GetGalCanvas()->GetView();
71  BOARD* board = GetBoard();
72  std::vector<MODULE*> newFootprints;
73  // keep trace of the initial baord area, if we want to place new footprints
74  // outside the existinag board
75  EDA_RECT bbox = board->GetBoundingBox();
76 
77  netlist.SetIsDryRun( aIsDryRun );
78  netlist.SetFindByTimeStamp( aSelectByTimeStamp );
79  netlist.SetDeleteExtraFootprints( aDeleteExtraFootprints );
80  netlist.SetReplaceFootprints( aChangeFootprints );
81 
82  try
83  {
84  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
85  &netlist, aNetlistFileName, aCmpFileName ) );
86 
87  if( !netlistReader.get() )
88  {
89  msg.Printf( _( "Cannot open netlist file \"%s\"." ), GetChars( aNetlistFileName ) );
90  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
91  return;
92  }
93 
94  SetLastNetListRead( aNetlistFileName );
95  netlistReader->LoadNetlist();
96  LoadFootprints( netlist, aReporter );
97  }
98  catch( const IO_ERROR& ioe )
99  {
100  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
101  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
102  return;
103  }
104 
105  // Clear undo and redo lists to avoid inconsistencies between lists
106  if( !netlist.IsDryRun() )
107  GetScreen()->ClearUndoRedoList();
108 
109  if( !netlist.IsDryRun() )
110  {
111  // Remove old modules
112  for( MODULE* module = board->m_Modules; module; module = module->Next() )
113  {
114  module->RunOnChildren( std::bind( &KIGFX::VIEW::Remove, view, _1 ) );
115  view->Remove( module );
116  }
117  }
118 
119  // Clear selection, just in case a selected item has to be removed
120  m_toolManager->RunAction( PCB_ACTIONS::selectionClear, true );
121 
122  netlist.SortByReference();
123  board->ReplaceNetlist( netlist, aDeleteSinglePadNets, &newFootprints, aReporter );
124 
125  // If it was a dry run, nothing has changed so we're done.
126  if( netlist.IsDryRun() )
127  return;
128 
129  if( IsGalCanvasActive() )
130  {
131  SpreadFootprints( &newFootprints, false, false, GetCrossHairPosition() );
132 
133  if( !newFootprints.empty() )
134  {
135  for( MODULE* footprint : newFootprints )
136  {
137  m_toolManager->RunAction( PCB_ACTIONS::selectItem, true, footprint );
138  }
139  m_toolManager->InvokeTool( "pcbnew.InteractiveEdit" );
140  }
141  }
142  else
143  {
144  wxPoint placementAreaPosition;
145 
146  // Place area to the left side of the board.
147  // if the board is empty, the bbox position is (0,0)
148  placementAreaPosition.x = bbox.GetEnd().x + Millimeter2iu( 10 );
149  placementAreaPosition.y = bbox.GetOrigin().y;
150 
151  SpreadFootprints( &newFootprints, false, false, placementAreaPosition );
152  }
153 
154  OnModify();
155 
156  SetCurItem( NULL );
157 
158  // Reload modules
159  for( MODULE* module = board->m_Modules; module; module = module->Next() )
160  {
161  module->RunOnChildren( std::bind( &KIGFX::VIEW::Add, view, _1, -1 ) );
162  view->Add( module );
163  }
164 
165  if( aDeleteUnconnectedTracks && board->m_Track )
166  {
167  // Remove erroneous tracks. This should probably pushed down to the #BOARD object.
168  RemoveMisConnectedTracks();
169  }
170 
171  // Rebuild the board connectivity:
172  Compile_Ratsnest( NULL, true );
173  board->GetRatsnest()->ProcessBoard();
174 
175  SetMsgPanel( board );
176  m_canvas->Refresh();
177 }
178 
179 
181 {
182  if( GetBoard()->m_Modules == NULL )
183  {
184  DisplayError( this, _( "No footprints" ) );
185  return 0;
186  }
187 
188  wxArrayString listnames;
189 
190  MODULE* module;
191 
192  for( module = GetBoard()->m_Modules; module; module = module->Next() )
193  listnames.Add( module->GetReference() );
194 
195  wxArrayString headers;
196  headers.Add( wxT( "Module" ) );
197  std::vector<wxArrayString> itemsToDisplay;
198 
199  // Conversion from wxArrayString to vector of ArrayString
200  for( unsigned i = 0; i < listnames.GetCount(); i++ )
201  {
202  wxArrayString item;
203  item.Add( listnames[i] );
204  itemsToDisplay.push_back( item );
205  }
206 
207  EDA_LIST_DIALOG dlg( this, _( "Components" ), headers, itemsToDisplay, wxEmptyString );
208 
209  if( dlg.ShowModal() != wxID_OK )
210  return NULL;
211 
212  wxString ref = dlg.GetTextSelection();
213 
214  for( module = GetBoard()->m_Modules; module; module = module->Next() )
215  {
216  if( module->GetReference() == ref )
217  break;
218  }
219 
220  return module;
221 }
222 
223 
224 #define ALLOW_PARTIAL_FPID 1
225 
226 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER* aReporter )
227  throw( IO_ERROR, PARSE_ERROR )
228 {
229  wxString msg;
230  LIB_ID lastFPID;
231  COMPONENT* component;
232  MODULE* module = 0;
233  MODULE* fpOnBoard;
234 
235  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
236  return;
237 
238  aNetlist.SortByFPID();
239 
240  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
241  {
242  component = aNetlist.GetComponent( ii );
243 
244 #if ALLOW_PARTIAL_FPID
245  // The FPID is ok as long as there is a footprint portion coming
246  // from eeschema.
247  if( !component->GetFPID().GetLibItemName().size() )
248 #else
249  if( component->GetFPID().empty() )
250 #endif
251  {
252  if( aReporter )
253  {
254  msg.Printf( _( "No footprint defined for component '%s'.\n" ),
255  GetChars( component->GetReference() ) );
256  aReporter->Report( msg, REPORTER::RPT_ERROR );
257  }
258 
259  continue;
260  }
261 
262  // Check if component footprint is already on BOARD and only load the footprint from
263  // the library if it's needed. Nickname can be blank.
264  if( aNetlist.IsFindByTimeStamp() )
265  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
266  else
267  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
268 
269  bool footprintMisMatch = fpOnBoard &&
270  fpOnBoard->GetFPID() != component->GetFPID();
271 
272  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
273  {
274  if( aReporter )
275  {
276  msg.Printf( _( "Footprint of component '%s' changed: board footprint '%s', netlist footprint '%s'\n" ),
277  GetChars( component->GetReference() ),
278  GetChars( fpOnBoard->GetFPID().Format() ),
279  GetChars( component->GetFPID().Format() ) );
280  aReporter->Report( msg, REPORTER::RPT_WARNING );
281  }
282 
283  continue;
284  }
285 
286  if( !aNetlist.GetReplaceFootprints() )
287  footprintMisMatch = false;
288 
289  bool loadFootprint = (fpOnBoard == NULL) || footprintMisMatch;
290 
291  if( loadFootprint && (component->GetFPID() != lastFPID) )
292  {
293  module = NULL;
294 
295 #if ALLOW_PARTIAL_FPID
296  // The LIB_ID is ok as long as there is a footprint portion coming
297  // the library if it's needed. Nickname can be blank.
298  if( !component->GetFPID().GetLibItemName().size() )
299 #else
300  if( !component->GetFPID().IsValid() )
301 #endif
302  {
303  if( aReporter )
304  {
305  msg.Printf( _( "Component '%s' footprint ID '%s' is not "
306  "valid.\n" ),
307  GetChars( component->GetReference() ),
308  GetChars( component->GetFPID().Format() ) );
309  aReporter->Report( msg, REPORTER::RPT_ERROR );
310  }
311 
312  continue;
313  }
314 
315  // loadFootprint() can find a footprint with an empty nickname in fpid.
316  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
317 
318  if( module )
319  {
320  lastFPID = component->GetFPID();
321  }
322  else
323  {
324  if( aReporter )
325  {
326  msg.Printf( _( "Component '%s' footprint '%s' was not found in "
327  "any libraries in the footprint library table.\n" ),
328  GetChars( component->GetReference() ),
329  GetChars( component->GetFPID().GetLibItemName() ) );
330  aReporter->Report( msg, REPORTER::RPT_ERROR );
331  }
332 
333  continue;
334  }
335  }
336  else
337  {
338  // Footprint already loaded from a library, duplicate it (faster)
339  if( module == NULL )
340  continue; // Module does not exist in any library.
341 
342  module = new MODULE( *module );
343  }
344 
345  if( loadFootprint && module != NULL )
346  component->SetModule( module );
347  }
348 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
void Add(BOARD_ITEM *aBoardItem, ADD_MODE aMode=ADD_INSERT) override
>
bool IsValid() const
Function Format.
Definition: lib_id.h:178
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: class_board.h:827
bool empty() const
Function empty.
Definition: lib_id.h:199
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:164
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog...
Definition: displlst.cpp:137
This file is part of the common library.
PROJECT & Prj()
Definition: kicad.cpp:293
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
MODULE * Next() const
Definition: class_module.h:99
void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:340
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Function GetNetlistReader attempts to determine the net list file type of aNetlistFileName and return...
class EDA_LIST_DIALOG
void SetFindByTimeStamp(bool aFindByTimeStamp)
Definition: pcb_netlist.h:310
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
Class LIB_ID.
Definition: lib_id.h:56
void ReadPcbNetlist(const wxString &aNetlistFileName, const wxString &aCmpFileName, REPORTER *aReporter, bool aChangeFootprint, bool aDeleteBadTracks, bool aDeleteExtraFootprints, bool aSelectByTimestamp, bool aDeleteSinglePadNets, bool aIsDryRun)
Function ReadPcbNetlist reads aNetlistFileName and updates the footprints (load missing footprints an...
MODULE * ListAndSelectModuleName()
Function ListAndSelectModuleName builds and shows a list of existing modules on board that the user c...
void SortByReference()
const wxString & GetReference() const
Definition: pcb_netlist.h:149
const LIB_ID & GetFPID() const
Definition: class_module.h:151
const wxPoint & GetOrigin() const
void ReplaceNetlist(NETLIST &aNetlist, bool aDeleteSinglePadNets, std::vector< MODULE * > *aNewFootprints, REPORTER *aReporter=NULL)
Function ReplaceNetlist updates the BOARD according to aNetlist.
void SetReplaceFootprints(bool aReplaceFootprints)
Definition: pcb_netlist.h:314
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:213
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
void ProcessBoard()
Function ProcessBoard() Prepares data for computing (computes a list of current nodes and connections...
The common library.
Helper dialog and control classes.
void SetIsDryRun(bool aIsDryRun)
Definition: pcb_netlist.h:306
RN_DATA * GetRatsnest() const
Function GetRatsnest() returns list of missing connections between components/tracks.
Definition: class_board.h:287
Class COMPONENT is used to store components and all of their related information found in a netlist...
Definition: pcb_netlist.h:83
const wxPoint GetEnd() const
BOARD * GetBoard()
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
void LoadFootprints(NETLIST &aNetlist, REPORTER *aReporter)
Function loadFootprints loads the footprints for each COMPONENT in aNetlist from the list of librarie...
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller's report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:94
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
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:166
const wxString & GetReference() const
Function GetReference.
Definition: class_module.h:411
DLIST< MODULE > m_Modules
Definition: class_board.h:243
static TOOL_ACTION selectItem
Selects an item (specified as the event parameter).
Definition: pcb_actions.h:56
Class EDA_RECT handles the component boundary box.
UTF8 Format() const
Function Format.
Definition: lib_id.cpp:263
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:47
void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:310
DLIST< TRACK > m_Track
Definition: class_board.h:244
bool IsDryRun() const
Definition: pcb_netlist.h:308
Module description (excepted pads)
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:299
Class VIEW.
Definition: view.h:58
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
MODULE * loadFootprint(const LIB_ID &aFootprintId)
Function loadFootprint attempts to load aFootprintId from the footprint library table.
Definition: loadcmp.cpp:331