KiCad PCB EDA Suite
BACK_ANNOTATE Class Reference

Back annotation algorithm class used to receive, check, and apply a NETLIST from Pcbnew. More...

#include <backannotate.h>

Classes

struct  PCB_FP_DATA
 Container for Pcbnew footprint data.Map to hold NETLIST footprints data. More...
 

Public Types

using PCB_FOOTPRINTS_MAP = std::map< wxString, std::shared_ptr< PCB_FP_DATA > >
 
using CHANGELIST_ITEM = std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > >
 

Public Member Functions

 BACK_ANNOTATE (SCH_EDIT_FRAME *aFrame, REPORTER &aReporter, bool aRelinkFootprints, bool aProcessFootprints, bool aProcessValues, bool aProcessReferences, bool aProcessNetNames, bool aDryRun)
 
 ~BACK_ANNOTATE ()
 
bool FetchNetlistFromPCB (std::string &aNetlist)
 Get netlist from the Pcbnew. More...
 
void PushNewLinksToPCB ()
 
bool BackAnnotateSymbols (const std::string &aNetlist)
 Run back annotation algorithm. More...
 

Private Member Functions

void getPcbModulesFromString (const std::string &aPayload)
 Parse netlist sent over KiWay express mail interface and fill m_pcbModules. More...
 
void getChangeList ()
 
void checkForUnusedSymbols ()
 Check if some symbols are not represented in PCB footprints and vice versa. More...
 
void applyChangelist ()
 Apply changelist to the schematic. More...
 
void processNetNameChange (const wxString &aRef, SCH_PIN *aPin, const SCH_CONNECTION *aConnection, const wxString &aOldName, const wxString &aNewName)
 

Private Attributes

REPORTERm_reporter
 
bool m_matchByReference
 
bool m_processFootprints
 
bool m_processValues
 
bool m_processReferences
 
bool m_processNetNames
 
bool m_dryRun
 
PCB_FOOTPRINTS_MAP m_pcbFootprints
 
SCH_REFERENCE_LIST m_refs
 
SCH_MULTI_UNIT_REFERENCE_MAP m_multiUnitsRefs
 
std::deque< CHANGELIST_ITEMm_changelist
 
SCH_EDIT_FRAMEm_frame
 
int m_changesCount
 
bool m_appendUndo
 

Detailed Description

Back annotation algorithm class used to receive, check, and apply a NETLIST from Pcbnew.

The following checks are made:

  • Schematic symbol exists, but linked Pcbnew footprint missing.
  • Pcbnew footprint exists but no schematic symbol connected to.
  • Pcbnew footprint is standalone.
  • Schematic sheet is reused one or more times and user trying to change footprint or value only for few of them.
  • Schematic symbols share same path.
  • More than one Pcbnew footprint linked to same path.

Definition at line 55 of file backannotate.h.

Member Typedef Documentation

◆ CHANGELIST_ITEM

using BACK_ANNOTATE::CHANGELIST_ITEM = std::pair<SCH_REFERENCE, std::shared_ptr<PCB_FP_DATA> >

Definition at line 80 of file backannotate.h.

◆ PCB_FOOTPRINTS_MAP

using BACK_ANNOTATE::PCB_FOOTPRINTS_MAP = std::map<wxString, std::shared_ptr<PCB_FP_DATA> >

Definition at line 78 of file backannotate.h.

Constructor & Destructor Documentation

◆ BACK_ANNOTATE()

BACK_ANNOTATE::BACK_ANNOTATE ( SCH_EDIT_FRAME aFrame,
REPORTER aReporter,
bool  aRelinkFootprints,
bool  aProcessFootprints,
bool  aProcessValues,
bool  aProcessReferences,
bool  aProcessNetNames,
bool  aDryRun 
)

Definition at line 42 of file backannotate.cpp.

44  :
45  m_reporter( aReporter ),
46  m_matchByReference( aRelinkFootprints ),
47  m_processFootprints( aProcessFootprints ),
48  m_processValues( aProcessValues ),
49  m_processReferences( aProcessReferences ),
50  m_processNetNames( aProcessNetNames ),
51  m_dryRun( aDryRun ),
52  m_frame( aFrame ),
53  m_changesCount( 0 ),
54  m_appendUndo( false )
55 {
56 }
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
bool m_processNetNames
Definition: backannotate.h:139
bool m_processFootprints
Definition: backannotate.h:136
bool m_processReferences
Definition: backannotate.h:138
bool m_matchByReference
Definition: backannotate.h:135
REPORTER & m_reporter
Definition: backannotate.h:133
bool m_processValues
Definition: backannotate.h:137

