You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

668 lines
19 KiB

18 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019-2022 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. * @file pcbnew/cross-probing.cpp
  25. * @brief Cross probing functions to handle communication to and from Eeschema.
  26. * Handle messages between Pcbnew and Eeschema via a socket, the port numbers are
  27. * KICAD_PCB_PORT_SERVICE_NUMBER (currently 4242) (Eeschema to Pcbnew)
  28. * KICAD_SCH_PORT_SERVICE_NUMBER (currently 4243) (Pcbnew to Eeschema)
  29. * Note: these ports must be enabled for firewall protection
  30. */
  31. #include <board.h>
  32. #include <board_design_settings.h>
  33. #include <footprint.h>
  34. #include <pad.h>
  35. #include <pcb_track.h>
  36. #include <pcb_group.h>
  37. #include <zone.h>
  38. #include <collectors.h>
  39. #include <eda_dde.h>
  40. #include <kiface_base.h>
  41. #include <kiway_express.h>
  42. #include <string_utils.h>
  43. #include <netlist_reader/pcb_netlist.h>
  44. #include <netlist_reader/board_netlist_updater.h>
  45. #include <painter.h>
  46. #include <pcb_edit_frame.h>
  47. #include <pcbnew_settings.h>
  48. #include <render_settings.h>
  49. #include <tool/tool_manager.h>
  50. #include <tools/pcb_actions.h>
  51. #include <tools/pcb_selection_tool.h>
  52. #include <netlist_reader/netlist_reader.h>
  53. #include <wx/log.h>
  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. * $NET: "net name" Highlight the given net
  59. * $NETS: "net name 1,net name 2" Highlight all given nets
  60. * $CLEAR Clear existing highlight
  61. * They are a keyword followed by a quoted string.
  62. */
  63. void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
  64. {
  65. char line[1024];
  66. wxString msg;
  67. wxString modName;
  68. char* idcmd;
  69. char* text;
  70. int netcode = -1;
  71. bool multiHighlight = false;
  72. FOOTPRINT* footprint = nullptr;
  73. PAD* pad = nullptr;
  74. BOARD* pcb = GetBoard();
  75. CROSS_PROBING_SETTINGS& crossProbingSettings = GetPcbNewSettings()->m_CrossProbing;
  76. KIGFX::VIEW* view = m_toolManager->GetView();
  77. KIGFX::RENDER_SETTINGS* renderSettings = view->GetPainter()->GetSettings();
  78. strncpy( line, cmdline, sizeof(line) - 1 );
  79. line[sizeof(line) - 1] = 0;
  80. idcmd = strtok( line, " \n\r" );
  81. text = strtok( nullptr, "\"\n\r" );
  82. if( idcmd == nullptr )
  83. return;
  84. if( strcmp( idcmd, "$NET:" ) == 0 )
  85. {
  86. if( !crossProbingSettings.auto_highlight )
  87. return;
  88. wxString net_name = FROM_UTF8( text );
  89. NETINFO_ITEM* netinfo = pcb->FindNet( net_name );
  90. if( netinfo )
  91. {
  92. netcode = netinfo->GetNetCode();
  93. std::vector<MSG_PANEL_ITEM> items;
  94. netinfo->GetMsgPanelInfo( this, items );
  95. SetMsgPanel( items );
  96. }
  97. }
  98. if( strcmp( idcmd, "$NETS:" ) == 0 )
  99. {
  100. if( !crossProbingSettings.auto_highlight )
  101. return;
  102. wxStringTokenizer netsTok = wxStringTokenizer( FROM_UTF8( text ), wxT( "," ) );
  103. bool first = true;
  104. while( netsTok.HasMoreTokens() )
  105. {
  106. NETINFO_ITEM* netinfo = pcb->FindNet( netsTok.GetNextToken() );
  107. if( netinfo )
  108. {
  109. if( first )
  110. {
  111. // TODO: Once buses are included in netlist, show bus name
  112. std::vector<MSG_PANEL_ITEM> items;
  113. netinfo->GetMsgPanelInfo( this, items );
  114. SetMsgPanel( items );
  115. first = false;
  116. pcb->SetHighLightNet( netinfo->GetNetCode() );
  117. renderSettings->SetHighlight( true, netinfo->GetNetCode() );
  118. multiHighlight = true;
  119. }
  120. else
  121. {
  122. pcb->SetHighLightNet( netinfo->GetNetCode(), true );
  123. renderSettings->SetHighlight( true, netinfo->GetNetCode(), true );
  124. }
  125. }
  126. }
  127. netcode = -1;
  128. }
  129. else if( strcmp( idcmd, "$CLEAR" ) == 0 )
  130. {
  131. if( renderSettings->IsHighlightEnabled() )
  132. {
  133. renderSettings->SetHighlight( false );
  134. view->UpdateAllLayersColor();
  135. }
  136. if( pcb->IsHighLightNetON() )
  137. {
  138. pcb->ResetNetHighLight();
  139. SetMsgPanel( pcb );
  140. }
  141. GetCanvas()->Refresh();
  142. return;
  143. }
  144. BOX2I bbox;
  145. if( footprint )
  146. {
  147. bbox = footprint->GetBoundingBox( true, false ); // No invisible text in bbox calc
  148. if( pad )
  149. m_toolManager->RunAction( PCB_ACTIONS::highlightItem, true, (void*) pad );
  150. else
  151. m_toolManager->RunAction( PCB_ACTIONS::highlightItem, true, (void*) footprint );
  152. }
  153. else if( netcode > 0 || multiHighlight )
  154. {
  155. if( !multiHighlight )
  156. {
  157. renderSettings->SetHighlight( ( netcode >= 0 ), netcode );
  158. pcb->SetHighLightNet( netcode );
  159. }
  160. else
  161. {
  162. // Just pick the first one for area calculation
  163. netcode = *pcb->GetHighLightNetCodes().begin();
  164. }
  165. pcb->HighLightON();
  166. auto merge_area =
  167. [netcode, &bbox]( BOARD_CONNECTED_ITEM* aItem )
  168. {
  169. if( aItem->GetNetCode() == netcode )
  170. bbox.Merge( aItem->GetBoundingBox() );
  171. };
  172. if( crossProbingSettings.center_on_items )
  173. {
  174. for( ZONE* zone : pcb->Zones() )
  175. merge_area( zone );
  176. for( PCB_TRACK* track : pcb->Tracks() )
  177. merge_area( track );
  178. for( FOOTPRINT* fp : pcb->Footprints() )
  179. {
  180. for( PAD* p : fp->Pads() )
  181. merge_area( p );
  182. }
  183. }
  184. }
  185. else
  186. {
  187. renderSettings->SetHighlight( false );
  188. }
  189. if( crossProbingSettings.center_on_items && bbox.GetWidth() != 0 && bbox.GetHeight() != 0 )
  190. {
  191. if( crossProbingSettings.zoom_to_fit )
  192. GetToolManager()->GetTool<PCB_SELECTION_TOOL>()->ZoomFitCrossProbeBBox( bbox );
  193. FocusOnLocation( bbox.Centre() );
  194. }
  195. view->UpdateAllLayersColor();
  196. // Ensure the display is refreshed, because in some installs the refresh is done only
  197. // when the gal canvas has the focus, and that is not the case when crossprobing from
  198. // Eeschema:
  199. GetCanvas()->Refresh();
  200. }
  201. std::string FormatProbeItem( BOARD_ITEM* aItem )
  202. {
  203. FOOTPRINT* footprint;
  204. if( !aItem )
  205. return "$CLEAR: \"HIGHLIGHTED\""; // message to clear highlight state
  206. switch( aItem->Type() )
  207. {
  208. case PCB_FOOTPRINT_T:
  209. footprint = (FOOTPRINT*) aItem;
  210. return StrPrintf( "$PART: \"%s\"", TO_UTF8( footprint->GetReference() ) );
  211. case PCB_PAD_T:
  212. {
  213. footprint = static_cast<FOOTPRINT*>( aItem->GetParent() );
  214. wxString pad = static_cast<PAD*>( aItem )->GetNumber();
  215. return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
  216. TO_UTF8( footprint->GetReference() ),
  217. TO_UTF8( pad ) );
  218. }
  219. case PCB_FP_TEXT_T:
  220. {
  221. footprint = static_cast<FOOTPRINT*>( aItem->GetParent() );
  222. FP_TEXT* text = static_cast<FP_TEXT*>( aItem );
  223. const char* text_key;
  224. /* This can't be a switch since the break need to pull out
  225. * from the outer switch! */
  226. if( text->GetType() == FP_TEXT::TEXT_is_REFERENCE )
  227. text_key = "$REF:";
  228. else if( text->GetType() == FP_TEXT::TEXT_is_VALUE )
  229. text_key = "$VAL:";
  230. else
  231. break;
  232. return StrPrintf( "$PART: \"%s\" %s \"%s\"",
  233. TO_UTF8( footprint->GetReference() ),
  234. text_key,
  235. TO_UTF8( text->GetText() ) );
  236. }
  237. default:
  238. break;
  239. }
  240. return "";
  241. }
  242. template <typename ItemContainer>
  243. void collectItemsForSyncParts( ItemContainer& aItems, std::set<wxString>& parts )
  244. {
  245. for( EDA_ITEM* item : aItems )
  246. {
  247. switch( item->Type() )
  248. {
  249. case PCB_GROUP_T:
  250. {
  251. PCB_GROUP* group = static_cast<PCB_GROUP*>( item );
  252. collectItemsForSyncParts( group->GetItems(), parts );
  253. break;
  254. }
  255. case PCB_FOOTPRINT_T:
  256. {
  257. FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
  258. wxString ref = footprint->GetReference();
  259. parts.emplace( wxT( "F" ) + EscapeString( ref, CTX_IPC ) );
  260. break;
  261. }
  262. case PCB_PAD_T:
  263. {
  264. PAD* pad = static_cast<PAD*>( item );
  265. FOOTPRINT* footprint = static_cast<FOOTPRINT*>( pad->GetParentFootprint() );
  266. wxString ref = footprint->GetReference();
  267. parts.emplace( wxT( "P" ) + EscapeString( ref, CTX_IPC ) + wxT( "/" )
  268. + EscapeString( pad->GetNumber(), CTX_IPC ) );
  269. break;
  270. }
  271. default: break;
  272. }
  273. }
  274. }
  275. void PCB_EDIT_FRAME::SendSelectItemsToSch( const std::deque<EDA_ITEM*>& aItems,
  276. EDA_ITEM* aFocusItem, bool aForce )
  277. {
  278. std::string command = "$SELECT: ";
  279. if( aFocusItem )
  280. {
  281. std::deque<EDA_ITEM*> focusItems = { aFocusItem };
  282. std::set<wxString> focusParts;
  283. collectItemsForSyncParts( focusItems, focusParts );
  284. if( focusParts.size() > 0 )
  285. {
  286. command += "1,";
  287. command += *focusParts.begin();
  288. command += ",";
  289. }
  290. else
  291. {
  292. command += "0,";
  293. }
  294. }
  295. else
  296. {
  297. command += "0,";
  298. }
  299. std::set<wxString> parts;
  300. collectItemsForSyncParts( aItems, parts );
  301. if( parts.empty() )
  302. return;
  303. for( wxString part : parts )
  304. {
  305. command += part;
  306. command += ",";
  307. }
  308. command.pop_back();
  309. if( Kiface().IsSingle() )
  310. {
  311. SendCommand( MSG_TO_PCB, command );
  312. }
  313. else
  314. {
  315. // Typically ExpressMail is going to be s-expression packets, but since
  316. // we have existing interpreter of the selection packet on the other
  317. // side in place, we use that here.
  318. Kiway().ExpressMail( FRAME_SCH, aForce ? MAIL_SELECTION_FORCE : MAIL_SELECTION, command,
  319. this );
  320. }
  321. }
  322. void PCB_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
  323. {
  324. std::string packet = StrPrintf( "$NET: \"%s\"", TO_UTF8( aNetName ) );
  325. if( !packet.empty() )
  326. {
  327. if( Kiface().IsSingle() )
  328. {
  329. SendCommand( MSG_TO_SCH, packet );
  330. }
  331. else
  332. {
  333. // Typically ExpressMail is going to be s-expression packets, but since
  334. // we have existing interpreter of the cross probe packet on the other
  335. // side in place, we use that here.
  336. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  337. }
  338. }
  339. }
  340. void PCB_EDIT_FRAME::SendCrossProbeItem( BOARD_ITEM* aSyncItem )
  341. {
  342. std::string packet = FormatProbeItem( aSyncItem );
  343. if( !packet.empty() )
  344. {
  345. if( Kiface().IsSingle() )
  346. {
  347. SendCommand( MSG_TO_SCH, packet );
  348. }
  349. else
  350. {
  351. // Typically ExpressMail is going to be s-expression packets, but since
  352. // we have existing interpreter of the cross probe packet on the other
  353. // side in place, we use that here.
  354. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  355. }
  356. }
  357. }
  358. std::vector<BOARD_ITEM*> PCB_EDIT_FRAME::FindItemsFromSyncSelection( std::string syncStr )
  359. {
  360. wxArrayString syncArray = wxStringTokenize( syncStr, "," );
  361. std::vector<std::pair<int, BOARD_ITEM*>> orderPairs;
  362. for( FOOTPRINT* footprint : GetBoard()->Footprints() )
  363. {
  364. if( footprint == nullptr )
  365. continue;
  366. wxString fpSheetPath = footprint->GetPath().AsString().BeforeLast( '/' );
  367. wxString fpUUID = footprint->m_Uuid.AsString();
  368. if( fpSheetPath.IsEmpty() )
  369. fpSheetPath += '/';
  370. if( fpUUID.empty() )
  371. continue;
  372. wxString fpRefEscaped = EscapeString( footprint->GetReference(), CTX_IPC );
  373. for( unsigned index = 0; index < syncArray.size(); ++index )
  374. {
  375. wxString syncEntry = syncArray[index];
  376. if( syncEntry.empty() )
  377. continue;
  378. wxString syncData = syncEntry.substr( 1 );
  379. switch( syncEntry.GetChar( 0 ).GetValue() )
  380. {
  381. case 'S': // Select sheet with subsheets: S<Sheet path>
  382. if( fpSheetPath.StartsWith( syncData ) )
  383. {
  384. orderPairs.emplace_back( index, footprint );
  385. }
  386. break;
  387. case 'F': // Select footprint: F<Reference>
  388. if( syncData == fpRefEscaped )
  389. {
  390. orderPairs.emplace_back( index, footprint );
  391. }
  392. break;
  393. case 'P': // Select pad: P<Footprint reference>/<Pad number>
  394. {
  395. if( syncData.StartsWith( fpRefEscaped ) )
  396. {
  397. wxString selectPadNumberEscaped =
  398. syncData.substr( fpRefEscaped.size() + 1 ); // Skips the slash
  399. wxString selectPadNumber = UnescapeString( selectPadNumberEscaped );
  400. for( PAD* pad : footprint->Pads() )
  401. {
  402. if( selectPadNumber == pad->GetNumber() )
  403. {
  404. orderPairs.emplace_back( index, pad );
  405. }
  406. }
  407. }
  408. break;
  409. }
  410. default: break;
  411. }
  412. }
  413. }
  414. std::sort(
  415. orderPairs.begin(), orderPairs.end(),
  416. []( const std::pair<int, BOARD_ITEM*>& a, const std::pair<int, BOARD_ITEM*>& b ) -> bool
  417. {
  418. return a.first < b.first;
  419. } );
  420. std::vector<BOARD_ITEM*> items;
  421. items.reserve( orderPairs.size() );
  422. for( const std::pair<int, BOARD_ITEM*>& pair : orderPairs )
  423. items.push_back( pair.second );
  424. return items;
  425. }
  426. void PCB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  427. {
  428. std::string& payload = mail.GetPayload();
  429. switch( mail.Command() )
  430. {
  431. case MAIL_PCB_GET_NETLIST:
  432. {
  433. NETLIST netlist;
  434. STRING_FORMATTER sf;
  435. for( FOOTPRINT* footprint : GetBoard()->Footprints() )
  436. {
  437. if( footprint->GetAttributes() & FP_BOARD_ONLY )
  438. continue; // Don't add board-only footprints to the netlist
  439. COMPONENT* component = new COMPONENT( footprint->GetFPID(), footprint->GetReference(),
  440. footprint->GetValue(), footprint->GetPath(), {} );
  441. for( PAD* pad : footprint->Pads() )
  442. {
  443. const wxString& netname = pad->GetShortNetname();
  444. if( !netname.IsEmpty() )
  445. {
  446. component->AddNet( pad->GetNumber(), netname, pad->GetPinFunction(),
  447. pad->GetPinType() );
  448. }
  449. }
  450. netlist.AddComponent( component );
  451. }
  452. netlist.Format( "pcb_netlist", &sf, 0, CTL_OMIT_FILTERS );
  453. payload = sf.GetString();
  454. break;
  455. }
  456. case MAIL_PCB_UPDATE_LINKS:
  457. try
  458. {
  459. NETLIST netlist;
  460. FetchNetlistFromSchematic( netlist, wxEmptyString );
  461. BOARD_NETLIST_UPDATER updater( this, GetBoard() );
  462. updater.SetLookupByTimestamp( false );
  463. updater.SetDeleteUnusedFootprints( false );
  464. updater.SetReplaceFootprints( false );
  465. updater.UpdateNetlist( netlist );
  466. bool dummy;
  467. OnNetlistChanged( updater, &dummy );
  468. }
  469. catch( const IO_ERROR& )
  470. {
  471. assert( false ); // should never happen
  472. return;
  473. }
  474. break;
  475. case MAIL_CROSS_PROBE:
  476. ExecuteRemoteCommand( payload.c_str() );
  477. break;
  478. case MAIL_SELECTION:
  479. if( !GetPcbNewSettings()->m_CrossProbing.on_selection )
  480. break;
  481. KI_FALLTHROUGH;
  482. case MAIL_SELECTION_FORCE:
  483. {
  484. // $SELECT: <mode 0 - only footprints, 1 - with connections>,<spec1>,<spec2>,<spec3>
  485. std::string prefix = "$SELECT: ";
  486. if( !payload.compare( 0, prefix.size(), prefix ) )
  487. {
  488. std::string del = ",";
  489. std::string paramStr = payload.substr( prefix.size() );
  490. int modeEnd = paramStr.find( del );
  491. bool selectConnections = false;
  492. try
  493. {
  494. if( std::stoi( paramStr.substr( 0, modeEnd ) ) == 1 )
  495. selectConnections = true;
  496. }
  497. catch( std::invalid_argument& )
  498. {
  499. wxFAIL;
  500. }
  501. std::vector<BOARD_ITEM*> items =
  502. FindItemsFromSyncSelection( paramStr.substr( modeEnd + 1 ) );
  503. m_probingSchToPcb = true; // recursion guard
  504. if( selectConnections )
  505. {
  506. GetToolManager()->RunAction( PCB_ACTIONS::syncSelectionWithNets, true,
  507. static_cast<void*>( &items ) );
  508. }
  509. else
  510. {
  511. GetToolManager()->RunAction( PCB_ACTIONS::syncSelection, true,
  512. static_cast<void*>( &items ) );
  513. }
  514. m_probingSchToPcb = false;
  515. }
  516. break;
  517. }
  518. case MAIL_PCB_UPDATE:
  519. m_toolManager->RunAction( ACTIONS::updatePcbFromSchematic, true );
  520. break;
  521. case MAIL_IMPORT_FILE:
  522. {
  523. // Extract file format type and path (plugin type and path separated with \n)
  524. size_t split = payload.find( '\n' );
  525. wxCHECK( split != std::string::npos, /*void*/ );
  526. int importFormat;
  527. try
  528. {
  529. importFormat = std::stoi( payload.substr( 0, split ) );
  530. }
  531. catch( std::invalid_argument& )
  532. {
  533. wxFAIL;
  534. importFormat = -1;
  535. }
  536. std::string path = payload.substr( split + 1 );
  537. wxASSERT( !path.empty() );
  538. if( importFormat >= 0 )
  539. importFile( path, importFormat );
  540. break;
  541. }
  542. // many many others.
  543. default:
  544. ;
  545. }
  546. }