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  bool* runDragCommand )
68 {
69  wxString msg;
70  NETLIST netlist;
71  KIGFX::VIEW* view = GetGalCanvas()->GetView();
72  BOARD* board = GetBoard();
73  std::vector<MODULE*> newFootprints;
74  // keep trace of the initial baord area, if we want to place new footprints
75  // outside the existinag board
76  EDA_RECT bbox = board->GetBoundingBox();
77 
78  netlist.SetIsDryRun( aIsDryRun );
79  netlist.SetFindByTimeStamp( aSelectByTimeStamp );
80  netlist.SetDeleteExtraFootprints( aDeleteExtraFootprints );
81  netlist.SetReplaceFootprints( aChangeFootprints );
82 
83  try
84  {
85  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
86  &netlist, aNetlistFileName, aCmpFileName ) );
87 
88  if( !netlistReader.get() )
89  {
90  msg.Printf( _( "Cannot open netlist file \"%s\"." ), GetChars( aNetlistFileName ) );
91  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
92  return;
93  }
94 
95  SetLastNetListRead( aNetlistFileName );
96  netlistReader->LoadNetlist();
97  LoadFootprints( netlist, aReporter );
98  }
99  catch( const IO_ERROR& ioe )
100  {
101  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
102  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
103  return;
104  }
105 
106  // Clear undo and redo lists to avoid inconsistencies between lists
107  if( !netlist.IsDryRun() )
108  GetScreen()->ClearUndoRedoList();
109 
110  if( !netlist.IsDryRun() )
111  {
112  // Remove old modules
113  for( MODULE* module = board->m_Modules; module; module = module->Next() )
114  view->Remove( module );
115  }
116 
117  // Clear selection, just in case a selected item has to be removed
118  m_toolManager->RunAction( PCB_ACTIONS::selectionClear, true );
119  *runDragCommand = false;
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  wxPoint placementAreaPosition = GetCrossHairPosition();
129 
130  if( !IsGalCanvasActive() )
131  {
132  // In legacy mode place area to the left side of the board.
133  // if the board is empty, the bbox position is (0,0)
134  placementAreaPosition.x = bbox.GetEnd().x + Millimeter2iu( 10 );
135  placementAreaPosition.y = bbox.GetOrigin().y;
136  }
137 
138  SpreadFootprints( &newFootprints, false, false, placementAreaPosition );
139 
140  // Reload modules
141  for( MODULE* module = board->m_Modules; module; module = module->Next() )
142  view->Add( module );
143 
144  if( IsGalCanvasActive() )
145  {
146  // Start drag command for new modules
147  if( !newFootprints.empty() )
148  {
149  for( MODULE* footprint : newFootprints )
150  m_toolManager->RunAction( PCB_ACTIONS::selectItem, true, footprint );
151 
152  *runDragCommand = true;
153  }
154  }
155 
156  OnModify();
157 
158  SetCurItem( NULL );
159 
160  if( aDeleteUnconnectedTracks && board->m_Track )
161  {
162  // Remove erroneous tracks. This should probably pushed down to the #BOARD object.
163  RemoveMisConnectedTracks();
164  }
165 
166  // Rebuild the board connectivity:
167  board->GetConnectivity()->Build( board );
168 
169  // TODO is there a way to extract information about which nets were modified?
170  for( auto track : board->Tracks() )
171  view->Update( track );
172 
173  SetMsgPanel( board );
174  m_canvas->Refresh();
175 }
176 
177 
179 {
180  if( GetBoard()->m_Modules == NULL )
181  {
182  DisplayError( this, _( "No footprints" ) );
183  return 0;
184  }
185 
186  wxArrayString listnames;
187 
188  MODULE* module;
189 
190  for( module = GetBoard()->m_Modules; module; module = module->Next() )
191  listnames.Add( module->GetReference() );
192 
193  wxArrayString headers;
194  headers.Add( wxT( "Module" ) );
195  std::vector<wxArrayString> itemsToDisplay;
196 
197  // Conversion from wxArrayString to vector of ArrayString
198  for( unsigned i = 0; i < listnames.GetCount(); i++ )
199  {
200  wxArrayString item;
201  item.Add( listnames[i] );
202  itemsToDisplay.push_back( item );
203  }
204 
205  EDA_LIST_DIALOG dlg( this, _( "Components" ), headers, itemsToDisplay, wxEmptyString );
206 
207  if( dlg.ShowModal() != wxID_OK )
208  return NULL;
209 
210  wxString ref = dlg.GetTextSelection();
211 
212  for( module = GetBoard()->m_Modules; module; module = module->Next() )
213  {
214  if( module->GetReference() == ref )
215  break;
216  }
217 
218  return module;
219 }
220 
221 
222 #define ALLOW_PARTIAL_FPID 1
223 
224 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
225 {
226  wxString msg;
227  LIB_ID lastFPID;
228  COMPONENT* component;
229  MODULE* module = 0;
230  MODULE* fpOnBoard;
231 
232  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
233  return;
234 
235  aNetlist.SortByFPID();
236 
237  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
238  {
239  component = aNetlist.GetComponent( ii );
240 
241 #if ALLOW_PARTIAL_FPID
242  // The FPID is ok as long as there is a footprint portion coming
243  // from eeschema.
244  if( !component->GetFPID().GetLibItemName().size() )
245 #else
246  if( component->GetFPID().empty() )
247 #endif
248  {
249  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
250  component->GetReference() );
251  aReporter.Report( msg, REPORTER::RPT_ERROR );
252 
253  continue;
254  }
255 
256  // Check if component footprint is already on BOARD and only load the footprint from
257  // the library if it's needed. Nickname can be blank.
258  if( aNetlist.IsFindByTimeStamp() )
259  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
260  else
261  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
262 
263  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
264 
265  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
266  {
267  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
268  component->GetReference(),
269  GetChars( fpOnBoard->GetFPID().Format() ),
270  GetChars( component->GetFPID().Format() ) );
271  aReporter.Report( msg, REPORTER::RPT_WARNING );
272 
273  continue;
274  }
275 
276  if( !aNetlist.GetReplaceFootprints() )
277  footprintMisMatch = false;
278 
279  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
280  continue;
281 
282  if( component->GetFPID() != lastFPID )
283  {
284  module = NULL;
285 
286 #if ALLOW_PARTIAL_FPID
287  // The LIB_ID is ok as long as there is a footprint portion coming
288  // the library if it's needed. Nickname can be blank.
289  if( !component->GetFPID().GetLibItemName().size() )
290 #else
291  if( !component->GetFPID().IsValid() )
292 #endif
293  {
294  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
295  component->GetReference(),
296  GetChars( component->GetFPID().Format() ) );
297  aReporter.Report( msg, REPORTER::RPT_ERROR );
298 
299  continue;
300  }
301 
302  // loadFootprint() can find a footprint with an empty nickname in fpid.
303  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
304 
305  if( module )
306  {
307  lastFPID = component->GetFPID();
308  }
309  else
310  {
311  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
312  component->GetReference(),
313  GetChars( component->GetFPID().GetLibItemName() ) );
314  aReporter.Report( msg, REPORTER::RPT_ERROR );
315 
316  continue;
317  }
318  }
319  else
320  {
321  // Footprint already loaded from a library, duplicate it (faster)
322  if( module == NULL )
323  continue; // Module does not exist in any library.
324 
325  module = new MODULE( *module );
326  }
327 
328  if( module )
329  component->SetModule( module );
330  }
331 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:53
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: class_board.h:798
void SortByFPID()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
wxString GetTextSelection(int aColumn=0)
Function GetTextSelection return the selected text from aColumn in the wxListCtrl in the dialog.
Definition: displlst.cpp:156
This file is part of the common library.
PROJECT & Prj()
Definition: kicad.cpp:303
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:313
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Function loadFootprints loads the footprints for each COMPONENT in aNetlist from the list of librarie...
Definition: netlist.cpp:224
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
virtual void Remove(VIEW_ITEM *aItem)
Function Remove() Removes a VIEW_ITEM from the view.
Definition: view.cpp:376
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...
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:256
class EDA_LIST_DIALOG
void SetFindByTimeStamp(bool aFindByTimeStamp)
Definition: pcb_netlist.h:311
const LIB_ID & GetFPID() const
Definition: class_module.h:192
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
bool IsValid() const
Definition: lib_id.h:171
bool empty() const
Definition: lib_id.h:186
MODULE * ListAndSelectModuleName()
Function ListAndSelectModuleName builds and shows a list of existing modules on board that the user c...
Definition: netlist.cpp:178
void SortByReference()
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
const wxPoint GetEnd() const
Definition: eda_rect.h:114
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
bool IsDryRun() const
Definition: pcb_netlist.h:309
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
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const wxPoint GetOrigin() const
Definition: eda_rect.h:112
const wxString & GetReference() const
Definition: pcb_netlist.h:151
std::shared_ptr< CONNECTIVITY_DATA > GetConnectivity() const
Function GetConnectivity() returns list of missing connections between components/tracks.
Definition: class_board.h:297
const wxString & GetReference() const
Function GetReference.
Definition: class_module.h:462
Definition of file extensions used in Kicad.
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
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:1539
UTF8 Format() const
Definition: lib_id.cpp:237
Class COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:83
BOARD * GetBoard()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
void ReadPcbNetlist(const wxString &aNetlistFileName, const wxString &aCmpFileName, REPORTER &aReporter, bool aChangeFootprint, bool aDeleteBadTracks, bool aDeleteExtraFootprints, bool aSelectByTimestamp, bool aDeleteSinglePadNets, bool aIsDryRun, bool *runDragCommand)
Function ReadPcbNetlist reads aNetlistFileName and updates the footprints (load missing footprints an...
Definition: netlist.cpp:58
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:265
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:171
DLIST< MODULE > m_Modules
Definition: class_board.h:249
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:320
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.
MODULE * Next() const
Definition: class_module.h:122
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:244
DLIST_ITERATOR_WRAPPER< TRACK > Tracks()
Definition: class_board.h:254
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:346
DLIST< TRACK > m_Track
Definition: class_board.h:250
Module description (excepted pads)
void SetDeleteExtraFootprints(bool aDeleteExtraFootprints)
Definition: pcb_netlist.h:300
Class VIEW.
Definition: view.h:61
std::string::size_type size() const
Definition: utf8.h:115
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:243
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.
const wxString & GetTimeStamp() const
Definition: pcb_netlist.h:170
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.