◆ ~BACK_ANNOTATE()

BACK_ANNOTATE::~BACK_ANNOTATE ( )

Definition at line 59 of file backannotate.cpp.

60 {
61 }

Member Function Documentation

◆ applyChangelist()

void BACK_ANNOTATE::applyChangelist ( )
private

Apply changelist to the schematic.

Definition at line 311 of file backannotate.cpp.

312 {
313  wxString msg;
314 
315  // Apply changes from change list
316  for( CHANGELIST_ITEM& item : m_changelist )
317  {
318  SCH_REFERENCE& ref = item.first;
319  PCB_FP_DATA& fpData = *item.second;
320  SCH_SYMBOL* symbol = ref.GetSymbol();
321  SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
322  wxString oldFootprint = ref.GetFootprint();
323  wxString oldValue = ref.GetValue();
324  bool skip = ( ref.GetSymbol()->GetFlags() & SKIP_STRUCT ) > 0;
325 
326  if( m_processReferences && ref.GetRef() != fpData.m_ref && !skip )
327  {
328  ++m_changesCount;
329  msg.Printf( _( "Change '%s' reference designator to '%s'." ),
330  ref.GetFullRef(),
331  fpData.m_ref );
332 
333  if( !m_dryRun )
334  {
336  m_appendUndo = true;
337  symbol->SetRef( &ref.GetSheetPath(), fpData.m_ref );
338  }
339 
341  }
342 
343  if( m_processFootprints && oldFootprint != fpData.m_footprint && !skip )
344  {
345  ++m_changesCount;
346  msg.Printf( _( "Change %s footprint assignment from '%s' to '%s'." ),
347  ref.GetFullRef(),
348  oldFootprint,
349  fpData.m_footprint );
350 
351  if( !m_dryRun )
352  {
354  m_appendUndo = true;
355  symbol->SetFootprint( &ref.GetSheetPath(), fpData.m_footprint );
356  }
357 
359  }
360 
361  if( m_processValues && oldValue != fpData.m_value && !skip )
362  {
363  ++m_changesCount;
364  msg.Printf( _( "Change %s value from '%s' to '%s'." ),
365  ref.GetFullRef(),
366  oldValue,
367  fpData.m_value );
368 
369  if( !m_dryRun )
370  {
372  m_appendUndo = true;
373  symbol->SetValue( &ref.GetSheetPath(), fpData.m_value );
374  }
375 
377  }
378 
379  if( m_processNetNames )
380  {
381  for( const std::pair<const wxString, wxString>& entry : fpData.m_pinMap )
382  {
383  const wxString& pinNumber = entry.first;
384  const wxString& shortNetName = entry.second;
385  SCH_PIN* pin = symbol->GetPin( pinNumber );
386 
387  if( !pin )
388  {
389  msg.Printf( _( "Cannot find %s pin '%s'." ),
390  ref.GetFullRef(),
391  pinNumber );
393 
394  continue;
395  }
396 
397  SCH_CONNECTION* connection = pin->Connection( &ref.GetSheetPath() );
398 
399  if( connection && connection->Name( true ) != shortNetName )
400  {
401  processNetNameChange( ref.GetFullRef(), pin, connection,
402  connection->Name( true ), shortNetName );
403  }
404  }
405  }
406  }
407 
408  if( !m_dryRun )
409  {
412  }
413 
415 }
void UpdateNetHighlightStatus()
void RecalculateConnections(SCH_CLEANUP_FLAGS aCleanupFlags)
Generate the connection data for the entire schematic hierarchy.
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
const SCH_SHEET_PATH & GetSheetPath() const
void SetValue(const SCH_SHEET_PATH *sheet, const wxString &aValue)
Definition: sch_symbol.cpp:594
bool m_processNetNames
Definition: backannotate.h:139
bool m_processFootprints
Definition: backannotate.h:136
bool m_processReferences
Definition: backannotate.h:138
SCH_SYMBOL * GetSymbol() const
void SetFootprint(const SCH_SHEET_PATH *sheet, const wxString &aFootprint)
Definition: sch_symbol.cpp:645
REPORTER & m_reporter
Definition: backannotate.h:133
wxString GetRef() const
bool m_processValues
Definition: backannotate.h:137
void processNetNameChange(const wxString &aRef, SCH_PIN *aPin, const SCH_CONNECTION *aConnection, const wxString &aOldName, const wxString &aNewName)
wxString Name(bool aIgnoreSheet=false) const
wxString GetFullRef() const
const wxString GetFootprint() const
#define _(s)
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
EDA_ITEM_FLAGS GetFlags() const
Definition: eda_item.h:154
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:108
Schematic symbol object.
Definition: sch_symbol.h:78
#define SKIP_STRUCT
flag indicating that the structure should be ignored
SCH_SCREEN * LastScreen()
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend)
Create a copy of the current schematic item, and put it in the undo list.
SCH_PIN * GetPin(const wxString &number) const
Find a symbol pin by number.
Definition: sch_symbol.cpp:840
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
Definition: sch_symbol.cpp:481
const wxString GetValue() const
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:145
A helper to define a symbol's reference designator in a schematic.

