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.

371 lines
11 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
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
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 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 <fctsys.h>
  32. #include <pgm_base.h>
  33. #include <kiface_i.h>
  34. #include <kiway_express.h>
  35. #include <pcb_edit_frame.h>
  36. #include <eda_dde.h>
  37. #include <macros.h>
  38. #include <pcbnew_id.h>
  39. #include <class_board.h>
  40. #include <class_module.h>
  41. #include <class_track.h>
  42. #include <class_zone.h>
  43. #include <collectors.h>
  44. #include <pcbnew.h>
  45. #include <pcb_netlist.h>
  46. #include <tools/pcb_actions.h>
  47. #include <tool/tool_manager.h>
  48. #include <tools/selection_tool.h>
  49. #include <pcb_painter.h>
  50. /* Execute a remote command send by Eeschema via a socket,
  51. * port KICAD_PCB_PORT_SERVICE_NUMBER
  52. * cmdline = received command from Eeschema
  53. * Commands are
  54. * $PART: "reference" put cursor on component
  55. * $PIN: "pin name" $PART: "reference" put cursor on the footprint pin
  56. * $NET: "net name" highlight the given net (if highlight tool is active)
  57. * $CLEAR Clear existing highlight
  58. * They are a keyword followed by a quoted string.
  59. */
  60. void PCB_EDIT_FRAME::ExecuteRemoteCommand( const char* cmdline )
  61. {
  62. char line[1024];
  63. wxString msg;
  64. wxString modName;
  65. char* idcmd;
  66. char* text;
  67. int netcode = -1;
  68. MODULE* module = NULL;
  69. D_PAD* pad = NULL;
  70. BOARD* pcb = GetBoard();
  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. wxString net_name = FROM_UTF8( text );
  82. NETINFO_ITEM* netinfo = pcb->FindNet( net_name );
  83. if( netinfo )
  84. {
  85. netcode = netinfo->GetNet();
  86. MSG_PANEL_ITEMS items;
  87. netinfo->GetMsgPanelInfo( GetUserUnits(), items );
  88. SetMsgPanel( items );
  89. }
  90. }
  91. else if( strcmp( idcmd, "$PIN:" ) == 0 )
  92. {
  93. wxString pinName = FROM_UTF8( text );
  94. text = strtok( NULL, " \n\r" );
  95. if( text && strcmp( text, "$PART:" ) == 0 )
  96. text = strtok( NULL, "\"\n\r" );
  97. modName = FROM_UTF8( text );
  98. module = pcb->FindModuleByReference( modName );
  99. if( module )
  100. pad = module->FindPadByName( pinName );
  101. if( pad )
  102. netcode = pad->GetNetCode();
  103. if( module == NULL )
  104. msg.Printf( _( "%s not found" ), modName );
  105. else if( pad == NULL )
  106. msg.Printf( _( "%s pin %s not found" ), modName, pinName );
  107. else
  108. msg.Printf( _( "%s pin %s found" ), modName, pinName );
  109. SetStatusText( msg );
  110. }
  111. else if( strcmp( idcmd, "$PART:" ) == 0 )
  112. {
  113. pcb->ResetNetHighLight();
  114. modName = FROM_UTF8( text );
  115. module = pcb->FindModuleByReference( modName );
  116. if( module )
  117. msg.Printf( _( "%s found" ), modName );
  118. else
  119. msg.Printf( _( "%s not found" ), modName );
  120. SetStatusText( msg );
  121. }
  122. else if( strcmp( idcmd, "$SHEET:" ) == 0 )
  123. {
  124. msg.Printf( _( "Selecting all from sheet \"%s\"" ), FROM_UTF8( text ) );
  125. wxString sheetStamp( FROM_UTF8( text ) );
  126. SetStatusText( msg );
  127. GetToolManager()->RunAction( PCB_ACTIONS::selectOnSheetFromEeschema, true,
  128. static_cast<void*>( &sheetStamp ) );
  129. return;
  130. }
  131. else if( strcmp( idcmd, "$CLEAR" ) == 0 )
  132. {
  133. renderSettings->SetHighlight( false );
  134. view->UpdateAllLayersColor();
  135. pcb->ResetNetHighLight();
  136. SetMsgPanel( pcb );
  137. GetCanvas()->Refresh();
  138. return;
  139. }
  140. BOX2I bbox = { { 0, 0 }, { 0, 0 } };
  141. if( module )
  142. {
  143. m_toolManager->RunAction( PCB_ACTIONS::highlightItem, true, (void*) module );
  144. bbox = module->GetBoundingBox();
  145. }
  146. else if( netcode > 0 )
  147. {
  148. renderSettings->SetHighlight( ( netcode >= 0 ), netcode );
  149. pcb->SetHighLightNet( netcode );
  150. auto merge_area = [netcode, &bbox]( BOARD_CONNECTED_ITEM* aItem )
  151. {
  152. if( aItem->GetNetCode() == netcode )
  153. {
  154. if( bbox.GetWidth() == 0 )
  155. bbox = aItem->GetBoundingBox();
  156. else
  157. bbox.Merge( aItem->GetBoundingBox() );
  158. }
  159. };
  160. for( auto zone : pcb->Zones() )
  161. merge_area( zone );
  162. for( auto track : pcb->Tracks() )
  163. merge_area( track );
  164. for( auto mod : pcb->Modules() )
  165. for ( auto mod_pad : mod->Pads() )
  166. merge_area( mod_pad );
  167. }
  168. else
  169. {
  170. renderSettings->SetHighlight( false );
  171. }
  172. if( bbox.GetWidth() > 0 && bbox.GetHeight() > 0 )
  173. {
  174. auto bbSize = bbox.Inflate( bbox.GetWidth() * 0.2f ).GetSize();
  175. auto screenSize = view->ToWorld( GetCanvas()->GetClientSize(), false );
  176. double ratio = std::max( fabs( bbSize.x / screenSize.x ),
  177. fabs( bbSize.y / screenSize.y ) );
  178. // Try not to zoom on every cross-probe; it gets very noisy
  179. if( ratio < 0.1 || ratio > 1.0 )
  180. view->SetScale( view->GetScale() / ratio );
  181. view->SetCenter( bbox.Centre() );
  182. }
  183. view->UpdateAllLayersColor();
  184. // Ensure the display is refreshed, because in some installs the refresh is done only
  185. // when the gal canvas has the focus, and that is not the case when crossprobing from
  186. // Eeschema:
  187. GetCanvas()->Refresh();
  188. }
  189. std::string FormatProbeItem( BOARD_ITEM* aItem )
  190. {
  191. MODULE* module;
  192. if( !aItem )
  193. return "$CLEAR: \"HIGHLIGHTED\""; // message to clear highlight state
  194. switch( aItem->Type() )
  195. {
  196. case PCB_MODULE_T:
  197. module = (MODULE*) aItem;
  198. return StrPrintf( "$PART: \"%s\"", TO_UTF8( module->GetReference() ) );
  199. case PCB_PAD_T:
  200. {
  201. module = (MODULE*) aItem->GetParent();
  202. wxString pad = ((D_PAD*)aItem)->GetName();
  203. return StrPrintf( "$PART: \"%s\" $PAD: \"%s\"",
  204. TO_UTF8( module->GetReference() ),
  205. TO_UTF8( pad ) );
  206. }
  207. case PCB_MODULE_TEXT_T:
  208. {
  209. module = static_cast<MODULE*>( aItem->GetParent() );
  210. TEXTE_MODULE* text_mod = static_cast<TEXTE_MODULE*>( aItem );
  211. const char* text_key;
  212. /* This can't be a switch since the break need to pull out
  213. * from the outer switch! */
  214. if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_REFERENCE )
  215. text_key = "$REF:";
  216. else if( text_mod->GetType() == TEXTE_MODULE::TEXT_is_VALUE )
  217. text_key = "$VAL:";
  218. else
  219. break;
  220. return StrPrintf( "$PART: \"%s\" %s \"%s\"",
  221. TO_UTF8( module->GetReference() ),
  222. text_key,
  223. TO_UTF8( text_mod->GetText() ) );
  224. }
  225. default:
  226. break;
  227. }
  228. return "";
  229. }
  230. /* Send a remote command to Eeschema via a socket,
  231. * aSyncItem = item to be located on schematic (module, pin or text)
  232. * Commands are
  233. * $PART: "reference" put cursor on component anchor
  234. * $PART: "reference" $PAD: "pad number" put cursor on the component pin
  235. * $PART: "reference" $REF: "reference" put cursor on the component ref
  236. * $PART: "reference" $VAL: "value" put cursor on the component value
  237. */
  238. void PCB_EDIT_FRAME::SendMessageToEESCHEMA( BOARD_ITEM* aSyncItem )
  239. {
  240. std::string packet = FormatProbeItem( aSyncItem );
  241. if( !packet.empty() )
  242. {
  243. if( Kiface().IsSingle() )
  244. SendCommand( MSG_TO_SCH, packet.c_str() );
  245. else
  246. {
  247. // Typically ExpressMail is going to be s-expression packets, but since
  248. // we have existing interpreter of the cross probe packet on the other
  249. // side in place, we use that here.
  250. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  251. }
  252. }
  253. }
  254. void PCB_EDIT_FRAME::SendCrossProbeNetName( const wxString& aNetName )
  255. {
  256. std::string packet = StrPrintf( "$NET: \"%s\"", TO_UTF8( aNetName ) );
  257. if( !packet.empty() )
  258. {
  259. if( Kiface().IsSingle() )
  260. SendCommand( MSG_TO_SCH, packet.c_str() );
  261. else
  262. {
  263. // Typically ExpressMail is going to be s-expression packets, but since
  264. // we have existing interpreter of the cross probe packet on the other
  265. // side in place, we use that here.
  266. Kiway().ExpressMail( FRAME_SCH, MAIL_CROSS_PROBE, packet, this );
  267. }
  268. }
  269. }
  270. void PCB_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  271. {
  272. const std::string& payload = mail.GetPayload();
  273. switch( mail.Command() )
  274. {
  275. case MAIL_CROSS_PROBE:
  276. ExecuteRemoteCommand( payload.c_str() );
  277. break;
  278. case MAIL_PCB_UPDATE:
  279. m_toolManager->RunAction( ACTIONS::updatePcbFromSchematic, true );
  280. break;
  281. case MAIL_IMPORT_FILE:
  282. {
  283. // Extract file format type and path (plugin type and path separated with \n)
  284. size_t split = payload.find( '\n' );
  285. wxCHECK( split != std::string::npos, /*void*/ );
  286. int importFormat;
  287. try
  288. {
  289. importFormat = std::stoi( payload.substr( 0, split ) );
  290. }
  291. catch( std::invalid_argument& )
  292. {
  293. wxFAIL;
  294. importFormat = -1;
  295. }
  296. std::string path = payload.substr( split + 1 );
  297. wxASSERT( !path.empty() );
  298. if( importFormat >= 0 )
  299. importFile( path, importFormat );
  300. }
  301. // many many others.
  302. default:
  303. ;
  304. }
  305. }