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 <class_drawpanel.h>
37 
38 #include <schframe.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 
46 
62 void SCH_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
63 {
64  char line[1024];
65 
66  strncpy( line, cmdline, sizeof(line) - 1 );
67  line[ sizeof(line) - 1 ] = '\0';
68 
69  char* idcmd = strtok( line, " \n\r" );
70  char* text = strtok( NULL, "\"\n\r" );
71 
72  if( (idcmd == NULL) || (text == NULL) )
73  return;
74 
75  if( strcmp( idcmd, "$PART:" ) != 0 )
76  return;
77 
78  wxString part_ref = FROM_UTF8( text );
79 
80  /* look for a complement */
81  idcmd = strtok( NULL, " \n\r" );
82 
83  if( idcmd == NULL ) // component only
84  {
85  FindComponentAndItem( part_ref, true, FIND_COMPONENT_ONLY, wxEmptyString, false );
86  return;
87  }
88 
89  text = strtok( NULL, "\"\n\r" );
90 
91  if( text == NULL )
92  return;
93 
94  wxString msg = FROM_UTF8( text );
95 
96  if( strcmp( idcmd, "$REF:" ) == 0 )
97  {
98  FindComponentAndItem( part_ref, true, FIND_REFERENCE, msg, false );
99  }
100  else if( strcmp( idcmd, "$VAL:" ) == 0 )
101  {
102  FindComponentAndItem( part_ref, true, FIND_VALUE, msg, false );
103  }
104  else if( strcmp( idcmd, "$PAD:" ) == 0 )
105  {
106  FindComponentAndItem( part_ref, true, FIND_PIN, msg, false );
107  }
108  else
109  {
110  FindComponentAndItem( part_ref, true, FIND_COMPONENT_ONLY, wxEmptyString, false );
111  }
112 }
113 
114 
115 std::string FormatProbeItem( EDA_ITEM* aItem, SCH_COMPONENT* aPart )
116 {
117  // Cross probing to Pcbnew if a pin or a component is found
118  switch( aItem->Type() )
119  {
120  case SCH_FIELD_T:
121  case LIB_FIELD_T:
122  if( aPart )
123  return StrPrintf( "$PART: %s",
124  TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
125  break;
126 
127  case SCH_COMPONENT_T:
128  aPart = (SCH_COMPONENT*) aItem;
129  return StrPrintf( "$PART: %s", TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
130 
131  case SCH_SHEET_T:
132  {
133  SCH_SHEET* sheet = (SCH_SHEET*)aItem;
134  return StrPrintf( "$SHEET: %8.8lX", (unsigned long) sheet->GetTimeStamp() );
135  }
136 
137  case LIB_PIN_T:
138  {
139  if( !aPart )
140  break;
141 
142  LIB_PIN* pin = (LIB_PIN*) aItem;
143 
144  if( !pin->GetNumber().IsEmpty() )
145  {
146  return StrPrintf( "$PIN: %s $PART: %s", TO_UTF8( pin->GetNumber() ),
147  TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
148  }
149  else
150  {
151  return StrPrintf( "$PART: %s", TO_UTF8( aPart->GetField( REFERENCE )->GetText() ) );
152  }
153  }
154  break;
155 
156  default:
157  break;
158  }
159 
160  return "";
161 }
162 
163 
165 {
166  wxASSERT( aObjectToSync ); // fix the caller
167 
168  if( !aObjectToSync )
169  return;
170 
171  std::string packet = FormatProbeItem( aObjectToSync, aLibItem );
172 
173  if( packet.size() )
174  {
175  if( Kiface().IsSingle() )
176  SendCommand( MSG_TO_PCB, packet.c_str() );
177  else
178  {
179  // Typically ExpressMail is going to be s-expression packets, but since
180  // we have existing interpreter of the cross probe packet on the other
181  // side in place, we use that here.
182  Kiway().ExpressMail( FRAME_PCB, MAIL_CROSS_PROBE, packet, this );
183  }
184  }
185 }
186 
187 
189 {
190  const std::string& payload = mail.GetPayload();
191 
192  switch( mail.Command() )
193  {
194  case MAIL_CROSS_PROBE:
195  ExecuteRemoteCommand( payload.c_str() );
196  break;
197 
199  {
200  wxCommandEvent dummy;
201 
202  OnUpdatePCB( dummy );
203  break;
204  }
205 
207  try
208  {
209  backAnnotateFootprints( payload );
210  }
211  catch( const IO_ERROR& DBG( ioe ) )
212  {
213  DBG( printf( "%s: ioe:%s\n", __func__, TO_UTF8( ioe.What() ) );)
214  }
215  break;
216 
217  case MAIL_SCH_REFRESH:
218  GetCanvas()->Refresh();
219  break;
220 
221  default:
222  ;
223  }
224 }
Definition of the SCH_SHEET class for Eeschema.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:225
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.
Find an item by it's reference designator.
Definition: schframe.h:107
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
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
CVPCB->SCH footprint stuffing at cvpcb termination.
Definition: mail_type.h:40
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:331
bool SendCommand(int service, const char *cmdline)
Definition: eda_dde.cpp:134
Field Reference of part, i.e. "IC21".
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
const wxString & GetNumber() const
Definition: lib_pin.h:183
This file contains miscellaneous commonly used macros and functions.
Find a component pin in the schematic.
Definition: schframe.h:106
int StrPrintf(std::string *result, const char *format,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
#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.
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:130
SCH_ITEM * FindComponentAndItem(const wxString &aReference, bool aSearchHierarchy, SCH_SEARCH_T aSearchType, const wxString &aSearchText, bool aWarpMouse)
Finds a component in the schematic and an item in this component.
Definition: find.cpp:109
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
void OnUpdatePCB(wxCommandEvent &event)
Definition: schframe.cpp:860
The the schematic editor to refresh the display.
Definition: mail_type.h:47
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
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
Find an item by it's value field.
Definition: schframe.h:108
Definition the SCH_COMPONENT class for Eeschema.
see class PGM_BASE
Find a component in the schematic.
Definition: schframe.h:105
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, const std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:387
#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:178
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...
PCB<->SCH, CVPCB->SCH cross-probing.
Definition: mail_type.h:39
#define DBG(x)
Definition: fctsys.h:33
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:47
timestamp_t GetTimeStamp() const
Definition: base_struct.h:231