References _, CHANGED, EDA_ITEM::GetFlags(), SCH_REFERENCE::GetFootprint(), SCH_REFERENCE::GetFullRef(), SCH_SYMBOL::GetPin(), SCH_REFERENCE::GetRef(), SCH_REFERENCE::GetSheetPath(), SCH_REFERENCE::GetSymbol(), SCH_REFERENCE::GetValue(), SCH_SHEET_PATH::LastScreen(), m_appendUndo, m_changelist, m_changesCount, m_dryRun, BACK_ANNOTATE::PCB_FP_DATA::m_footprint, m_frame, BACK_ANNOTATE::PCB_FP_DATA::m_pinMap, m_processFootprints, m_processNetNames, m_processReferences, m_processValues, BACK_ANNOTATE::PCB_FP_DATA::m_ref, m_reporter, BACK_ANNOTATE::PCB_FP_DATA::m_value, SCH_CONNECTION::Name(), NO_CLEANUP, pin, processNetNameChange(), SCH_EDIT_FRAME::RecalculateConnections(), REPORTER::ReportHead(), RPT_SEVERITY_ACTION, RPT_SEVERITY_ERROR, RPT_SEVERITY_INFO, SCH_EDIT_FRAME::SaveCopyInUndoList(), SCH_SYMBOL::SetFootprint(), SCH_SYMBOL::SetRef(), SCH_SYMBOL::SetValue(), SKIP_STRUCT, and SCH_EDIT_FRAME::UpdateNetHighlightStatus().

Referenced by BackAnnotateSymbols().

◆ BackAnnotateSymbols()

bool BACK_ANNOTATE::BackAnnotateSymbols ( const std::string &  aNetlist)

Run back annotation algorithm.

If any errors, back annotation doesn't run.

Parameters
aNetlistis the netlist to use for back annotation.
Returns
true if back annotation completed success.

Definition at line 64 of file backannotate.cpp.

65 {
66  m_changesCount = 0;
67  m_appendUndo = false;
68  wxString msg;
69 
71  && !m_processNetNames )
72  {
73  m_reporter.ReportTail( _( "Select at least one property to back annotate." ),
75  return false;
76  }
77 
78  getPcbModulesFromString( aNetlist );
79 
81  sheets.GetSymbols( m_refs, false );
83 
84  getChangeList();
86 
88 
89  return true;
90 }
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GetMultiUnitSymbols(SCH_MULTI_UNIT_REFERENCE_MAP &aRefList, bool aIncludePowerSymbols=true) const
Add a SCH_REFERENCE_LIST object to aRefList for each same-reference set of multi-unit parts in the li...
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
void GetSymbols(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanSymbols=false) const
Add a SCH_REFERENCE object to aReferences for each symbol in the list of sheets.
bool m_processNetNames
Definition: backannotate.h:139
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.h:99
bool m_processFootprints
Definition: backannotate.h:136
bool m_processReferences
Definition: backannotate.h:138
void getPcbModulesFromString(const std::string &aPayload)
Parse netlist sent over KiWay express mail interface and fill m_pcbModules.
bool m_matchByReference
Definition: backannotate.h:135
void checkForUnusedSymbols()
Check if some symbols are not represented in PCB footprints and vice versa.
REPORTER & m_reporter
Definition: backannotate.h:133
bool m_processValues
Definition: backannotate.h:137
SCHEMATIC & Schematic() const
#define _(s)
void getChangeList()
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:87
void applyChangelist()
Apply changelist to the schematic.
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:143
SCH_MULTI_UNIT_REFERENCE_MAP m_multiUnitsRefs
Definition: backannotate.h:144

