cross-probing.cpp 5.96 KB
Newer Older
1 2 3 4
/**
 * @file pcbnew/cross-probing.cpp
 * @brief Cross probing functions to handle communication to andfrom Eeschema.
 */
5

6 7 8 9
/**
 * 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)
10 11
 * Note: these ports must be enabled for firewall protection
 */
12

13
#include <fctsys.h>
14
#include <pgm_base.h>
15
#include <kiface_i.h>
Dick Hollenbeck's avatar
Dick Hollenbeck committed
16
#include <kiway_express.h>
17 18 19
#include <wxPcbStruct.h>
#include <eda_dde.h>
#include <macros.h>
20

21 22 23
#include <pcbnew_id.h>
#include <class_board.h>
#include <class_module.h>
24

25 26
#include <collectors.h>
#include <pcbnew.h>
27 28


29 30 31
/* Execute a remote command send by Eeschema via a socket,
 * port KICAD_PCB_PORT_SERVICE_NUMBER
 * cmdline = received command from Eeschema
32 33
 * Commands are
 * $PART: "reference"   put cursor on component
34
 * $PIN: "pin name"  $PART: "reference" put cursor on the footprint pin
35
 */
36
void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
37
{
38 39 40 41 42 43
    char            line[1024];
    wxString        msg;
    wxString        modName;
    char*           idcmd;
    char*           text;
    MODULE*         module = 0;
44
    BOARD* pcb = GetBoard();
45
    wxPoint         pos;
46 47 48 49 50

    strncpy( line, cmdline, sizeof(line) - 1 );

    idcmd = strtok( line, " \n\r" );
    text  = strtok( NULL, " \n\r" );
dickelbeck's avatar
dickelbeck committed
51 52

    if( !idcmd || !text )
53 54 55 56
        return;

    if( strcmp( idcmd, "$PART:" ) == 0 )
    {
57
        modName = FROM_UTF8( text );
58

59
        module = pcb->FindModuleByReference( modName );
60

dickelbeck's avatar
dickelbeck committed
61
        if( module )
62
            msg.Printf( _( "%s found" ), GetChars( modName ) );
dickelbeck's avatar
dickelbeck committed
63
        else
64
            msg.Printf( _( "%s not found" ), GetChars( modName ) );
65

66
        SetStatusText( msg );
67

68
        if( module )
69
            pos = module->GetPosition();
70
    }
dickelbeck's avatar
dickelbeck committed
71
    else if( strcmp( idcmd, "$PIN:" ) == 0 )
72
    {
dickelbeck's avatar
dickelbeck committed
73
        wxString pinName;
74 75 76
        D_PAD*   pad     = NULL;
        int      netcode = -1;

77
        pinName = FROM_UTF8( text );
78 79 80 81 82

        text = strtok( NULL, " \n\r" );
        if( text && strcmp( text, "$PART:" ) == 0 )
            text = strtok( NULL, "\n\r" );

83
        modName = FROM_UTF8( text );
dickelbeck's avatar
dickelbeck committed
84

85
        module = pcb->FindModuleByReference( modName );
86

87
        if( module )
dickelbeck's avatar
dickelbeck committed
88
            pad = module->FindPadByName( pinName );
89 90

        if( pad )
91
        {
92
            netcode = pad->GetNetCode();
93

94
            // put cursor on the pad:
95
            pos = pad->GetPosition();
96
        }
97

98
        if( netcode > 0 )               // highlight the pad net
99
        {
100 101
            pcb->HighLightON();
            pcb->SetHighLightNet( netcode );
102 103 104
        }
        else
        {
105 106
            pcb->HighLightOFF();
            pcb->SetHighLightNet( -1 );
107 108 109
        }

        if( module == NULL )
110
        {
111
            msg.Printf( _( "%s not found" ), GetChars( modName ) );
112
        }
113
        else if( pad == NULL )
dickelbeck's avatar
dickelbeck committed
114
        {
115
            msg.Printf( _( "%s pin %s not found" ), GetChars( modName ), GetChars( pinName ) );
116
            SetCurItem( module );
dickelbeck's avatar
dickelbeck committed
117
        }
118
        else
dickelbeck's avatar
dickelbeck committed
119
        {
120
            msg.Printf( _( "%s pin %s found" ), GetChars( modName ), GetChars( pinName ) );
121
            SetCurItem( pad );
dickelbeck's avatar
dickelbeck committed
122
        }
123

124
        SetStatusText( msg );
125 126
    }

127
    if( module )  // if found, center the module on screen, and redraw the screen.
128
    {
129 130
        SetCrossHairPosition( pos );
        RedrawScreen( pos, false );
131
    }
132 133 134
}


Dick Hollenbeck's avatar
Dick Hollenbeck committed
135
std::string FormatProbeItem( BOARD_ITEM* aItem )
136
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
137
    MODULE*     module;
138

Dick Hollenbeck's avatar
Dick Hollenbeck committed
139
    switch( aItem->Type() )
140
    {
141
    case PCB_MODULE_T:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
142 143
        module = (MODULE*) aItem;
        return StrPrintf( "$PART: \"%s\"", TO_UTF8( module->GetReference() ) );
144

145
    case PCB_PAD_T:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
146 147 148 149 150 151 152 153
        {
            module = (MODULE*) aItem->GetParent();
            wxString pad = ((D_PAD*)aItem)->GetPadName();

            return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
                     TO_UTF8( module->GetReference() ),
                     TO_UTF8( pad ) );
        }
154

155
    case PCB_MODULE_TEXT_T:
Dick Hollenbeck's avatar
Dick Hollenbeck committed
156 157
        {
            module = (MODULE*) aItem->GetParent();
158

Dick Hollenbeck's avatar
Dick Hollenbeck committed
159
            TEXTE_MODULE*   text_mod = (TEXTE_MODULE*) aItem;
160

Dick Hollenbeck's avatar
Dick Hollenbeck committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
            const char*     text_key;

            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() ) );
        }
175 176 177 178 179

    default:
        break;
    }

Dick Hollenbeck's avatar
Dick Hollenbeck committed
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
    return "";
}


/**
 * Send a remote command to Eeschema via a socket,
 * @param objectToSync = 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() )
205
    {
206
        if( Kiface().IsSingle() )
Dick Hollenbeck's avatar
Dick Hollenbeck committed
207
            SendCommand( MSG_TO_SCH, packet.c_str() );
208 209
        else
        {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
210 211 212 213
            // 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 );
214
        }
215 216
    }
}
Dick Hollenbeck's avatar
Dick Hollenbeck committed
217

218 219 220

void PCB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
221 222 223 224 225 226 227
    const std::string& payload = mail.GetPayload();

    switch( mail.Command() )
    {
    case MAIL_CROSS_PROBE:
        ExecuteRemoteCommand( payload.c_str() );
        break;
228

Dick Hollenbeck's avatar
Dick Hollenbeck committed
229
    // many many others.
230 231
    default:
        ;
Dick Hollenbeck's avatar
Dick Hollenbeck committed
232
    }
233 234
}