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_draw_panel_gal.h>
36 #include <confirm.h>
37 #include <dialog_helpers.h>
38 #include <pcb_edit_frame.h>
39 #include "pcb_netlist.h"
40 #include "netlist_reader.h"
41 #include <reporter.h>
43 #include <lib_id.h>
44 #include <fp_lib_table.h>
45 #include <class_board.h>
46 #include <class_module.h>
47 #include <ratsnest_data.h>
48 #include <pcbnew.h>
49 #include <io_mgr.h>
50 #include "board_netlist_updater.h"
51 #include <tool/tool_manager.h>
52 #include <tools/pcb_actions.h>
53 #include <tools/selection_tool.h>
54 #include <view/view.h>
55 
56 extern void SpreadFootprints( std::vector<MODULE*>* aFootprints,
57  wxPoint aSpreadAreaPosition );
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  SetLastPath( LAST_PATH_NETLIST, 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  SetLastPath( LAST_PATH_NETLIST, aFilename );
90 
91  return true;
92 }
93 
94 
95 void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
96 {
97  BOARD* board = GetBoard();
98 
99  SetMsgPanel( board );
100 
101  // Update rendered tracks and vias net labels
102  // TODO is there a way to extract information about which nets were modified?
103  for( auto track : board->Tracks() )
104  GetCanvas()->GetView()->Update( track );
105 
106  std::vector<MODULE*> newFootprints = aUpdater.GetAddedComponents();
107 
108  // Spread new footprints.
109  wxPoint areaPosition = (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition();
110  EDA_RECT bbox = board->GetBoundingBox();
111 
112  GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
113 
114  SpreadFootprints( &newFootprints, areaPosition );
115 
116  // Start drag command for new modules
117  if( !newFootprints.empty() )
118  {
119  for( MODULE* footprint : newFootprints )
120  GetToolManager()->RunAction( PCB_ACTIONS::selectItem, true, footprint );
121 
122  *aRunDragCommand = true;
123 
124  // Now fix a reference point to move the footprints.
125  // We use the first footprint in list as reference point
126  // The graphic cursor will be on this fp when moving the footprints.
127  SELECTION_TOOL* selTool = GetToolManager()->GetTool<SELECTION_TOOL>();
128  PCBNEW_SELECTION& selection = selTool->GetSelection();
129  selection.SetReferencePoint( newFootprints[0]->GetPosition() );
130  }
131 
132  GetCanvas()->Refresh();
133 }
134 
135 
136 #define ALLOW_PARTIAL_FPID 1
137 
138 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
139 {
140  wxString msg;
141  LIB_ID lastFPID;
142  COMPONENT* component;
143  MODULE* module = 0;
144  MODULE* fpOnBoard;
145 
146  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
147  return;
148 
149  aNetlist.SortByFPID();
150 
151  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
152  {
153  component = aNetlist.GetComponent( ii );
154 
155 #if ALLOW_PARTIAL_FPID
156  // The FPID is ok as long as there is a footprint portion coming
157  // from eeschema.
158  if( !component->GetFPID().GetLibItemName().size() )
159 #else
160  if( component->GetFPID().empty() )
161 #endif
162  {
163  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
164  component->GetReference() );
165  aReporter.Report( msg, REPORTER::RPT_ERROR );
166 
167  continue;
168  }
169 
170  // Check if component footprint is already on BOARD and only load the footprint from
171  // the library if it's needed. Nickname can be blank.
172  if( aNetlist.IsFindByTimeStamp() )
173  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetTimeStamp(), true );
174  else
175  fpOnBoard = m_Pcb->FindModule( aNetlist.GetComponent( ii )->GetReference() );
176 
177  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
178 
179  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
180  {
181  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
182  component->GetReference(),
183  GetChars( fpOnBoard->GetFPID().Format() ),
184  GetChars( component->GetFPID().Format() ) );
185  aReporter.Report( msg, REPORTER::RPT_WARNING );
186 
187  continue;
188  }
189 
190  if( !aNetlist.GetReplaceFootprints() )
191  footprintMisMatch = false;
192 
193  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
194  continue;
195 
196  if( component->GetFPID() != lastFPID )
197  {
198  module = NULL;
199 
200 #if ALLOW_PARTIAL_FPID
201  // The LIB_ID is ok as long as there is a footprint portion coming
202  // the library if it's needed. Nickname can be blank.
203  if( !component->GetFPID().GetLibItemName().size() )
204 #else
205  if( !component->GetFPID().IsValid() )
206 #endif
207  {
208  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
209  component->GetReference(),
210  GetChars( component->GetFPID().Format() ) );
211  aReporter.Report( msg, REPORTER::RPT_ERROR );
212 
213  continue;
214  }
215 
216  // loadFootprint() can find a footprint with an empty nickname in fpid.
217  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
218 
219  if( module )
220  {
221  lastFPID = component->GetFPID();
222  }
223  else
224  {
225  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
226  component->GetReference(),
227  GetChars( component->GetFPID().GetLibItemName() ) );
228  aReporter.Report( msg, REPORTER::RPT_ERROR );
229 
230  continue;
231  }
232  }
233  else
234  {
235  // Footprint already loaded from a library, duplicate it (faster)
236  if( module == NULL )
237  continue; // Module does not exist in any library.
238 
239  module = new MODULE( *module );
240  }
241 
242  if( module )
243  component->SetModule( module );
244  }
245 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:73
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: class_board.h:780
void SortByFPID()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
This file is part of the common library.
Class BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
PROJECT & Prj()
Definition: kicad.cpp:297
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:138
Class SELECTION_TOOL.
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...
BOARD_NETLIST_UPDATER class definition.
std::vector< MODULE * > GetAddedComponents() const
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:256
const LIB_ID & GetFPID() const
Definition: class_module.h:222
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
PCBNEW_SELECTION & GetSelection()
Function GetSelection()
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:166
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:243
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 wxString & GetReference() const
Definition: pcb_netlist.h:151
Definition of file extensions used in Kicad.
Helper dialog and control classes.
#define _(s)
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:101
see class PGM_BASE
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:161
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:320
static TOOL_ACTION selectItem
Selects an item (specified as the event parameter).
Definition: pcb_actions.h:76
void SpreadFootprints(std::vector< MODULE * > *aFootprints, wxPoint aSpreadAreaPosition)
Footprints (after loaded by reading a netlist for instance) are moved to be in a small free area (out...
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.
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:244
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:47
Module description (excepted pads)
std::string::size_type size() const
Definition: utf8.h:115
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
TRACKS & Tracks()
Definition: class_board.h:218
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.