KiCad PCB EDA Suite
pcbnew/netlist_reader/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 <kiway.h>
34 #include <pcb_edit_frame.h>
37 #include <reporter.h>
38 #include <lib_id.h>
39 #include <fp_lib_table.h>
40 #include <board.h>
41 #include <footprint.h>
42 #include <ratsnest/ratsnest_data.h>
43 #include <io_mgr.h>
44 #include "board_netlist_updater.h"
45 #include <tool/tool_manager.h>
46 #include <tools/pcb_actions.h>
48 #include <project/project_file.h> // LAST_PATH_TYPE
49 
50 
51 extern void SpreadFootprints( std::vector<FOOTPRINT*>* aFootprints, wxPoint aSpreadAreaPosition );
52 
53 
54 bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
55  REPORTER& aReporter )
56 {
57  wxString msg;
58 
59  try
60  {
61  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
62  &aNetlist, aFilename, wxEmptyString ) );
63 
64  if( !netlistReader.get() )
65  {
66  msg.Printf( _( "Cannot open netlist file \"%s\"." ), aFilename );
67  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
68  return false;
69  }
70 
71  SetLastPath( LAST_PATH_NETLIST, aFilename );
72  netlistReader->LoadNetlist();
73  LoadFootprints( aNetlist, aReporter );
74  }
75  catch( const IO_ERROR& ioe )
76  {
77  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
78  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
79  return false;
80  }
81 
82  SetLastPath( LAST_PATH_NETLIST, aFilename );
83 
84  return true;
85 }
86 
87 
88 void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
89 {
90  std::string dummyPayload;
91  Kiway().ExpressMail( FRAME_SCH, MAIL_SCH_CLEAN_NETCLASSES, dummyPayload, this );
92 
93  BOARD* board = GetBoard();
94 
95  SetMsgPanel( board );
96 
97  // Update rendered tracks and vias net labels
98  // TODO is there a way to extract information about which nets were modified?
99  for( auto track : board->Tracks() )
100  GetCanvas()->GetView()->Update( track );
101 
102  std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedComponents();
103 
104  // Spread new footprints.
105  wxPoint areaPosition = (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition();
106  EDA_RECT bbox = board->GetBoundingBox();
107 
108  GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
109 
110  SpreadFootprints( &newFootprints, areaPosition );
111 
112  // Start drag command for new footprints
113  if( !newFootprints.empty() )
114  {
115  for( FOOTPRINT* footprint : newFootprints )
116  GetToolManager()->RunAction( PCB_ACTIONS::selectItem, true, footprint );
117 
118  *aRunDragCommand = true;
119 
120  // Now fix a reference point to move the footprints.
121  // We use the first footprint in list as reference point
122  // The graphic cursor will be on this fp when moving the footprints.
123  PCB_SELECTION_TOOL* selTool = GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
124  PCB_SELECTION& selection = selTool->GetSelection();
125  selection.SetReferencePoint( newFootprints[0]->GetPosition() );
126  }
127 
128  Compile_Ratsnest( true );
129 
130  GetCanvas()->Refresh();
131 }
132 
133 
134 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
135 {
136  wxString msg;
137  LIB_ID lastFPID;
138  COMPONENT* component;
139  FOOTPRINT* footprint = nullptr;
140  FOOTPRINT* fpOnBoard = nullptr;
141 
142  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
143  return;
144 
145  aNetlist.SortByFPID();
146 
147  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
148  {
149  component = aNetlist.GetComponent( ii );
150 
151  // The FPID is ok as long as there is a footprint portion coming from eeschema.
152  if( !component->GetFPID().GetLibItemName().size() )
153  {
154  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
155  component->GetReference() );
156  aReporter.Report( msg, RPT_SEVERITY_ERROR );
157 
158  continue;
159  }
160 
161  // Check if component footprint is already on BOARD and only load the footprint from
162  // the library if it's needed. Nickname can be blank.
163  if( aNetlist.IsFindByTimeStamp() )
164  {
165  for( const KIID& uuid : component->GetKIIDs() )
166  {
167  KIID_PATH path = component->GetPath();
168  path.push_back( uuid );
169 
170  if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) )
171  break;
172  }
173  }
174  else
175  fpOnBoard = m_pcb->FindFootprintByReference( component->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  fpOnBoard->GetFPID().Format().wx_str(),
184  component->GetFPID().Format().wx_str() );
185  aReporter.Report( msg, RPT_SEVERITY_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  footprint = nullptr;
199 
200  // The LIB_ID is ok as long as there is a footprint portion coming the library if
201  // it's needed. Nickname can be blank.
202  if( !component->GetFPID().GetLibItemName().size() )
203  {
204  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
205  component->GetReference(),
206  component->GetFPID().Format().wx_str() );
207  aReporter.Report( msg, RPT_SEVERITY_ERROR );
208 
209  continue;
210  }
211 
212  // loadFootprint() can find a footprint with an empty nickname in fpid.
213  footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
214 
215  if( footprint )
216  {
217  lastFPID = component->GetFPID();
218  }
219  else
220  {
221  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint "
222  "library table.\n" ),
223  component->GetReference(),
224  component->GetFPID().GetLibItemName().wx_str() );
225  aReporter.Report( msg, RPT_SEVERITY_ERROR );
226 
227  continue;
228  }
229  }
230  else
231  {
232  // Footprint already loaded from a library, duplicate it (faster)
233  if( !footprint )
234  continue; // Footprint does not exist in any library.
235 
236  footprint = new FOOTPRINT( *footprint );
237  const_cast<KIID&>( footprint->m_Uuid ) = KIID();
238  }
239 
240  if( footprint )
241  component->SetFootprint( footprint );
242  }
243 }
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:63
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: board.h:796
void SortByFPID()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:106
Update the BOARD with a new netlist.
PROJECT & Prj()
Definition: kicad.cpp:386
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:281
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each SCH_COMPONENT in aNetlist from the list of libraries.
Class that computes missing connections on a PCB.
bool ReadNetlistFromFile(const wxString &aFilename, NETLIST &aNetlist, REPORTER &aReporter)
Read a netlist from a file into a NETLIST object.
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.
unsigned GetCount() const
Function GetCount.
Definition: pcb_netlist.h:237
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:439
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:64
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.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:40
void SpreadFootprints(std::vector< FOOTPRINT * > *aFootprints, wxPoint aSpreadAreaPosition)
Footprints (after loaded by reading a netlist for instance) are moved to be in a small free area (out...
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:168
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
Definition: kiid.h:44
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:264
FOOTPRINT * loadFootprint(const LIB_ID &aFootprintId)
Attempts to load aFootprintId from the footprint library table.
NETLIST stores all of information read from a netlist along with the flags used to update the NETLIST...
Definition: pcb_netlist.h:207
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
const wxString & GetReference() const
Definition: pcb_netlist.h:158
const LIB_ID & GetFPID() const
Definition: footprint.h:190
UTF8 Format() const
Definition: lib_id.cpp:233
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:85
BOARD * GetBoard()
const KIID m_Uuid
Definition: eda_item.h:524
virtual 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:246
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define _(s)
Definition: 3d_actions.cpp:33
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:284
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:66
wxString wx_str() const
Definition: utf8.cpp:51
Handle the component boundary box.
Definition: eda_rect.h:42
The selection tool: currently supports:
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:225
std::vector< FOOTPRINT * > GetAddedComponents() const
std::string::size_type size() const
Definition: utf8.h:110
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:173
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
TRACKS & Tracks()
Definition: board.h:300
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
KIWAY Kiway
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:175