References _, applyChangelist(), checkForUnusedSymbols(), getChangeList(), SCH_SHEET_LIST::GetMultiUnitSymbols(), getPcbModulesFromString(), SCHEMATIC::GetSheets(), SCH_SHEET_LIST::GetSymbols(), m_appendUndo, m_changesCount, m_frame, m_matchByReference, m_multiUnitsRefs, m_processFootprints, m_processNetNames, m_processReferences, m_processValues, m_refs, m_reporter, REPORTER::ReportTail(), RPT_SEVERITY_ERROR, and SCH_EDIT_FRAME::Schematic().

◆ checkForUnusedSymbols()

void BACK_ANNOTATE::checkForUnusedSymbols ( )
private

Check if some symbols are not represented in PCB footprints and vice versa.

m_refs must be sorted by path.

Definition at line 268 of file backannotate.cpp.

269 {
271 
272  std::sort( m_changelist.begin(), m_changelist.end(),
273  []( const CHANGELIST_ITEM& a, const CHANGELIST_ITEM& b )
274  {
275  return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
276  } );
277 
278  size_t i = 0;
279 
280  for( const std::pair<SCH_REFERENCE, std::shared_ptr<PCB_FP_DATA>>& item : m_changelist )
281  {
282  // Refs and changelist are both sorted by paths, so we just go over m_refs and
283  // generate errors before we will find m_refs member to which item linked
284  while( i < m_refs.GetCount() && m_refs[i].GetPath() != item.first.GetPath() )
285  {
286  const SCH_REFERENCE& ref = m_refs[i];
287 
288  if( ref.GetSymbol()->GetIncludeOnBoard() )
289  {
290  wxString msg = wxString::Format( _( "Footprint '%s' is not present on PCB. "
291  "Corresponding symbols in schematic must be "
292  "manually deleted (if desired)." ),
293  m_refs[i].GetFullRef() );
295  }
296 
297  ++i;
298  }
299 
300  ++i;
301  }
302 
303  if( m_matchByReference && !m_frame->ReadyToNetlist( _( "Re-linking footprints requires a fully "
304  "annotated schematic." ) ) )
305  {
306  m_reporter.ReportTail( _( "Footprint re-linking cancelled by user." ), RPT_SEVERITY_ERROR );
307  }
308 }
bool ReadyToNetlist(const wxString &aAnnotateMessage)
Check if we are ready to write a netlist file for the current schematic.
static bool sortByTimeStamp(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.h:99
SCH_SYMBOL * GetSymbol() const
bool m_matchByReference
Definition: backannotate.h:135
REPORTER & m_reporter
Definition: backannotate.h:133
void SortByTimeStamp()
Sort the flat list by Time Stamp (sheet path + timestamp).
size_t GetCount() const
#define _(s)
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
bool GetIncludeOnBoard() const
Definition: sch_symbol.h:665
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:143
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:145
A helper to define a symbol's reference designator in a schematic.

References _, Format(), SCH_REFERENCE_LIST::GetCount(), SCH_SYMBOL::GetIncludeOnBoard(), SCH_REFERENCE::GetSymbol(), m_changelist, m_frame, m_matchByReference, m_refs, m_reporter, SCH_EDIT_FRAME::ReadyToNetlist(), REPORTER::ReportTail(), RPT_SEVERITY_ERROR, RPT_SEVERITY_WARNING, SCH_REFERENCE_LIST::SortByTimeStamp(), and SCH_REFERENCE_LIST::sortByTimeStamp().

Referenced by BackAnnotateSymbols().

◆ FetchNetlistFromPCB()

bool BACK_ANNOTATE::FetchNetlistFromPCB ( std::string &  aNetlist)

Get netlist from the Pcbnew.

Parameters
aNetlistis the netlist for the board editor.
Returns
true if success.

Definition at line 93 of file backannotate.cpp.

94 {
95  if( Kiface().IsSingle() )
96  {
97  DisplayErrorMessage( m_frame, _( "Cannot fetch PCB netlist because eeschema is opened "
98  "in stand-alone mode.\n"
99  "You must launch the KiCad project manager and create "
100  "a project." ) );
101  return false;
102  }
103 
104  KIWAY_PLAYER* frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, false );
105 
106  if( !frame )
107  {
108  wxFileName fn( m_frame->Prj().GetProjectFullName() );
109  fn.SetExt( PcbFileExtension );
110 
111  frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, true );
112  frame->OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ) );
113  }
114 
116  return true;
117 }
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Open a project or set of files given by aFileList.
Definition: kiway_player.h:118
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:64
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:292
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:383
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:477
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
#define _(s)
#define PcbFileExtension
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:116

