KiCad PCB EDA Suite
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 <pcb_edit_frame.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  // TODO is there a way to extract information about which nets were modified?
173  for( auto track : board->Tracks() )
174  view->Update( track );
175 
176  SetMsgPanel( board );
177  m_canvas->Refresh();
178 }
179 
180 
182 {
183  if( GetBoard()->m_Modules == NULL )
184  {
185  DisplayError( this, _( "No footprints" ) );
186  return 0;
187  }
188 
189  wxArrayString listnames;
190 
191  MODULE* module;
192 
193  for( module = GetBoard()->m_Modules; module; module = module->Next() )
194  listnames.Add( module->GetReference() );
195 
196  wxArrayString headers;
197  headers.Add( wxT( "Module" ) );
198  std::vector<wxArrayString> itemsToDisplay;
199 
200  // Conversion from wxArrayString to vector of ArrayString
201  for( unsigned i = 0; i < listnames.GetCount(); i++ )
202  {
203  wxArrayString item;
204  item.Add( listnames[i] );
205  itemsToDisplay.push_back( item );
206  }
207 
208  EDA_LIST_DIALOG dlg( this, _( "Components" ), headers, itemsToDisplay, wxEmptyString );
209 
210  if( dlg.ShowModal() != wxID_OK )
211  return NULL;
212 
213  wxString ref = dlg.GetTextSelection();
214 
215  for( module = GetBoard()->m_Modules; module; module = module->Next() )
216  {
217  if( module->GetReference() == ref )
218  break;
219  }
220 
221  return module;
222 }
223 
224 
225 #define ALLOW_PARTIAL_FPID 1
226 
227 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
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  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
253  component->GetReference() );
254  aReporter.Report( msg, REPORTER::RPT_ERROR );
255 
256  continue;
257  }
258 
259  // Check if component footprint is already on BOARD and only load the footprint from
260  // the library if it's needed. Nickname can be blank.
261  if( aNetlist.IsFindByTimeStamp() )
262  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
263  else
264  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
265 
266  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
267 
268  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
269  {
270  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
271  component->GetReference(),
272  GetChars( fpOnBoard->GetFPID().Format() ),
273  GetChars( component->GetFPID().Format() ) );
274  aReporter.Report( msg, REPORTER::RPT_WARNING );
275 
276  continue;
277  }
278 
279  if( !aNetlist.GetReplaceFootprints() )
280  footprintMisMatch = false;
281 
282  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
283  continue;
284 
285  if( component->GetFPID() != lastFPID )
286  {
287  module = NULL;
288 
289 #if ALLOW_PARTIAL_FPID
290  // The LIB_ID is ok as long as there is a footprint portion coming
291  // the library if it's needed. Nickname can be blank.
292  if( !component->GetFPID().GetLibItemName().size() )
293 #else
294  if( !component->GetFPID().IsValid() )
295 #endif
296  {
297  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
298  component->GetReference(),
299  GetChars( component->GetFPID().Format() ) );
300  aReporter.Report( msg, REPORTER::RPT_ERROR );
301 
302  continue;
303  }
304 
305  // loadFootprint() can find a footprint with an empty nickname in fpid.
306  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
307 
308  if( module )
309  {
310  lastFPID = component->GetFPID();
311  }
312  else
313  {
314  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
315  component->GetReference(),
316  GetChars( component->GetFPID().GetLibItemName() ) );
317  aReporter.Report( msg, REPORTER::RPT_ERROR );
318 
319  continue;
320  }
321  }
322  else
323  {
324  // Footprint already loaded from a library, duplicate it (faster)
325  if( module == NULL )
326  continue; // Module does not exist in any library.
327 
328  module = new MODULE( *module );
329  }
330 
331  if( module )
332  component->SetModule( module );
333  }
334 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
bool IsValid() const
Definition: lib_id.h:171
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes...
Definition: class_board.h:792
void SortByFPID()
bool empty() const
Definition: lib_id.h:186
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog...
Definition: displlst.cpp:156
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
This file is part of the common library.
PROJECT & Prj()
Definition: kicad.cpp:292
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Function loadFootprints loads the footprints for each COMPONENT in aNetlist from the list of librarie...
Definition: netlist.cpp:227
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
MODULE * Next() const
Definition: class_module.h:123
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:374
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:311
std::string::size_type size() const
Definition: utf8.h:115
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:244
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...
Definition: netlist.cpp:181
void SortByReference()
const wxString & GetReference() const
Definition: pcb_netlist.h:151
const LIB_ID & GetFPID() const
Definition: class_module.h:193
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
>
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:320
void SetReplaceFootprints(bool aReplaceFootprints)
Definition: pcb_netlist.h:315
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:214
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:313
The common library.
void ReplaceNetlist(NETLIST &aNetlist, bool aDeleteSinglePadNets, std::vector< MODULE * > *aNewFootprints, REPORTER &aReporter)
Function ReplaceNetlist updates the BOARD according to aNetlist.
Helper dialog and control classes.
void SetIsDryRun(bool aIsDryRun)
Definition: pcb_netlist.h:307
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...
Definition: netlist.cpp:58
const wxString & GetTimeStamp() const
Definition: pcb_netlist.h:170
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1530
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
Definition: eda_rect.h:114
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:265
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
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:170
const wxString & GetReference() const
Function GetReference.
Definition: class_module.h:463
DLIST< MODULE > m_Modules
Definition: class_board.h:248
size_t i
Definition: json11.cpp:597
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.
Definition: eda_rect.h:44
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:237
DLIST_ITERATOR_WRAPPER< TRACK > Tracks()
Definition: class_board.h:253
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:344
DLIST< TRACK > m_Track
Definition: class_board.h:249
bool IsDryRun() const
Definition: pcb_netlist.h:309
Module description (excepted pads)
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:300
Class VIEW.
Definition: view.h:61
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:256
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
MODULE * loadFootprint(const LIB_ID &aFootprintId)
Function loadFootprint attempts to load aFootprintId from the footprint library table.
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:296
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.