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.

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