References _, DisplayErrorMessage(), KIWAY::ExpressMail(), FRAME_PCB_EDITOR, PROJECT::GetProjectFullName(), Kiface(), KIWAY_HOLDER::Kiway(), m_frame, MAIL_PCB_GET_NETLIST, KIWAY_PLAYER::OpenProjectFiles(), PcbFileExtension, KIWAY::Player(), and KIWAY_HOLDER::Prj().

◆ getChangeList()

void BACK_ANNOTATE::getChangeList ( )
private

Definition at line 211 of file backannotate.cpp.

212 {
213  for( std::pair<const wxString, std::shared_ptr<PCB_FP_DATA>>& fpData : m_pcbFootprints )
214  {
215  const wxString& pcbPath = fpData.first;
216  auto& pcbData = fpData.second;
217  int refIndex;
218  bool foundInMultiunit = false;
219 
220  for( std::pair<const wxString, SCH_REFERENCE_LIST>& item : m_multiUnitsRefs )
221  {
222  SCH_REFERENCE_LIST& refList = item.second;
223 
224  if( m_matchByReference )
225  refIndex = refList.FindRef( pcbPath );
226  else
227  refIndex = refList.FindRefByPath( pcbPath );
228 
229  if( refIndex >= 0 )
230  {
231  // If footprint linked to multi unit symbol, we add all symbol's units to
232  // the change list
233  foundInMultiunit = true;
234 
235  for( size_t i = 0; i < refList.GetCount(); ++i )
236  {
237  refList[ i ].GetSymbol()->ClearFlags(SKIP_STRUCT );
238  m_changelist.emplace_back( CHANGELIST_ITEM( refList[i], pcbData ) );
239  }
240 
241  break;
242  }
243  }
244 
245  if( foundInMultiunit )
246  continue;
247 
248  if( m_matchByReference )
249  refIndex = m_refs.FindRef( pcbPath );
250  else
251  refIndex = m_refs.FindRefByPath( pcbPath );
252 
253  if( refIndex >= 0 )
254  {
255  m_refs[ refIndex ].GetSymbol()->ClearFlags( SKIP_STRUCT );
256  m_changelist.emplace_back( CHANGELIST_ITEM( m_refs[refIndex], pcbData ) );
257  }
258  else
259  {
260  // Haven't found linked symbol in multiunits or common refs. Generate error
261  wxString msg = wxString::Format( _( "Cannot find symbol for footprint '%s'." ),
262  pcbData->m_ref );
264  }
265  }
266 }
int FindRefByPath(const wxString &aPath) const
Search the list for a symbol with the given KIID path.
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.h:99
bool m_matchByReference
Definition: backannotate.h:135
Container to create a flattened list of symbols because in a complex hierarchy, a symbol can be used ...
int FindRef(const wxString &aPath) const
Search the list for a symbol with a given reference.
REPORTER & m_reporter
Definition: backannotate.h:133
size_t GetCount() const
#define _(s)
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
#define SKIP_STRUCT
flag indicating that the structure should be ignored
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:143
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:145
PCB_FOOTPRINTS_MAP m_pcbFootprints
Definition: backannotate.h:142
SCH_MULTI_UNIT_REFERENCE_MAP m_multiUnitsRefs
Definition: backannotate.h:144

