KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcbnew/netlist_reader/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 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>
35#include <netlist_reader/netlist_reader.h>
36#include <reporter.h>
37#include <lib_id.h>
39#include <board.h>
41#include <footprint.h>
42#include <spread_footprints.h>
44#include <pcb_io/pcb_io_mgr.h>
46#include <tool/tool_manager.h>
47#include <tools/drc_tool.h>
48#include <tools/pcb_actions.h>
50#include <project/project_file.h> // LAST_PATH_TYPE
51#include <project_pcb.h>
52
53
54bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
55 REPORTER& aReporter )
56{
57 wxString msg;
58
59 try
60 {
61 std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
62 &aNetlist, aFilename, wxEmptyString ) );
63
64 if( !netlistReader.get() )
65 {
66 msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
67 DisplayErrorMessage( this, msg );
68 return false;
69 }
70
71 SetLastPath( LAST_PATH_NETLIST, aFilename );
72 netlistReader->LoadNetlist();
73 LoadFootprints( aNetlist, aReporter );
74 }
75 catch( const IO_ERROR& ioe )
76 {
77 msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
78 DisplayErrorMessage( this, msg );
79 return false;
80 }
81
82 SetLastPath( LAST_PATH_NETLIST, aFilename );
83
84 return true;
85}
86
87
88void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
89{
90 BOARD* board = GetBoard();
91
92 SetMsgPanel( board );
93
94 // Re-sync nets and netclasses
95 board->SynchronizeNetsAndNetClasses( false );
96
97 // Recompute component classes
100
101 // Resync DRC rules to account for new aggregate netclass / component class rules
102 DRC_TOOL* drcTool = m_toolManager->GetTool<DRC_TOOL>();
103
104 try
105 {
107 }
108 catch( PARSE_ERROR& )
109 {
110 }
111
112 // Update rendered track/via/pad net labels, and any text items that might reference a
113 // netName or netClass
114 int netNamesCfg = GetPcbNewSettings()->m_Display.m_NetNames;
115
117 [&]( KIGFX::VIEW_ITEM* aItem ) -> int
118 {
119 if( dynamic_cast<PCB_TRACK*>( aItem ) )
120 {
121 if( netNamesCfg == 2 || netNamesCfg == 3 )
122 return KIGFX::REPAINT;
123 }
124 else if( dynamic_cast<PAD*>( aItem ) )
125 {
126 if( netNamesCfg == 1 || netNamesCfg == 3 )
127 return KIGFX::REPAINT;
128 }
129
130 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
131
132 if( text && text->HasTextVars() )
133 {
134 text->ClearRenderCache();
135 text->ClearBoundingBoxCache();
137 }
138
139 return 0;
140 } );
141
142 // Spread new footprints.
143 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
144
146
147 SpreadFootprints( &newFootprints, { 0, 0 }, true );
148
149 // Start drag command for new footprints
150 if( !newFootprints.empty() )
151 {
152 EDA_ITEMS items;
153 std::copy( newFootprints.begin(), newFootprints.end(), std::back_inserter( items ) );
155
156 *aRunDragCommand = true;
157 }
158
159 Compile_Ratsnest( true );
160
162
163 GetCanvas()->Refresh();
164}
165
166
168{
169 wxString msg;
170 LIB_ID lastFPID;
171 COMPONENT* component;
172 FOOTPRINT* footprint = nullptr;
173 FOOTPRINT* fpOnBoard = nullptr;
174
175 if( aNetlist.IsEmpty() || PROJECT_PCB::FootprintLibAdapter( &Prj() )->Rows().empty() )
176 return;
177
178 aNetlist.SortByFPID();
179
180 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
181 {
182 component = aNetlist.GetComponent( ii );
183
184 // The FPID is ok as long as there is a footprint portion coming from eeschema.
185 if( !component->GetFPID().GetLibItemName().size() )
186 {
187 msg.Printf( _( "No footprint defined for symbol %s." ),
188 component->GetReference() );
189 aReporter.Report( msg, RPT_SEVERITY_ERROR );
190
191 continue;
192 }
193
194 // Check if component footprint is already on BOARD and only load the footprint from
195 // the library if it's needed. Nickname can be blank.
196 if( aNetlist.IsFindByTimeStamp() )
197 {
198 for( const KIID& uuid : component->GetKIIDs() )
199 {
200 KIID_PATH path = component->GetPath();
201 path.push_back( uuid );
202
203 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) != nullptr )
204 break;
205 }
206 }
207 else
208 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
209
210 // When the schematic-side FPID has no library nickname (legacy format), match
211 // only by item name so we don't flag a mismatch against a fully qualified board FPID.
212 bool footprintMisMatch = false;
213
214 if( fpOnBoard )
215 {
216 if( component->GetFPID().IsLegacy() )
217 {
218 footprintMisMatch =
219 fpOnBoard->GetFPID().GetLibItemName() != component->GetFPID().GetLibItemName();
220 }
221 else
222 {
223 footprintMisMatch = fpOnBoard->GetFPID() != component->GetFPID();
224 }
225 }
226
227 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
228 {
229 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
230 component->GetReference(),
231 fpOnBoard->GetFPID().Format().wx_str(),
232 component->GetFPID().Format().wx_str() );
233 aReporter.Report( msg, RPT_SEVERITY_WARNING );
234
235 continue;
236 }
237
238 if( !aNetlist.GetReplaceFootprints() )
239 footprintMisMatch = false;
240
241 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
242 continue;
243
244 if( component->GetFPID() != lastFPID )
245 {
246 footprint = nullptr;
247
248 // The LIB_ID is ok as long as there is a footprint portion coming the library if
249 // it's needed. Nickname can be blank.
250 if( !component->GetFPID().GetLibItemName().size() )
251 {
252 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
253 component->GetReference(),
254 component->GetFPID().Format().wx_str() );
255 aReporter.Report( msg, RPT_SEVERITY_ERROR );
256
257 continue;
258 }
259
260 // loadFootprint() can find a footprint with an empty nickname in fpid.
261 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
262
263 if( footprint )
264 {
265 lastFPID = component->GetFPID();
266 }
267 else
268 {
269 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
270 "library table." ),
271 component->GetReference(),
272 component->GetFPID().GetLibItemName().wx_str() );
273 aReporter.Report( msg, RPT_SEVERITY_ERROR );
274
275 continue;
276 }
277 }
278 else
279 {
280 // Footprint already loaded from a library, duplicate it (faster)
281 if( !footprint )
282 continue; // Footprint does not exist in any library.
283
284 footprint = new FOOTPRINT( *footprint );
285 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
286 }
287
288 if( footprint )
289 component->SetFootprint( footprint );
290 }
291}
static TOOL_ACTION selectionClear
Clear the current selection.
Definition actions.h:224
static TOOL_ACTION selectItems
Select a list of items (specified as the event parameter)
Definition actions.h:232
Update the BOARD with a new netlist.
std::vector< FOOTPRINT * > GetAddedFootprints() const
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
void SynchronizeNetsAndNetClasses(bool aResetTrackAndViaSizes)
Copy NETCLASS info to each NET, based on NET membership in a NETCLASS.
Definition board.cpp:2677
COMPONENT_CLASS_MANAGER & GetComponentClassManager()
Gets the component class manager.
Definition board.h:1407
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 component information found in a netlist.
const KIID_PATH & GetPath() const
const wxString & GetReference() const
void SetFootprint(FOOTPRINT *aFootprint)
const std::vector< KIID > & GetKIIDs() const
const LIB_ID & GetFPID() const
void InitEngine(const wxFileName &aRulePath)
Initialize the DRC engine.
std::shared_ptr< DRC_ENGINE > GetDRCEngine()
Definition drc_tool.h:87
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:522
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:351
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
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:1586
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
bool IsLegacy() const
Definition lib_id.h:180
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.
bool GetReplaceFootprints() const
unsigned GetCount() const
COMPONENT * GetComponent(unsigned aIndex)
Return the COMPONENT at aIndex.
bool IsFindByTimeStamp() const
Definition pad.h:55
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.
void UpdateVariantSelectionCtrl()
Update the variant selection dropdown with the current board's variant names.
void OnNetlistChanged(BOARD_NETLIST_UPDATER &aUpdater, bool *aRunDragCommand)
Called after netlist is updated.
void LoadFootprints(NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each #SCH_COMPONENT in aNetlist from the list of libraries.
static FOOTPRINT_LIBRARY_ADAPTER * FootprintLibAdapter(PROJECT *aProject)
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
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
std::string::size_type size() const
Definition utf8.h:116
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:202
This file is part of the common library.
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
PROJECT & Prj()
Definition kicad.cpp:642
@ 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
Class that computes missing connections on a PCB.
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
std::vector< EDA_ITEM * > EDA_ITEMS
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,...
std::string path