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 41 of file backannotate.cpp.

43  :
44  m_reporter( aReporter ),
45  m_matchByReference( aRelinkFootprints ),
46  m_processFootprints( aProcessFootprints ),
47  m_processValues( aProcessValues ),
48  m_processReferences( aProcessReferences ),
49  m_processNetNames( aProcessNetNames ),
50  m_dryRun( aDryRun ),
51  m_frame( aFrame ),
52  m_changesCount( 0 ),
53  m_appendUndo( false )
54 {
55 }
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 58 of file backannotate.cpp.

59 {
60 }

Member Function Documentation

◆ applyChangelist()

void BACK_ANNOTATE::applyChangelist ( )
private

Apply changelist to the schematic.

Definition at line 310 of file backannotate.cpp.

311 {
312  wxString msg;
313 
314  // Apply changes from change list
315  for( CHANGELIST_ITEM& item : m_changelist )
316  {
317  SCH_REFERENCE& ref = item.first;
318  PCB_FP_DATA& fpData = *item.second;
319  SCH_COMPONENT* symbol = ref.GetSymbol();
320  SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
321  wxString oldFootprint = ref.GetFootprint();
322  wxString oldValue = ref.GetValue();
323  bool skip = ( ref.GetSymbol()->GetFlags() & SKIP_STRUCT ) > 0;
324 
325  if( m_processReferences && ref.GetRef() != fpData.m_ref && !skip )
326  {
327  ++m_changesCount;
328  msg.Printf( _( "Change '%s' reference designator to '%s'." ),
329  ref.GetFullRef(),
330  fpData.m_ref );
331 
332  if( !m_dryRun )
333  {
335  m_appendUndo = true;
336  symbol->SetRef( &ref.GetSheetPath(), fpData.m_ref );
337  }
338 
340  }
341 
342  if( m_processFootprints && oldFootprint != fpData.m_footprint && !skip )
343  {
344  ++m_changesCount;
345  msg.Printf( _( "Change %s footprint assignment from '%s' to '%s'." ),
346  ref.GetFullRef(),
347  oldFootprint,
348  fpData.m_footprint );
349 
350  if( !m_dryRun )
351  {
353  m_appendUndo = true;
354  symbol->SetFootprint( &ref.GetSheetPath(), fpData.m_footprint );
355  }
356 
358  }
359 
360  if( m_processValues && oldValue != fpData.m_value && !skip )
361  {
362  ++m_changesCount;
363  msg.Printf( _( "Change %s value from '%s' to '%s'." ),
364  ref.GetFullRef(),
365  oldValue,
366  fpData.m_value );
367 
368  if( !m_dryRun )
369  {
371  m_appendUndo = true;
372  symbol->SetValue( &ref.GetSheetPath(), fpData.m_value );
373  }
374 
376  }
377 
378  if( m_processNetNames )
379  {
380  for( const std::pair<const wxString, wxString>& entry : fpData.m_pinMap )
381  {
382  const wxString& pinNumber = entry.first;
383  const wxString& shortNetName = entry.second;
384  SCH_PIN* pin = symbol->GetPin( pinNumber );
385 
386  if( !pin )
387  {
388  msg.Printf( _( "Cannot find %s pin '%s'." ),
389  ref.GetFullRef(),
390  pinNumber );
392 
393  continue;
394  }
395 
396  SCH_CONNECTION* connection = pin->Connection( &ref.GetSheetPath() );
397 
398  if( connection && connection->Name( true ) != shortNetName )
399  {
400  processNetNameChange( ref.GetFullRef(), pin, connection,
401  connection->Name( true ), shortNetName );
402  }
403  }
404  }
405  }
406 
407  if( !m_dryRun )
408  {
411  }
412 
414 }
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
bool m_processNetNames
Definition: backannotate.h:139
bool m_processFootprints
Definition: backannotate.h:136
bool m_processReferences
Definition: backannotate.h:138
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
const wxString GetFootprint() const
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
SCH_COMPONENT * GetSymbol() const
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:102
wxString GetFullRef()
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:482
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.
#define _(s)
Definition: 3d_actions.cpp:33
Schematic symbol object.
Definition: sch_symbol.h:78
const wxString GetValue() const
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:145
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: eda_item.h:118
void SetValue(const SCH_SHEET_PATH *sheet, const wxString &aValue)
Definition: sch_symbol.cpp:614
STATUS_FLAGS GetFlags() const
Definition: eda_item.h:205
void SetFootprint(const SCH_SHEET_PATH *sheet, const wxString &aFootprint)
Definition: sch_symbol.cpp:664
A helper to define a symbol's reference designator in a schematic.
SCH_PIN * GetPin(const wxString &number) const
Find a symbol pin by number.
Definition: sch_symbol.cpp:858

