KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <confirm.h>
31#include <kiway.h>
32#include <pcb_edit_frame.h>
35#include <reporter.h>
36#include <lib_id.h>
37#include <fp_lib_table.h>
38#include <board.h>
39#include <footprint.h>
40#include <spread_footprints.h>
42#include <io_mgr.h>
44#include <tool/tool_manager.h>
45#include <tools/pcb_actions.h>
47#include <project/project_file.h> // LAST_PATH_TYPE
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 DisplayErrorMessage( this, msg );
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 DisplayErrorMessage( this, msg );
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 track/via/pad net labels, and any text items that might reference a
91 // netName or netClass
92 int netNamesCfg = GetPcbNewSettings()->m_Display.m_NetNames;
93
95 [&]( KIGFX::VIEW_ITEM* aItem ) -> int
96 {
97 if( dynamic_cast<PCB_TRACK*>( aItem ) )
98 {
99 if( netNamesCfg == 2 || netNamesCfg == 3 )
100 return KIGFX::REPAINT;
101 }
102 else if( dynamic_cast<PAD*>( aItem ) )
103 {
104 if( netNamesCfg == 1 || netNamesCfg == 3 )
105 return KIGFX::REPAINT;
106 }
107
108 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
109
110 if( text && text->HasTextVars() )
111 {
112 text->ClearRenderCache();
113 text->ClearBoundingBoxCache();
115 }
116
117 return 0;
118 } );
119
120 // Spread new footprints.
121 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
122
124
125 SpreadFootprints( &newFootprints, { 0, 0 }, true );
126
127 // Start drag command for new footprints
128 if( !newFootprints.empty() )
129 {
130 for( FOOTPRINT* footprint : newFootprints )
132
133 *aRunDragCommand = true;
134 }
135
136 Compile_Ratsnest( true );
137
138 GetCanvas()->Refresh();
139}
140
141
143{
144 wxString msg;
145 LIB_ID lastFPID;
146 COMPONENT* component;
147 FOOTPRINT* footprint = nullptr;
148 FOOTPRINT* fpOnBoard = nullptr;
149
150 if( aNetlist.IsEmpty() || Prj().PcbFootprintLibs()->IsEmpty() )
151 return;
152
153 aNetlist.SortByFPID();
154
155 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
156 {
157 component = aNetlist.GetComponent( ii );
158
159 // The FPID is ok as long as there is a footprint portion coming from eeschema.
160 if( !component->GetFPID().GetLibItemName().size() )
161 {
162 msg.Printf( _( "No footprint defined for symbol %s." ),
163 component->GetReference() );
164 aReporter.Report( msg, RPT_SEVERITY_ERROR );
165
166 continue;
167 }
168
169 // Check if component footprint is already on BOARD and only load the footprint from
170 // the library if it's needed. Nickname can be blank.
171 if( aNetlist.IsFindByTimeStamp() )
172 {
173 for( const KIID& uuid : component->GetKIIDs() )
174 {
175 KIID_PATH path = component->GetPath();
176 path.push_back( uuid );
177
178 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) != nullptr )
179 break;
180 }
181 }
182 else
183 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
184
185 bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
186
187 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
188 {
189 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
190 component->GetReference(),
191 fpOnBoard->GetFPID().Format().wx_str(),
192 component->GetFPID().Format().wx_str() );
193 aReporter.Report( msg, RPT_SEVERITY_WARNING );
194
195 continue;
196 }
197
198 if( !aNetlist.GetReplaceFootprints() )
199 footprintMisMatch = false;
200
201 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
202 continue;
203
204 if( component->GetFPID() != lastFPID )
205 {
206 footprint = nullptr;
207
208 // The LIB_ID is ok as long as there is a footprint portion coming the library if
209 // it's needed. Nickname can be blank.
210 if( !component->GetFPID().GetLibItemName().size() )
211 {
212 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
213 component->GetReference(),
214 component->GetFPID().Format().wx_str() );
215 aReporter.Report( msg, RPT_SEVERITY_ERROR );
216
217 continue;
218 }
219
220 // loadFootprint() can find a footprint with an empty nickname in fpid.
221 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
222
223 if( footprint )
224 {
225 lastFPID = component->GetFPID();
226 }
227 else
228 {
229 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
230 "library table." ),
231 component->GetReference(),
232 component->GetFPID().GetLibItemName().wx_str() );
233 aReporter.Report( msg, RPT_SEVERITY_ERROR );
234
235 continue;
236 }
237 }
238 else
239 {
240 // Footprint already loaded from a library, duplicate it (faster)
241 if( !footprint )
242 continue; // Footprint does not exist in any library.
243
244 footprint = new FOOTPRINT( *footprint );
245 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
246 }
247
248 if( footprint )
249 component->SetFootprint( footprint );
250 }
251}
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
FOOTPRINT * FindFootprintByPath(const KIID_PATH &aPath) const
Search for a FOOTPRINT within this board with the given path.
Definition: board.cpp:1595
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:1583
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:86
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:150
const wxString & GetReference() const
Definition: pcb_netlist.h:127
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:40
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:152
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:145
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
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
const KIID m_Uuid
Definition: eda_item.h:482
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:80
const LIB_ID & GetFPID() const
Definition: footprint.h:230
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
An abstract base class for deriving all objects that can be added to a VIEW.
Definition: view_item.h:77
void UpdateAllItemsConditionally(int aUpdateFlags, std::function< bool(VIEW_ITEM *)> aCondition)
Update items in the view according to the given flags and condition.
Definition: view.cpp:1511
Definition: kiid.h:49
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:118
const UTF8 & GetLibItemName() const
Definition: lib_id.h:102
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:223
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:287
void SortByFPID()
unsigned GetCount() const
Definition: pcb_netlist.h:244
bool IsEmpty() const
Definition: pcb_netlist.h:234
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:252
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:284
Definition: pad.h:58
DISPLAY_OPTIONS m_Display
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:68
static TOOL_ACTION selectItem
Select an item (specified as the event parameter).
Definition: pcb_actions.h:71
PCBNEW_SETTINGS * GetPcbNewSettings() const
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:142
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:55
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
Definition: tool_manager.h:145
std::string::size_type size() const
Definition: utf8.h:110
wxString wx_str() const
Definition: utf8.cpp:45
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:307
This file is part of the common library.
#define _(s)
PROJECT & Prj()
Definition: kicad.cpp:571
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:52
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:49
@ LAST_PATH_NETLIST
Definition: project_file.h:49
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...