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  view->Remove( module );
115  }
116  }
117 
118  // Clear selection, just in case a selected item has to be removed
119  m_toolManager->RunAction( PCB_ACTIONS::selectionClear, true );
120 
121  netlist.SortByReference();
122  board->ReplaceNetlist( netlist, aDeleteSinglePadNets, &newFootprints, aReporter );
123 
124  // If it was a dry run, nothing has changed so we're done.
125  if( netlist.IsDryRun() )
126  return;
127 
128  if( IsGalCanvasActive() )
129  {
130  SpreadFootprints( &newFootprints, false, false, GetCrossHairPosition() );
131 
132  if( !newFootprints.empty() )
133  {
134  for( MODULE* footprint : newFootprints )
135  {
136  m_toolManager->RunAction( PCB_ACTIONS::selectItem, true, footprint );
137  }
138  m_toolManager->InvokeTool( "pcbnew.InteractiveEdit" );
139  }
140  }
141  else
142  {
143  wxPoint placementAreaPosition;
144 
145  // Place area to the left side of the board.
146  // if the board is empty, the bbox position is (0,0)
147  placementAreaPosition.x = bbox.GetEnd().x + Millimeter2iu( 10 );
148  placementAreaPosition.y = bbox.GetOrigin().y;
149 
150  SpreadFootprints( &newFootprints, false, false, placementAreaPosition );
151  }
152 
153  OnModify();
154 
155  SetCurItem( NULL );
156 
157  // Reload modules
158  for( MODULE* module = board->m_Modules; module; module = module->Next() )
159  {
160  view->Add( module );
161  }
162 
163  if( aDeleteUnconnectedTracks && board->m_Track )
164  {
165  // Remove erroneous tracks. This should probably pushed down to the #BOARD object.
166  RemoveMisConnectedTracks();
167  }
168 
169  // Rebuild the board connectivity:
170  board->GetConnectivity()->Build( board );
171 
172  SetMsgPanel( board );
173  m_canvas->Refresh();
174 }
175 
176 
178 {
179  if( GetBoard()->m_Modules == NULL )
180  {
181  DisplayError( this, _( "No footprints" ) );
182  return 0;
183  }
184 
185  wxArrayString listnames;
186 
187  MODULE* module;
188 
189  for( module = GetBoard()->m_Modules; module; module = module->Next() )
190  listnames.Add( module->GetReference() );
191 
192  wxArrayString headers;
193  headers.Add( wxT( "Module" ) );
194  std::vector<wxArrayString> itemsToDisplay;
195 
196  // Conversion from wxArrayString to vector of ArrayString
197  for( unsigned i = 0; i < listnames.GetCount(); i++ )
198  {
199  wxArrayString item;
200  item.Add( listnames[i] );
201  itemsToDisplay.push_back( item );
202  }
203 
204  EDA_LIST_DIALOG dlg( this, _( "Components" ), headers, itemsToDisplay, wxEmptyString );
205 
206  if( dlg.ShowModal() != wxID_OK )
207  return NULL;
208 
209  wxString ref = dlg.GetTextSelection();
210 
211  for( module = GetBoard()->m_Modules; module; module = module->Next() )
212  {
213  if( module->GetReference() == ref )
214  break;
215  }
216 
217  return module;
218 }
219 
220 
221 #define ALLOW_PARTIAL_FPID 1
222 
223 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER* aReporter )
224 {
225  wxString msg;
226  LIB_ID lastFPID;
227  COMPONENT* component;
228  MODULE* module = 0;
229  MODULE* fpOnBoard;
230 
231  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
232  return;
233 
234  aNetlist.SortByFPID();
235 
236  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
237  {
238  component = aNetlist.GetComponent( ii );
239 
240 #if ALLOW_PARTIAL_FPID
241  // The FPID is ok as long as there is a footprint portion coming
242  // from eeschema.
243  if( !component->GetFPID().GetLibItemName().size() )
244 #else
245  if( component->GetFPID().empty() )
246 #endif
247  {
248  if( aReporter )
249  {
250  msg.Printf( _( "No footprint defined for component '%s'.\n" ),
251  GetChars( component->GetReference() ) );
252  aReporter->Report( msg, REPORTER::RPT_ERROR );
253  }
254 
255  continue;
256  }
257 
258  // Check if component footprint is already on BOARD and only load the footprint from
259  // the library if it's needed. Nickname can be blank.
260  if( aNetlist.IsFindByTimeStamp() )
261  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
262  else
263  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
264 
265  bool footprintMisMatch = fpOnBoard &&
266  fpOnBoard->GetFPID() != component->GetFPID();
267 
268  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
269  {
270  if( aReporter )
271  {
272  msg.Printf( _( "Footprint of component '%s' changed: board footprint '%s', netlist footprint '%s'\n" ),
273  GetChars( component->GetReference() ),
274  GetChars( fpOnBoard->GetFPID().Format() ),
275  GetChars( component->GetFPID().Format() ) );
276  aReporter->Report( msg, REPORTER::RPT_WARNING );
277  }
278 
279  continue;
280  }
281 
282  if( !aNetlist.GetReplaceFootprints() )
283  footprintMisMatch = false;
284 
285  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
286  continue;
287 
288  if( component->GetFPID() != lastFPID )
289  {
290  module = NULL;
291 
292 #if ALLOW_PARTIAL_FPID
293  // The LIB_ID is ok as long as there is a footprint portion coming
294  // the library if it's needed. Nickname can be blank.
295  if( !component->GetFPID().GetLibItemName().size() )
296 #else
297  if( !component->GetFPID().IsValid() )
298 #endif
299  {
300  if( aReporter )
301  {
302  msg.Printf( _( "Component '%s' footprint ID '%s' is not "
303  "valid.\n" ),
304  GetChars( component->GetReference() ),
305  GetChars( component->GetFPID().Format() ) );
306  aReporter->Report( msg, REPORTER::RPT_ERROR );
307  }
308 
309  continue;
310  }
311 
312  // loadFootprint() can find a footprint with an empty nickname in fpid.
313  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
314 
315  if( module )
316  {
317  lastFPID = component->GetFPID();
318  }
319  else
320  {
321  if( aReporter )
322  {
323  msg.Printf( _( "Component '%s' footprint '%s' was not found in "
324  "any libraries in the footprint library table.\n" ),
325  GetChars( component->GetReference() ),
326  GetChars( component->GetFPID().GetLibItemName() ) );
327  aReporter->Report( msg, REPORTER::RPT_ERROR );
328  }
329 
330  continue;
331  }
332  }
333  else
334  {
335  // Footprint already loaded from a library, duplicate it (faster)
336  if( module == NULL )
337  continue; // Module does not exist in any library.
338 
339  module = new MODULE( *module );
340  }
341 
342  if( module )
343  component->SetModule( module );
344  }
345 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
bool IsValid() const
Definition: lib_id.h:155
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: class_board.h:785
void SortByFPID()
bool empty() const
Definition: lib_id.h:170
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:270
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
MODULE * Next() const
Definition: class_module.h:120
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:342
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
std::string::size_type size() const
Definition: utf8.h:115
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
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
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:184
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
Definition: lib_id.h:115
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
void ReadPcbNetlist(const wxString &aNetlistFileName, const wxString &aCmpFileName, REPORTER *aReporter, bool aChangeFootprint, bool aDeleteBadTracks, bool aDeleteExtraFootprints, bool aSelectByTimestamp, bool aDeleteSinglePadNets, bool aIsDryRun) override
Function ReadPcbNetlist reads aNetlistFileName and updates the footprints (load missing footprints an...
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:439
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
Definition: lib_id.cpp:263
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:47
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:312
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:73
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)
Return true if the table is empty.