References _, CHANGED, EDA_ITEM::GetFlags(), SCH_REFERENCE::GetFootprint(), SCH_REFERENCE::GetFullRef(), SCH_COMPONENT::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_COMPONENT::SetFootprint(), SCH_COMPONENT::SetRef(), SCH_COMPONENT::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 63 of file backannotate.cpp.

64 {
65  m_changesCount = 0;
66  m_appendUndo = false;
67  wxString msg;
68 
70  && !m_processNetNames )
71  {
72  m_reporter.ReportTail( _( "Select at least one property to back annotate." ),
74  return false;
75  }
76 
77  getPcbModulesFromString( aNetlist );
78 
80  sheets.GetSymbols( m_refs, false );
82 
83  getChangeList();
85 
87 
88  return true;
89 }
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:93
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
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.
#define _(s)
Definition: 3d_actions.cpp:33
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().

Referenced by ReannotateFromPCBNew().

◆ 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 267 of file backannotate.cpp.

268 {
270 
271  std::sort( m_changelist.begin(), m_changelist.end(),
272  []( const CHANGELIST_ITEM& a, const CHANGELIST_ITEM& b )
273  {
274  return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
275  } );
276 
277  size_t i = 0;
278 
279  for( const std::pair<SCH_REFERENCE, std::shared_ptr<PCB_FP_DATA>>& item : m_changelist )
280  {
281  // Refs and changelist are both sorted by paths, so we just go over m_refs and
282  // generate errors before we will find m_refs member to which item linked
283  while( i < m_refs.GetCount() && m_refs[i].GetPath() != item.first.GetPath() )
284  {
285  const SCH_REFERENCE& ref = m_refs[i];
286 
287  if( ref.GetSymbol()->GetIncludeOnBoard() )
288  {
289  wxString msg = wxString::Format( _( "Footprint '%s' is not present on PCB. "
290  "Corresponding symbols in schematic must be "
291  "manually deleted (if desired)." ),
292  m_refs[i].GetFullRef() );
294  }
295 
296  ++i;
297  }
298 
299  ++i;
300  }
301 
302  if( m_matchByReference && !m_frame->ReadyToNetlist( _( "Re-linking footprints requires a fully "
303  "annotated schematic." ) ) )
304  {
305  m_reporter.ReportTail( _( "Footprint re-linking cancelled by user." ), RPT_SEVERITY_ERROR );
306  }
307 }
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
bool GetIncludeOnBoard() const
Definition: sch_symbol.h:662
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:93
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).
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_FP_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
SCH_COMPONENT * GetSymbol() const
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
unsigned GetCount() const
#define _(s)
Definition: 3d_actions.cpp:33
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_COMPONENT::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 92 of file backannotate.cpp.

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

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 210 of file backannotate.cpp.

