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 <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 #include <wx/msgdlg.h>
50 
51 
52 extern void SpreadFootprints( std::vector<FOOTPRINT*>* aFootprints, wxPoint aSpreadAreaPosition );
53 
54 
55 bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
56  REPORTER& aReporter )
57 {
58  wxString msg;
59 
60  try
61  {
62  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
63  &aNetlist, aFilename, wxEmptyString ) );
64 
65  if( !netlistReader.get() )
66  {
67  msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
68  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
69  return false;
70  }
71 
72  SetLastPath( LAST_PATH_NETLIST, aFilename );
73  netlistReader->LoadNetlist();
74  LoadFootprints( aNetlist, aReporter );
75  }
76  catch( const IO_ERROR& ioe )
77  {
78  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
79  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
80  return false;
81  }
82 
83  SetLastPath( LAST_PATH_NETLIST, aFilename );
84 
85  return true;
86 }
87 
88 
89 void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
90 {
91  std::string dummyPayload;
92  Kiway().ExpressMail( FRAME_SCH, MAIL_SCH_CLEAN_NETCLASSES, dummyPayload, this );
93 
94  BOARD* board = GetBoard();
95 
96  SetMsgPanel( board );
97 
98  // Update rendered tracks and vias net labels
99  // TODO is there a way to extract information about which nets were modified?
100  for( auto track : board->Tracks() )
101  GetCanvas()->GetView()->Update( track );
102 
103  std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
104 
105  // Spread new footprints.
106  wxPoint areaPosition = (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition();
107  EDA_RECT bbox = board->GetBoundingBox();
108 
109  GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
110 
111  SpreadFootprints( &newFootprints, areaPosition );
112 
113  // Start drag command for new footprints
114  if( !newFootprints.empty() )
115  {
116  for( FOOTPRINT* footprint : newFootprints )
117  GetToolManager()->RunAction( PCB_ACTIONS::selectItem, true, footprint );
118 
119  *aRunDragCommand = true;
120 
121  // Now fix a reference point to move the footprints.
122  // We use the first footprint in list as reference point
123  // The graphic cursor will be on this fp when moving the footprints.
124  PCB_SELECTION_TOOL* selTool = GetToolManager()->GetTool<PCB_SELECTION_TOOL>();
125  PCB_SELECTION& selection = selTool->GetSelection();
126  selection.SetReferencePoint( newFootprints[0]->GetPosition() );
127  }
128 
129  Compile_Ratsnest( true );
130 
131  GetCanvas()->Refresh();
132 }
133 
134 
135 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
136 {
137  wxString msg;
138  LIB_ID lastFPID;
139  COMPONENT* component;
140  FOOTPRINT* footprint = nullptr;
141  FOOTPRINT* fpOnBoard = nullptr;
142 
143  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
144  return;
145 
146  aNetlist.SortByFPID();
147 
148  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
149  {
150  component = aNetlist.GetComponent( ii );
151 
152  // The FPID is ok as long as there is a footprint portion coming from eeschema.
153  if( !component->GetFPID().GetLibItemName().size() )
154  {
155  msg.Printf( _( "No footprint defined for symbol %s." ),
156  component->GetReference() );
157  aReporter.Report( msg, RPT_SEVERITY_ERROR );
158 
159  continue;
160  }
161 
162  // Check if component footprint is already on BOARD and only load the footprint from
163  // the library if it's needed. Nickname can be blank.
164  if( aNetlist.IsFindByTimeStamp() )
165  {
166  for( const KIID& uuid : component->GetKIIDs() )
167  {
168  KIID_PATH path = component->GetPath();
169  path.push_back( uuid );
170 
171  if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) )
172  break;
173  }
174  }
175  else
176  fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
177 
178  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
179 
180  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
181  {
182  msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
183  component->GetReference(),
184  fpOnBoard->GetFPID().Format().wx_str(),
185  component->GetFPID().Format().wx_str() );
186  aReporter.Report( msg, RPT_SEVERITY_WARNING );
187 
188  continue;
189  }
190 
191  if( !aNetlist.GetReplaceFootprints() )
192  footprintMisMatch = false;
193 
194  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
195  continue;
196 
197  if( component->GetFPID() != lastFPID )
198  {
199  footprint = nullptr;
200 
201  // The LIB_ID is ok as long as there is a footprint portion coming the library if
202  // it's needed. Nickname can be blank.
203  if( !component->GetFPID().GetLibItemName().size() )
204  {
205  msg.Printf( _( "%s footprint ID '%s' is not valid." ),
206  component->GetReference(),
207  component->GetFPID().Format().wx_str() );
208  aReporter.Report( msg, RPT_SEVERITY_ERROR );
209 
210  continue;
211  }
212 
213  // loadFootprint() can find a footprint with an empty nickname in fpid.
214  footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
215 
216  if( footprint )
217  {
218  lastFPID = component->GetFPID();
219  }
220  else
221  {
222  msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
223  "library table." ),
224  component->GetReference(),
225  component->GetFPID().GetLibItemName().wx_str() );
226  aReporter.Report( msg, RPT_SEVERITY_ERROR );
227 
228  continue;
229  }
230  }
231  else
232  {
233  // Footprint already loaded from a library, duplicate it (faster)
234  if( !footprint )
235  continue; // Footprint does not exist in any library.
236 
237  footprint = new FOOTPRINT( *footprint );
238  const_cast<KIID&>( footprint->m_Uuid ) = KIID();
239  }
240 
241  if( footprint )
242  component->SetFootprint( footprint );
243  }
244 }
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: board.h:723
void SortByFPID()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:104
Update the BOARD with a new netlist.
PROJECT & Prj()
Definition: kicad.cpp:403
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:268
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each #SCH_COMPONENT in aNetlist from the list of libraries.
Definition: netlist.cpp:135
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.
Definition: netlist.cpp:55
static NETLIST_READER * GetNetlistReader(NETLIST *aNetlist, const wxString &aNetlistFileName, const wxString &aCompFootprintFileName=wxEmptyString)
Attempt to determine the net list file type of aNetlistFileName and return the appropriate NETLIST_RE...
unsigned GetCount() const
Definition: pcb_netlist.h:228
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:477
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
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:89
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:133
PCB_SELECTION & GetSelection()
Return the set of currently selected items.
Definition: kiid.h:44
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:187
FOOTPRINT * loadFootprint(const LIB_ID &aFootprintId)
Attempts to load aFootprintId from the footprint library table.
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:206
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
const wxString & GetReference() const
Definition: pcb_netlist.h:123
#define _(s)
std::vector< FOOTPRINT * > GetAddedFootprints() const
const LIB_ID & GetFPID() const
Definition: footprint.h:194
UTF8 Format() const
Definition: lib_id.cpp:116
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:84
BOARD * GetBoard()
const KIID m_Uuid
Definition: eda_item.h:475
virtual FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:283
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:236
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:271
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:62
wxString wx_str() const
Definition: utf8.cpp:46
Handle the component boundary box.
Definition: eda_rect.h:42
The selection tool: currently supports:
bool IsEmpty() const
Definition: pcb_netlist.h:218
std::string::size_type size() const
Definition: utf8.h:110
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:138
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:230
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
KIWAY Kiway
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:140