1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/**
* @file pcbnew/cross-probing.cpp
* @brief Cross probing functions to handle communication to andfrom Eeschema.
*/
/**
* Handle messages between Pcbnew and Eeschema via a socket, the port numbers are
* KICAD_PCB_PORT_SERVICE_NUMBER (currently 4242) (Eeschema to Pcbnew)
* KICAD_SCH_PORT_SERVICE_NUMBER (currently 4243) (Pcbnew to Eeschema)
* Note: these ports must be enabled for firewall protection
*/
#include <fctsys.h>
#include <pgm_base.h>
#include <kiface_i.h>
#include <kiway_express.h>
#include <wxPcbStruct.h>
#include <eda_dde.h>
#include <macros.h>
#include <pcbnew_id.h>
#include <class_board.h>
#include <class_module.h>
#include <collectors.h>
#include <pcbnew.h>
#include <tools/common_actions.h>
#include <tool/tool_manager.h>
#include <pcb_draw_panel_gal.h>
/* Execute a remote command send by Eeschema via a socket,
* port KICAD_PCB_PORT_SERVICE_NUMBER
* cmdline = received command from Eeschema
* Commands are
* $PART: "reference" put cursor on component
* $PIN: "pin name" $PART: "reference" put cursor on the footprint pin
*/
void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
{
char line[1024];
wxString msg;
wxString modName;
char* idcmd;
char* text;
MODULE* module = NULL;
D_PAD* pad = NULL;
BOARD* pcb = GetBoard();
wxPoint pos;
strncpy( line, cmdline, sizeof(line) - 1 );
line[sizeof(line) - 1] = 0;
idcmd = strtok( line, " \n\r" );
text = strtok( NULL, " \n\r" );
if( !idcmd || !text )
return;
if( strcmp( idcmd, "$PART:" ) == 0 )
{
modName = FROM_UTF8( text );
module = pcb->FindModuleByReference( modName );
if( module )
msg.Printf( _( "%s found" ), GetChars( modName ) );
else
msg.Printf( _( "%s not found" ), GetChars( modName ) );
SetStatusText( msg );
if( module )
pos = module->GetPosition();
}
else if( strcmp( idcmd, "$PIN:" ) == 0 )
{
wxString pinName;
int netcode = -1;
pinName = FROM_UTF8( text );
text = strtok( NULL, " \n\r" );
if( text && strcmp( text, "$PART:" ) == 0 )
text = strtok( NULL, "\n\r" );
modName = FROM_UTF8( text );
module = pcb->FindModuleByReference( modName );
if( module )
pad = module->FindPadByName( pinName );
if( pad )
{
netcode = pad->GetNetCode();
// put cursor on the pad:
pos = pad->GetPosition();
}
if( netcode > 0 ) // highlight the pad net
{
pcb->HighLightON();
pcb->SetHighLightNet( netcode );
}
else
{
pcb->HighLightOFF();
pcb->SetHighLightNet( -1 );
}
if( module == NULL )
{
msg.Printf( _( "%s not found" ), GetChars( modName ) );
}
else if( pad == NULL )
{
msg.Printf( _( "%s pin %s not found" ), GetChars( modName ), GetChars( pinName ) );
SetCurItem( module );
}
else
{
msg.Printf( _( "%s pin %s found" ), GetChars( modName ), GetChars( pinName ) );
SetCurItem( pad );
}
SetStatusText( msg );
}
if( module ) // if found, center the module on screen, and redraw the screen.
{
if( IsGalCanvasActive() )
{
GetToolManager()->RunAction( COMMON_ACTIONS::crossProbeSchToPcb,
true,
pad ?
static_cast<BOARD_ITEM*>( pad ) :
static_cast<BOARD_ITEM*>( module )
);
}
else
{
SetCrossHairPosition( pos );
RedrawScreen( pos, false );
}
}
}
std::string FormatProbeItem( BOARD_ITEM* aItem )
{
MODULE* module;
switch( aItem->Type() )
{
case PCB_MODULE_T:
module = (MODULE*) aItem;
return StrPrintf( "$PART: \"%s\"", TO_UTF8( module->GetReference() ) );
case PCB_PAD_T:
{
module = (MODULE*) aItem->GetParent();
wxString pad = ((D_PAD*)aItem)->GetPadName();
return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
TO_UTF8( module->GetReference() ),
TO_UTF8( pad ) );
}
case PCB_MODULE_TEXT_T:
{
module = static_cast<MODULE*>( aItem->GetParent() );
TEXTE_MODULE* text_mod = static_cast<TEXTE_MODULE*>( aItem );
const char* text_key;
/* This can't be a switch since the break need to pull out
* from the outer switch! */
if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
text_key = "$REF:";
else if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
text_key = "$VAL:";
else
break;
return StrPrintf( "$PART: \"%s\" %s \"%s\"",
TO_UTF8( module->GetReference() ),
text_key,
TO_UTF8( text_mod->GetText() ) );
}
default:
break;
}
return "";
}
/* Send a remote command to Eeschema via a socket,
* aSyncItem = item to be located on schematic (module, pin or text)
* Commands are
* $PART: "reference" put cursor on component anchor
* $PART: "reference" $PAD: "pad number" put cursor on the component pin
* $PART: "reference" $REF: "reference" put cursor on the component ref
* $PART: "reference" $VAL: "value" put cursor on the component value
*/
void PCB_EDIT_FRAME::SendMessageToEESCHEMA( BOARD_ITEM* aSyncItem )
{
#if 1
wxASSERT( aSyncItem ); // can't we fix the caller?
#else
if( !aSyncItem )
return;
#endif
std::string packet = FormatProbeItem( aSyncItem );
if( packet.size() )
{
if( Kiface().IsSingle() )
SendCommand( MSG_TO_SCH, packet.c_str() );
else
{
// Typically ExpressMail is going to be s-expression packets, but since
// we have existing interpreter of the cross probe packet on the other
// side in place, we use that here.
Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
}
}
}
void PCB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
{
const std::string& payload = mail.GetPayload();
switch( mail.Command() )
{
case MAIL_CROSS_PROBE:
ExecuteRemoteCommand( payload.c_str() );
break;
// many many others.
default:
;
}
}
|