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.

596 lines
20 KiB

18 years ago
18 years ago
12 years ago
12 years ago
12 years ago
5 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
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-2020 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 <footprint.h>
  33. #include <track.h>
  34. #include <collectors.h>
  35. #include <eda_dde.h>
  36. #include <kiface_i.h>
  37. #include <kiway_express.h>
  38. #include <netlist_reader/pcb_netlist.h>
  39. #include <netlist_reader/board_netlist_updater.h>
  40. #include <painter.h>
  41. #include <pcb_edit_frame.h>
  42. #include <pcbnew_settings.h>
  43. #include <render_settings.h>
  44. #include <tool/tool_manager.h>
  45. #include <tools/pcb_actions.h>
  46. #include <tools/pcb_selection_tool.h>
  47. #include <netlist_reader/netlist_reader.h>
  48. /* Execute a remote command send by Eeschema via a socket,
  49. * port KICAD_PCB_PORT_SERVICE_NUMBER
  50. * cmdline = received command from Eeschema
  51. * Commands are
  52. * $PART: "reference" put cursor on component
  53. * $PIN: "pin name" $PART: "reference" put cursor on the footprint pin
  54. * $NET: "net name" highlight the given net (if highlight tool is active)
  55. * $CLEAR Clear existing highlight
  56. * They are a keyword followed by a quoted string.
  57. */
  58. void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
  59. {
  60. char line[1024];
  61. wxString msg;
  62. wxString modName;
  63. char* idcmd;
  64. char* text;
  65. int netcode = -1;
  66. bool multiHighlight = false;
  67. FOOTPRINT* footprint = nullptr;
  68. PAD* pad = nullptr;
  69. BOARD* pcb = GetBoard();
  70. CROSS_PROBING_SETTINGS& crossProbingSettings = GetPcbNewSettings()->m_CrossProbing;
  71. KIGFX::VIEW* view = m_toolManager->GetView();
  72. KIGFX::RENDER_SETTINGS* renderSettings = view->GetPainter()->GetSettings();
  73. strncpy( line, cmdline, sizeof(line) - 1 );
  74. line[sizeof(line) - 1] = 0;
  75. idcmd = strtok( line, " \n\r" );
  76. text = strtok( NULL, "\"\n\r" );
  77. if( idcmd == NULL )
  78. return;
  79. if( strcmp( idcmd, "$NET:" ) == 0 )
  80. {
  81. if( !crossProbingSettings.auto_highlight )
  82. return;
  83. wxString net_name = FROM_UTF8( text );
  84. NETINFO_ITEM* netinfo = pcb->FindNet( net_name );
  85. if( netinfo )
  86. {
  87. netcode = netinfo->GetNetCode();
  88. MSG_PANEL_ITEMS items;
  89. netinfo->GetMsgPanelInfo( this, items );
  90. SetMsgPanel( items );
  91. }
  92. }
  93. if( strcmp( idcmd, "$NETS:" ) == 0 )
  94. {
  95. if( !crossProbingSettings.auto_highlight )
  96. return;
  97. wxStringTokenizer netsTok = wxStringTokenizer( FROM_UTF8( text ), "," );
  98. bool first = true;
  99. while( netsTok.HasMoreTokens() )
  100. {
  101. NETINFO_ITEM* netinfo = pcb->FindNet( netsTok.GetNextToken() );
  102. if( netinfo )
  103. {
  104. if( first )
  105. {
  106. // TODO: Once buses are included in netlist, show bus name
  107. MSG_PANEL_ITEMS items;
  108. netinfo->GetMsgPanelInfo( this, items );
  109. SetMsgPanel( items );
  110. first = false;
  111. pcb->SetHighLightNet( netinfo->GetNetCode() );
  112. renderSettings->SetHighlight( true, netinfo->GetNetCode() );
  113. multiHighlight = true;
  114. }
  115. else
  116. {
  117. pcb->SetHighLightNet( netinfo->GetNetCode(), true );
  118. renderSettings->SetHighlight( true, netinfo->GetNetCode(), true );
  119. }
  120. }
  121. }
  122. netcode = -1;
  123. }
  124. else if( strcmp( idcmd, "$PIN:" ) == 0 )
  125. {
  126. wxString pinName = FROM_UTF8( text );
  127. text = strtok( NULL, " \n\r" );
  128. if( text && strcmp( text, "$PART:" ) == 0 )
  129. text = strtok( NULL, "\"\n\r" );
  130. modName = FROM_UTF8( text );
  131. footprint = pcb->FindFootprintByReference( modName );
  132. if( footprint )
  133. pad = footprint->FindPadByName( pinName );
  134. if( pad )
  135. netcode = pad->GetNetCode();
  136. if( footprint == NULL )
  137. msg.Printf( _( "%s not found" ), modName );
  138. else if( pad == NULL )
  139. msg.Printf( _( "%s pin %s not found" ), modName, pinName );
  140. else
  141. msg.Printf( _( "%s pin %s found" ), modName, pinName );
  142. SetStatusText( msg );
  143. }
  144. else if( strcmp( idcmd, "$PART:" ) == 0 )
  145. {
  146. pcb->ResetNetHighLight();
  147. modName = FROM_UTF8( text );
  148. footprint = pcb->FindFootprintByReference( modName );
  149. if( footprint )
  150. msg.Printf( _( "%s found" ), modName );
  151. else
  152. msg.Printf( _( "%s not found" ), modName );
  153. SetStatusText( msg );
  154. }
  155. else if( strcmp( idcmd, "$SHEET:" ) == 0 )
  156. {
  157. msg.Printf( _( "Selecting all from sheet \"%s\"" ), FROM_UTF8( text ) );
  158. wxString sheetUIID( FROM_UTF8( text ) );
  159. SetStatusText( msg );
  160. GetToolManager()->RunAction( PCB_ACTIONS::selectOnSheetFromEeschema, true,
  161. static_cast<void*>( &sheetUIID ) );
  162. return;
  163. }
  164. else if( strcmp( idcmd, "$CLEAR" ) == 0 )
  165. {
  166. if( renderSettings->IsHighlightEnabled() )
  167. {
  168. renderSettings->SetHighlight( false );
  169. view->UpdateAllLayersColor();
  170. }
  171. if( pcb->IsHighLightNetON() )
  172. {
  173. pcb->ResetNetHighLight();
  174. SetMsgPanel( pcb );
  175. }
  176. GetCanvas()->Refresh();
  177. return;
  178. }
  179. BOX2I bbox = { { 0, 0 }, { 0, 0 } };
  180. if( footprint )
  181. {
  182. bbox = footprint->GetBoundingBox( false ); // No invisible text in bbox calc
  183. if( pad )
  184. m_toolManager->RunAction( PCB_ACTIONS::highlightItem, true, (void*) pad );
  185. else
  186. m_toolManager->RunAction( PCB_ACTIONS::highlightItem, true, (void*) footprint );
  187. }
  188. else if( netcode > 0 || multiHighlight )
  189. {
  190. if( !multiHighlight )
  191. {
  192. renderSettings->SetHighlight( ( netcode >= 0 ), netcode );
  193. pcb->SetHighLightNet( netcode );
  194. }
  195. else
  196. {
  197. // Just pick the first one for area calculation
  198. netcode = *pcb->GetHighLightNetCodes().begin();
  199. }
  200. pcb->HighLightON();
  201. auto merge_area =
  202. [netcode, &bbox]( BOARD_CONNECTED_ITEM* aItem )
  203. {
  204. if( aItem->GetNetCode() == netcode )
  205. {
  206. if( bbox.GetWidth() == 0 )
  207. bbox = aItem->GetBoundingBox();
  208. else
  209. bbox.Merge( aItem->GetBoundingBox() );
  210. }
  211. };
  212. if( crossProbingSettings.center_on_items )
  213. {
  214. for( ZONE* zone : pcb->Zones() )
  215. merge_area( zone );
  216. for( TRACK* track : pcb->Tracks() )
  217. merge_area( track );
  218. for( FOOTPRINT* fp : pcb->Footprints() )
  219. {
  220. for( PAD* p : fp->Pads() )
  221. merge_area( p );
  222. }
  223. }
  224. }
  225. else
  226. {
  227. renderSettings->SetHighlight( false );
  228. }
  229. if( crossProbingSettings.center_on_items && bbox.GetWidth() > 0 && bbox.GetHeight() > 0 )
  230. {
  231. //#define DEFAULT_PCBNEW_CODE // Un-comment for normal full zoom KiCad algorithm
  232. #ifdef DEFAULT_PCBNEW_CODE
  233. auto bbSize = bbox.Inflate( bbox.GetWidth() * 0.2f ).GetSize();
  234. auto screenSize = view->ToWorld( GetCanvas()->GetClientSize(), false );
  235. // The "fabs" on x ensures the right answer when the view is flipped
  236. screenSize.x = std::max( 10.0, fabs( screenSize.x ) );
  237. screenSize.y = std::max( 10.0, screenSize.y );
  238. double ratio = std::max( fabs( bbSize.x / screenSize.x ), fabs( bbSize.y / screenSize.y ) );
  239. // Try not to zoom on every cross-probe; it gets very noisy
  240. if( crossProbingSettings.zoom_to_fit && ( ratio < 0.5 || ratio > 1.0 ) )
  241. view->SetScale( view->GetScale() / ratio );
  242. #endif // DEFAULT_PCBNEW_CODE
  243. #ifndef DEFAULT_PCBNEW_CODE // Do the scaled zoom
  244. auto bbSize = bbox.Inflate( bbox.GetWidth() * 0.2f ).GetSize();
  245. auto screenSize = view->ToWorld( GetCanvas()->GetClientSize(), false );
  246. // This code tries to come up with a zoom factor that doesn't simply zoom in
  247. // to the cross probed component, but instead shows a reasonable amount of the
  248. // circuit around it to provide context. This reduces or eliminates the need
  249. // to manually change the zoom because it's too close.
  250. // Using the default text height as a constant to compare against, use the
  251. // height of the bounding box of visible items for a footprint to figure out
  252. // if this is a big footprint (like a processor) or a small footprint (like a resistor).
  253. // This ratio is not useful by itself as a scaling factor. It must be "bent" to
  254. // provide good scaling at varying component sizes. Bigger components need less
  255. // scaling than small ones.
  256. double currTextHeight = Millimeter2iu( DEFAULT_TEXT_SIZE );
  257. double compRatio = bbSize.y / currTextHeight; // Ratio of component to text height
  258. double compRatioBent = 1.0; // This will end up as the scaling factor we apply to "ratio"
  259. // This is similar to the original KiCad code that scaled the zoom to make sure components
  260. // were visible on screen. It's simply a ratio of screen size to component size, and its
  261. // job is to zoom in to make the component fullscreen. Earlier in the code the
  262. // component BBox is given a 20% margin to add some breathing room. We compare
  263. // the height of this enlarged component bbox to the default text height. If a component
  264. // will end up with the sides clipped, we adjust later to make sure it fits on screen.
  265. //
  266. // The "fabs" on x ensures the right answer when the view is flipped
  267. screenSize.x = std::max( 10.0, fabs( screenSize.x ) );
  268. screenSize.y = std::max( 10.0, screenSize.y );
  269. double ratio = std::max( -1.0, fabs( bbSize.y / screenSize.y ) );
  270. // Original KiCad code for how much to scale the zoom
  271. double kicadRatio = std::max( fabs( bbSize.x / screenSize.x ),
  272. fabs( bbSize.y / screenSize.y ) );
  273. // LUT to scale zoom ratio to provide reasonable schematic context. Must work
  274. // with footprints of varying sizes (e.g. 0402 package and 200 pin BGA).
  275. // "first" is used as the input and "second" as the output
  276. //
  277. // "first" = compRatio (footprint height / default text height)
  278. // "second" = Amount to scale ratio by
  279. std::vector<std::pair<double, double>> lut{
  280. { 1, 8 },
  281. { 1.5, 5 },
  282. { 3, 3 },
  283. { 4.5, 2.5 },
  284. { 8, 2.0 },
  285. { 12, 1.7 },
  286. { 16, 1.5 },
  287. { 24, 1.3 },
  288. { 32, 1.0 },
  289. };
  290. std::vector<std::pair<double, double>>::iterator it;
  291. compRatioBent = lut.back().second; // Large component default
  292. if( compRatio >= lut.front().first )
  293. {
  294. // Use LUT to do linear interpolation of "compRatio" within "first", then
  295. // use that result to linearly interpolate "second" which gives the scaling
  296. // factor needed.
  297. for( it = lut.begin(); it < lut.end() - 1; it++ )
  298. {
  299. if( it->first <= compRatio && next( it )->first >= compRatio )
  300. {
  301. double diffx = compRatio - it->first;
  302. double diffn = next( it )->first - it->first;
  303. compRatioBent =
  304. it->second + ( next( it )->second - it->second ) * diffx / diffn;
  305. break; // We have our interpolated value
  306. }
  307. }
  308. }
  309. else
  310. compRatioBent = lut.front().second; // Small component default
  311. // If the width of the part we're probing is bigger than what the screen width will be
  312. // after the zoom, then punt and use the KiCad zoom algorithm since it guarantees the
  313. // part's width will be encompassed within the screen. This will apply to parts that are
  314. // much wider than they are tall.
  315. if( bbSize.x > screenSize.x * ratio * compRatioBent )
  316. {
  317. ratio = kicadRatio; // Use standard KiCad zoom algorithm for parts too wide to fit screen
  318. compRatioBent = 1.0; // Reset so we don't modify the "KiCad" ratio
  319. wxLogTrace( "CROSS_PROBE_SCALE",
  320. "Part TOO WIDE for screen. Using normal KiCad zoom ratio: %1.5f", ratio );
  321. }
  322. // Now that "compRatioBent" holds our final scaling factor we apply it to the original
  323. // fullscreen zoom ratio to arrive at the final ratio itself.
  324. ratio *= compRatioBent;
  325. bool alwaysZoom = false; // DEBUG - allows us to minimize zooming or not
  326. // Try not to zoom on every cross-probe; it gets very noisy
  327. if( ( ratio < 0.5 || ratio > 1.0 ) || alwaysZoom )
  328. view->SetScale( view->GetScale() / ratio );
  329. #endif // ifndef DEFAULT_PCBNEW_CODE
  330. view->SetCenter( bbox.Centre() );
  331. }
  332. view->UpdateAllLayersColor();
  333. // Ensure the display is refreshed, because in some installs the refresh is done only
  334. // when the gal canvas has the focus, and that is not the case when crossprobing from
  335. // Eeschema:
  336. GetCanvas()->Refresh();
  337. }
  338. std::string FormatProbeItem( BOARD_ITEM* aItem )
  339. {
  340. FOOTPRINT* footprint;
  341. if( !aItem )
  342. return "$CLEAR: \"HIGHLIGHTED\""; // message to clear highlight state
  343. switch( aItem->Type() )
  344. {
  345. case PCB_FOOTPRINT_T:
  346. footprint = (FOOTPRINT*) aItem;
  347. return StrPrintf( "$PART: \"%s\"", TO_UTF8( footprint->GetReference() ) );
  348. case PCB_PAD_T:
  349. {
  350. footprint = static_cast<FOOTPRINT*>( aItem->GetParent() );
  351. wxString pad = static_cast<PAD*>( aItem )->GetName();
  352. return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
  353. TO_UTF8( footprint->GetReference() ),
  354. TO_UTF8( pad ) );
  355. }
  356. case PCB_FP_TEXT_T:
  357. {
  358. footprint = static_cast<FOOTPRINT*>( aItem->GetParent() );
  359. FP_TEXT* text = static_cast<FP_TEXT*>( aItem );
  360. const char* text_key;
  361. /* This can't be a switch since the break need to pull out
  362. * from the outer switch! */
  363. if( text->GetType() == FP_TEXT::TEXT_is_REFERENCE )
  364. text_key = "$REF:";
  365. else if( text->GetType() == FP_TEXT::TEXT_is_VALUE )
  366. text_key = "$VAL:";
  367. else
  368. break;
  369. return StrPrintf( "$PART: \"%s\" %s \"%s\"",
  370. TO_UTF8( footprint->GetReference() ),
  371. text_key,
  372. TO_UTF8( text->GetText() ) );
  373. }
  374. default:
  375. break;
  376. }
  377. return "";
  378. }
  379. /* Send a remote command to Eeschema via a socket,
  380. * aSyncItem = item to be located on schematic (footprint, pin or text)
  381. * Commands are
  382. * $PART: "reference" put cursor on component anchor
  383. * $PART: "reference" $PAD: "pad number" put cursor on the component pin
  384. * $PART: "reference" $REF: "reference" put cursor on the component ref
  385. * $PART: "reference" $VAL: "value" put cursor on the component value
  386. */
  387. void PCB_EDIT_FRAME::SendMessageToEESCHEMA( BOARD_ITEM* aSyncItem )
  388. {
  389. std::string packet = FormatProbeItem( aSyncItem );
  390. if( !packet.empty() )
  391. {
  392. if( Kiface().IsSingle() )
  393. SendCommand( MSG_TO_SCH, packet.c_str() );
  394. else
  395. {
  396. // Typically ExpressMail is going to be s-expression packets, but since
  397. // we have existing interpreter of the cross probe packet on the other
  398. // side in place, we use that here.
  399. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  400. }
  401. }
  402. }
  403. void PCB_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
  404. {
  405. std::string packet = StrPrintf( "$NET: \"%s\"", TO_UTF8( aNetName ) );
  406. if( !packet.empty() )
  407. {
  408. if( Kiface().IsSingle() )
  409. SendCommand( MSG_TO_SCH, packet.c_str() );
  410. else
  411. {
  412. // Typically ExpressMail is going to be s-expression packets, but since
  413. // we have existing interpreter of the cross probe packet on the other
  414. // side in place, we use that here.
  415. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  416. }
  417. }
  418. }
  419. void PCB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  420. {
  421. std::string& payload = mail.GetPayload();
  422. switch( mail.Command() )
  423. {
  424. case MAIL_PCB_GET_NETLIST:
  425. {
  426. NETLIST netlist;
  427. STRING_FORMATTER sf;
  428. for( FOOTPRINT* footprint : this->GetBoard()->Footprints() )
  429. {
  430. COMPONENT* component = new COMPONENT( footprint->GetFPID(), footprint->GetReference(),
  431. footprint->GetValue(), footprint->GetPath() );
  432. for( PAD* pad : footprint->Pads() )
  433. {
  434. const wxString& netname = pad->GetShortNetname();
  435. if( !netname.IsEmpty() )
  436. component->AddNet( pad->GetName(), netname, wxEmptyString );
  437. }
  438. netlist.AddComponent( component );
  439. }
  440. netlist.Format( "pcb_netlist", &sf, 0, CTL_OMIT_FILTERS );
  441. payload = sf.GetString();
  442. }
  443. break;
  444. case MAIL_PCB_UPDATE_LINKS:
  445. try
  446. {
  447. NETLIST netlist;
  448. FetchNetlistFromSchematic( netlist, NO_ANNOTATION );
  449. BOARD_NETLIST_UPDATER updater( this, GetBoard() );
  450. updater.SetLookupByTimestamp( false );
  451. updater.SetDeleteUnusedComponents ( false );
  452. updater.SetReplaceFootprints( false );
  453. updater.SetDeleteSinglePadNets( false );
  454. updater.SetWarnPadNoNetInNetlist( false );
  455. updater.UpdateNetlist( netlist );
  456. bool dummy;
  457. OnNetlistChanged( updater, &dummy );
  458. }
  459. catch( const IO_ERROR& )
  460. {
  461. assert( false ); // should never happen
  462. return;
  463. }
  464. break;
  465. case MAIL_CROSS_PROBE:
  466. ExecuteRemoteCommand( payload.c_str() );
  467. break;
  468. case MAIL_PCB_UPDATE:
  469. m_toolManager->RunAction( ACTIONS::updatePcbFromSchematic, true );
  470. break;
  471. case MAIL_IMPORT_FILE:
  472. {
  473. // Extract file format type and path (plugin type and path separated with \n)
  474. size_t split = payload.find( '\n' );
  475. wxCHECK( split != std::string::npos, /*void*/ );
  476. int importFormat;
  477. try
  478. {
  479. importFormat = std::stoi( payload.substr( 0, split ) );
  480. }
  481. catch( std::invalid_argument& )
  482. {
  483. wxFAIL;
  484. importFormat = -1;
  485. }
  486. std::string path = payload.substr( split + 1 );
  487. wxASSERT( !path.empty() );
  488. if( importFormat >= 0 )
  489. importFile( path, importFormat );
  490. }
  491. break;
  492. // many many others.
  493. default:
  494. ;
  495. }
  496. }