References _, SCH_REFERENCE_LIST::FindRef(), SCH_REFERENCE_LIST::FindRefByPath(), Format(), SCH_REFERENCE_LIST::GetCount(), m_changelist, m_matchByReference, m_multiUnitsRefs, m_pcbFootprints, m_refs, m_reporter, REPORTER::ReportTail(), RPT_SEVERITY_ERROR, and SKIP_STRUCT.

Referenced by BackAnnotateSymbols().

◆ getPcbModulesFromString()

void BACK_ANNOTATE::getPcbModulesFromString ( const std::string &  aPayload)
private

Parse netlist sent over KiWay express mail interface and fill m_pcbModules.

Parameters
aPayloadis the netlist from Pcbnew.
Returns
number of errors during parsing.Create changelist.

Definition at line 128 of file backannotate.cpp.

129 {
130  auto getStr = []( const PTREE& pt ) -> wxString
131  {
132  return UTF8( pt.front().first );
133  };
134 
135  DSNLEXER lexer( aPayload, FROM_UTF8( __func__ ) );
136  PTREE doc;
137 
138  // NOTE: KiCad's PTREE scanner constructs a property *name* tree, not a property tree.
139  // Every token in the s-expr is stored as a property name; the property's value is then
140  // either the nested s-exprs or an empty PTREE; there are *no* literal property values.
141 
142  Scan( &doc, &lexer );
143 
144  PTREE& tree = doc.get_child( "pcb_netlist" );
145  wxString msg;
146  m_pcbFootprints.clear();
147 
148  for( const std::pair<const std::string, PTREE>& item : tree )
149  {
150  wxString path, value, footprint;
151  std::map<wxString, wxString> pinNetMap;
152  wxASSERT( item.first == "ref" );
153  wxString ref = getStr( item.second );
154 
155  try
156  {
157  if( m_matchByReference )
158  path = ref;
159  else
160  path = getStr( item.second.get_child( "timestamp" ) );
161 
162  if( path == "" )
163  {
164  msg.Printf( _( "Footprint '%s' has no assigned symbol." ), ref );
166  continue;
167  }
168 
169  footprint = getStr( item.second.get_child( "fpid" ) );
170  value = getStr( item.second.get_child( "value" ) );
171 
172  boost::optional<const PTREE&> nets = item.second.get_child_optional( "nets" );
173 
174  if( nets )
175  {
176  for( const std::pair<const std::string, PTREE>& pin_net : nets.get() )
177  {
178  wxASSERT( pin_net.first == "pin_net" );
179  wxString pinNumber = UTF8( pin_net.second.front().first );
180  wxString netName = UTF8( pin_net.second.back().first );
181  pinNetMap[ pinNumber ] = netName;
182  }
183  }
184  }
185  catch( ... )
186  {
187  wxLogWarning( "Cannot parse PCB netlist for back-annotation." );
188  }
189 
190  // Use lower_bound for not to iterate over map twice
191  auto nearestItem = m_pcbFootprints.lower_bound( path );
192 
193  if( nearestItem != m_pcbFootprints.end() && nearestItem->first == path )
194  {
195  // Module with this path already exists - generate error
196  msg.Printf( _( "Footprints '%s' and '%s' linked to same symbol." ),
197  nearestItem->second->m_ref,
198  ref );
200  }
201  else
202  {
203  // Add footprint to the map
204  auto data = std::make_shared<PCB_FP_DATA>( ref, footprint, value, pinNetMap );
205  m_pcbFootprints.insert( nearestItem, std::make_pair( path, data ) );
206  }
207  }
208 }
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
void Scan(PTREE *aTree, DSNLEXER *aLexer)
Fill an empty PTREE with information from a KiCad s-expression stream.
Definition: ptree.cpp:86
bool m_matchByReference
Definition: backannotate.h:135
REPORTER & m_reporter
Definition: backannotate.h:133
boost::property_tree::ptree PTREE
Definition: ptree.h:52
#define _(s)
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:108
Implement a lexical analyzer for the SPECCTRA DSN file format.
Definition: dsnlexer.h:78
PCB_FOOTPRINTS_MAP m_pcbFootprints
Definition: backannotate.h:142

