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 <pcb_io/pcb_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#include <project_pcb.h>
49
50
51bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
52 REPORTER& aReporter )
53{
54 wxString msg;
55
56 try
57 {
58 std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
59 &aNetlist, aFilename, wxEmptyString ) );
60
61 if( !netlistReader.get() )
62 {
63 msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
64 DisplayErrorMessage( this, msg );
65 return false;
66 }
67
68 SetLastPath( LAST_PATH_NETLIST, aFilename );
69 netlistReader->LoadNetlist();
70 LoadFootprints( aNetlist, aReporter );
71 }
72 catch( const IO_ERROR& ioe )
73 {
74 msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
75 DisplayErrorMessage( this, msg );
76 return false;
77 }
78
79 SetLastPath( LAST_PATH_NETLIST, aFilename );
80
81 return true;
82}
83
84
85void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
86{
87 BOARD* board = GetBoard();
88
89 SetMsgPanel( board );
90
91 // Update rendered track/via/pad net labels, and any text items that might reference a
92 // netName or netClass
93 int netNamesCfg = GetPcbNewSettings()->m_Display.m_NetNames;
94
96 [&]( KIGFX::VIEW_ITEM* aItem ) -> int
97 {
98 if( dynamic_cast<PCB_TRACK*>( aItem ) )
99 {
100 if( netNamesCfg == 2 || netNamesCfg == 3 )
101 return KIGFX::REPAINT;
102 }
103 else if( dynamic_cast<PAD*>( aItem ) )
104 {
105 if( netNamesCfg == 1 || netNamesCfg == 3 )
106 return KIGFX::REPAINT;
107 }
108
109 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
110
111 if( text && text->HasTextVars() )
112 {
113 text->ClearRenderCache();
114 text->ClearBoundingBoxCache();
116 }
117
118 return 0;
119 } );
120
121 // Spread new footprints.
122 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
123
125
126 SpreadFootprints( &newFootprints, { 0, 0 }, true );
127
128 // Start drag command for new footprints
129 if( !newFootprints.empty() )
130 {
131 EDA_ITEMS items;
132 std::copy( newFootprints.begin(), newFootprints.end(), std::back_inserter( items ) );
134
135 *aRunDragCommand = true;
136 }
137
138 Compile_Ratsnest( true );
139
140 GetCanvas()->Refresh();
141}
142
143
145{
146 wxString msg;
147 LIB_ID lastFPID;
148 COMPONENT* component;
149 FOOTPRINT* footprint = nullptr;
150 FOOTPRINT* fpOnBoard = nullptr;
151
152 if( aNetlist.IsEmpty() || PROJECT_PCB::PcbFootprintLibs( &Prj() )->IsEmpty() )
153 return;
154
155 aNetlist.SortByFPID();
156
157 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
158 {
159 component = aNetlist.GetComponent( ii );
160
161 // The FPID is ok as long as there is a footprint portion coming from eeschema.
162 if( !component->GetFPID().GetLibItemName().size() )
163 {
164 msg.Printf( _( "No footprint defined for symbol %s." ),
165 component->GetReference() );
166 aReporter.Report( msg, RPT_SEVERITY_ERROR );
167
168 continue;
169 }
170
171 // Check if component footprint is already on BOARD and only load the footprint from
172 // the library if it's needed. Nickname can be blank.
173 if( aNetlist.IsFindByTimeStamp() )
174 {
175 for( const KIID& uuid : component->GetKIIDs() )
176 {
177 KIID_PATH path = component->GetPath();
178 path.push_back( uuid );
179
180 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) != nullptr )
181 break;
182 }
183 }
184 else
185 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
186
187 bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
188
189 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
190 {
191 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
192 component->GetReference(),
193 fpOnBoard->GetFPID().Format().wx_str(),
194 component->GetFPID().Format().wx_str() );
195 aReporter.Report( msg, RPT_SEVERITY_WARNING );
196
197 continue;
198 }
199
200 if( !aNetlist.GetReplaceFootprints() )
201 footprintMisMatch = false;
202
203 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
204 continue;
205
206 if( component->GetFPID() != lastFPID )
207 {
208 footprint = nullptr;
209
210 // The LIB_ID is ok as long as there is a footprint portion coming the library if
211 // it's needed. Nickname can be blank.
212 if( !component->GetFPID().GetLibItemName().size() )
213 {
214 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
215 component->GetReference(),
216 component->GetFPID().Format().wx_str() );
217 aReporter.Report( msg, RPT_SEVERITY_ERROR );
218
219 continue;
220 }
221
222 // loadFootprint() can find a footprint with an empty nickname in fpid.
223 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
224
225 if( footprint )
226 {
227 lastFPID = component->GetFPID();
228 }
229 else
230 {
231 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
232 "library table." ),
233 component->GetReference(),
234 component->GetFPID().GetLibItemName().wx_str() );
235 aReporter.Report( msg, RPT_SEVERITY_ERROR );
236
237 continue;
238 }
239 }
240 else
241 {
242 // Footprint already loaded from a library, duplicate it (faster)
243 if( !footprint )
244 continue; // Footprint does not exist in any library.
245
246 footprint = new FOOTPRINT( *footprint );
247 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
248 }
249
250 if( footprint )
251 component->SetFootprint( footprint );
252 }
253}
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:281
FOOTPRINT * FindFootprintByPath(const KIID_PATH &aPath) const
Search for a FOOTPRINT within this board with the given path.
Definition: board.cpp:1902
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:1890
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
const KIID m_Uuid
Definition: eda_item.h:485
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:83
const LIB_ID & GetFPID() const
Definition: footprint.h:233
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
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:84
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:1531
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:59
DISPLAY_OPTIONS m_Display
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: pcb_actions.h:68
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: pcb_actions.h:76
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:51
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
Definition: netlist.cpp:85
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each #SCH_COMPONENT in aNetlist from the list of libraries.
Definition: netlist.cpp:144
static FP_LIB_TABLE * PcbFootprintLibs(PROJECT *aProject)
Return the table of footprint libraries without Kiway.
Definition: project_pcb.cpp:37
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:111
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:305
This file is part of the common library.
#define _(s)
std::vector< EDA_ITEM * > EDA_ITEMS
Define list of drawing items for screens.
Definition: eda_item.h:532
PROJECT & Prj()
Definition: kicad.cpp:595
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:57
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:54
@ 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...