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 
34 #include <fctsys.h>
35 #include <pgm_base.h>
36 #include <kiface_i.h>
37 #include <kiway_express.h>
38 #include <pcb_edit_frame.h>
39 #include <eda_dde.h>
40 #include <macros.h>
41 #include <pcbnew_id.h>
42 #include <class_board.h>
43 #include <class_module.h>
44 #include <class_track.h>
45 #include <class_zone.h>
46 #include <collectors.h>
47 #include <pcbnew.h>
48 #include <pcb_netlist.h>
49 #include <tools/pcb_actions.h>
50 #include <tool/tool_manager.h>
51 #include <tools/selection_tool.h>
52 #include <pcb_painter.h>
53 
54 /* Execute a remote command send by Eeschema via a socket,
55  * port KICAD_PCB_PORT_SERVICE_NUMBER
56  * cmdline = received command from Eeschema
57  * Commands are
58  * $PART: "reference" put cursor on component
59  * $PIN: "pin name" $PART: "reference" put cursor on the footprint pin
60  * $NET: "net name" highlight the given net (if highlight tool is active)
61  * $CLEAR Clear existing highlight
62  * They are a keyword followed by a quoted string.
63  */
64 void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
65 {
66  char line[1024];
67  wxString msg;
68  wxString modName;
69  char* idcmd;
70  char* text;
71  int netcode = -1;
72  MODULE* module = NULL;
73  D_PAD* pad = NULL;
74  BOARD* pcb = GetBoard();
75 
77  KIGFX::RENDER_SETTINGS* renderSettings = view->GetPainter()->GetSettings();
78 
79  strncpy( line, cmdline, sizeof(line) - 1 );
80  line[sizeof(line) - 1] = 0;
81 
82  idcmd = strtok( line, " \n\r" );
83  text = strtok( NULL, "\"\n\r" );
84 
85  if( idcmd == NULL )
86  return;
87 
88  if( strcmp( idcmd, "$NET:" ) == 0 )
89  {
90  wxString net_name = FROM_UTF8( text );
91 
92  NETINFO_ITEM* netinfo = pcb->FindNet( net_name );
93 
94  if( netinfo )
95  {
96  netcode = netinfo->GetNet();
97 
98  MSG_PANEL_ITEMS items;
99  netinfo->GetMsgPanelInfo( GetUserUnits(), items );
100  SetMsgPanel( items );
101  }
102  }
103  else if( strcmp( idcmd, "$PIN:" ) == 0 )
104  {
105  wxString pinName = FROM_UTF8( text );
106 
107  text = strtok( NULL, " \n\r" );
108 
109  if( text && strcmp( text, "$PART:" ) == 0 )
110  text = strtok( NULL, "\"\n\r" );
111 
112  modName = FROM_UTF8( text );
113 
114  module = pcb->FindModuleByReference( modName );
115 
116  if( module )
117  pad = module->FindPadByName( pinName );
118 
119  if( pad )
120  netcode = pad->GetNetCode();
121 
122  if( module == NULL )
123  msg.Printf( _( "%s not found" ), modName );
124  else if( pad == NULL )
125  msg.Printf( _( "%s pin %s not found" ), modName, pinName );
126  else
127  msg.Printf( _( "%s pin %s found" ), modName, pinName );
128 
129  SetStatusText( msg );
130  }
131  else if( strcmp( idcmd, "$PART:" ) == 0 )
132  {
133  pcb->ResetNetHighLight();
134 
135  modName = FROM_UTF8( text );
136 
137  module = pcb->FindModuleByReference( modName );
138 
139  if( module )
140  msg.Printf( _( "%s found" ), modName );
141  else
142  msg.Printf( _( "%s not found" ), modName );
143 
144  SetStatusText( msg );
145  }
146  else if( strcmp( idcmd, "$SHEET:" ) == 0 )
147  {
148  msg.Printf( _( "Selecting all from sheet \"%s\"" ), FROM_UTF8( text ) );
149  wxString sheetStamp( FROM_UTF8( text ) );
150  SetStatusText( msg );
152  static_cast<void*>( &sheetStamp ) );
153  return;
154  }
155  else if( strcmp( idcmd, "$CLEAR" ) == 0 )
156  {
157  renderSettings->SetHighlight( false );
158  view->UpdateAllLayersColor();
159 
160  pcb->ResetNetHighLight();
161  SetMsgPanel( pcb );
162 
163  GetCanvas()->Refresh();
164  return;
165  }
166 
167  BOX2I bbox = { { 0, 0 }, { 0, 0 } };
168 
169  if( module )
170  {
171  m_toolManager->RunAction( PCB_ACTIONS::highlightItem, true, (void*) module );
172  bbox = module->GetBoundingBox();
173  }
174  else if( netcode > 0 )
175  {
176  renderSettings->SetHighlight( ( netcode >= 0 ), netcode );
177 
178  pcb->SetHighLightNet( netcode );
179 
180  auto merge_area = [netcode, &bbox]( BOARD_CONNECTED_ITEM* aItem )
181  {
182  if( aItem->GetNetCode() == netcode )
183  {
184  if( bbox.GetWidth() == 0 )
185  bbox = aItem->GetBoundingBox();
186  else
187  bbox.Merge( aItem->GetBoundingBox() );
188  }
189  };
190 
191  for( auto zone : pcb->Zones() )
192  merge_area( zone );
193 
194  for( auto track : pcb->Tracks() )
195  merge_area( track );
196 
197  for( auto mod : pcb->Modules() )
198  for ( auto mod_pad : mod->Pads() )
199  merge_area( mod_pad );
200  }
201  else
202  {
203  renderSettings->SetHighlight( false );
204  }
205 
206  if( bbox.GetWidth() > 0 && bbox.GetHeight() > 0 )
207  {
208  auto bbSize = bbox.Inflate( bbox.GetWidth() * 0.2f ).GetSize();
209  auto screenSize = view->ToWorld( GetCanvas()->GetClientSize(), false );
210  double ratio = std::max( fabs( bbSize.x / screenSize.x ),
211  fabs( bbSize.y / screenSize.y ) );
212 
213  // Try not to zoom on every cross-probe; it gets very noisy
214  if( ratio < 0.1 || ratio > 1.0 )
215  view->SetScale( view->GetScale() / ratio );
216 
217  view->SetCenter( bbox.Centre() );
218  }
219 
220  view->UpdateAllLayersColor();
221  // Ensure the display is refreshed, because in some installs the refresh is done only
222  // when the gal canvas has the focus, and that is not the case when crossprobing from
223  // Eeschema:
224  GetCanvas()->Refresh();
225 }
226 
227 
228 std::string FormatProbeItem( BOARD_ITEM* aItem )
229 {
230  MODULE* module;
231 
232  if( !aItem )
233  return "$CLEAR: \"HIGHLIGHTED\""; // message to clear highlight state
234 
235  switch( aItem->Type() )
236  {
237  case PCB_MODULE_T:
238  module = (MODULE*) aItem;
239  return StrPrintf( "$PART: \"%s\"", TO_UTF8( module->GetReference() ) );
240 
241  case PCB_PAD_T:
242  {
243  module = (MODULE*) aItem->GetParent();
244  wxString pad = ((D_PAD*)aItem)->GetName();
245 
246  return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
247  TO_UTF8( module->GetReference() ),
248  TO_UTF8( pad ) );
249  }
250 
251  case PCB_MODULE_TEXT_T:
252  {
253  module = static_cast<MODULE*>( aItem->GetParent() );
254 
255  TEXTE_MODULE* text_mod = static_cast<TEXTE_MODULE*>( aItem );
256 
257  const char* text_key;
258 
259  /* This can't be a switch since the break need to pull out
260  * from the outer switch! */
261  if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
262  text_key = "$REF:";
263  else if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
264  text_key = "$VAL:";
265  else
266  break;
267 
268  return StrPrintf( "$PART: \"%s\" %s \"%s\"",
269  TO_UTF8( module->GetReference() ),
270  text_key,
271  TO_UTF8( text_mod->GetText() ) );
272  }
273 
274  default:
275  break;
276  }
277 
278  return "";
279 }
280 
281 
282 /* Send a remote command to Eeschema via a socket,
283  * aSyncItem = item to be located on schematic (module, pin or text)
284  * Commands are
285  * $PART: "reference" put cursor on component anchor
286  * $PART: "reference" $PAD: "pad number" put cursor on the component pin
287  * $PART: "reference" $REF: "reference" put cursor on the component ref
288  * $PART: "reference" $VAL: "value" put cursor on the component value
289  */
291 {
292  std::string packet = FormatProbeItem( aSyncItem );
293 
294  if( !packet.empty() )
295  {
296  if( Kiface().IsSingle() )
297  SendCommand( MSG_TO_SCH, packet.c_str() );
298  else
299  {
300  // Typically ExpressMail is going to be s-expression packets, but since
301  // we have existing interpreter of the cross probe packet on the other
302  // side in place, we use that here.
303  Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
304  }
305  }
306 }
307 
308 
309 void PCB_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
310 {
311  std::string packet = StrPrintf( "$NET: \"%s\"", TO_UTF8( aNetName ) );
312 
313  if( !packet.empty() )
314  {
315  if( Kiface().IsSingle() )
316  SendCommand( MSG_TO_SCH, packet.c_str() );
317  else
318  {
319  // Typically ExpressMail is going to be s-expression packets, but since
320  // we have existing interpreter of the cross probe packet on the other
321  // side in place, we use that here.
322  Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
323  }
324  }
325 }
326 
327 
329 {
330  const std::string& payload = mail.GetPayload();
331 
332  switch( mail.Command() )
333  {
334  case MAIL_CROSS_PROBE:
335  ExecuteRemoteCommand( payload.c_str() );
336  break;
337 
338  case MAIL_PCB_UPDATE:
340  break;
341 
342  case MAIL_IMPORT_FILE:
343  {
344  // Extract file format type and path (plugin type and path separated with \n)
345  size_t split = payload.find( '\n' );
346  wxCHECK( split != std::string::npos, /*void*/ );
347  int importFormat;
348 
349  try
350  {
351  importFormat = std::stoi( payload.substr( 0, split ) );
352  }
353  catch( std::invalid_argument& )
354  {
355  wxFAIL;
356  importFormat = -1;
357  }
358 
359  std::string path = payload.substr( split + 1 );
360  wxASSERT( !path.empty() );
361 
362  if( importFormat >= 0 )
363  importFile( path, importFormat );
364  }
365 
366  // many many others.
367  default:
368  ;
369  }
370 }
371 
NETINFO_ITEM * FindNet(int aNetcode) const
Function FindNet searches for a net with the given netcode.
KIGFX::VIEW * GetView() const
Definition: tool_manager.h:241
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_holder.h:56
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
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:62
bool importFile(const wxString &aFileName, int aFileType)
Load the given filename but sets the path to the current project path.
Class RENDER_SETTINGS Contains all the knowledge about how graphical objects are drawn on any output ...
Definition: painter.h:56
Class BOARD_ITEM is a base class for any item which can be embedded within the BOARD container class,...
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Function ToWorld() Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:475
static TOOL_ACTION highlightItem
Definition: pcb_actions.h:410
#define MSG_TO_SCH
Definition: eda_dde.h:46
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
bool RunAction(const std::string &aActionName, bool aNow=false, T aParam=NULL)
Function RunAction() Runs the specified action.
Definition: tool_manager.h:109
class D_PAD, a pad in a footprint
Definition: typeinfo.h:90
static TOOL_ACTION updatePcbFromSchematic
Definition: actions.h:153
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
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
Definitions for tracks, vias and zones.
This file contains miscellaneous commonly used macros and functions.
const wxString GetReference() const
Function GetReference.
Definition: class_module.h:416
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
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.
void ResetNetHighLight()
Function ResetNetHighLight Reset all high light data to the init state.
Definition: class_board.h:376
void SetCenter(const VECTOR2D &aCenter)
Function SetCenter() Sets the center point of the VIEW (i.e.
Definition: view.cpp:604
bool IsSingle() const
Function IsSingle is this KIFACE_I running under single_top?
Definition: kiface_i.h:115
MODULES & Modules()
Definition: class_board.h:236
void SetHighlight(bool aEnabled, int aNetcode=-1, bool aHighlightItems=false)
Function SetHighlight Turns on/off highlighting - it may be done for the active layer,...
Definition: painter.h:141
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
void ExecuteRemoteCommand(const char *cmdline) override
Execute a remote command send by Eeschema via a socket, port KICAD_PCB_PORT_SERVICE_NUMBER (currently...
coord_type GetWidth() const
Definition: box2.h:195
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
#define _(s)
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...
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: view.cpp:578
class TEXTE_MODULE, text in a footprint
Definition: typeinfo.h:93
void SetHighLightNet(int aNetCode)
Function SetHighLightNet.
Definition: class_board.h:392
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...
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:250
see class PGM_BASE
Vec Centre() const
Definition: box2.h:77
EDA_UNITS_T GetUserUnits() const
Return the user units currently in use.
int GetNet() const
Function GetNet.
Definition: netinfo.h:225
static TOOL_ACTION selectOnSheetFromEeschema
Selects all components on sheet from Eeschema crossprobing.
Definition: pcb_actions.h:99
#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:161
coord_type GetHeight() const
Definition: box2.h:196
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
TOOL_MANAGER * m_toolManager
Module description (excepted pads)
BOARD * GetBoard() const
Class VIEW.
Definition: view.h:61
MAIL_T Command()
Function Command returns the MAIL_T associated with this mail.
Definition: kiway_express.h:52
void UpdateAllLayersColor()
Function UpdateAllLayersColor() Applies the new coloring scheme to all layers.
Definition: view.cpp:798
double GetScale() const
Function GetScale()
Definition: view.h:257
BOARD_ITEM_CONTAINER * GetParent() 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
#define mod(a, n)
Definition: greymap.cpp:24
SCH->PCB forward update.
Definition: mail_type.h:43
TRACKS & Tracks()
Definition: class_board.h:227
KICAD_T Type() const
Function Type()
Definition: base_struct.h:210