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-2017 KiCad Developers, see AUTHORS.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  board->GetConnectivity()->Build( board );
173 
174  SetMsgPanel( board );
175  m_canvas->Refresh();
176 }
177 
178 
180 {
181  if( GetBoard()->m_Modules == NULL )
182  {
183  DisplayError( this, _( "No footprints" ) );
184  return 0;
185  }
186 
187  wxArrayString listnames;
188 
189  MODULE* module;
190 
191  for( module = GetBoard()->m_Modules; module; module = module->Next() )
192  listnames.Add( module->GetReference() );
193 
194  wxArrayString headers;
195  headers.Add( wxT( "Module" ) );
196  std::vector<wxArrayString> itemsToDisplay;
197 
198  // Conversion from wxArrayString to vector of ArrayString
199  for( unsigned i = 0; i < listnames.GetCount(); i++ )
200  {
201  wxArrayString item;
202  item.Add( listnames[i] );
203  itemsToDisplay.push_back( item );
204  }
205 
206  EDA_LIST_DIALOG dlg( this, _( "Components" ), headers, itemsToDisplay, wxEmptyString );
207 
208  if( dlg.ShowModal() != wxID_OK )
209  return NULL;
210 
211  wxString ref = dlg.GetTextSelection();
212 
213  for( module = GetBoard()->m_Modules; module; module = module->Next() )
214  {
215  if( module->GetReference() == ref )
216  break;
217  }
218 
219  return module;
220 }
221 
222 
223 #define ALLOW_PARTIAL_FPID 1
224 
225 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER* aReporter )
226 {
227  wxString msg;
228  LIB_ID lastFPID;
229  COMPONENT* component;
230  MODULE* module = 0;
231  MODULE* fpOnBoard;
232 
233  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
234  return;
235 
236  aNetlist.SortByFPID();
237 
238  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
239  {
240  component = aNetlist.GetComponent( ii );
241 
242 #if ALLOW_PARTIAL_FPID
243  // The FPID is ok as long as there is a footprint portion coming
244  // from eeschema.
245  if( !component->GetFPID().GetLibItemName().size() )
246 #else
247  if( component->GetFPID().empty() )
248 #endif
249  {
250  if( aReporter )
251  {
252  msg.Printf( _( "No footprint defined for component '%s'.\n" ),
253  GetChars( component->GetReference() ) );
254  aReporter->Report( msg, REPORTER::RPT_ERROR );
255  }
256 
257  continue;
258  }
259 
260  // Check if component footprint is already on BOARD and only load the footprint from
261  // the library if it's needed. Nickname can be blank.
262  if( aNetlist.IsFindByTimeStamp() )
263  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
264  else
265  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
266 
267  bool footprintMisMatch = fpOnBoard &&
268  fpOnBoard->GetFPID() != component->GetFPID();
269 
270  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
271  {
272  if( aReporter )
273  {
274  msg.Printf( _( "Footprint of component '%s' changed: board footprint '%s', netlist footprint '%s'\n" ),
275  GetChars( component->GetReference() ),
276  GetChars( FROM_UTF8( fpOnBoard->GetFPID().Format() ) ),
277  GetChars( FROM_UTF8( component->GetFPID().Format() ) ) );
278  aReporter->Report( msg, REPORTER::RPT_WARNING );
279  }
280 
281  continue;
282  }
283 
284  if( !aNetlist.GetReplaceFootprints() )
285  footprintMisMatch = false;
286 
287  bool loadFootprint = (fpOnBoard == NULL) || footprintMisMatch;
288 
289  if( loadFootprint && (component->GetFPID() != lastFPID) )
290  {
291  module = NULL;
292 
293 #if ALLOW_PARTIAL_FPID
294  // The LIB_ID is ok as long as there is a footprint portion coming
295  // the library if it's needed. Nickname can be blank.
296  if( !component->GetFPID().GetLibItemName().size() )
297 #else
298  if( !component->GetFPID().IsValid() )
299 #endif
300  {
301  if( aReporter )
302  {
303  msg.Printf( _( "Component '%s' footprint ID '%s' is not "
304  "valid.\n" ),
305  GetChars( component->GetReference() ),
306  GetChars( FROM_UTF8( component->GetFPID().Format() ) ) );
307  aReporter->Report( msg, REPORTER::RPT_ERROR );
308  }
309 
310  continue;
311  }
312 
313  // loadFootprint() can find a footprint with an empty nickname in fpid.
314  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
315 
316  if( module )
317  {
318  lastFPID = component->GetFPID();
319  }
320  else
321  {
322  if( aReporter )
323  {
324  msg.Printf( _( "Component '%s' footprint '%s' was not found in "
325  "any libraries in the footprint library table.\n" ),
326  GetChars( component->GetReference() ),
327  FROM_UTF8( component->GetFPID().GetLibItemName() ) );
328  aReporter->Report( msg, REPORTER::RPT_ERROR );
329  }
330 
331  continue;
332  }
333  }
334  else
335  {
336  // Footprint already loaded from a library, duplicate it (faster)
337  if( module == NULL )
338  continue; // Module does not exist in any library.
339 
340  module = new MODULE( *module );
341  }
342 
343  if( loadFootprint && module != NULL )
344  component->SetModule( module );
345  }
346 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
bool IsValid() const
Function IsValid.
Definition: lib_id.h:177
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: class_board.h:799
void SortByFPID()
bool empty() const
Function empty.
Definition: lib_id.h:198
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
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
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:100
void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:341
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:302
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:235
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:164
const wxPoint & GetOrigin() const
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
>
void ReplaceNetlist(NETLIST &aNetlist, bool aDeleteSinglePadNets, std::vector< MODULE * > *aNewFootprints, REPORTER *aReporter=NULL)
Function ReplaceNetlist updates the BOARD according to aNetlist.
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:311
void SetReplaceFootprints(bool aReplaceFootprints)
Definition: pcb_netlist.h:306
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:205
const UTF8 & GetLibItemName() const
Function GetLibItemName.
Definition: lib_id.h:129
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:304
The common library.
Helper dialog and control classes.
void SetIsDryRun(bool aIsDryRun)
Definition: pcb_netlist.h:298
const wxString & GetTimeStamp() const
Definition: pcb_netlist.h:168
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()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:256
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...
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:169
const wxString & GetReference() const
Function GetReference.
Definition: class_module.h:419
DLIST< MODULE > m_Modules
Definition: class_board.h:245
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.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
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:311
DLIST< TRACK > m_Track
Definition: class_board.h:246
bool IsDryRun() const
Definition: pcb_netlist.h:300
Module description (excepted pads)
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:291
Class VIEW.
Definition: view.h:58
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:247
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:334
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:290
bool IsEmpty(bool aIncludeFallback=true)
Function IsEmpty.