KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_netlist_utils.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 */
11
12#include "pcb_netlist_utils.h"
13
14#include <board.h>
16#include <footprint.h>
18#include <lib_id.h>
21#include <project_pcb.h>
22#include <reporter.h>
23
24
25FOOTPRINT* LoadFootprintFromProject( BOARD* aBoard, const LIB_ID& aFootprintId, bool aKeepUuid )
26{
27 FOOTPRINT* footprint = nullptr;
28
29 try
30 {
31 FOOTPRINT_LIBRARY_ADAPTER* adapter = PROJECT_PCB::FootprintLibAdapter( nullptr /* ignored */ );
32 footprint = adapter->LoadFootprintWithOptionalNickname( aFootprintId, aKeepUuid );
33 }
34 catch( const IO_ERROR& )
35 {
36 }
37
38 if( footprint )
39 {
40 // If the footprint is found, clear all net info to be sure there are no broken links to
41 // any netinfo list (should be not needed, but it can be edited from the footprint editor )
42 footprint->ClearAllNets();
43
44 if( aBoard && !aBoard->IsFootprintHolder() )
45 {
47
48 footprint->ApplyDefaultSettings( *aBoard, bds.m_StyleFPFields, bds.m_StyleFPText, bds.m_StyleFPShapes,
50 }
51 }
52
53 return footprint;
54}
55
56
57void LoadNetlistFootprints( BOARD* aBoard, NETLIST& aNetlist, REPORTER& aReporter )
58{
59 wxString msg;
60 LIB_ID lastFPID;
61 COMPONENT* component;
62 FOOTPRINT* footprint = nullptr;
63 FOOTPRINT* fpOnBoard = nullptr;
64
65 FOOTPRINT_LIBRARY_ADAPTER* adapter = PROJECT_PCB::FootprintLibAdapter( nullptr /* ignored */ );
66
67 if( aNetlist.IsEmpty() || adapter->Rows().empty() )
68 return;
69
70 // Make sure the libraries are loaded before we start loading the footprints.
71 adapter->AsyncLoad();
72 adapter->BlockUntilLoaded();
73
74 aNetlist.SortByFPID();
75
76 for( unsigned ii = 0; ii < aNetlist.GetCount(); ii++ )
77 {
78 component = aNetlist.GetComponent( ii );
79
80 if( !component->GetFPID().GetLibItemName().size() )
81 {
82 msg.Printf( _( "No footprint defined for symbol %s." ), component->GetReference() );
83 aReporter.Report( msg, RPT_SEVERITY_ERROR );
84 continue;
85 }
86
87 if( aNetlist.IsFindByTimeStamp() )
88 {
89 for( const KIID& uuid : component->GetKIIDs() )
90 {
91 KIID_PATH path = component->GetPath();
92 path.push_back( uuid );
93
94 if( ( fpOnBoard = aBoard->FindFootprintByPath( path ) ) != nullptr )
95 break;
96 }
97 }
98 else
99 {
100 fpOnBoard = aBoard->FindFootprintByReference( component->GetReference() );
101 }
102
103 bool footprintMisMatch = false;
104
105 if( fpOnBoard )
106 {
107 if( component->GetFPID().IsLegacy() )
108 {
109 footprintMisMatch = fpOnBoard->GetFPID().GetLibItemName()
110 != component->GetFPID().GetLibItemName();
111 }
112 else
113 {
114 footprintMisMatch = fpOnBoard->GetFPID() != component->GetFPID();
115 }
116 }
117
118 if( footprintMisMatch && !aNetlist.GetReplaceFootprints() )
119 {
120 msg.Printf( _( "Footprint of %s changed: board footprint '%s', netlist footprint '%s'." ),
121 component->GetReference(),
122 fpOnBoard->GetFPID().Format().wx_str(),
123 component->GetFPID().Format().wx_str() );
124 aReporter.Report( msg, RPT_SEVERITY_WARNING );
125 continue;
126 }
127
128 if( !aNetlist.GetReplaceFootprints() )
129 footprintMisMatch = false;
130
131 if( fpOnBoard && !footprintMisMatch )
132 continue;
133
134 if( component->GetFPID() != lastFPID )
135 {
136 footprint = nullptr;
137
138 if( !component->GetFPID().GetLibItemName().size() )
139 {
140 msg.Printf( _( "%s footprint ID '%s' is not valid." ),
141 component->GetReference(),
142 component->GetFPID().Format().wx_str() );
143 aReporter.Report( msg, RPT_SEVERITY_ERROR );
144 continue;
145 }
146
147 footprint = LoadFootprintFromProject( aBoard, component->GetFPID() );
148
149 if( footprint )
150 {
151 lastFPID = component->GetFPID();
152 }
153 else
154 {
155 msg.Printf( _( "%s footprint '%s' not found in any libraries in the footprint "
156 "library table." ),
157 component->GetReference(),
158 component->GetFPID().GetLibItemName().wx_str() );
159 aReporter.Report( msg, RPT_SEVERITY_ERROR );
160 continue;
161 }
162 }
163 else
164 {
165 if( !footprint )
166 continue;
167
168 footprint = new FOOTPRINT( *footprint );
169 footprint->ResetUuidDirect();
170 }
171
172 if( footprint )
173 component->SetFootprint( footprint );
174 }
175}
Container for design settings for a BOARD object.
void ResetUuidDirect()
Definition board_item.h:277
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:410
bool IsFootprintHolder() const
Find out if the board is being used to hold a single footprint for editing/viewing.
Definition board.h:440
FOOTPRINT * FindFootprintByPath(const KIID_PATH &aPath) const
Search for a FOOTPRINT within this board with the given path.
Definition board.cpp:3092
FOOTPRINT * FindFootprintByReference(const wxString &aReference) const
Search for a FOOTPRINT within this board with the given reference designator.
Definition board.cpp:3080
BOARD_DESIGN_SETTINGS & GetDesignSettings() const
Definition board.cpp:1298
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
An interface to the global shared library manager that is schematic-specific and linked to one projec...
FOOTPRINT * LoadFootprintWithOptionalNickname(const LIB_ID &aFootprintId, bool aKeepUUID)
Load a footprint having aFootprintId with possibly an empty nickname.
const LIB_ID & GetFPID() const
Definition footprint.h:474
void ApplyDefaultSettings(const BOARD &board, bool aStyleFields, bool aStyleText, bool aStyleShapes, bool aStyleDimensions, bool aStyleBarcodes)
Apply default board settings to the footprint field text properties.
void ClearAllNets()
Clear (i.e.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition kiid.h:46
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Like LIBRARY_MANAGER::Rows but filtered to the LIBRARY_TABLE_TYPE of this adapter.
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
UTF8 Format() const
Definition lib_id.cpp:132
const UTF8 & GetLibItemName() const
Definition lib_id.h:98
bool IsLegacy() const
Definition lib_id.h:176
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
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
std::string::size_type size() const
Definition utf8.h:112
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
void LoadNetlistFootprints(BOARD *aBoard, NETLIST &aNetlist, REPORTER &aReporter)
Load the footprints for each SCH_SYMBOL in aNetlist from the list of libraries.
FOOTPRINT * LoadFootprintFromProject(BOARD *aBoard, const LIB_ID &aFootprintId, bool aKeepUuid)
Load a footprint from the project library table and apply board default settings.
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
std::string path