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 <pad.h>
43#include <pcb_track.h>
44#include <spread_footprints.h>
46#include <pcb_io/pcb_io_mgr.h>
48#include <tool/tool_manager.h>
49#include <tools/drc_tool.h>
50#include <tools/pcb_actions.h>
52#include <project/project_file.h> // LAST_PATH_TYPE
53#include <project_pcb.h>
54
55
56bool PCB_EDIT_FRAME::ReadNetlistFromFile( const wxString &aFilename, NETLIST& aNetlist,
57 REPORTER& aReporter )
58{
59 wxString msg;
60
61 try
62 {
63 std::unique_ptr<NETLIST_READER> netlistReader( NETLIST_READER::GetNetlistReader(
64 &aNetlist, aFilename, wxEmptyString ) );
65
66 if( !netlistReader.get() )
67 {
68 msg.Printf( _( "Cannot open netlist file '%s'." ), aFilename );
69 DisplayErrorMessage( this, msg );
70 return false;
71 }
72
73 SetLastPath( LAST_PATH_NETLIST, aFilename );
74 netlistReader->LoadNetlist();
75 LoadFootprints( aNetlist, aReporter );
76 }
77 catch( const IO_ERROR& ioe )
78 {
79 msg.Printf( _( "Error loading netlist.\n%s" ), ioe.What().GetData() );
80 DisplayErrorMessage( this, msg );
81 return false;
82 }
83
84 SetLastPath( LAST_PATH_NETLIST, aFilename );
85
86 return true;
87}
88
89
90void PCB_EDIT_FRAME::OnNetlistChanged( BOARD_NETLIST_UPDATER& aUpdater, bool* aRunDragCommand )
91{
92 BOARD* board = GetBoard();
93
94 SetMsgPanel( board );
95
96 // Re-sync nets and netclasses
97 board->SynchronizeNetsAndNetClasses( false );
98
99 // Recompute component classes
102
103 // Resync DRC rules to account for new aggregate netclass / component class rules
104 DRC_TOOL* drcTool = m_toolManager->GetTool<DRC_TOOL>();
105
106 try
107 {
109 }
110 catch( PARSE_ERROR& )
111 {
112 }
113
114 // Update rendered track/via/pad net labels, and any text items that might reference a
115 // netName or netClass
116 int netNamesCfg = GetPcbNewSettings()->m_Display.m_NetNames;
117
119 [&]( KIGFX::VIEW_ITEM* aItem ) -> int
120 {
121 if( dynamic_cast<PCB_TRACK*>( aItem ) )
122 {
123 if( netNamesCfg == 2 || netNamesCfg == 3 )
124 return KIGFX::REPAINT;
125 }
126 else if( dynamic_cast<PAD*>( aItem ) )
127 {
128 if( netNamesCfg == 1 || netNamesCfg == 3 )
129 return KIGFX::REPAINT;
130 }
131
132 EDA_TEXT* text = dynamic_cast<EDA_TEXT*>( aItem );
133
134 if( text && text->HasTextVars() )
135 {
136 text->ClearRenderCache();
137 text->ClearBoundingBoxCache();
139 }
140
141 return 0;
142 } );
143
144 // Spread new footprints.
145 std::vector<FOOTPRINT*> newFootprints = aUpdater.GetAddedFootprints();
146
148
149 SpreadFootprints( &newFootprints, { 0, 0 }, true );
150
151 // Start drag command for new footprints
152 if( !newFootprints.empty() )
153 {
154 EDA_ITEMS items;
155 std::copy( newFootprints.begin(), newFootprints.end(), std::back_inserter( items ) );
157
158 *aRunDragCommand = true;
159 }
160
161 Compile_Ratsnest( true );
162
164
165 GetCanvas()->Refresh();
166}
167
168
170{
171 wxString msg;
172 LIB_ID lastFPID;
173 COMPONENT* component;
174 FOOTPRINT* footprint = nullptr;
175 FOOTPRINT* fpOnBoard = nullptr;
176
177 if( aNetlist.IsEmpty() || PROJECT_PCB::FootprintLibAdapter( &Prj() )->Rows().empty() )
178 return;
179
180 aNetlist.SortByFPID();
181
182 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
183 {
184 component = aNetlist.GetComponent( ii );
185
186 // The FPID is ok as long as there is a footprint portion coming from eeschema.
187 if( !component->GetFPID().GetLibItemName().size() )
188 {
189 msg.Printf( _( "No footprint defined for symbol %s." ),
190 component->GetReference() );
191 aReporter.Report( msg, RPT_SEVERITY_ERROR );
192
193 continue;
194 }
195
196 // Check if component footprint is already on BOARD and only load the footprint from
197 // the library if it's needed. Nickname can be blank.
198 if( aNetlist.IsFindByTimeStamp() )
199 {
200 for( const KIID& uuid : component->GetKIIDs() )
201 {
202 KIID_PATH path = component->GetPath();
203 path.push_back( uuid );
204
205 if( ( fpOnBoard = m_pcb->FindFootprintByPath( path ) ) != nullptr )
206 break;
207 }
208 }
209 else
210 fpOnBoard = m_pcb->FindFootprintByReference( component->GetReference() );
211
212 // When the schematic-side FPID has no library nickname (legacy format), match
213 // only by item name so we don't flag a mismatch against a fully qualified board FPID.
214 bool footprintMisMatch = false;
215
216 if( fpOnBoard )
217 {
218 if( component->GetFPID().IsLegacy() )
219 {
220 footprintMisMatch =
221 fpOnBoard->GetFPID().GetLibItemName() != component->GetFPID().GetLibItemName();
222 }
223 else
224 {
225 footprintMisMatch = fpOnBoard->GetFPID() != component->GetFPID();
226 }
227 }
228
229 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
230 {
231 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
232 component->GetReference(),
233 fpOnBoard->GetFPID().Format().wx_str(),
234 component->GetFPID().Format().wx_str() );
235 aReporter.Report( msg, RPT_SEVERITY_WARNING );
236
237 continue;
238 }
239
240 if( !aNetlist.GetReplaceFootprints() )
241 footprintMisMatch = false;
242
243 if( fpOnBoard && !footprintMisMatch ) // nothing else to do here
244 continue;
245
246 if( component->GetFPID() != lastFPID )
247 {
248 footprint = nullptr;
249
250 // The LIB_ID is ok as long as there is a footprint portion coming the library if
251 // it's needed. Nickname can be blank.
252 if( !component->GetFPID().GetLibItemName().size() )
253 {
254 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
255 component->GetReference(),
256 component->GetFPID().Format().wx_str() );
257 aReporter.Report( msg, RPT_SEVERITY_ERROR );
258
259 continue;
260 }
261
262 // loadFootprint() can find a footprint with an empty nickname in fpid.
263 footprint = PCB_BASE_FRAME::loadFootprint( component->GetFPID() );
264
265 if( footprint )
266 {
267 lastFPID = component->GetFPID();
268 }
269 else
270 {
271 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
272 "library table." ),
273 component->GetReference(),
274 component->GetFPID().GetLibItemName().wx_str() );
275 aReporter.Report( msg, RPT_SEVERITY_ERROR );
276
277 continue;
278 }
279 }
280 else
281 {
282 // Footprint already loaded from a library, duplicate it (faster)
283 if( !footprint )
284 continue; // Footprint does not exist in any library.
285
286 footprint = new FOOTPRINT( *footprint );
287 const_cast<KIID&>( footprint->m_Uuid ) = KIID();
288 }
289
290 if( footprint )
291 component->SetFootprint( footprint );
292 }
293}
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:2681
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:527
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:1600
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:36
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