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 <fctsys.h>
34 #include <pgm_base.h>
35 #include <confirm.h>
36 #include <pcb_edit_frame.h>
37 #include "pcb_netlist.h"
38 #include "netlist_reader.h"
39 #include <reporter.h>
40 #include <lib_id.h>
41 #include <fp_lib_table.h>
42 #include <class_board.h>
43 #include <class_module.h>
44 #include <ratsnest_data.h>
45 #include <pcbnew.h>
46 #include <io_mgr.h>
47 #include "board_netlist_updater.h"
48 #include <tool/tool_manager.h>
49 #include <tools/pcb_actions.h>
50 #include <tools/selection_tool.h>
51 #include <view/view.h>
52 
53 extern void SpreadFootprints( std::vector<MODULE*>* aFootprints,
54  wxPoint aSpreadAreaPosition );
55 
56 
57 bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename,
58  NETLIST& aNetlist,
59  REPORTER& aReporter )
60 {
61  wxString msg;
62 
63  try
64  {
65  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
66  &aNetlist, aFilename, wxEmptyString ) );
67 
68  if( !netlistReader.get() )
69  {
70  msg.Printf( _( "Cannot open netlist file \"%s\"." ), GetChars( aFilename ) );
71  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
72  return false;
73  }
74 
75  SetLastPath( LAST_PATH_NETLIST, aFilename );
76  netlistReader->LoadNetlist();
77  LoadFootprints( aNetlist, aReporter );
78  }
79  catch( const IO_ERROR& ioe )
80  {
81  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
82  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
83  return false;
84  }
85 
86  SetLastPath( LAST_PATH_NETLIST, aFilename );
87 
88  return true;
89 }
90 
91 
92 void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
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<MODULE*> newFootprints = aUpdater.GetAddedComponents();
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 modules
114  if( !newFootprints.empty() )
115  {
116  for( MODULE* 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  SELECTION_TOOL* selTool = GetToolManager()->GetTool<SELECTION_TOOL>();
125  PCBNEW_SELECTION& selection = selTool->GetSelection();
126  selection.SetReferencePoint( newFootprints[0]->GetPosition() );
127  }
128 
129  GetCanvas()->Refresh();
130 }
131 
132 
133 #define ALLOW_PARTIAL_FPID 1
134 
135 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
136 {
137  wxString msg;
138  LIB_ID lastFPID;
139  COMPONENT* component;
140  MODULE* module = nullptr;
141  MODULE* fpOnBoard;
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 #if ALLOW_PARTIAL_FPID
153  // The FPID is ok as long as there is a footprint portion coming from eeschema.
154  if( !component->GetFPID().GetLibItemName().size() )
155 #else
156  if( component->GetFPID().empty() )
157 #endif
158  {
159  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
160  component->GetReference() );
161  aReporter.Report( msg, RPT_SEVERITY_ERROR );
162 
163  continue;
164  }
165 
166  // Check if component footprint is already on BOARD and only load the footprint from
167  // the library if it's needed. Nickname can be blank.
168  if( aNetlist.IsFindByTimeStamp() )
169  fpOnBoard = m_Pcb->FindModuleByPath( aNetlist.GetComponent( ii )->GetPath() );
170  else
171  fpOnBoard = m_Pcb->FindModuleByReference( aNetlist.GetComponent( ii )->GetReference() );
172 
173  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
174 
175  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
176  {
177  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
178  component->GetReference(),
179  GetChars( fpOnBoard->GetFPID().Format() ),
180  GetChars( component->GetFPID().Format() ) );
181  aReporter.Report( msg, RPT_SEVERITY_WARNING );
182 
183  continue;
184  }
185 
186  if( !aNetlist.GetReplaceFootprints() )
187  footprintMisMatch = false;
188 
189  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
190  continue;
191 
192  if( component->GetFPID() != lastFPID )
193  {
194  module = NULL;
195 
196 #if ALLOW_PARTIAL_FPID
197  // The LIB_ID is ok as long as there is a footprint portion coming
198  // the library if it's needed. Nickname can be blank.
199  if( !component->GetFPID().GetLibItemName().size() )
200 #else
201  if( !component->GetFPID().IsValid() )
202 #endif
203  {
204  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
205  component->GetReference(),
206  GetChars( component->GetFPID().Format() ) );
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  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
214 
215  if( module )
216  {
217  lastFPID = component->GetFPID();
218  }
219  else
220  {
221  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
222  component->GetReference(),
223  GetChars( component->GetFPID().GetLibItemName() ) );
224  aReporter.Report( msg, RPT_SEVERITY_ERROR );
225 
226  continue;
227  }
228  }
229  else
230  {
231  // Footprint already loaded from a library, duplicate it (faster)
232  if( module == NULL )
233  continue; // Module does not exist in any library.
234 
235  module = new MODULE( *module );
236  }
237 
238  if( module )
239  component->SetModule( module );
240  }
241 }
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:755
void SortByFPID()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
This file is part of the common library.
BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
PROJECT & Prj()
Definition: kicad.cpp:317
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:311
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Function loadFootprints loads the footprints for each COMPONENT in aNetlist from the list of librarie...
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.
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:254
const LIB_ID & GetFPID() const
Definition: class_module.h:219
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:62
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.
bool IsValid() const
Definition: lib_id.h:171
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...
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
bool empty() const
Definition: lib_id.h:186
PCBNEW_SELECTION & GetSelection()
Function GetSelection()
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:164
void SetReferencePoint(const VECTOR2I &aP)
Definition: selection.h:244
NETLIST stores all of information read from a netlist along with the flags used to update the NETLIST...
Definition: pcb_netlist.h:212
#define NULL
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
const wxString & GetReference() const
Definition: pcb_netlist.h:149
UTF8 Format() const
Definition: lib_id.cpp:237
COMPONENT is used to store components and all of their related information found in a netlist.
Definition: pcb_netlist.h:80
BOARD * GetBoard()
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:427
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:263
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
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:163
#define _(s)
Definition: 3d_actions.cpp:33
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:318
static TOOL_ACTION selectItem
Selects an item (specified as the event parameter).
Definition: pcb_actions.h:76
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
bool IsEmpty() const
Function IsEmpty()
Definition: pcb_netlist.h:242
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:47
Module description (excepted pads)
std::string::size_type size() const
Definition: utf8.h:115
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:168
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.
TRACKS & Tracks()
Definition: class_board.h:220
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.