References _, FROM_UTF8(), m_matchByReference, m_pcbFootprints, m_reporter, path, REPORTER::ReportHead(), RPT_SEVERITY_ERROR, RPT_SEVERITY_WARNING, and Scan().

Referenced by BackAnnotateSymbols().

◆ processNetNameChange()

void BACK_ANNOTATE::processNetNameChange ( const wxString &  aRef,
SCH_PIN aPin,
const SCH_CONNECTION aConnection,
const wxString &  aOldName,
const wxString &  aNewName 
)
private

Definition at line 497 of file backannotate.cpp.

500 {
501  wxString msg;
502 
503  // Find a physically-connected driver. We can't use the SCH_CONNECTION's m_driver because
504  // it has already been resolved by merging subgraphs with the same label, etc., and our
505  // name change may cause that resolution to change.
506 
507  std::set<SCH_ITEM*> connectedItems;
508  SCH_ITEM* driver = nullptr;
510 
511  addConnections( aPin, aConnection->Sheet(), connectedItems );
512 
513  for( SCH_ITEM* item : connectedItems )
514  {
516 
517  if( priority > driverPriority )
518  {
519  driver = item;
520  driverPriority = priority;
521  }
522  }
523 
524  switch( driver->Type() )
525  {
526  case SCH_LABEL_T:
527  case SCH_GLOBAL_LABEL_T:
528  case SCH_HIER_LABEL_T:
529  case SCH_SHEET_PIN_T:
530  ++m_changesCount;
531 
532  msg.Printf( _( "Change %s pin %s net label from '%s' to '%s'." ),
533  aRef,
534  aPin->GetShownNumber(),
535  aOldName,
536  aNewName );
537 
538  if( !m_dryRun )
539  {
540  SCH_SCREEN* screen = aConnection->Sheet().LastScreen();
541 
543  m_appendUndo = true;
544  static_cast<SCH_TEXT*>( driver )->SetText( aNewName );
545  }
546 
548  break;
549 
550  case SCH_PIN_T:
551  {
552  SCH_PIN* schPin = static_cast<SCH_PIN*>( driver );
553  LABEL_SPIN_STYLE spin = orientLabel( schPin );
554 
555  if( schPin->IsPowerConnection() )
556  {
557  msg.Printf( _( "Net %s cannot be changed to %s because it is driven by a power pin." ),
558  aOldName,
559  aNewName );
560 
562  break;
563  }
564 
565  ++m_changesCount;
566  msg.Printf( _( "Add label '%s' to %s pin %s net." ),
567  aNewName,
568  aRef,
569  aPin->GetShownNumber() );
570 
571  if( !m_dryRun )
572  {
573  SCHEMATIC_SETTINGS& settings = m_frame->Schematic().Settings();
574  SCH_LABEL* label = new SCH_LABEL( driver->GetPosition(), aNewName );
575  label->SetParent( &m_frame->Schematic() );
576  label->SetTextSize( wxSize( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) );
577  label->SetLabelSpinStyle( spin );
578  label->SetFlags( IS_NEW );
579 
580  SCH_SCREEN* screen = aConnection->Sheet().LastScreen();
582  m_appendUndo = true;
583  }
584 
586  }
587  break;
588 
589  default:
590  break;
591  }
592 }
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
#define IS_NEW
New item, just created.
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:167
void SetFlags(EDA_ITEM_FLAGS aMask)
Definition: eda_item.h:152
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:258
virtual wxPoint GetPosition() const
Definition: eda_item.h:251
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:115
REPORTER & m_reporter
Definition: backannotate.h:133
void addConnections(SCH_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath, std::set< SCH_ITEM * > &connectedItems)
wxString GetShownNumber() const
Definition: sch_pin.cpp:104
SCHEMATIC & Schematic() const
#define _(s)
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:108
SCH_SHEET_PATH Sheet() const
void AddItemToScreenAndUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItem, bool aUndoAppend)
Add an item to the schematic and adds the changes to the undo/redo container.
SCH_SCREEN * LastScreen()
static LABEL_SPIN_STYLE orientLabel(SCH_PIN *aPin)
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend)
Create a copy of the current schematic item, and put it in the undo list.
These settings were stored in SCH_BASE_FRAME previously.
bool IsPowerConnection() const
Definition: sch_pin.h:131
virtual void SetLabelSpinStyle(LABEL_SPIN_STYLE aSpinStyle)
Set a spin or rotation angle, along with specific horizontal and vertical justification styles with e...
Definition: sch_text.cpp:330
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:182
PRIORITY GetDriverPriority()
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:112

