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/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 <project/project_file.h> // LAST_PATH_TYPE
52 #include <view/view.h>
53 
54 extern void SpreadFootprints( std::vector<MODULE*>* aFootprints,
55  wxPoint aSpreadAreaPosition );
56 
57 
58 bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename,
59  NETLIST& aNetlist,
60  REPORTER& aReporter )
61 {
62  wxString msg;
63 
64  try
65  {
66  std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
67  &aNetlist, aFilename, wxEmptyString ) );
68 
69  if( !netlistReader.get() )
70  {
71  msg.Printf( _( "Cannot open netlist file \"%s\"." ), GetChars( aFilename ) );
72  wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
73  return false;
74  }
75 
76  SetLastPath( LAST_PATH_NETLIST, aFilename );
77  netlistReader->LoadNetlist();
78  LoadFootprints( aNetlist, aReporter );
79  }
80  catch( const IO_ERROR& ioe )
81  {
82  msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
83  wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
84  return false;
85  }
86 
87  SetLastPath( LAST_PATH_NETLIST, aFilename );
88 
89  return true;
90 }
91 
92 
93 void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
94 {
95  BOARD* board = GetBoard();
96 
97  SetMsgPanel( board );
98 
99  // Update rendered tracks and vias net labels
100  // TODO is there a way to extract information about which nets were modified?
101  for( auto track : board->Tracks() )
102  GetCanvas()->GetView()->Update( track );
103 
104  std::vector<MODULE*> newFootprints = aUpdater.GetAddedComponents();
105 
106  // Spread new footprints.
107  wxPoint areaPosition = (wxPoint) GetCanvas()->GetViewControls()->GetCursorPosition();
108  EDA_RECT bbox = board->GetBoundingBox();
109 
110  GetToolManager()->RunAction( PCB_ACTIONS::selectionClear, true );
111 
112  SpreadFootprints( &newFootprints, areaPosition );
113 
114  // Start drag command for new modules
115  if( !newFootprints.empty() )
116  {
117  for( MODULE* footprint : newFootprints )
118  GetToolManager()->RunAction( PCB_ACTIONS::selectItem, true, footprint );
119 
120  *aRunDragCommand = true;
121 
122  // Now fix a reference point to move the footprints.
123  // We use the first footprint in list as reference point
124  // The graphic cursor will be on this fp when moving the footprints.
125  SELECTION_TOOL* selTool = GetToolManager()->GetTool<SELECTION_TOOL>();
126  PCBNEW_SELECTION& selection = selTool->GetSelection();
127  selection.SetReferencePoint( newFootprints[0]->GetPosition() );
128  }
129 
130  GetCanvas()->Refresh();
131 }
132 
133 
134 #define ALLOW_PARTIAL_FPID 1
135 
136 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
137 {
138  wxString msg;
139  LIB_ID lastFPID;
140  COMPONENT* component;
141  MODULE* module = nullptr;
142  MODULE* fpOnBoard;
143 
144  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
145  return;
146 
147  aNetlist.SortByFPID();
148 
149  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
150  {
151  component = aNetlist.GetComponent( ii );
152 
153 #if ALLOW_PARTIAL_FPID
154  // The FPID is ok as long as there is a footprint portion coming from eeschema.
155  if( !component->GetFPID().GetLibItemName().size() )
156 #else
157  if( component->GetFPID().empty() )
158 #endif
159  {
160  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
161  component->GetReference() );
162  aReporter.Report( msg, RPT_SEVERITY_ERROR );
163 
164  continue;
165  }
166 
167  // Check if component footprint is already on BOARD and only load the footprint from
168  // the library if it's needed. Nickname can be blank.
169  if( aNetlist.IsFindByTimeStamp() )
170  fpOnBoard = m_Pcb->FindModuleByPath( aNetlist.GetComponent( ii )->GetPath() );
171  else
172  fpOnBoard = m_Pcb->FindModuleByReference( aNetlist.GetComponent( ii )->GetReference() );
173 
174  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
175 
176  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
177  {
178  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
179  component->GetReference(),
180  GetChars( fpOnBoard->GetFPID().Format() ),
181  GetChars( component->GetFPID().Format() ) );
182  aReporter.Report( msg, RPT_SEVERITY_WARNING );
183 
184  continue;
185  }
186 
187  if( !aNetlist.GetReplaceFootprints() )
188  footprintMisMatch = false;
189 
190  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
191  continue;
192 
193  if( component->GetFPID() != lastFPID )
194  {
195  module = NULL;
196 
197 #if ALLOW_PARTIAL_FPID
198  // The LIB_ID is ok as long as there is a footprint portion coming
199  // the library if it's needed. Nickname can be blank.
200  if( !component->GetFPID().GetLibItemName().size() )
201 #else
202  if( !component->GetFPID().IsValid() )
203 #endif
204  {
205  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
206  component->GetReference(),
207  GetChars( component->GetFPID().Format() ) );
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  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
215 
216  if( module )
217  {
218  lastFPID = component->GetFPID();
219  }
220  else
221  {
222  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
223  component->GetReference(),
224  GetChars( component->GetFPID().GetLibItemName() ) );
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( module == NULL )
234  continue; // Module does not exist in any library.
235 
236  module = new MODULE( *module );
237  }
238 
239  if( module )
240  component->SetModule( module );
241  }
242 }
static TOOL_ACTION selectionClear
Clears the current selection.
Definition: pcb_actions.h:62
const EDA_RECT GetBoundingBox() const override
Function GetBoundingBox returns the orthogonal, bounding box of this object for display purposes.
Definition: class_board.h:781
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:215
REPORTER is 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.
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:245
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:284
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:153
see class PGM_BASE
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:180
#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:65
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
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:257
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.