KiCad PCB EDA Suite
BACK_ANNOTATE Class Reference

Back annotation algorithm class used to recieve, check, and apply a NETLIST from PCBNEW. More...

#include <backannotate.h>

Classes

struct  PCB_MODULE_DATA
 Struct to hold PCBnew module data. More...
 

Public Types

using PCB_MODULES_MAP = std::map< wxString, std::shared_ptr< PCB_MODULE_DATA > >
 

Map to hold NETLIST modules data

More...
 
using CHANGELIST_ITEM = std::pair< SCH_REFERENCE, std::shared_ptr< PCB_MODULE_DATA > >
 

Public Member Functions

 BACK_ANNOTATE (SCH_EDIT_FRAME *aFrame, REPORTER &aReporter, bool aRelinkFootprints, bool aProcessFootprints, bool aProcessValues, bool aProcessReferences, bool aProcessNetNames, bool aIgnoreOtherProjects, 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 epress mail interface and fill m_pcbModules. More...
 
void getChangeList ()
 

Create changelist

More...
 
void checkForUnusedSymbols ()
 Check if some symbols are not represented in PCB modules and vice versa. More...
 
void applyChangelist ()
 Apply changelist to the schematic. More...
 
void processNetNameChange (SCH_CONNECTION *aConn, 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_ignoreOtherProjects
 
bool m_dryRun
 
PCB_MODULES_MAP m_pcbModules
 
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 recieve, check, and apply a NETLIST from PCBNEW.

The following checks are made:

  • Schematic symbol exists, but linked PCBnew module missing
  • PCBnew module exists but no schematic symbol connected to
  • PCBnew module 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 module 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_MODULE_DATA> >

Definition at line 80 of file backannotate.h.

◆ PCB_MODULES_MAP

using BACK_ANNOTATE::PCB_MODULES_MAP = std::map<wxString, std::shared_ptr<PCB_MODULE_DATA> >

Map to hold NETLIST modules 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  aIgnoreOtherProjects,
bool  aDryRun 
)

Definition at line 38 of file backannotate.cpp.

41  :
42  m_reporter( aReporter ),
43  m_matchByReference( aRelinkFootprints ),
44  m_processFootprints( aProcessFootprints ),
45  m_processValues( aProcessValues ),
46  m_processReferences( aProcessReferences ),
47  m_processNetNames( aProcessNetNames ),
48  m_ignoreOtherProjects( aIgnoreOtherProjects ),
49  m_dryRun( aDryRun ),
50  m_frame( aFrame ),
51  m_changesCount( 0 ),
52  m_appendUndo( false )
53 {
54 }
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:119
bool m_processNetNames
Definition: backannotate.h:111
bool m_processFootprints
Definition: backannotate.h:108
bool m_processReferences
Definition: backannotate.h:110
bool m_matchByReference
Definition: backannotate.h:107
REPORTER & m_reporter
Definition: backannotate.h:105
bool m_processValues
Definition: backannotate.h:109
bool m_ignoreOtherProjects
Definition: backannotate.h:112

◆ ~BACK_ANNOTATE()

BACK_ANNOTATE::~BACK_ANNOTATE ( )

Definition at line 57 of file backannotate.cpp.

58 {
59 }

Member Function Documentation

◆ applyChangelist()

void BACK_ANNOTATE::applyChangelist ( )
private

Apply changelist to the schematic.

Definition at line 297 of file backannotate.cpp.

298 {
299  std::set<wxString> handledNetChanges;
300  wxString msg;
301 
302  // Apply changes from change list
303  for( CHANGELIST_ITEM& item : m_changelist )
304  {
305  SCH_REFERENCE& ref = item.first;
306  PCB_MODULE_DATA& module = *item.second;
307  SCH_COMPONENT* comp = ref.GetComp();
308  SCH_SCREEN* screen = ref.GetSheetPath().LastScreen();
309  wxString oldFootprint = ref.GetFootprint();
310  wxString oldValue = ref.GetValue();
311  bool skip = ( ref.GetComp()->GetFlags() & SKIP_STRUCT ) > 0;
312 
313  if( m_processReferences && ref.GetRef() != module.m_ref && !skip )
314  {
315  ++m_changesCount;
316  msg.Printf( _( "Change \"%s\" reference designator to \"%s\"." ),
317  ref.GetFullRef(),
318  module.m_ref );
319 
320  if( !m_dryRun )
321  {
323  m_appendUndo = true;
324  comp->SetRef( &ref.GetSheetPath(), module.m_ref );
325  }
326 
328  }
329 
330  if( m_processFootprints && oldFootprint != module.m_footprint && !skip )
331  {
332  ++m_changesCount;
333  msg.Printf( _( "Change %s footprint from \"%s\" to \"%s\"." ),
334  ref.GetFullRef(),
335  oldFootprint,
336  module.m_footprint );
337 
338  if( !m_dryRun )
339  {
341  m_appendUndo = true;
342  comp->SetFootprint( &ref.GetSheetPath(), module.m_footprint );
343  }
344 
346  }
347 
348  if( m_processValues && oldValue != module.m_value && !skip )
349  {
350  ++m_changesCount;
351  msg.Printf( _( "Change %s value from \"%s\" to \"%s\"." ),
352  ref.GetFullRef(),
353  oldValue,
354  module.m_value );
355 
356  if( !m_dryRun )
357  {
359  m_appendUndo = true;
360  comp->SetValue( &ref.GetSheetPath(), module.m_value );
361  }
362 
364  }
365 
366  if( m_processNetNames )
367  {
368  for( const std::pair<const wxString, wxString>& entry : module.m_pinMap )
369  {
370  const wxString& pinNumber = entry.first;
371  const wxString& shortNetName = entry.second;
372  SCH_PIN* pin = comp->GetPin( pinNumber );
373  SCH_CONNECTION* conn = pin->Connection( ref.GetSheetPath() );
374 
375  wxString key = shortNetName + ref.GetSheetPath().PathAsString();
376 
377  if( handledNetChanges.count( key ) )
378  continue;
379  else
380  handledNetChanges.insert( key );
381 
382  if( conn && conn->Name( true ) != shortNetName )
383  processNetNameChange( conn, conn->Name( true ), shortNetName );
384  }
385  }
386  }
387 
388  if( !m_dryRun )
389  {
392  }
393 
395 }
void UpdateNetHighlightStatus()
void RecalculateConnections(SCH_CLEANUP_FLAGS aCleanupFlags)
Generates the connection data for the entire schematic hierarchy.
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:119
const SCH_SHEET_PATH & GetSheetPath() const
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:128
SCH_COMPONENT * GetComp() const
bool m_processNetNames
Definition: backannotate.h:111
bool m_processFootprints
Definition: backannotate.h:108
SCH_PIN * GetPin(const wxString &number)
Find a symbol pin by number.
wxString PathAsString() const
Function PathAsString the path uses the time stamps which do not changes even when editing sheet para...
bool m_processReferences
Definition: backannotate.h:110
REPORTER & m_reporter
Definition: backannotate.h:105
wxString GetRef() const
bool m_processValues
Definition: backannotate.h:109
wxString Name(bool aIgnoreSheet=false) const
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_MODULE_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
void processNetNameChange(SCH_CONNECTION *aConn, const wxString &aOldName, const wxString &aNewName)
const wxString GetFootprint() const
SCH_CONNECTION * Connection(const SCH_SHEET_PATH &aPath) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:150
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:103
wxString GetFullRef()
Return reference name with unit altogether
void SetRef(const SCH_SHEET_PATH *aSheet, const wxString &aReference)
Set the reference for the given sheet path for this symbol.
SCH_SCREEN * LastScreen()
Function LastScreen.
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
#define _(s)
Definition: 3d_actions.cpp:33
Schematic symbol object.
Definition: sch_component.h:80
const wxString GetValue() const
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:118
void SetValue(const SCH_SHEET_PATH *sheet, const wxString &aValue)
STATUS_FLAGS GetFlags() const
Definition: base_struct.h:234
void SetFootprint(const SCH_SHEET_PATH *sheet, const wxString &aFootprint)
SCH_REFERENCE is used as a helper to define a component's reference designator in a schematic.

References _, CHANGED, SCH_ITEM::Connection(), SCH_REFERENCE::GetComp(), EDA_ITEM::GetFlags(), SCH_REFERENCE::GetFootprint(), SCH_REFERENCE::GetFullRef(), SCH_COMPONENT::GetPin(), SCH_REFERENCE::GetRef(), SCH_REFERENCE::GetSheetPath(), SCH_REFERENCE::GetValue(), SCH_SHEET_PATH::LastScreen(), m_appendUndo, m_changelist, m_changesCount, m_dryRun, BACK_ANNOTATE::PCB_MODULE_DATA::m_footprint, m_frame, BACK_ANNOTATE::PCB_MODULE_DATA::m_pinMap, m_processFootprints, m_processNetNames, m_processReferences, m_processValues, BACK_ANNOTATE::PCB_MODULE_DATA::m_ref, m_reporter, BACK_ANNOTATE::PCB_MODULE_DATA::m_value, SCH_CONNECTION::Name(), NO_CLEANUP, SCH_SHEET_PATH::PathAsString(), processNetNameChange(), SCH_EDIT_FRAME::RecalculateConnections(), REPORTER::ReportHead(), RPT_SEVERITY_ACTION, 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. only report

Parameters
aNetlistnetlist to run back annotation from
Returns
true if success

Definition at line 62 of file backannotate.cpp.

63 {
64  m_changesCount = 0;
65  m_appendUndo = false;
66  wxString msg;
67 
69  {
70  m_reporter.ReportTail( _( "Select at least one property to back annotate." ),
72  return false;
73  }
74 
75  getPcbModulesFromString( aNetlist );
76 
78  sheets.GetComponents( m_refs, false );
80 
81  getChangeList();
83 
85 
86  return true;
87 }
SCH_SHEET_LIST.
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:89
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:119
bool m_processNetNames
Definition: backannotate.h:111
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:94
bool m_processFootprints
Definition: backannotate.h:108
bool m_processReferences
Definition: backannotate.h:110
void getPcbModulesFromString(const std::string &aPayload)
Parse netlist sent over KiWay epress mail interface and fill m_pcbModules.
void GetMultiUnitComponents(SCH_MULTI_UNIT_REFERENCE_MAP &aRefList, bool aIncludePowerSymbols=true) const
Function GetMultiUnitComponents adds a SCH_REFERENCE_LIST object to aRefList for each same-reference ...
void checkForUnusedSymbols()
Check if some symbols are not represented in PCB modules and vice versa.
REPORTER & m_reporter
Definition: backannotate.h:105
bool m_processValues
Definition: backannotate.h:109
void GetComponents(SCH_REFERENCE_LIST &aReferences, bool aIncludePowerSymbols=true, bool aForceIncludeOrphanComponents=false) const
Function GetComponents adds a SCH_REFERENCE() object to aReferences for each component in the list of...
SCHEMATIC & Schematic() const
void getChangeList()
Create changelist
void applyChangelist()
Apply changelist to the schematic.
#define _(s)
Definition: 3d_actions.cpp:33
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:116
SCH_MULTI_UNIT_REFERENCE_MAP m_multiUnitsRefs
Definition: backannotate.h:117

References _, applyChangelist(), checkForUnusedSymbols(), getChangeList(), SCH_SHEET_LIST::GetComponents(), SCH_SHEET_LIST::GetMultiUnitComponents(), getPcbModulesFromString(), SCHEMATIC::GetSheets(), m_appendUndo, m_changesCount, m_frame, 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(), and DIALOG_UPDATE_FROM_PCB::updateData().

◆ checkForUnusedSymbols()

void BACK_ANNOTATE::checkForUnusedSymbols ( )
private

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

m_refs must be sorted by path

Definition at line 263 of file backannotate.cpp.

264 {
266 
267  std::sort( m_changelist.begin(), m_changelist.end(),
268  []( const CHANGELIST_ITEM& a, const CHANGELIST_ITEM& b )
269  {
270  return SCH_REFERENCE_LIST::sortByTimeStamp( a.first, b.first );
271  } );
272 
273  size_t i = 0;
274 
275  for( auto& item : m_changelist )
276  {
277  // Refs and changelist are both sorted by paths, so we just go over m_refs and
278  // generate errors before we will find m_refs member to which item linked
279  while( i < m_refs.GetCount() && m_refs[i].GetPath() != item.first.GetPath() )
280  {
281  wxString msg;
282  msg.Printf( _( "Cannot find footprint for \"%s\" symbol" ), m_refs[i++].GetFullRef() );
284  }
285 
286  ++i;
287  }
288 
290  {
291  m_reporter.ReportTail( _( "Cannot relink footprints because schematic is not fully annotated" ),
293  }
294 }
static bool sortByTimeStamp(const SCH_REFERENCE &item1, const SCH_REFERENCE &item2)
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:119
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:94
bool ReadyToNetlist(bool aSilent=false, bool aSilentAnnotate=false)
Checks if we are ready to write a netlist file for the current schematic.
bool m_matchByReference
Definition: backannotate.h:107
REPORTER & m_reporter
Definition: backannotate.h:105
void SortByTimeStamp()
Function SortComponentsByTimeStamp sort the flat list by Time Stamp (sheet path + timestamp).
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_MODULE_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
unsigned GetCount() const
Function GetCount.
#define _(s)
Definition: 3d_actions.cpp:33
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:116
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:118

References _, SCH_REFERENCE_LIST::GetCount(), m_changelist, m_frame, m_matchByReference, m_refs, m_reporter, SCH_EDIT_FRAME::ReadyToNetlist(), REPORTER::ReportTail(), RPT_SEVERITY_ERROR, 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
aNetlistreference to where netlist will be stored
Returns
true if success

Definition at line 89 of file backannotate.cpp.

90 {
91  if( Kiface().IsSingle() )
92  {
93  DisplayErrorMessage( m_frame, _( "Cannot fetch PCB netlist because eeschema is opened "
94  "in stand-alone mode.\n"
95  "You must launch the KiCad project manager and create "
96  "a project." ) );
97  return false;
98  }
99 
100  KIWAY_PLAYER* frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, false );
101 
102  if( !frame )
103  {
104  wxFileName fn( m_frame->Prj().GetProjectFullName() );
105  fn.SetExt( PcbFileExtension );
106 
107  frame = m_frame->Kiway().Player( FRAME_PCB_EDITOR, true );
108  frame->OpenProjectFiles( std::vector<wxString>( 1, fn.GetFullPath() ) );
109  }
110 
112  return true;
113 }
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Function OpenProjectFiles is abstract, and opens a project or set of files given by aFileList.
Definition: kiway_player.h:118
KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of ...
Definition: kiway_player.h:59
KIWAY & Kiway() const
Function Kiway returns 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:252
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:119
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:343
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
#define PcbFileExtension
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:118
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:437
#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

Create changelist

Definition at line 206 of file backannotate.cpp.

207 {
208  for( std::pair<const wxString, std::shared_ptr<PCB_MODULE_DATA>>& module : m_pcbModules )
209  {
210  const wxString& pcbPath = module.first;
211  auto& pcbData = module.second;
212  int refIndex;
213  bool foundInMultiunit = false;
214 
215  for( std::pair<const wxString, SCH_REFERENCE_LIST>& item : m_multiUnitsRefs )
216  {
217  SCH_REFERENCE_LIST& refList = item.second;
218 
219  if( m_matchByReference )
220  refIndex = refList.FindRef( pcbPath );
221  else
222  refIndex = refList.FindRefByPath( pcbPath );
223 
224  if( refIndex >= 0 )
225  {
226  // If module linked to multi unit symbol, we add all symbol's units to
227  // the change list
228  foundInMultiunit = true;
229 
230  for( size_t i = 0; i < refList.GetCount(); ++i )
231  {
232  refList[i].GetComp()->ClearFlags( SKIP_STRUCT );
233  m_changelist.emplace_back( CHANGELIST_ITEM( refList[i], pcbData ) );
234  }
235 
236  break;
237  }
238  }
239 
240  if( foundInMultiunit )
241  continue;
242 
243  if( m_matchByReference )
244  refIndex = m_refs.FindRef( pcbPath );
245  else
246  refIndex = m_refs.FindRefByPath( pcbPath );
247 
248  if( refIndex >= 0 )
249  {
250  m_refs[refIndex].GetComp()->ClearFlags( SKIP_STRUCT );
251  m_changelist.emplace_back( CHANGELIST_ITEM( m_refs[refIndex], pcbData ) );
252  }
253  else
254  {
255  // Haven't found linked symbol in multiunits or common refs. Generate error
256  wxString msg;
257  msg.Printf( _( "Cannot find symbol for \"%s\" footprint" ), pcbData->m_ref );
259  }
260  }
261 }
int FindRefByPath(const wxString &aPath) const
searches the list for a component with the given KIID path
#define SKIP_STRUCT
flag indicating that the structure should be ignored
Definition: base_struct.h:128
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:94
PCB_MODULES_MAP m_pcbModules
Definition: backannotate.h:115
bool m_matchByReference
Definition: backannotate.h:107
SCH_REFERENCE_LIST is used to create a flattened list of components because in a complex hierarchy,...
int FindRef(const wxString &aPath) const
searches the list for a component with a given reference.
REPORTER & m_reporter
Definition: backannotate.h:105
std::pair< SCH_REFERENCE, std::shared_ptr< PCB_MODULE_DATA > > CHANGELIST_ITEM
Definition: backannotate.h:80
unsigned GetCount() const
Function GetCount.
#define _(s)
Definition: 3d_actions.cpp:33
SCH_REFERENCE_LIST m_refs
Definition: backannotate.h:116
std::deque< CHANGELIST_ITEM > m_changelist
Definition: backannotate.h:118
SCH_MULTI_UNIT_REFERENCE_MAP m_multiUnitsRefs
Definition: backannotate.h:117

References _, SCH_REFERENCE_LIST::FindRef(), SCH_REFERENCE_LIST::FindRefByPath(), SCH_REFERENCE_LIST::GetCount(), m_changelist, m_matchByReference, m_multiUnitsRefs, m_pcbModules, 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 epress mail interface and fill m_pcbModules.

Parameters
aPayload- netlist from PCBnew
Returns
number of errors during parsing

Definition at line 124 of file backannotate.cpp.

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

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

Referenced by BackAnnotateSymbols().

◆ processNetNameChange()

void BACK_ANNOTATE::processNetNameChange ( SCH_CONNECTION aConn,
const wxString &  aOldName,
const wxString &  aNewName 
)
private

Definition at line 465 of file backannotate.cpp.

467 {
468  wxString msg;
469  SCH_ITEM* driver = aConn->Driver();
470 
471  auto editMatchingLabels =
472  [this]( SCH_SCREEN* aScreen, KICAD_T aType, const wxString& oldName,
473  const wxString& newName )
474  {
475  for( SCH_ITEM* schItem : aScreen->Items().OfType( aType ) )
476  {
477  SCH_TEXT* label = static_cast<SCH_TEXT*>( schItem );
478 
479  if( EscapeString( label->GetShownText(), CTX_NETNAME ) == oldName )
480  {
482  m_appendUndo = true;
483  static_cast<SCH_TEXT*>( label )->SetText( newName );
484  }
485  }
486  };
487 
488  switch( driver->Type() )
489  {
490  case SCH_LABEL_T:
491  ++m_changesCount;
492  msg.Printf( _( "Change \"%s\" labels to \"%s\"." ), aOldName, aNewName );
493 
494  if( !m_dryRun )
495  {
496  SCH_SCREEN* screen = aConn->Sheet().LastScreen();
497 
498  for( SCH_ITEM* label : screen->Items().OfType( SCH_LABEL_T ) )
499  {
500  SCH_CONNECTION* conn = label->Connection( aConn->Sheet() );
501 
502  if( conn && conn->Driver() == driver )
503  {
505  m_appendUndo = true;
506  static_cast<SCH_TEXT*>( label )->SetText( aNewName );
507  }
508  }
509  }
510 
512  break;
513 
514  case SCH_GLOBAL_LABEL_T:
515  ++m_changesCount;
516  msg.Printf( _( "Change \"%s\" global labels to \"%s\"." ), aOldName, aNewName );
517 
518  if( !m_dryRun )
519  {
520  for( const SCH_SHEET_PATH& sheet : m_frame->Schematic().GetSheets() )
521  editMatchingLabels( sheet.LastScreen(), SCH_GLOBAL_LABEL_T, aOldName, aNewName );
522  }
523 
525  break;
526 
527  case SCH_HIER_LABEL_T:
528  ++m_changesCount;
529  msg.Printf( _( "Change \"%s\" hierarchical label to \"%s\"." ), aOldName, aNewName );
530 
531  if( !m_dryRun )
532  {
533  SCH_SCREEN* screen = aConn->Sheet().LastScreen();
534  editMatchingLabels( screen, SCH_HIER_LABEL_T, aOldName, aNewName );
535 
536  SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( driver->GetParent() );
537  wxASSERT( sheet );
538 
539  if( !sheet )
540  break;
541 
542  screen = sheet->GetScreen();
543 
544  for( SCH_SHEET_PIN* pin : sheet->GetPins() )
545  {
546  if( EscapeString( pin->GetShownText(), CTX_NETNAME ) == aOldName )
547  {
549  m_appendUndo = true;
550  static_cast<SCH_TEXT*>( pin )->SetText( aNewName );
551  }
552  }
553  }
554 
556  break;
557 
558  case SCH_SHEET_PIN_T:
559  ++m_changesCount;
560  msg.Printf( _( "Change \"%s\" hierarchical label to \"%s\"." ), aOldName, aNewName );
561 
562  if( !m_dryRun )
563  {
564  SCH_SCREEN* screen = aConn->Sheet().LastScreen();
566  m_appendUndo = true;
567  static_cast<SCH_TEXT*>( driver )->SetText( aNewName );
568 
569  SCH_SHEET* sheet = static_cast<SCH_SHEET_PIN*>( driver )->GetParent();
570  screen = sheet->GetScreen();
571  editMatchingLabels( screen, SCH_HIER_LABEL_T, aOldName, aNewName );
572  }
573 
575  break;
576 
577  case SCH_PIN_T:
578  {
579  SCH_PIN* schPin = static_cast<SCH_PIN*>( driver );
580  LABEL_SPIN_STYLE spin = orientLabel( schPin );
581 
582  if( schPin->IsPowerConnection() )
583  {
584  msg.Printf( _( "Net \"%s\" cannot be changed to \"%s\" because it "
585  "is driven by a power pin." ),
586  aOldName,
587  aNewName );
588 
590  break;
591  }
592 
593  ++m_changesCount;
594  msg.Printf( _( "Add label \"%s\" to net \"%s\"." ), aNewName, aOldName );
595 
596  if( !m_dryRun )
597  {
598  SCHEMATIC_SETTINGS& settings = m_frame->Schematic().Settings();
599  SCH_LABEL* label = new SCH_LABEL( driver->GetPosition(), aNewName );
600  label->SetParent( &m_frame->Schematic() );
601  label->SetTextSize( wxSize( settings.m_DefaultTextSize,
602  settings.m_DefaultTextSize ) );
603  label->SetLabelSpinStyle( spin );
604  label->SetFlags( IS_NEW );
605 
606  SCH_SCREEN* screen = aConn->Sheet().LastScreen();
608  m_appendUndo = true;
609  }
610 
612  }
613  break;
614 
615  default:
616  break;
617  }
618 }
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:89
SCH_EDIT_FRAME * m_frame
Definition: backannotate.h:119
SCHEMATIC_SETTINGS & Settings() const
Definition: schematic.cpp:124
EE_TYPE OfType(KICAD_T aType)
Definition: sch_rtree.h:219
void SetTextSize(const wxSize &aNewSize)
Definition: eda_text.h:238
virtual wxPoint GetPosition() const
Definition: base_struct.h:337
SCH_SCREEN * GetScreen() const
Definition: sch_sheet.h:282
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
virtual void SetParent(EDA_ITEM *aParent)
Definition: base_struct.h:196
REPORTER & m_reporter
Definition: backannotate.h:105
#define IS_NEW
New item, just created.
Definition: base_struct.h:117
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:232
SCHEMATIC & Schematic() const
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
SCH_ITEM * Driver() const
EDA_ITEM * GetParent() const
Definition: base_struct.h:195
Define a sheet pin (label) used in sheets to create hierarchical schematics.
Definition: sch_sheet.h:84
SCH_SHEET_PATH.
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: sch_text.cpp:483
SCH_CONNECTION * Connection(const SCH_SHEET_PATH &aPath) const
Retrieve the connection associated with this object in the given sheet.
Definition: sch_item.cpp:150
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:216
std::vector< SCH_SHEET_PIN * > & GetPins()
Definition: sch_sheet.h:362
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.h:103
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()
Function LastScreen.
static LABEL_SPIN_STYLE orientLabel(SCH_PIN *aPin)
Each graphical item can have a SCH_CONNECTION describing its logical connection (to a bus or net).
#define _(s)
Definition: 3d_actions.cpp:33
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which ar...
Definition: string.cpp:77
EE_RTREE & Items()
Definition: sch_screen.h:158
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:229
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:194
KICAD_T Type() const
Function Type()
Definition: base_struct.h:193

References _, SCH_EDIT_FRAME::AddItemToScreenAndUndoList(), CHANGED, CTX_NETNAME, SCH_CONNECTION::Driver(), EscapeString(), EDA_ITEM::GetParent(), SCH_SHEET::GetPins(), EDA_ITEM::GetPosition(), SCH_SHEET::GetScreen(), SCHEMATIC::GetSheets(), SCH_TEXT::GetShownText(), IS_NEW, SCH_PIN::IsPowerConnection(), SCH_SCREEN::Items(), SCH_SHEET_PATH::LastScreen(), m_appendUndo, m_changesCount, SCHEMATIC_SETTINGS::m_DefaultTextSize, m_dryRun, m_frame, m_reporter, EE_RTREE::OfType(), 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 116 of file backannotate.cpp.

117 {
118  std::string nullPayload;
119 
121 }
KIWAY & Kiway() const
Function Kiway returns 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:119
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:437

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 122 of file backannotate.h.

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

◆ m_changelist

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

Definition at line 118 of file backannotate.h.

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

◆ m_changesCount

int BACK_ANNOTATE::m_changesCount
private

Definition at line 121 of file backannotate.h.

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

◆ m_dryRun

bool BACK_ANNOTATE::m_dryRun
private

Definition at line 113 of file backannotate.h.

Referenced by applyChangelist(), and processNetNameChange().

◆ m_frame

◆ m_ignoreOtherProjects

bool BACK_ANNOTATE::m_ignoreOtherProjects
private

Definition at line 112 of file backannotate.h.

◆ m_matchByReference

bool BACK_ANNOTATE::m_matchByReference
private

Definition at line 107 of file backannotate.h.

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

◆ m_multiUnitsRefs

SCH_MULTI_UNIT_REFERENCE_MAP BACK_ANNOTATE::m_multiUnitsRefs
private

Definition at line 117 of file backannotate.h.

Referenced by BackAnnotateSymbols(), and getChangeList().

◆ m_pcbModules

PCB_MODULES_MAP BACK_ANNOTATE::m_pcbModules
private

Definition at line 115 of file backannotate.h.

Referenced by getChangeList(), and getPcbModulesFromString().

◆ m_processFootprints

bool BACK_ANNOTATE::m_processFootprints
private

Definition at line 108 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_processNetNames

bool BACK_ANNOTATE::m_processNetNames
private

Definition at line 111 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_processReferences

bool BACK_ANNOTATE::m_processReferences
private

Definition at line 110 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_processValues

bool BACK_ANNOTATE::m_processValues
private

Definition at line 109 of file backannotate.h.

Referenced by applyChangelist(), and BackAnnotateSymbols().

◆ m_refs

SCH_REFERENCE_LIST BACK_ANNOTATE::m_refs
private

Definition at line 116 of file backannotate.h.

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

◆ m_reporter


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