References _, addConnections(), SCH_EDIT_FRAME::AddItemToScreenAndUndoList(), CHANGED, CONNECTION_SUBGRAPH::GetDriverPriority(), EDA_ITEM::GetPosition(), SCH_PIN::GetShownNumber(), IS_NEW, SCH_PIN::IsPowerConnection(), SCH_SHEET_PATH::LastScreen(), m_appendUndo, m_changesCount, SCHEMATIC_SETTINGS::m_DefaultTextSize, m_dryRun, m_frame, m_reporter, CONNECTION_SUBGRAPH::NONE, orientLabel(), REPORTER::ReportHead(), RPT_SEVERITY_ACTION, RPT_SEVERITY_ERROR, SCH_EDIT_FRAME::SaveCopyInUndoList(), SCH_GLOBAL_LABEL_T, SCH_HIER_LABEL_T, SCH_LABEL_T, SCH_PIN_T, SCH_SHEET_PIN_T, SCH_EDIT_FRAME::Schematic(), EDA_ITEM::SetFlags(), SCH_TEXT::SetLabelSpinStyle(), EDA_ITEM::SetParent(), EDA_TEXT::SetTextSize(), SCHEMATIC::Settings(), SCH_CONNECTION::Sheet(), and EDA_ITEM::Type().

Referenced by applyChangelist().

◆ PushNewLinksToPCB()

void BACK_ANNOTATE::PushNewLinksToPCB ( )

Definition at line 120 of file backannotate.cpp.

121 {
122  std::string nullPayload;
123 
125 }
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:53
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:477

References KIWAY::ExpressMail(), FRAME_PCB_EDITOR, KIWAY_HOLDER::Kiway(), m_frame, and MAIL_PCB_UPDATE_LINKS.

Member Data Documentation

◆ m_appendUndo

bool BACK_ANNOTATE::m_appendUndo
private

Definition at line 149 of file backannotate.h.

Referenced by applyChangelist(), BackAnnotateSymbols(), and processNetNameChange().

◆ m_changelist

std::deque<CHANGELIST_ITEM> BACK_ANNOTATE::m_changelist
private

Definition at line 145 of file backannotate.h.

Referenced by applyChangelist(), checkForUnusedSymbols(), and getChangeList().

◆ m_changesCount

int BACK_ANNOTATE::m_changesCount
private

Definition at line 148 of file backannotate.h.

Referenced by applyChangelist(), BackAnnotateSymbols(), and processNetNameChange().

◆ m_dryRun

bool BACK_ANNOTATE::m_dryRun
private

Definition at line 140 of file backannotate.h.

Referenced by applyChangelist(), and processNetNameChange().

◆ m_frame

◆ m_matchByReference

bool BACK_ANNOTATE::m_matchByReference
private

◆ m_multiUnitsRefs

SCH_MULTI_UNIT_REFERENCE_MAP BACK_ANNOTATE::m_multiUnitsRefs
private

Definition at line 144 of file backannotate.h.

Referenced by BackAnnotateSymbols(), and getChangeList().

◆ m_pcbFootprints

PCB_FOOTPRINTS_MAP BACK_ANNOTATE::m_pcbFootprints
private

Definition at line 142 of file backannotate.h.

Referenced by getChangeList(), and getPcbModulesFromString().

◆ m_processFootprints

bool BACK_ANNOTATE::m_processFootprints
private

Definition at line 136 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_processNetNames

bool BACK_ANNOTATE::m_processNetNames
private

Definition at line 139 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_processReferences

bool BACK_ANNOTATE::m_processReferences
private

Definition at line 138 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_processValues

bool BACK_ANNOTATE::m_processValues
private

Definition at line 137 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_refs

SCH_REFERENCE_LIST BACK_ANNOTATE::m_refs
private

Definition at line 143 of file backannotate.h.

Referenced by BackAnnotateSymbols(), checkForUnusedSymbols(), and getChangeList().

◆ m_reporter


The documentation for this class was generated from the following files: