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 #include <board_netlist_updater.h>
53 
54 #include <tool/tool_manager.h>
55 #include <tools/pcb_actions.h>
56 #include <tools/selection_tool.h>
57 #include <view/view.h>
58 
59 
60 bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename,
61  NETLIST& aNetlist,
62  REPORTER& aReporter )
63 {
64  wxString msg;
65 
66  try
67  {
68  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
69  &aNetlist, aFilename, wxEmptyString ) );
70 
71  if( !netlistReader.get() )
72  {
73  msg.Printf( _( "Cannot open netlist file \"%s\"." ), GetChars( aFilename ) );
74  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
75  return false;
76  }
77 
78  SetLastNetListRead( aFilename );
79  netlistReader->LoadNetlist();
80  LoadFootprints( aNetlist, aReporter );
81  }
82  catch( const IO_ERROR& ioe )
83  {
84  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
85  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
86  return false;
87  }
88 
89  SetLastNetListRead( aFilename );
90 
91  return true;
92 }
93 
94 
96  bool* aRunDragCommand )
97 {
98  BOARD* board = GetBoard();
99 
100  SetCurItem( nullptr );
101  SetMsgPanel( board );
102 
103  TOOL_MANAGER* toolManager = GetToolManager();
104 
105  // Update rendered tracks and vias net labels
106  auto view = GetGalCanvas()->GetView();
107 
108  // TODO is there a way to extract information about which nets were modified?
109  for( auto track : board->Tracks() )
110  view->Update( track );
111 
112  std::vector<MODULE*> newFootprints = aUpdater.GetAddedComponents();
113 
114  // Spread new footprints.
115  wxPoint areaPosition = GetCrossHairPosition();
116  EDA_RECT bbox = board->GetBoundingBox();
117 
118  if( !IsGalCanvasActive() )
119  {
120  // In legacy mode place area to the left side of the board.
121  // if the board is empty, the bbox position is (0,0)
122  areaPosition.x = bbox.GetEnd().x + Millimeter2iu( 10 );
123  areaPosition.y = bbox.GetOrigin().y;
124  }
125 
126  toolManager->RunAction( PCB_ACTIONS::selectionClear, true );
127 
128  SpreadFootprints( &newFootprints, false, false, areaPosition, false );
129 
130  if( IsGalCanvasActive() )
131  {
132  // Start drag command for new modules
133  if( !newFootprints.empty() )
134  {
135  for( MODULE* footprint : newFootprints )
136  toolManager->RunAction( PCB_ACTIONS::selectItem, true, footprint );
137 
138  *aRunDragCommand = true;
139 
140  // Now fix a reference point to move the footprints.
141  // We use the first footprint in list as reference point
142  // The graphic cursor will be on this fp when moving the footprints.
143  SELECTION_TOOL* selTool = toolManager->GetTool<SELECTION_TOOL>();
144  SELECTION& selection = selTool->GetSelection();
145  selection.SetReferencePoint( newFootprints[0]->GetPosition() );
146  }
147  }
148 
149  GetCanvas()->Refresh();
150 }
151 
152 
154 {
155  if( GetBoard()->m_Modules == NULL )
156  {
157  DisplayError( this, _( "No footprints" ) );
158  return 0;
159  }
160 
161  wxArrayString listnames;
162 
163  MODULE* module;
164 
165  for( module = GetBoard()->m_Modules; module; module = module->Next() )
166  listnames.Add( module->GetReference() );
167 
168  wxArrayString headers;
169  headers.Add( wxT( "Module" ) );
170  std::vector<wxArrayString> itemsToDisplay;
171 
172  // Conversion from wxArrayString to vector of ArrayString
173  for( unsigned i = 0; i < listnames.GetCount(); i++ )
174  {
175  wxArrayString item;
176  item.Add( listnames[i] );
177  itemsToDisplay.push_back( item );
178  }
179 
180  EDA_LIST_DIALOG dlg( this, _( "Components" ), headers, itemsToDisplay, wxEmptyString );
181 
182  if( dlg.ShowModal() != wxID_OK )
183  return NULL;
184 
185  wxString ref = dlg.GetTextSelection();
186 
187  for( module = GetBoard()->m_Modules; module; module = module->Next() )
188  {
189  if( module->GetReference() == ref )
190  break;
191  }
192 
193  return module;
194 }
195 
196 
197 #define ALLOW_PARTIAL_FPID 1
198 
199 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
200 {
201  wxString msg;
202  LIB_ID lastFPID;
203  COMPONENT* component;
204  MODULE* module = 0;
205  MODULE* fpOnBoard;
206 
207  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
208  return;
209 
210  aNetlist.SortByFPID();
211 
212  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
213  {
214  component = aNetlist.GetComponent( ii );
215 
216 #if ALLOW_PARTIAL_FPID
217  // The FPID is ok as long as there is a footprint portion coming
218  // from eeschema.
219  if( !component->GetFPID().GetLibItemName().size() )
220 #else
221  if( component->GetFPID().empty() )
222 #endif
223  {
224  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
225  component->GetReference() );
226  aReporter.Report( msg, REPORTER::RPT_ERROR );
227 
228  continue;
229  }
230 
231  // Check if component footprint is already on BOARD and only load the footprint from
232  // the library if it's needed. Nickname can be blank.
233  if( aNetlist.IsFindByTimeStamp() )
234  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
235  else
236  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
237 
238  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
239 
240  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
241  {
242  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
243  component->GetReference(),
244  GetChars( fpOnBoard->GetFPID().Format() ),
245  GetChars( component->GetFPID().Format() ) );
246  aReporter.Report( msg, REPORTER::RPT_WARNING );
247 
248  continue;
249  }
250 
251  if( !aNetlist.GetReplaceFootprints() )
252  footprintMisMatch = false;
253 
254  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
255  continue;
256 
257  if( component->GetFPID() != lastFPID )
258  {
259  module = NULL;
260 
261 #if ALLOW_PARTIAL_FPID
262  // The LIB_ID is ok as long as there is a footprint portion coming
263  // the library if it's needed. Nickname can be blank.
264  if( !component->GetFPID().GetLibItemName().size() )
265 #else
266  if( !component->GetFPID().IsValid() )
267 #endif
268  {
269  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
270  component->GetReference(),
271  GetChars( component->GetFPID().Format() ) );
272  aReporter.Report( msg, REPORTER::RPT_ERROR );
273 
274  continue;
275  }
276 
277  // loadFootprint() can find a footprint with an empty nickname in fpid.
278  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
279 
280  if( module )
281  {
282  lastFPID = component->GetFPID();
283  }
284  else
285  {
286  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
287  component->GetReference(),
288  GetChars( component->GetFPID().GetLibItemName() ) );
289  aReporter.Report( msg, REPORTER::RPT_ERROR );
290 
291  continue;
292  }
293  }
294  else
295  {
296  // Footprint already loaded from a library, duplicate it (faster)
297  if( module == NULL )
298  continue; // Module does not exist in any library.
299 
300  module = new MODULE( *module );
301  }
302 
303  if( module )
304  component->SetModule( module );
305  }
306 }
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:790
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.
Class BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
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:199
Class SELECTION_TOOL.
Class BOARD to handle a board.
Class that computes missing connections on a PCB.
bool ReadNetlistFromFile(const wxString &aFilename, NETLIST &aNetlist, REPORTER &aReporter)
Reads a netlist from a file into a NETLIST object.
Definition: netlist.cpp:60
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...
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
BOARD_NETLIST_UPDATER class definition.
std::vector< MODULE * > GetAddedComponents() const
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:256
class EDA_LIST_DIALOG
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
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
Definition: netlist.cpp:95
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:153
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:462
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
Class TOOL_MANAGER.
Definition: tool_manager.h:49
const wxPoint GetEnd() const
Definition: eda_rect.h:114
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:208
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_INSERT) override
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
Definition of file extensions used in Kicad.
SELECTION & GetSelection()
Function GetSelection()
Helper dialog and control classes.
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.
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:100
see class PGM_BASE
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
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:251
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:47
Module description (excepted pads)
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:244
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.