KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <dick@softplc.com>
6 * Copyright (C) 2013-2016 Wayne Stambaugh <stambaughw@verizon.net>
7 * Copyright The KiCad Developers, see AUTHORS.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 and netclasses
94 board->SynchronizeNetsAndNetClasses( false );
95
96 // Recompute component classes
99
100 // Resync DRC rules to account for new aggregate netclass / component class rules
101 DRC_TOOL* drcTool = m_toolManager->GetTool<DRC_TOOL>();
102
103 try
104 {
105 drcTool->GetDRCEngine()->InitEngine( GetDesignRulesPath() );
106 }
107 catch( PARSE_ERROR& )
108 {
109 }
110
111 // Update rendered track/via/pad net labels, and any text items that might reference a
112 // netName or netClass
113 int netNamesCfg = GetPcbNewSettings()->m_Display.m_NetNames;
114
116 [&]( KIGFX::VIEW_ITEM* aItem ) -> int
117 {
118 if( dynamic_cast<PCB_TRACK*>( aItem ) )
119 {
120 if( netNamesCfg == 2 || netNamesCfg == 3 )
121 return KIGFX::REPAINT;
122 }
123 else if( dynamic_cast<PAD*>( aItem ) )
124 {
125 if( netNamesCfg == 1 || netNamesCfg == 3 )
126 return KIGFX::REPAINT;
127 }
128
129 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
130
131 if( text && text->HasTextVars() )
132 {
133 text->ClearRenderCache();
134 text->ClearBoundingBoxCache();
136 }
137
138 return 0;
139 } );
140
141 // Spread new footprints.
142 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
143
145
146 SpreadFootprints( &newFootprints, { 0, 0 }, true );
147
148 // Start drag command for new footprints
149 if( !newFootprints.empty() )
150 {
151 EDA_ITEMS items;
152 std::copy( newFootprints.begin(), newFootprints.end(), std::back_inserter( items ) );
154
155 *aRunDragCommand = true;
156 }
157
158 Compile_Ratsnest( true );
159
160 GetCanvas()->Refresh();
161}
162
163
165{
166 wxString msg;
167 LIB_ID lastFPID;
168 COMPONENT* component;
169 FOOTPRINT* footprint = nullptr;
170 FOOTPRINT* fpOnBoard = nullptr;
171
172 if( aNetlist.IsEmpty() || PROJECT_PCB::PcbFootprintLibs( &Prj() )->IsEmpty() )
173 return;
174
175 aNetlist.SortByFPID();
176
177 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
178 {
179 component = aNetlist.GetComponent( ii );
180
181 // The FPID is ok as long as there is a footprint portion coming from eeschema.
182 if( !component->GetFPID().GetLibItemName().size() )
183 {
184 msg.Printf( _( "No footprint defined for symbol %s." ),
185 component->GetReference() );
186 aReporter.Report( msg, RPT_SEVERITY_ERROR );
187
188 continue;
189 }
190
191 // Check if component footprint is already on BOARD and only load the footprint from
192 // the library if it's needed. Nickname can be blank.
193 if( aNetlist.IsFindByTimeStamp() )
194 {
195 for( const KIID& uuid : component->GetKIIDs() )
196 {
197 KIID_PATH path = component->GetPath();
198 path.push_back( uuid );
199
200 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) != nullptr )
201 break;
202 }
203 }
204 else
205 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
206
207 bool footprintMisMatch = fpOnBoard && fpOnBoard->GetFPID() != component->GetFPID();
208
209 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
210 {
211 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
212 component->GetReference(),
213 fpOnBoard->GetFPID().Format().wx_str(),
214 component->GetFPID().Format().wx_str() );
215 aReporter.Report( msg, RPT_SEVERITY_WARNING );
216
217 continue;
218 }
219
220 if( !aNetlist.GetReplaceFootprints() )
221 footprintMisMatch = false;
222
223 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
224 continue;
225
226 if( component->GetFPID() != lastFPID )
227 {
228 footprint = nullptr;
229
230 // The LIB_ID is ok as long as there is a footprint portion coming the library if
231 // it's needed. Nickname can be blank.
232 if( !component->GetFPID().GetLibItemName().size() )
233 {
234 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
235 component->GetReference(),
236 component->GetFPID().Format().wx_str() );
237 aReporter.Report( msg, RPT_SEVERITY_ERROR );
238
239 continue;
240 }
241
242 // loadFootprint() can find a footprint with an empty nickname in fpid.
243 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
244
245 if( footprint )
246 {
247 lastFPID = component->GetFPID();
248 }
249 else
250 {
251 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
252 "library table." ),
253 component->GetReference(),
254 component->GetFPID().GetLibItemName().wx_str() );
255 aReporter.Report( msg, RPT_SEVERITY_ERROR );
256
257 continue;
258 }
259 }
260 else
261 {
262 // Footprint already loaded from a library, duplicate it (faster)
263 if( !footprint )
264 continue; // Footprint does not exist in any library.
265
266 footprint = new FOOTPRINT( *footprint );
267 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
268 }
269
270 if( footprint )
271 component->SetFootprint( footprint );
272 }
273}
static TOOL_ACTION selectionClear
Clear the current selection.
Definition: actions.h:220
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition: actions.h:228
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:297
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition: board.cpp:2144
FOOTPRINT * FindFootprintByPath(const KIID_PATH &aPath) const
Search for a FOOTPRINT within this board with the given path.
Definition: board.cpp:2109
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition: board.cpp:2097
COMPONENT_CLASS_MANAGER & GetComponentClassManager()
Gets the component class manager.
Definition: board.h:1316
void InvalidateComponentClasses()
Invalidates any caches component classes and recomputes caches if required.
void RebuildRequiredCaches(FOOTPRINT *aFootprint=nullptr) const
Rebuilds any caches that may be required by custom assignment rules.
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:153
const wxString & GetReference() const
Definition: pcb_netlist.h:130
void SetFootprint(FOOTPRINT *aFootprint)
Definition: pcb_netlist.cpp:42
const std::vector< KIID > & GetKIIDs() const
Definition: pcb_netlist.h:155
const LIB_ID & GetFPID() const
Definition: pcb_netlist.h:148
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:498
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:253
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:86
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:1559
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:119
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:255
bool GetReplaceFootprints() const
Definition: pcb_netlist.h:319
void SortByFPID()
unsigned GetCount() const
Definition: pcb_netlist.h:276
bool IsEmpty() const
Definition: pcb_netlist.h:266
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
Definition: pcb_netlist.h:284
bool IsFindByTimeStamp() const
Definition: pcb_netlist.h:316
Definition: pad.h:54
DISPLAY_OPTIONS m_Display
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)
Attempt 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:164
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:73
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition: reporter.h:102
TOOL_MANAGER * m_toolManager
Definition: tools_holder.h:171
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:194
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:548
PROJECT & Prj()
Definition: kicad.cpp:597
@ REPAINT
Item needs to be redrawn.
Definition: view_item.h:58
@ GEOMETRY
Position or shape has changed.
Definition: view_item.h:55
@ LAST_PATH_NETLIST
Definition: project_file.h:51
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