KiCad PCB EDA Suite
netlist.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 1992-2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2013-2016 Wayne Stambaugh <[email protected]>
7 * Copyright (C) 1992-2022 KiCad Developers, see change_log.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <functional>
28using namespace std::placeholders;
29
30#include <kiway.h>
31#include <pcb_edit_frame.h>
34#include <reporter.h>
35#include <lib_id.h>
36#include <fp_lib_table.h>
37#include <board.h>
38#include <footprint.h>
39#include <spread_footprints.h>
41#include <io_mgr.h>
43#include <tool/tool_manager.h>
44#include <tools/pcb_actions.h>
46#include <project/project_file.h> // LAST_PATH_TYPE
47#include <wx/msgdlg.h>
48
49
50bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
51 REPORTER& aReporter )
52{
53 wxString msg;
54
55 try
56 {
57 std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
58 &aNetlist, aFilename, wxEmptyString ) );
59
60 if( !netlistReader.get() )
61 {
62 msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
63 wxMessageBox( msg, _( "Netlist Load Error." ), wxOK | wxICON_ERROR, this );
64 return false;
65 }
66
67 SetLastPath( LAST_PATH_NETLIST, aFilename );
68 netlistReader->LoadNetlist();
69 LoadFootprints( aNetlist, aReporter );
70 }
71 catch( const IO_ERROR& ioe )
72 {
73 msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
74 wxMessageBox( msg, _( "Netlist Load Error" ), wxOK | wxICON_ERROR );
75 return false;
76 }
77
78 SetLastPath( LAST_PATH_NETLIST, aFilename );
79
80 return true;
81}
82
83
84void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
85{
86 BOARD* board = GetBoard();
87
88 SetMsgPanel( board );
89
90 // Update rendered tracks and vias net labels
91 // TODO is there a way to extract information about which nets were modified?
92 for( auto track : board->Tracks() )
93 GetCanvas()->GetView()->Update( track );
94
95 // Spread new footprints.
96 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
97
99
100 SpreadFootprints( &newFootprints, { 0, 0 }, true );
101
102 // Start drag command for new footprints
103 if( !newFootprints.empty() )
104 {
105 for( FOOTPRINT* footprint : newFootprints )
106 GetToolManager()->RunAction( PCB_ACTIONS::selectItem, true, footprint );
107
108 *aRunDragCommand = true;
109 }
110
111 Compile_Ratsnest( true );
112
113 GetCanvas()->Refresh();
114}
115
116
118{
119 wxString msg;
120 LIB_ID lastFPID;
121 COMPONENT* component;
122 FOOTPRINT* footprint = nullptr;
123 FOOTPRINT* fpOnBoard = nullptr;
124
125 if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
126 return;
127
128 aNetlist.SortByFPID();
129
130 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
131 {
132 component = aNetlist.GetComponent( ii );
133
134 // The FPID is ok as long as there is a footprint portion coming from eeschema.
135 if( !component->GetFPID().GetLibItemName().size() )
136 {
137 msg.Printf( _( "No footprint defined for symbol %s." ),
138 component->GetReference() );
139 aReporter.Report( msg, RPT_SEVERITY_ERROR );
140
141 continue;
142 }
143
144 // Check if component footprint is already on BOARD and only load the footprint from
145 // the library if it's needed. Nickname can be blank.
146 if( aNetlist.IsFindByTimeStamp() )
147 {
148 for( const KIID& uuid : component->GetKIIDs() )
149 {
150 KIID_PATH path = component->GetPath();
151 path.push_back( uuid );
152
153 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) )
154 break;
155 }
156 }
157 else
158 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
159
160 bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
161
162 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
163 {
164 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
165 component->GetReference(),
166 fpOnBoard->GetFPID().Format().wx_str(),
167 component->GetFPID().Format().wx_str() );
168 aReporter.Report( msg, RPT_SEVERITY_WARNING );
169
170 continue;
171 }
172
173 if( !aNetlist.GetReplaceFootprints() )
174 footprintMisMatch = false;
175
176 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
177 continue;
178
179 if( component->GetFPID() != lastFPID )
180 {
181 footprint = nullptr;
182
183 // The LIB_ID is ok as long as there is a footprint portion coming the library if
184 // it's needed. Nickname can be blank.
185 if( !component->GetFPID().GetLibItemName().size() )
186 {
187 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
188 component->GetReference(),
189 component->GetFPID().Format().wx_str() );
190 aReporter.Report( msg, RPT_SEVERITY_ERROR );
191
192 continue;
193 }
194
195 // loadFootprint() can find a footprint with an empty nickname in fpid.
196 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
197
198 if( footprint )
199 {
200 lastFPID = component->GetFPID();
201 }
202 else
203 {
204 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
205 "library table." ),
206 component->GetReference(),
207 component->GetFPID().GetLibItemName().wx_str() );
208 aReporter.Report( msg, RPT_SEVERITY_ERROR );
209
210 continue;
211 }
212 }
213 else
214 {
215 // Footprint already loaded from a library, duplicate it (faster)
216 if( !footprint )
217 continue; // Footprint does not exist in any library.
218
219 footprint = new FOOTPRINT( *footprint );
220 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
221 }
222
223 if( footprint )
224 component->SetFootprint( footprint );
225 }
226}
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:265
FOOTPRINT * FindFootprintByPath(const KIID_PATH &aPath) const
Search for a FOOTPRINT within this board with the given path.
Definition: board.cpp:1435
TRACKS & Tracks()
Definition: board.h:304
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:1423
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:85
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:143
const wxString & GetReference() const
Definition: pcb_netlist.h:126
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:40
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:145
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:138
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
const KIID m_Uuid
Definition: eda_item.h:492
const LIB_ID & GetFPID() const
Definition: footprint.h:203
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition: pcb_view.cpp:92
Definition: kiid.h:48
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
UTF8 Format() const
Definition: lib_id.cpp:117
const UTF8 & GetLibItemName() const
Definition: lib_id.h:101
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...
Store information read from a netlist along with the flags used to update the NETLIST in the BOARD.
Definition: pcb_netlist.h:213
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:277
void SortByFPID()
unsigned GetCount() const
Definition: pcb_netlist.h:234
bool IsEmpty() const
Definition: pcb_netlist.h:224
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:242
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:274
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:59
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:62
FOOTPRINT * loadFootprint(const LIB_ID &aFootprintId)
Attempts to load aFootprintId from the footprint library table.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
BOARD * GetBoard() const
void Compile_Ratsnest(bool aDisplayStatus)
Create the entire board ratsnest.
Definition: ratsnest.cpp:35
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the #VIEW instance used in the panel.
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
bool ReadNetlistFromFile(const wxString &aFilename, NETLIST &aNetlist, REPORTER &aReporter)
Read a netlist from a file into a NETLIST object.
Definition: netlist.cpp:50
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
Definition: netlist.cpp:84
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each #SCH_COMPONENT in aNetlist from the list of libraries.
Definition: netlist.cpp:117
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:71
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Definition: tools_holder.h:54
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Run the specified action.
Definition: tool_manager.h:142
std::string::size_type size() const
Definition: utf8.h:110
wxString wx_str() const
Definition: utf8.cpp:46
#define _(s)
@ LAST_PATH_NETLIST
Definition: project_file.h:48
Class that computes missing connections on a PCB.
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
void SpreadFootprints(std::vector< FOOTPRINT * > *aFootprints, VECTOR2I aTargetBoxPosition, bool aGroupBySheet, int aComponentGap, int aGroupGap)
Footprints (after loaded by reading a netlist for instance) are moved to be in a small free area (out...