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 <drc/drc_engine.h>
33#include <pcb_edit_frame.h>
36#include <reporter.h>
37#include <lib_id.h>
38#include <fp_lib_table.h>
39#include <board.h>
40#include <footprint.h>
41#include <spread_footprints.h>
43#include <pcb_io/pcb_io_mgr.h>
45#include <tool/tool_manager.h>
46#include <tools/drc_tool.h>
47#include <tools/pcb_actions.h>
49#include <project/project_file.h> // LAST_PATH_TYPE
50#include <project_pcb.h>
51
52
53bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
54 REPORTER& aReporter )
55{
56 wxString msg;
57
58 try
59 {
60 std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
61 &aNetlist, aFilename, wxEmptyString ) );
62
63 if( !netlistReader.get() )
64 {
65 msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
66 DisplayErrorMessage( this, msg );
67 return false;
68 }
69
70 SetLastPath( LAST_PATH_NETLIST, aFilename );
71 netlistReader->LoadNetlist();
72 LoadFootprints( aNetlist, aReporter );
73 }
74 catch( const IO_ERROR& ioe )
75 {
76 msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
77 DisplayErrorMessage( this, msg );
78 return false;
79 }
80
81 SetLastPath( LAST_PATH_NETLIST, aFilename );
82
83 return true;
84}
85
86
87void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
88{
89 BOARD* board = GetBoard();
90
91 SetMsgPanel( board );
92
93 // Re-sync nets, netclasses, and DRC rules to account for new aggregate netclass rules
94 board->SynchronizeNetsAndNetClasses( false );
95
97
98 try
99 {
100 drcTool->GetDRCEngine()->InitEngine( GetDesignRulesPath() );
101 }
102 catch( PARSE_ERROR& )
103 {
104 }
105
106 // Update rendered track/via/pad net labels, and any text items that might reference a
107 // netName or netClass
108 int netNamesCfg = GetPcbNewSettings()->m_Display.m_NetNames;
109
111 [&]( KIGFX::VIEW_ITEM* aItem ) -> int
112 {
113 if( dynamic_cast<PCB_TRACK*>( aItem ) )
114 {
115 if( netNamesCfg == 2 || netNamesCfg == 3 )
116 return KIGFX::REPAINT;
117 }
118 else if( dynamic_cast<PAD*>( aItem ) )
119 {
120 if( netNamesCfg == 1 || netNamesCfg == 3 )
121 return KIGFX::REPAINT;
122 }
123
124 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
125
126 if( text && text->HasTextVars() )
127 {
128 text->ClearRenderCache();
129 text->ClearBoundingBoxCache();
131 }
132
133 return 0;
134 } );
135
136 // Spread new footprints.
137 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
138
140
141 SpreadFootprints( &newFootprints, { 0, 0 }, true );
142
143 // Start drag command for new footprints
144 if( !newFootprints.empty() )
145 {
146 EDA_ITEMS items;
147 std::copy( newFootprints.begin(), newFootprints.end(), std::back_inserter( items ) );
149
150 *aRunDragCommand = true;
151 }
152
153 Compile_Ratsnest( true );
154
155 GetCanvas()->Refresh();
156}
157
158
160{
161 wxString msg;
162 LIB_ID lastFPID;
163 COMPONENT* component;
164 FOOTPRINT* footprint = nullptr;
165 FOOTPRINT* fpOnBoard = nullptr;
166
167 if( aNetlist.IsEmpty() || PROJECT_PCB::PcbFootprintLibs( &Prj() )->IsEmpty() )
168 return;
169
170 aNetlist.SortByFPID();
171
172 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
173 {
174 component = aNetlist.GetComponent( ii );
175
176 // The FPID is ok as long as there is a footprint portion coming from eeschema.
177 if( !component->GetFPID().GetLibItemName().size() )
178 {
179 msg.Printf( _( "No footprint defined for symbol %s." ),
180 component->GetReference() );
181 aReporter.Report( msg, RPT_SEVERITY_ERROR );
182
183 continue;
184 }
185
186 // Check if component footprint is already on BOARD and only load the footprint from
187 // the library if it's needed. Nickname can be blank.
188 if( aNetlist.IsFindByTimeStamp() )
189 {
190 for( const KIID& uuid : component->GetKIIDs() )
191 {
192 KIID_PATH path = component->GetPath();
193 path.push_back( uuid );
194
195 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) != nullptr )
196 break;
197 }
198 }
199 else
200 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
201
202 bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
203
204 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
205 {
206 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
207 component->GetReference(),
208 fpOnBoard->GetFPID().Format().wx_str(),
209 component->GetFPID().Format().wx_str() );
210 aReporter.Report( msg, RPT_SEVERITY_WARNING );
211
212 continue;
213 }
214
215 if( !aNetlist.GetReplaceFootprints() )
216 footprintMisMatch = false;
217
218 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
219 continue;
220
221 if( component->GetFPID() != lastFPID )
222 {
223 footprint = nullptr;
224
225 // The LIB_ID is ok as long as there is a footprint portion coming the library if
226 // it's needed. Nickname can be blank.
227 if( !component->GetFPID().GetLibItemName().size() )
228 {
229 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
230 component->GetReference(),
231 component->GetFPID().Format().wx_str() );
232 aReporter.Report( msg, RPT_SEVERITY_ERROR );
233
234 continue;
235 }
236
237 // loadFootprint() can find a footprint with an empty nickname in fpid.
238 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
239
240 if( footprint )
241 {
242 lastFPID = component->GetFPID();
243 }
244 else
245 {
246 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
247 "library table." ),
248 component->GetReference(),
249 component->GetFPID().GetLibItemName().wx_str() );
250 aReporter.Report( msg, RPT_SEVERITY_ERROR );
251
252 continue;
253 }
254 }
255 else
256 {
257 // Footprint already loaded from a library, duplicate it (faster)
258 if( !footprint )
259 continue; // Footprint does not exist in any library.
260
261 footprint = new FOOTPRINT( *footprint );
262 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
263 }
264
265 if( footprint )
266 component->SetFootprint( footprint );
267 }
268}
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:2051
FOOTPRINT * FindFootprintByPath(const KIID_PATH &aPath) const
Search for a FOOTPRINT within this board with the given path.
Definition: board.cpp:2016
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:2004
Store all of the related footprint information found in a netlist.
Definition: pcb_netlist.h:88
const KIID_PATH & GetPath() const
Definition: pcb_netlist.h:152
const wxString & GetReference() const
Definition: pcb_netlist.h:129
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:42
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:154
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:147
std::shared_ptr< DRC_ENGINE > GetDRCEngine()
Definition: drc_tool.h:78
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:489
A mix-in class (via multiple inheritance) that handles texts such as labels, parts,...
Definition: eda_text.h:79
const LIB_ID & GetFPID() const
Definition: footprint.h:248
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:1573
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:241
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:305
void SortByFPID()
unsigned GetCount() const
Definition: pcb_netlist.h:262
bool IsEmpty() const
Definition: pcb_netlist.h:252
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:270
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:302
Definition: pad.h:54
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
wxString GetDesignRulesPath()
Return the absolute path to the design rules file for the currently-loaded board.
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:53
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
Definition: netlist.cpp:87
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each #SCH_COMPONENT in aNetlist from the list of libraries.
Definition: netlist.cpp:159
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:72
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:167
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:150
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:195
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:536
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:50
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...
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:120