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.

443 lines
13 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
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
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /**
  2. * @file pcbnew/cross-probing.cpp
  3. * @brief Cross probing functions to handle communication to andfrom Eeschema.
  4. */
  5. /**
  6. * Handle messages between Pcbnew and Eeschema via a socket, the port numbers are
  7. * KICAD_PCB_PORT_SERVICE_NUMBER (currently 4242) (Eeschema to Pcbnew)
  8. * KICAD_SCH_PORT_SERVICE_NUMBER (currently 4243) (Pcbnew to Eeschema)
  9. * Note: these ports must be enabled for firewall protection
  10. */
  11. #include <fctsys.h>
  12. #include <pgm_base.h>
  13. #include <kiface_i.h>
  14. #include <kiway_express.h>
  15. #include <pcb_edit_frame.h>
  16. #include <eda_dde.h>
  17. #include <macros.h>
  18. #include <pcbnew_id.h>
  19. #include <class_board.h>
  20. #include <class_module.h>
  21. #include <class_track.h>
  22. #include <class_zone.h>
  23. #include <collectors.h>
  24. #include <pcbnew.h>
  25. #include <board_netlist_updater.h>
  26. #include <netlist_reader.h>
  27. #include <pcb_netlist.h>
  28. #include <dialogs/dialog_update_pcb.h>
  29. #include <tools/pcb_actions.h>
  30. #include <tool/tool_manager.h>
  31. #include <tools/selection_tool.h>
  32. #include <pcb_draw_panel_gal.h>
  33. #include <pcb_painter.h>
  34. /* Execute a remote command send by Eeschema via a socket,
  35. * port KICAD_PCB_PORT_SERVICE_NUMBER
  36. * cmdline = received command from Eeschema
  37. * Commands are
  38. * $PART: "reference" put cursor on component
  39. * $PIN: "pin name" $PART: "reference" put cursor on the footprint pin
  40. * $NET: "net name" highlight the given net (if highlight tool is active)
  41. */
  42. void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
  43. {
  44. char line[1024];
  45. wxString msg;
  46. wxString modName;
  47. char* idcmd;
  48. char* text;
  49. MODULE* module = NULL;
  50. D_PAD* pad = NULL;
  51. BOARD* pcb = GetBoard();
  52. wxPoint pos;
  53. strncpy( line, cmdline, sizeof(line) - 1 );
  54. line[sizeof(line) - 1] = 0;
  55. idcmd = strtok( line, " \n\r" );
  56. text = strtok( NULL, " \n\r" );
  57. if( idcmd == NULL )
  58. return;
  59. if( strcmp( idcmd, "$NET:" ) == 0 )
  60. {
  61. if( GetToolId() == ID_PCB_HIGHLIGHT_BUTT )
  62. {
  63. wxString net_name = FROM_UTF8( text );
  64. NETINFO_ITEM* netinfo = pcb->FindNet( net_name );
  65. int netcode = 0;
  66. if( netinfo )
  67. netcode = netinfo->GetNet();
  68. if( IsGalCanvasActive() )
  69. {
  70. auto view = m_toolManager->GetView();
  71. auto rs = view->GetPainter()->GetSettings();
  72. rs->SetHighlight( true, netcode );
  73. view->UpdateAllLayersColor();
  74. BOX2I bbox;
  75. bool first = true;
  76. auto merge_area = [netcode, &bbox, &first]( BOARD_CONNECTED_ITEM* aItem )
  77. {
  78. if( aItem->GetNetCode() == netcode )
  79. {
  80. if( first )
  81. {
  82. bbox = aItem->GetBoundingBox();
  83. first = false;
  84. }
  85. else
  86. {
  87. bbox.Merge( aItem->GetBoundingBox() );
  88. }
  89. }
  90. };
  91. for( auto zone : pcb->Zones() )
  92. merge_area( zone );
  93. for( auto track : pcb->Tracks() )
  94. merge_area( track );
  95. for( auto mod : pcb->Modules() )
  96. for ( auto mod_pad : mod->Pads() )
  97. merge_area( mod_pad );
  98. if( netcode > 0 && bbox.GetWidth() > 0 && bbox.GetHeight() > 0 )
  99. {
  100. auto bbSize = bbox.Inflate( bbox.GetWidth() * 0.2f ).GetSize();
  101. auto screenSize = view->ToWorld( GetGalCanvas()->GetClientSize(), false );
  102. double ratio = std::max( fabs( bbSize.x / screenSize.x ),
  103. fabs( bbSize.y / screenSize.y ) );
  104. double scale = view->GetScale() / ratio;
  105. view->SetScale( scale );
  106. view->SetCenter( bbox.Centre() );
  107. }
  108. GetGalCanvas()->Refresh();
  109. }
  110. else
  111. {
  112. if( netcode > 0 )
  113. {
  114. pcb->HighLightON();
  115. pcb->SetHighLightNet( netcode );
  116. }
  117. else
  118. {
  119. pcb->HighLightOFF();
  120. pcb->SetHighLightNet( -1 );
  121. }
  122. }
  123. }
  124. return;
  125. }
  126. if( text == NULL )
  127. return;
  128. if( strcmp( idcmd, "$PART:" ) == 0 )
  129. {
  130. modName = FROM_UTF8( text );
  131. module = pcb->FindModuleByReference( modName );
  132. if( module )
  133. msg.Printf( _( "%s found" ), GetChars( modName ) );
  134. else
  135. msg.Printf( _( "%s not found" ), GetChars( modName ) );
  136. SetStatusText( msg );
  137. if( module )
  138. pos = module->GetPosition();
  139. }
  140. else if( strcmp( idcmd, "$SHEET:" ) == 0 )
  141. {
  142. msg.Printf( _( "Selecting all from sheet \"%s\"" ), FROM_UTF8( text ) );
  143. wxString sheetStamp( FROM_UTF8( text ) );
  144. SetStatusText( msg );
  145. GetToolManager()->RunAction( PCB_ACTIONS::selectOnSheetFromEeschema, true,
  146. static_cast<void*>( &sheetStamp ) );
  147. return;
  148. }
  149. else if( strcmp( idcmd, "$PIN:" ) == 0 )
  150. {
  151. wxString pinName;
  152. int netcode = -1;
  153. pinName = FROM_UTF8( text );
  154. text = strtok( NULL, " \n\r" );
  155. if( text && strcmp( text, "$PART:" ) == 0 )
  156. text = strtok( NULL, "\n\r" );
  157. modName = FROM_UTF8( text );
  158. module = pcb->FindModuleByReference( modName );
  159. if( module )
  160. pad = module->FindPadByName( pinName );
  161. if( pad )
  162. {
  163. netcode = pad->GetNetCode();
  164. // put cursor on the pad:
  165. pos = pad->GetPosition();
  166. }
  167. if( netcode > 0 ) // highlight the pad net
  168. {
  169. pcb->HighLightON();
  170. pcb->SetHighLightNet( netcode );
  171. }
  172. else
  173. {
  174. pcb->HighLightOFF();
  175. pcb->SetHighLightNet( -1 );
  176. }
  177. if( module == NULL )
  178. {
  179. msg.Printf( _( "%s not found" ), GetChars( modName ) );
  180. }
  181. else if( pad == NULL )
  182. {
  183. msg.Printf( _( "%s pin %s not found" ), GetChars( modName ), GetChars( pinName ) );
  184. SetCurItem( module );
  185. }
  186. else
  187. {
  188. msg.Printf( _( "%s pin %s found" ), GetChars( modName ), GetChars( pinName ) );
  189. SetCurItem( pad );
  190. }
  191. SetStatusText( msg );
  192. }
  193. if( module ) // if found, center the module on screen, and redraw the screen.
  194. {
  195. if( IsGalCanvasActive() )
  196. {
  197. GetToolManager()->RunAction( PCB_ACTIONS::crossProbeSchToPcb,
  198. true,
  199. pad ?
  200. static_cast<BOARD_ITEM*>( pad ) :
  201. static_cast<BOARD_ITEM*>( module )
  202. );
  203. }
  204. else
  205. {
  206. SetCrossHairPosition( pos );
  207. RedrawScreen( pos, false );
  208. }
  209. }
  210. }
  211. std::string FormatProbeItem( BOARD_ITEM* aItem )
  212. {
  213. MODULE* module;
  214. switch( aItem->Type() )
  215. {
  216. case PCB_MODULE_T:
  217. module = (MODULE*) aItem;
  218. return StrPrintf( "$PART: \"%s\"", TO_UTF8( module->GetReference() ) );
  219. case PCB_PAD_T:
  220. {
  221. module = (MODULE*) aItem->GetParent();
  222. wxString pad = ((D_PAD*)aItem)->GetName();
  223. return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
  224. TO_UTF8( module->GetReference() ),
  225. TO_UTF8( pad ) );
  226. }
  227. case PCB_MODULE_TEXT_T:
  228. {
  229. module = static_cast<MODULE*>( aItem->GetParent() );
  230. TEXTE_MODULE* text_mod = static_cast<TEXTE_MODULE*>( aItem );
  231. const char* text_key;
  232. /* This can't be a switch since the break need to pull out
  233. * from the outer switch! */
  234. if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
  235. text_key = "$REF:";
  236. else if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
  237. text_key = "$VAL:";
  238. else
  239. break;
  240. return StrPrintf( "$PART: \"%s\" %s \"%s\"",
  241. TO_UTF8( module->GetReference() ),
  242. text_key,
  243. TO_UTF8( text_mod->GetText() ) );
  244. }
  245. default:
  246. break;
  247. }
  248. return "";
  249. }
  250. /* Send a remote command to Eeschema via a socket,
  251. * aSyncItem = item to be located on schematic (module, pin or text)
  252. * Commands are
  253. * $PART: "reference" put cursor on component anchor
  254. * $PART: "reference" $PAD: "pad number" put cursor on the component pin
  255. * $PART: "reference" $REF: "reference" put cursor on the component ref
  256. * $PART: "reference" $VAL: "value" put cursor on the component value
  257. */
  258. void PCB_EDIT_FRAME::SendMessageToEESCHEMA( BOARD_ITEM* aSyncItem )
  259. {
  260. #if 1
  261. wxASSERT( aSyncItem ); // can't we fix the caller?
  262. #else
  263. if( !aSyncItem )
  264. return;
  265. #endif
  266. std::string packet = FormatProbeItem( aSyncItem );
  267. if( packet.size() )
  268. {
  269. if( Kiface().IsSingle() )
  270. SendCommand( MSG_TO_SCH, packet.c_str() );
  271. else
  272. {
  273. // Typically ExpressMail is going to be s-expression packets, but since
  274. // we have existing interpreter of the cross probe packet on the other
  275. // side in place, we use that here.
  276. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  277. }
  278. }
  279. }
  280. void PCB_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
  281. {
  282. std::string packet = StrPrintf( "$NET: \"%s\"", TO_UTF8( aNetName ) );
  283. if( packet.size() )
  284. {
  285. if( Kiface().IsSingle() )
  286. SendCommand( MSG_TO_SCH, packet.c_str() );
  287. else
  288. {
  289. // Typically ExpressMail is going to be s-expression packets, but since
  290. // we have existing interpreter of the cross probe packet on the other
  291. // side in place, we use that here.
  292. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  293. }
  294. }
  295. }
  296. void PCB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  297. {
  298. const std::string& payload = mail.GetPayload();
  299. switch( mail.Command() )
  300. {
  301. case MAIL_CROSS_PROBE:
  302. ExecuteRemoteCommand( payload.c_str() );
  303. break;
  304. case MAIL_SCH_PCB_UPDATE:
  305. {
  306. NETLIST netlist;
  307. size_t split = payload.find( '\n' );
  308. wxCHECK( split != std::string::npos, /*void*/ );
  309. // Extract options and netlist
  310. std::string options = payload.substr( 0, split );
  311. std::string netlistData = payload.substr( split + 1 );
  312. // Quiet update options
  313. bool by_reference = options.find( "by-reference" ) != std::string::npos;
  314. bool by_timestamp = options.find( "by-timestamp" ) != std::string::npos;
  315. wxASSERT( !( by_reference && by_timestamp ) ); // only one at a time please
  316. try
  317. {
  318. STRING_LINE_READER* lineReader = new STRING_LINE_READER( netlistData, _( "Eeschema netlist" ) );
  319. KICAD_NETLIST_READER netlistReader( lineReader, &netlist );
  320. netlistReader.LoadNetlist();
  321. }
  322. catch( const IO_ERROR& )
  323. {
  324. assert( false ); // should never happen
  325. }
  326. if( by_reference || by_timestamp )
  327. {
  328. netlist.SetDeleteExtraFootprints( false );
  329. netlist.SetFindByTimeStamp( by_timestamp );
  330. netlist.SetReplaceFootprints( true );
  331. BOARD_NETLIST_UPDATER updater( this, GetBoard() );
  332. updater.SetLookupByTimestamp( by_timestamp );
  333. updater.SetDeleteUnusedComponents( false );
  334. updater.SetReplaceFootprints( true );
  335. updater.SetDeleteSinglePadNets( false );
  336. updater.UpdateNetlist( netlist );
  337. }
  338. else
  339. {
  340. DIALOG_UPDATE_PCB updateDialog( this, &netlist );
  341. updateDialog.PerformUpdate( true );
  342. updateDialog.ShowModal();
  343. }
  344. break;
  345. }
  346. case MAIL_IMPORT_FILE:
  347. {
  348. // Extract file format type and path (plugin type and path separated with \n)
  349. size_t split = payload.find( '\n' );
  350. wxCHECK( split != std::string::npos, /*void*/ );
  351. int importFormat;
  352. try
  353. {
  354. importFormat = std::stoi( payload.substr( 0, split ) );
  355. }
  356. catch( std::invalid_argument& )
  357. {
  358. wxFAIL;
  359. importFormat = -1;
  360. }
  361. std::string path = payload.substr( split + 1 );
  362. wxASSERT( !path.empty() );
  363. if( importFormat >= 0 )
  364. importFile( path, importFormat );
  365. }
  366. // many many others.
  367. default:
  368. ;
  369. }
  370. }