KiCad PCB EDA Suite
pcbnew/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) 2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 
37 #include <fctsys.h>
38 #include <pgm_base.h>
39 #include <kiface_i.h>
40 #include <kiway_express.h>
41 #include <pcb_edit_frame.h>
42 #include <eda_dde.h>
43 #include <macros.h>
44 
45 #include <pcbnew_id.h>
46 #include <class_board.h>
47 #include <class_module.h>
48 #include <class_track.h>
49 #include <class_zone.h>
50 
51 #include <collectors.h>
52 #include <pcbnew.h>
53 #include <pcb_netlist.h>
55 
56 #include <tools/pcb_actions.h>
57 #include <tool/tool_manager.h>
58 #include <tools/selection_tool.h>
59 #include <pcb_draw_panel_gal.h>
60 #include <pcb_painter.h>
61 
62 /* Execute a remote command send by Eeschema via a socket,
63  * port KICAD_PCB_PORT_SERVICE_NUMBER
64  * cmdline = received command from Eeschema
65  * Commands are
66  * $PART: "reference" put cursor on component
67  * $PIN: "pin name" $PART: "reference" put cursor on the footprint pin
68  * $NET: "net name" highlight the given net (if highlight tool is active)
69  * $CLEAR Clear existing highlight
70  * They are a keyword followed by a quoted string.
71  */
72 void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
73 {
74  char line[1024];
75  wxString msg;
76  wxString modName;
77  char* idcmd;
78  char* text;
79  MODULE* module = NULL;
80  D_PAD* pad = NULL;
81  BOARD* pcb = GetBoard();
82  wxPoint pos;
83 
84  strncpy( line, cmdline, sizeof(line) - 1 );
85  line[sizeof(line) - 1] = 0;
86 
87  idcmd = strtok( line, " \n\r" );
88  text = strtok( NULL, "\"\n\r" );
89 
90  if( idcmd == NULL )
91  return;
92 
93  if( strcmp( idcmd, "$NET:" ) == 0 )
94  {
96  {
97  wxString net_name = FROM_UTF8( text );
98  NETINFO_ITEM* netinfo = pcb->FindNet( net_name );
99  int netcode = -1;
100 
101  if( netinfo )
102  netcode = netinfo->GetNet();
103 
104  if( netcode > 0 )
105  {
106  pcb->SetHighLightNet( netcode );
107 
108  if( netinfo )
109  {
110  MSG_PANEL_ITEMS items;
111  netinfo->GetMsgPanelInfo( GetUserUnits(), items );
112  SetMsgPanel( items );
113  }
114  }
115 
116  if( IsGalCanvasActive() )
117  {
118  auto view = m_toolManager->GetView();
119  auto rs = view->GetPainter()->GetSettings();
120  rs->SetHighlight( ( netcode >= 0 ), netcode );
121  view->UpdateAllLayersColor();
122 
123  BOX2I bbox;
124  bool first = true;
125 
126  auto merge_area = [netcode, &bbox, &first]( BOARD_CONNECTED_ITEM* aItem )
127  {
128  if( aItem->GetNetCode() == netcode )
129  {
130  if( first )
131  {
132  bbox = aItem->GetBoundingBox();
133  first = false;
134  }
135  else
136  {
137  bbox.Merge( aItem->GetBoundingBox() );
138  }
139  }
140  };
141 
142  for( auto zone : pcb->Zones() )
143  merge_area( zone );
144 
145  for( auto track : pcb->Tracks() )
146  merge_area( track );
147 
148  for( auto mod : pcb->Modules() )
149  for ( auto mod_pad : mod->Pads() )
150  merge_area( mod_pad );
151 
152  if( netcode > 0 && bbox.GetWidth() > 0 && bbox.GetHeight() > 0 )
153  {
154  auto bbSize = bbox.Inflate( bbox.GetWidth() * 0.2f ).GetSize();
155  auto screenSize = view->ToWorld( GetGalCanvas()->GetClientSize(), false );
156  double ratio = std::max( fabs( bbSize.x / screenSize.x ),
157  fabs( bbSize.y / screenSize.y ) );
158  double scale = view->GetScale() / ratio;
159 
160  view->SetScale( scale );
161  view->SetCenter( bbox.Centre() );
162  }
163 
164  GetGalCanvas()->Refresh();
165  }
166  else
167  {
168  if( netcode > 0 )
169  {
170  pcb->HighLightON();
171  pcb->SetHighLightNet( netcode );
172  }
173  else
174  {
175  pcb->HighLightOFF();
176  pcb->SetHighLightNet( -1 );
177  }
178  }
179  }
180 
181  return;
182  }
183  else if( strcmp( idcmd, "$CLEAR" ) == 0 )
184  {
185  auto view = m_toolManager->GetView();
186  auto rs = view->GetPainter()->GetSettings();
187  rs->SetHighlight( false );
188  view->UpdateAllLayersColor();
189 
190  pcb->ResetHighLight();
191  SetMsgPanel( pcb );
192 
193  GetGalCanvas()->Refresh();
194  }
195 
196  if( text == NULL )
197  return;
198 
199  if( strcmp( idcmd, "$PART:" ) == 0 )
200  {
201  modName = FROM_UTF8( text );
202 
203  module = pcb->FindModuleByReference( modName );
204 
205  if( module )
206  msg.Printf( _( "%s found" ), modName );
207  else
208  msg.Printf( _( "%s not found" ), modName );
209 
210  SetStatusText( msg );
211 
212  if( module )
213  pos = module->GetPosition();
214  }
215  else if( strcmp( idcmd, "$SHEET:" ) == 0 )
216  {
217  msg.Printf( _( "Selecting all from sheet \"%s\"" ), FROM_UTF8( text ) );
218  wxString sheetStamp( FROM_UTF8( text ) );
219  SetStatusText( msg );
221  static_cast<void*>( &sheetStamp ) );
222  return;
223  }
224  else if( strcmp( idcmd, "$PIN:" ) == 0 )
225  {
226  wxString pinName;
227  int netcode = -1;
228 
229  pinName = FROM_UTF8( text );
230 
231  text = strtok( NULL, " \n\r" );
232 
233  if( text && strcmp( text, "$PART:" ) == 0 )
234  text = strtok( NULL, "\"\n\r" );
235 
236  modName = FROM_UTF8( text );
237 
238  module = pcb->FindModuleByReference( modName );
239 
240  if( module )
241  pad = module->FindPadByName( pinName );
242 
243  if( pad )
244  {
245  netcode = pad->GetNetCode();
246 
247  // put cursor on the pad:
248  pos = pad->GetPosition();
249  }
250 
251  if( netcode > 0 ) // highlight the pad net
252  {
253  pcb->HighLightON();
254  pcb->SetHighLightNet( netcode );
255  }
256  else
257  {
258  pcb->HighLightOFF();
259  pcb->SetHighLightNet( -1 );
260  }
261 
262  if( module == NULL )
263  {
264  msg.Printf( _( "%s not found" ), modName );
265  }
266  else if( pad == NULL )
267  {
268  msg.Printf( _( "%s pin %s not found" ), modName, pinName );
269  SetCurItem( module );
270  }
271  else
272  {
273  msg.Printf( _( "%s pin %s found" ), modName, pinName );
274  SetCurItem( pad );
275  }
276 
277  SetStatusText( msg );
278  }
279 
280  if( module ) // if found, center the module on screen, and redraw the screen.
281  {
282  if( IsGalCanvasActive() )
283  {
285  true,
286  pad ?
287  static_cast<BOARD_ITEM*>( pad ) :
288  static_cast<BOARD_ITEM*>( module )
289  );
290  }
291  else
292  {
293  SetCrossHairPosition( pos );
294  RedrawScreen( pos, false );
295  }
296  }
297 }
298 
299 
300 std::string FormatProbeItem( BOARD_ITEM* aItem )
301 {
302  MODULE* module;
303 
304  if( !aItem )
305  return "$CLEAR: \"HIGHLIGHTED\""; // message to clear highlight state
306 
307  switch( aItem->Type() )
308  {
309  case PCB_MODULE_T:
310  module = (MODULE*) aItem;
311  return StrPrintf( "$PART: \"%s\"", TO_UTF8( module->GetReference() ) );
312 
313  case PCB_PAD_T:
314  {
315  module = (MODULE*) aItem->GetParent();
316  wxString pad = ((D_PAD*)aItem)->GetName();
317 
318  return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
319  TO_UTF8( module->GetReference() ),
320  TO_UTF8( pad ) );
321  }
322 
323  case PCB_MODULE_TEXT_T:
324  {
325  module = static_cast<MODULE*>( aItem->GetParent() );
326 
327  TEXTE_MODULE* text_mod = static_cast<TEXTE_MODULE*>( aItem );
328 
329  const char* text_key;
330 
331  /* This can't be a switch since the break need to pull out
332  * from the outer switch! */
333  if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
334  text_key = "$REF:";
335  else if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
336  text_key = "$VAL:";
337  else
338  break;
339 
340  return StrPrintf( "$PART: \"%s\" %s \"%s\"",
341  TO_UTF8( module->GetReference() ),
342  text_key,
343  TO_UTF8( text_mod->GetText() ) );
344  }
345 
346  default:
347  break;
348  }
349 
350  return "";
351 }
352 
353 
354 /* Send a remote command to Eeschema via a socket,
355  * aSyncItem = item to be located on schematic (module, pin or text)
356  * Commands are
357  * $PART: "reference" put cursor on component anchor
358  * $PART: "reference" $PAD: "pad number" put cursor on the component pin
359  * $PART: "reference" $REF: "reference" put cursor on the component ref
360  * $PART: "reference" $VAL: "value" put cursor on the component value
361  */
363 {
364  std::string packet = FormatProbeItem( aSyncItem );
365 
366  if( packet.size() )
367  {
368  if( Kiface().IsSingle() )
369  SendCommand( MSG_TO_SCH, packet.c_str() );
370  else
371  {
372  // Typically ExpressMail is going to be s-expression packets, but since
373  // we have existing interpreter of the cross probe packet on the other
374  // side in place, we use that here.
375  Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
376  }
377  }
378 }
379 
380 
381 void PCB_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
382 {
383  std::string packet = StrPrintf( "$NET: \"%s\"", TO_UTF8( aNetName ) );
384 
385  if( packet.size() )
386  {
387  if( Kiface().IsSingle() )
388  SendCommand( MSG_TO_SCH, packet.c_str() );
389  else
390  {
391  // Typically ExpressMail is going to be s-expression packets, but since
392  // we have existing interpreter of the cross probe packet on the other
393  // side in place, we use that here.
394  Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
395  }
396  }
397 }
398 
399 
401 {
402  const std::string& payload = mail.GetPayload();
403 
404  switch( mail.Command() )
405  {
406  case MAIL_CROSS_PROBE:
407  ExecuteRemoteCommand( payload.c_str() );
408  break;
409 
410  case MAIL_PCB_UPDATE:
411  {
412  NETLIST netlist;
413 
415  UpdatePCBFromNetlist( netlist );
416 
417  break;
418  }
419 
420  case MAIL_IMPORT_FILE:
421  {
422  // Extract file format type and path (plugin type and path separated with \n)
423  size_t split = payload.find( '\n' );
424  wxCHECK( split != std::string::npos, /*void*/ );
425  int importFormat;
426 
427  try
428  {
429  importFormat = std::stoi( payload.substr( 0, split ) );
430  }
431  catch( std::invalid_argument& )
432  {
433  wxFAIL;
434  importFormat = -1;
435  }
436 
437  std::string path = payload.substr( split + 1 );
438  wxASSERT( !path.empty() );
439 
440  if( importFormat >= 0 )
441  importFile( path, importFormat );
442  }
443 
444  // many many others.
445  default:
446  ;
447  }
448 }
449 
TOOL_MANAGER * m_toolManager
Definition: draw_frame.h:130
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:252
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:61
int GetNetCode() const
Function GetNetCode.
DDE server & client.
void SendCrossProbeNetName(const wxString &aNetName)
Sends a net name to eeschema for highlighting.
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:61
bool importFile(const wxString &aFileName, int aFileType)
Load the given filename but sets the path to the current project path.
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
void UpdatePCBFromNetlist(NETLIST &aNetlist)
Function UpdatePCBFromNetlist.
Class BOARD to handle a board.
#define MSG_TO_SCH
Definition: eda_dde.h:46
EDA_DRAW_PANEL_GAL * GetGalCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
Definition: draw_frame.h:931
void ResetHighLight()
Function ResetHighLight Reset all high light data to the init state.
Definition: class_board.h:358
void SetCurItem(BOARD_ITEM *aItem, bool aDisplayInfo=true)
Function SetCurItem sets the currently selected item and displays it in the MsgPanel.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:125
void HighLightOFF()
Function HighLightOFF Disable highlight.
Definition: class_board.h:389
Classes to handle copper zones.
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
bool SendCommand(int service, const char *cmdline)
Definition: eda_dde.cpp:132
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
PAINTER * GetPainter() const
Function GetPainter() Returns the painter object used by the view for drawing VIEW_ITEMS.
Definition: view.h:199
static TOOL_ACTION crossProbeSchToPcb
Definition: pcb_actions.h:316
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
Functions relatives to tracks, vias and segments used to fill zones.
This file contains miscellaneous commonly used macros and functions.
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:462
bool IsGalCanvasActive() const
Function IsGalCanvasActive is used to check which canvas (GAL-based or standard) is currently in use.
Definition: draw_frame.h:924
#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 KiwayMailIn(KIWAY_EXPRESS &aEvent) override
Function KiwayMailIn receives KIWAY_EXPRESS messages from other players.
class MODULE, a footprint
Definition: typeinfo.h:89
D_PAD * FindPadByName(const wxString &aPadName) const
Function FindPadByName returns a D_PAD* with a matching name.
Class NETLIST stores all of information read from a netlist along with the flags used to update the N...
Definition: pcb_netlist.h:214
DLIST_ITERATOR_WRAPPER< MODULE > Modules()
Definition: class_board.h:252
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:115
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
virtual void ExecuteRemoteCommand(const char *cmdline) override
Execute a remote command send by Eeschema via a socket, port KICAD_PCB_PORT_SERVICE_NUMBER (currently...
EDA_UNITS_T GetUserUnits() const override
Return the user units currently in use.
Definition: draw_frame.h:289
TOOL_MANAGER * GetToolManager() const override
Return the tool manager instance, if any.
Definition: draw_frame.h:937
BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Function Merge modifies the position and size of the rectangle in order to contain aRect.
Definition: box2.h:384
std::string FormatProbeItem(BOARD_ITEM *aItem)
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...
void HighLightON()
Function HighLightON Enable highlight.
Definition: class_board.h:396
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:374
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:407
void SendMessageToEESCHEMA(BOARD_ITEM *objectToSync)
Function SendMessageToEESCHEMA sends a message to the schematic editor so that it may move its cursor...
virtual RENDER_SETTINGS * GetSettings()=0
Function GetSettings Returns pointer to current settings that are going to be used when drawing items...
virtual void RedrawScreen(const wxPoint &aCenterPoint, bool aWarpPointer)
Redraw the entire screen area by updating the scroll bars and mouse pointer in order to have aCenterP...
Class NETINFO_ITEM handles the data for a net.
Definition: netinfo.h:65
BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Function Inflate inflates the rectangle horizontally by dx and vertically by dy.
Definition: box2.h:300
ZONE_CONTAINERS & Zones()
Definition: class_board.h:254
const int scale
see class PGM_BASE
int GetNet() const
Function GetNet.
Definition: netinfo.h:227
static TOOL_ACTION selectOnSheetFromEeschema
Selects all components on sheet from Eeschema crossprobing.
Definition: pcb_actions.h:79
#define max(a, b)
Definition: auxiliary.h:86
Import a different format file.
Definition: mail_type.h:44
Class BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:170
MODULE * FindModuleByReference(const wxString &aReference) const
Function FindModuleByReference searches for a MODULE within this board with the given reference desig...
std::vector< MSG_PANEL_ITEM > MSG_PANEL_ITEMS
Definition: msgpanel.h:102
void GetMsgPanelInfo(EDA_UNITS_T aUnits, std::vector< MSG_PANEL_ITEM > &aList) override
Function GetMsgPanelInfo returns the information about the NETINFO_ITEM in aList to display in the me...
PCB<->SCH, CVPCB->SCH cross-probing.
Definition: mail_type.h:39
DLIST_ITERATOR_WRAPPER< TRACK > Tracks()
Definition: class_board.h:251
Module description (excepted pads)
BOARD * GetBoard() const
MAIL_T Command()
Function Command returns the MAIL_T associated with this mail.
Definition: kiway_express.h:52
void SetCrossHairPosition(const wxPoint &aPosition, bool aSnapToGrid=true)
Set the screen cross hair position to aPosition in logical (drawing) units.
const wxPoint GetPosition() const override
Definition: class_pad.h:222
BOARD_ITEM_CONTAINER * GetParent() const
void SetHighlight(bool aEnabled, int aNetcode=-1)
Function SetHighlight Turns on/off highlighting - it may be done for the active layer or the specifie...
Definition: painter.h:140
const wxPoint GetPosition() const override
Definition: class_module.h:183
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
#define mod(a, n)
Definition: greymap.cpp:24
SCH->PCB forward update.
Definition: mail_type.h:43
bool FetchNetlistFromSchematic(NETLIST &aNetlist, FETCH_NETLIST_MODE aMode)
int GetToolId() const
Definition: draw_frame.h:524
KICAD_T Type() const
Function Type()
Definition: base_struct.h:204