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 <kiway.h>
35 #include <pcb_edit_frame.h>
38 #include <reporter.h>
39 #include <lib_id.h>
40 #include <fp_lib_table.h>
41 #include <class_board.h>
42 #include <class_module.h>
43 #include <ratsnest/ratsnest_data.h>
44 #include <io_mgr.h>
45 #include "board_netlist_updater.h"
46 #include <tool/tool_manager.h>
47 #include <tools/pcb_actions.h>
48 #include <tools/selection_tool.h>
49 #include <project/project_file.h> // LAST_PATH_TYPE
50 //#include <view/view.h>
51 
52 extern void SpreadFootprints( std::vector<MODULE*>* 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\"." ), GetChars( 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<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 void PCB_EDIT_FRAME::LoadFootprints( NETLIST& aNetlist, REPORTER& aReporter )
134 {
135  wxString msg;
136  LIB_ID lastFPID;
137  COMPONENT* component;
138  MODULE* module = nullptr;
139  MODULE* fpOnBoard;
140 
141  if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
142  return;
143 
144  aNetlist.SortByFPID();
145 
146  for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
147  {
148  component = aNetlist.GetComponent( ii );
149 
150  // The FPID is ok as long as there is a footprint portion coming from eeschema.
151  if( !component->GetFPID().GetLibItemName().size() )
152  {
153  msg.Printf( _( "No footprint defined for symbol \"%s\".\n" ),
154  component->GetReference() );
155  aReporter.Report( msg, RPT_SEVERITY_ERROR );
156 
157  continue;
158  }
159 
160  // Check if component footprint is already on BOARD and only load the footprint from
161  // the library if it's needed. Nickname can be blank.
162  if( aNetlist.IsFindByTimeStamp() )
163  fpOnBoard = m_Pcb->FindModuleByPath( aNetlist.GetComponent( ii )->GetPath() );
164  else
165  fpOnBoard = m_Pcb->FindModuleByReference( aNetlist.GetComponent( ii )->GetReference() );
166 
167  bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
168 
169  if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
170  {
171  msg.Printf( _( "Footprint of %s changed: board footprint \"%s\", netlist footprint \"%s\"." ),
172  component->GetReference(),
173  GetChars( fpOnBoard->GetFPID().Format() ),
174  GetChars( component->GetFPID().Format() ) );
175  aReporter.Report( msg, RPT_SEVERITY_WARNING );
176 
177  continue;
178  }
179 
180  if( !aNetlist.GetReplaceFootprints() )
181  footprintMisMatch = false;
182 
183  if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
184  continue;
185 
186  if( component->GetFPID() != lastFPID )
187  {
188  module = nullptr;
189 
190  // The LIB_ID is ok as long as there is a footprint portion coming the library if
191  // it's needed. Nickname can be blank.
192  if( !component->GetFPID().GetLibItemName().size() )
193  {
194  msg.Printf( _( "%s footprint ID \"%s\" is not valid." ),
195  component->GetReference(),
196  GetChars( component->GetFPID().Format() ) );
197  aReporter.Report( msg, RPT_SEVERITY_ERROR );
198 
199  continue;
200  }
201 
202  // loadFootprint() can find a footprint with an empty nickname in fpid.
203  module = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
204 
205  if( module )
206  {
207  lastFPID = component->GetFPID();
208  }
209  else
210  {
211  msg.Printf( _( "%s footprint \"%s\" not found in any libraries in the footprint library table.\n" ),
212  component->GetReference(),
213  GetChars( component->GetFPID().GetLibItemName() ) );
214  aReporter.Report( msg, RPT_SEVERITY_ERROR );
215 
216  continue;
217  }
218  }
219  else
220  {
221  // Footprint already loaded from a library, duplicate it (faster)
222  if( !module )
223  continue; // Module does not exist in any library.
224 
225  module = new MODULE( *module );
226  const_cast<KIID&>( module->m_Uuid ) = KIID();
227  }
228 
229  if( module )
230  component->SetModule( module );
231  }
232 }
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:754
void SortByFPID()
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
BOARD_NETLIST_UPDATER updates the BOARD with a new netlist.
PROJECT & Prj()
Definition: kicad.cpp:318
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:268
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:224
const LIB_ID & GetFPID() const
Definition: class_module.h:221
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.
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.
PCBNEW_SELECTION & GetSelection()
Function GetSelection()
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:157
Definition: common.h:68
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:194
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:29
const wxString & GetReference() const
Definition: pcb_netlist.h:147
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()
const KIID m_Uuid
Definition: base_struct.h:162
VTBL_ENTRY FP_LIB_TABLE * PcbFootprintLibs(KIWAY &aKiway)
Return the table of footprint libraries.
Definition: project.cpp:286
COMPONENT * GetComponent(unsigned aIndex)
Function GetComponent returns the COMPONENT at aIndex.
Definition: pcb_netlist.h:233
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:437
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
BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:178
#define _(s)
Definition: 3d_actions.cpp:33
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:271
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:212
void SetModule(MODULE *aModule)
Definition: pcb_netlist.cpp:40
std::string::size_type size() const
Definition: utf8.h:115
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:162
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:246
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
KIWAY Kiway