211 {
212  for( std::pair<const wxString, std::shared_ptr<PCB_FP_DATA>>& fpData : m_pcbFootprints )
213  {
214  const wxString& pcbPath = fpData.first;
215  auto& pcbData = fpData.second;
216  int refIndex;
217  bool foundInMultiunit = false;
218 
219  for( std::pair<const wxString, SCH_REFERENCE_LIST>& item : m_multiUnitsRefs )
220  {
221  SCH_REFERENCE_LIST& refList = item.second;
222 
223  if( m_matchByReference )
224  refIndex = refList.FindRef( pcbPath );
225  else
226  refIndex = refList.FindRefByPath( pcbPath );
227 
228  if( refIndex >= 0 )
229  {
230  // If footprint linked to multi unit symbol, we add all symbol's units to
231  // the change list
232  foundInMultiunit = true;
233 
234  for( size_t i = 0; i < refList.GetCount(); ++i )
235  {
236  refList[ i ].GetSymbol()->ClearFlags(SKIP_STRUCT );
237  m_changelist.emplace_back( CHANGELIST_ITEM( refList[i], pcbData ) );
238  }
239 
240  break;
241  }
242  }
243 
244  if( foundInMultiunit )
245  continue;
246 
247  if( m_matchByReference )
248  refIndex = m_refs.FindRef( pcbPath );
249  else
250  refIndex = m_refs.FindRefByPath( pcbPath );
251 
252  if( refIndex >= 0 )
253  {
254  m_refs[ refIndex ].GetSymbol()->ClearFlags( SKIP_STRUCT );
255  m_changelist.emplace_back( CHANGELIST_ITEM( m_refs[refIndex], pcbData ) );
256  }
257  else
258  {
259  // Haven't found linked symbol in multiunits or common refs. Generate error
260  wxString msg = wxString::Format( _( "Cannot find symbol for footprint '%s'." ),
261  pcbData->m_ref );
263  }
264  }
265 }
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:93
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
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
unsigned GetCount() const
#define _(s)
Definition: 3d_actions.cpp:33
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:143
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:145
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: eda_item.h:118
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 127 of file backannotate.cpp.

128 {
129  auto getStr = []( const PTREE& pt ) -> wxString
130  {
131  return UTF8( pt.front().first );
132  };
133 
134  DSNLEXER lexer( aPayload, FROM_UTF8( __func__ ) );
135  PTREE doc;
136 
137  // NOTE: KiCad's PTREE scanner constructs a property *name* tree, not a property tree.
138  // Every token in the s-expr is stored as a property name; the property's value is then
139  // either the nested s-exprs or an empty PTREE; there are *no* literal property values.
140 
141  Scan( &doc, &lexer );
142 
143  PTREE& tree = doc.get_child( "pcb_netlist" );
144  wxString msg;
145  m_pcbFootprints.clear();
146 
147  for( const std::pair<const std::string, PTREE>& item : tree )
148  {
149  wxString path, value, footprint;
150  std::map<wxString, wxString> pinNetMap;
151  wxASSERT( item.first == "ref" );
152  wxString ref = getStr( item.second );
153 
154  try
155  {
156  if( m_matchByReference )
157  path = ref;
158  else
159  path = getStr( item.second.get_child( "timestamp" ) );
160 
161  if( path == "" )
162  {
163  msg.Printf( _( "Footprint '%s' has no assigned symbol." ), ref );
165  continue;
166  }
167 
168  footprint = getStr( item.second.get_child( "fpid" ) );
169  value = getStr( item.second.get_child( "value" ) );
170 
171  boost::optional<const PTREE&> nets = item.second.get_child_optional( "nets" );
172 
173  if( nets )
174  {
175  for( const std::pair<const std::string, PTREE>& pin_net : nets.get() )
176  {
177  wxASSERT( pin_net.first == "pin_net" );
178  wxString pinNumber = UTF8( pin_net.second.front().first );
179  wxString netName = UTF8( pin_net.second.back().first );
180  pinNetMap[ pinNumber ] = netName;
181  }
182  }
183  }
184  catch( ... )
185  {
186  wxLogWarning( "Cannot parse PCB netlist for back-annotation." );
187  }
188 
189  // Use lower_bound for not to iterate over map twice
190  auto nearestItem = m_pcbFootprints.lower_bound( path );
191 
192  if( nearestItem != m_pcbFootprints.end() && nearestItem->first == path )
193  {
194  // Module with this path already exists - generate error
195  msg.Printf( _( "Footprints '%s' and '%s' linked to same symbol." ),
196  nearestItem->second->m_ref,
197  ref );
199  }
200  else
201  {
202  // Add footprint to the map
203  auto data = std::make_shared<PCB_FP_DATA>( ref, footprint, value, pinNetMap );
204  m_pcbFootprints.insert( nearestItem, std::make_pair( path, data ) );
205  }
206  }
207 }
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
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:102
#define _(s)
Definition: 3d_actions.cpp:33
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 496 of file backannotate.cpp.

