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.

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