KiCad PCB EDA Suite
eeschema/cross-probing.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
30 #include <fctsys.h>
31 #include <pgm_base.h>
32 #include <kiface_i.h>
33 #include <kiway_express.h>
34 #include <macros.h>
35 #include <eda_dde.h>
36 #include <sch_draw_panel.h>
37 
38 #include <sch_edit_frame.h>
39 #include <general.h>
40 #include <eeschema_id.h>
41 #include <lib_draw_item.h>
42 #include <lib_pin.h>
43 #include <sch_component.h>
44 #include <sch_sheet.h>
45 #include <sch_view.h>
46 
47 
64 void SCH_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
65 {
66  char line[1024];
67 
68  strncpy( line, cmdline, sizeof(line) - 1 );
69  line[ sizeof(line) - 1 ] = '\0';
70 
71  char* idcmd = strtok( line, " \n\r" );
72  char* text = strtok( NULL, "\"\n\r" );
73 
74  if( idcmd == NULL )
75  return;
76 
77  if( strcmp( idcmd, "$NET:" ) == 0 )
78  {
79  if( GetToolId() == ID_HIGHLIGHT )
80  {
81  m_SelectedNetName = FROM_UTF8( text );
82 
83  SetStatusText( _( "Selected net: " ) + m_SelectedNetName );
84  std::vector<EDA_ITEM*> itemsToRedraw;
85  SetCurrentSheetHighlightFlags( &itemsToRedraw );
86 
87  // Be sure hightlight change will be redrawn
88  KIGFX::VIEW* view = GetGalCanvas()->GetView();
89 
90  for( auto item : itemsToRedraw )
92 
93  //view->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
94  GetGalCanvas()->Refresh();
95  }
96 
97  return;
98  }
99 
100  if( text == NULL )
101  return;
102 
103  if( strcmp( idcmd, "$PART:" ) != 0 )
104  return;
105 
106  wxString part_ref = FROM_UTF8( text );
107 
108  /* look for a complement */
109  idcmd = strtok( NULL, " \n\r" );
110 
111  if( idcmd == NULL ) // component only
112  {
113  FindComponentAndItem( part_ref, true, FIND_COMPONENT_ONLY, wxEmptyString );
114  return;
115  }
116 
117  text = strtok( NULL, "\"\n\r" );
118 
119  if( text == NULL )
120  return;
121 
122  wxString msg = FROM_UTF8( text );
123 
124  if( strcmp( idcmd, "$REF:" ) == 0 )
125  {
126  FindComponentAndItem( part_ref, true, FIND_REFERENCE, msg );
127  }
128  else if( strcmp( idcmd, "$VAL:" ) == 0 )
129  {
130  FindComponentAndItem( part_ref, true, FIND_VALUE, msg );
131  }
132  else if( strcmp( idcmd, "$PAD:" ) == 0 )
133  {
134  FindComponentAndItem( part_ref, true, FIND_PIN, msg );
135  }
136  else
137  {
138  FindComponentAndItem( part_ref, true, FIND_COMPONENT_ONLY, wxEmptyString );
139  }
140 }
141 
142 
143 std::string FormatProbeItem( EDA_ITEM* aItem, SCH_COMPONENT* aPart )
144 {
145  // Cross probing to Pcbnew if a pin or a component is found
146  switch( aItem->Type() )
147  {
148  case SCH_FIELD_T:
149  case LIB_FIELD_T:
150  if( aPart )
151  return StrPrintf( "$PART: %s", TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
152  break;
153 
154  case SCH_COMPONENT_T:
155  aPart = (SCH_COMPONENT*) aItem;
156  return StrPrintf( "$PART: %s", TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
157 
158  case SCH_SHEET_T:
159  {
160  SCH_SHEET* sheet = (SCH_SHEET*)aItem;
161  return StrPrintf( "$SHEET: %8.8lX", (unsigned long) sheet->GetTimeStamp() );
162  }
163 
164  case LIB_PIN_T:
165  {
166  if( !aPart )
167  break;
168 
169  LIB_PIN* pin = (LIB_PIN*) aItem;
170 
171  if( !pin->GetNumber().IsEmpty() )
172  {
173  return StrPrintf( "$PIN: %s $PART: %s", TO_UTF8( pin->GetNumber() ),
174  TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
175  }
176  else
177  {
178  return StrPrintf( "$PART: %s", TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
179  }
180  }
181  break;
182 
183  default:
184  break;
185  }
186 
187  return "";
188 }
189 
190 
192 {
193  wxASSERT( aObjectToSync ); // fix the caller
194 
195  if( !aObjectToSync )
196  return;
197 
198  std::string packet = FormatProbeItem( aObjectToSync, aLibItem );
199 
200  if( packet.size() )
201  {
202  if( Kiface().IsSingle() )
203  SendCommand( MSG_TO_PCB, packet.c_str() );
204  else
205  {
206  // Typically ExpressMail is going to be s-expression packets, but since
207  // we have existing interpreter of the cross probe packet on the other
208  // side in place, we use that here.
209  Kiway().ExpressMail( FRAME_PCB, MAIL_CROSS_PROBE, packet, this );
210  }
211  }
212 }
213 
214 
215 void SCH_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
216 {
217  std::string packet = StrPrintf( "$NET: %s", TO_UTF8( aNetName ) );
218 
219  if( packet.size() )
220  {
221  if( Kiface().IsSingle() )
222  SendCommand( MSG_TO_PCB, packet.c_str() );
223  else
224  {
225  // Typically ExpressMail is going to be s-expression packets, but since
226  // we have existing interpreter of the cross probe packet on the other
227  // side in place, we use that here.
228  Kiway().ExpressMail( FRAME_PCB, MAIL_CROSS_PROBE, packet, this );
229  }
230  }
231 }
232 
233 
235 {
236  const std::string& payload = mail.GetPayload();
237 
238  switch( mail.Command() )
239  {
240  case MAIL_CROSS_PROBE:
241  ExecuteRemoteCommand( payload.c_str() );
242  break;
243 
245  doUpdatePcb( payload );
246  break;
247 
249  try
250  {
251  backAnnotateFootprints( payload );
252  }
253  catch( const IO_ERROR& DBG( ioe ) )
254  {
255  DBG( printf( "%s: ioe:%s\n", __func__, TO_UTF8( ioe.What() ) );)
256  }
257  break;
258 
259  case MAIL_SCH_REFRESH:
260  {
261  SCH_SCREENS schematic;
262  schematic.UpdateSymbolLinks();
263  schematic.TestDanglingEnds();
264 
266  GetCanvas()->Refresh();
267  break;
268  }
269  case MAIL_IMPORT_FILE:
270  {
271  // Extract file format type and path (plugin type and path separated with \n)
272  size_t split = payload.find( '\n' );
273  wxCHECK( split != std::string::npos, /*void*/ );
274  int importFormat;
275 
276  try
277  {
278  importFormat = std::stoi( payload.substr( 0, split ) );
279  }
280  catch( std::invalid_argument& )
281  {
282  wxFAIL;
283  importFormat = -1;
284  }
285 
286  std::string path = payload.substr( split + 1 );
287  wxASSERT( !path.empty() );
288 
289  if( importFormat >= 0 )
290  importFile( path, importFormat );
291  }
292  break;
293 
294  case MAIL_SCH_SAVE:
295  if( SaveProject() )
296  Kiway().ExpressMail( FRAME_CVPCB, MAIL_STATUS, _( "Schematic saved" ).ToStdString() );
297  break;
298 
299  default:
300  ;
301  }
302 }
Definition of the SCH_SHEET class for Eeschema.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
Class KIWAY_EXPRESS carries a payload from one KIWAY_PLAYER to another within a PROJECT.
Definition: kiway_express.h:39
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_player.h:60
DDE server & client.
int StrPrintf(std::string *aResult, const char *aFormat,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
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:53
void SendCrossProbeNetName(const wxString &aNetName)
Sends a net name to eeschema for highlighting.
CVPCB->SCH footprint stuffing.
Definition: mail_type.h:40
Find a component in the schematic.
void UpdateAllItems(int aUpdateFlags)
Updates all items in the view according to the given flags.
Definition: view.cpp:1434
Find an item by it&#39;s value field.
bool SendCommand(int service, const char *cmdline)
Definition: eda_dde.cpp:132
void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
KIGFX::VIEW * GetView() const
Function GetView() Returns a pointer to the VIEW instance used in the panel.
Class VIEW_ITEM - is an abstract base class for deriving all objects that can be added to a VIEW...
Definition: view_item.h:84
Item needs to be redrawn.
Definition: view_item.h:61
CVPCB->SCH save the schematic.
Definition: mail_type.h:41
bool importFile(const wxString &aFileName, int aFileType)
Load the given filename but sets the path to the current project path.
Field Reference of part, i.e. "IC21".
Find a component pin in the schematic.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
SCH_ITEM * FindComponentAndItem(const wxString &aReference, bool aSearchHierarchy, SCH_SEARCH_T aSearchType, const wxString &aSearchText)
Finds a component in the schematic and an item in this component.
Definition: find.cpp:109
const wxString & GetNumber() const
Definition: lib_pin.h:194
This file contains miscellaneous commonly used macros and functions.
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:386
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes...
Definition: macros.h:47
void SendMessageToPCBNEW(EDA_ITEM *aObjectToSync, SCH_COMPONENT *aPart)
Send a message to Pcbnew via a socket connection.
Item is being added to the view.
Definition: view_item.h:60
SCH_FIELD * GetField(int aFieldNdx) const
Returns a field in this symbol.
const wxString & GetText() const
Function GetText returns the string associated with the text object.
Definition: eda_text.h:143
SCH_DRAW_PANEL * GetCanvas() const override
std::string FormatProbeItem(EDA_ITEM *aItem, SCH_COMPONENT *aPart)
void KiwayMailIn(KIWAY_EXPRESS &aEvent) override
Function KiwayMailIn receives KIWAY_EXPRESS messages from other players.
void backAnnotateFootprints(const std::string &aChangedSetOfReferences)
Definition: backanno.cpp:51
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1530
KIGFX::SCH_VIEW * GetView() const
The the schematic editor to refresh the display.
Definition: mail_type.h:49
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
void doUpdatePcb(const wxString &aUpdateOptions="")
Updates netlist and sends it to pcbnew.
const std::string & GetPayload()
Function Payload returns the payload, which can be any text but it typicall self identifying s-expres...
Definition: kiway_express.h:62
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:115
wxString m_SelectedNetName
Definition the SCH_COMPONENT class for Eeschema.
see class PGM_BASE
void UpdateSymbolLinks(bool aForce=false)
Initialize or reinitialize the weak reference to the LIB_PART for each SCH_COMPONENT found in the ful...
void TestDanglingEnds()
Import a different format file.
Definition: mail_type.h:44
bool SetCurrentSheetHighlightFlags(std::vector< EDA_ITEM * > *aItemsToRedrawList)
Set or reset the BRIGHTENED of connected objects inside the current sheet, according to the highlight...
int GetToolId() const
Definition: draw_frame.h:519
Find an item by it&#39;s reference designator.
#define MSG_TO_PCB
Definition: eda_dde.h:45
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
virtual void ExecuteRemoteCommand(const char *cmdline) override
Execute a remote command send by Pcbnew via a socket, port KICAD_SCH_PORT_SERVICE_NUMBER (currently 4...
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:928
PCB<->SCH, CVPCB->SCH cross-probing.
Definition: mail_type.h:39
#define DBG(x)
Definition: fctsys.h:33
Class VIEW.
Definition: view.h:61
MAIL_T Command()
Function Command returns the MAIL_T associated with this mail.
Definition: kiway_express.h:52
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition: sch_screen.h:523
timestamp_t GetTimeStamp() const
Definition: base_struct.h:207