499 {
500  wxString msg;
501 
502  // Find a physically-connected driver. We can't use the SCH_CONNECTION's m_driver because
503  // it has already been resolved by merging subgraphs with the same label, etc., and our
504  // name change may cause that resolution to change.
505 
506  std::set<SCH_ITEM*> connectedItems;
507  SCH_ITEM* driver = nullptr;
509 
510  addConnections( aPin, aConnection->Sheet(), connectedItems );
511 
512  for( SCH_ITEM* item : connectedItems )
513  {
515 
516  if( priority > driverPriority )
517  {
518  driver = item;
519  driverPriority = priority;
520  }
521  }
522 
523  switch( driver->Type() )
524  {
525  case SCH_LABEL_T:
526  case SCH_GLOBAL_LABEL_T:
527  case SCH_HIER_LABEL_T:
528  case SCH_SHEET_PIN_T:
529  ++m_changesCount;
530 
531  msg.Printf( _( "Change %s pin %s net label from '%s' to '%s'." ),
532  aRef,
533  aPin->GetNumber(),
534  aOldName,
535  aNewName );
536 
537  if( !m_dryRun )
538  {
539  SCH_SCREEN* screen = aConnection->Sheet().LastScreen();
540 
542  m_appendUndo = true;
543  static_cast<SCH_TEXT*>( driver )->SetText( aNewName );
544  }
545 
547  break;
548 
549  case SCH_PIN_T:
550  {
551  SCH_PIN* schPin = static_cast<SCH_PIN*>( driver );
552  LABEL_SPIN_STYLE spin = orientLabel( schPin );
553 
554  if( schPin->IsPowerConnection() )
555  {
556  msg.Printf( _( "Net %s cannot be changed to '%s' because it is driven by a power "
557  "pin." ), aOldName, aNewName );
558 
560  break;
561  }
562 
563  ++m_changesCount;
564  msg.Printf( _( "Add label '%s' to %s pin %s net." ), aNewName, aRef, aPin->GetNumber() );
565 
566  if( !m_dryRun )
567  {
568  SCHEMATIC_SETTINGS& settings = m_frame->Schematic().Settings();
569  SCH_LABEL* label = new SCH_LABEL( driver->GetPosition(), aNewName );
570  label->SetParent( &m_frame->Schematic() );
571  label->SetTextSize( wxSize( settings.m_DefaultTextSize, settings.m_DefaultTextSize ) );
572  label->SetLabelSpinStyle( spin );
573  label->SetFlags( IS_NEW );
574 
575  SCH_SCREEN* screen = aConnection->Sheet().LastScreen();
577  m_appendUndo = true;
578  }
579 
581  }
582  break;
583 
584  default:
585  break;
586  }
587 }
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:146
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:127
wxString GetNumber() const
Definition: sch_pin.h:107
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:244
virtual wxPoint GetPosition() const
Definition: eda_item.h:302
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:166
REPORTER & m_reporter
Definition: backannotate.h:133
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:203
void addConnections(SCH_ITEM *aItem, const SCH_SHEET_PATH &aSheetPath, std::set< SCH_ITEM * > &connectedItems)
SCHEMATIC & Schematic() const
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:102
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.
#define _(s)
Definition: 3d_actions.cpp:33
These settings were stored in SCH_BASE_FRAME previously.
bool IsPowerConnection() const
Definition: sch_pin.h:123
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:248
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197
#define IS_NEW
New item, just created.
Definition: eda_item.h:107
PRIORITY GetDriverPriority()
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163

References _, addConnections(), SCH_EDIT_FRAME::AddItemToScreenAndUndoList(), CHANGED, CONNECTION_SUBGRAPH::GetDriverPriority(), SCH_PIN::GetNumber(), EDA_ITEM::GetPosition(), 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 119 of file backannotate.cpp.

120 {
121  std::string nullPayload;
122 
124 }
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:56
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:451

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: