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.

1562 lines
47 KiB

Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
Make the new schematic and symbol library file formats the default. This is a very large and potentially disruptive change so this will be an unusually long and detailed commit message. The new file formats are now the default in both the schematic and symbol library editors. Existing symbol libraries will be saved in their current format until new features are added to library symbols. Once this happens, both the legacy schematic and symbol file formats will be no longer be savable and existing libraries will have to be converted. Saving to the legacy file formats is still available for round robin testing and should not be used for normal editing. When loading the legacy schematic file, it is imperative that the schematic library symbols are rescued and/or remapped to valid library identifiers. Otherwise, there will be no way to link to the original library symbol and the user will be required manually set the library identifier. The cached symbol will be saved in the schematic file so the last library symbol in the cache will still be used but there will be no way to update it from the original library. The next save after loading a legacy schematic file will be converted to the s-expression file format. Schematics with hierarchical sheets will automatically have all sheet file name extensions changed to .kicad_sym and saved to the new format as well. Appending schematics requires that the schematic to append has already been converted to the new file format. This is required to ensure that library symbols are guaranteed to be valid for the appended schematic. The schematic symbol library symbol link resolution has been moved out of the SCH_COMPONENT object and move into the SCH_SCREEN object that owns the symbol. This was done to ensure that there is a single place where the library symbol links get resolved rather than the dozen or so different code paths that previously existed. It also removes the necessity of the SCH_COMPONENT object of requiring any knowledge of the symbol library table and/or the cache library. When opening an s-expression schematic, the legacy cache library is not loaded so any library symbols not rescued cannot be loaded. Broken library symbol links will have to be manually resolved by adding the cache library to the symbol library table and changing the links in the schematic symbol. Now that the library symbols are embedded in the schematic file, the SCH_SCREEN object maintains the list of library symbols for the schematic automatically. No external manipulation of this library cache should ever occur. ADDED: S-expression schematic and symbol library file formats.
6 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 CERN
  5. * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <class_library.h>
  25. #include <confirm.h>
  26. #include <connection_graph.h>
  27. #include <dialogs/dialog_fields_editor_global.h>
  28. #include <dialogs/dialog_page_settings.h>
  29. #include <dialogs/dialog_paste_special.h>
  30. #include <erc.h>
  31. #include <fctsys.h>
  32. #include <invoke_sch_dialog.h>
  33. #include <kiway.h>
  34. #include <netlist_exporters/netlist_exporter_pspice.h>
  35. #include <netlist_object.h>
  36. #include <sch_edit_frame.h>
  37. #include <sch_legacy_plugin.h>
  38. #include <sch_line.h>
  39. #include <sch_painter.h>
  40. #include <sch_sheet.h>
  41. #include <sch_view.h>
  42. #include <advanced_config.h>
  43. #include <sim/sim_plot_frame.h>
  44. #include <simulation_cursors.h>
  45. #include <status_popup.h>
  46. #include <tool/picker_tool.h>
  47. #include <tool/tool_manager.h>
  48. #include <tools/ee_actions.h>
  49. #include <tools/ee_selection.h>
  50. #include <tools/ee_selection_tool.h>
  51. #include <tools/sch_editor_control.h>
  52. #include <ws_proxy_undo_item.h>
  53. #include <dialogs/dialog_page_settings.h>
  54. #include <dialogs/dialog_fields_editor_global.h>
  55. #include <invoke_sch_dialog.h>
  56. #include <dialogs/dialog_paste_special.h>
  57. #include <netlist_exporters/netlist_exporter_pspice.h>
  58. #include <math/util.h> // for KiROUND
  59. #include <dialog_update_from_pcb.h>
  60. int SCH_EDITOR_CONTROL::New( const TOOL_EVENT& aEvent )
  61. {
  62. m_frame->NewProject();
  63. return 0;
  64. }
  65. int SCH_EDITOR_CONTROL::Open( const TOOL_EVENT& aEvent )
  66. {
  67. m_frame->LoadProject();
  68. return 0;
  69. }
  70. int SCH_EDITOR_CONTROL::Save( const TOOL_EVENT& aEvent )
  71. {
  72. m_frame->SaveProject();
  73. return 0;
  74. }
  75. int SCH_EDITOR_CONTROL::SaveAs( const TOOL_EVENT& aEvent )
  76. {
  77. m_frame->Save_File( true );
  78. return 0;
  79. }
  80. int SCH_EDITOR_CONTROL::ShowSchematicSetup( const TOOL_EVENT& aEvent )
  81. {
  82. m_frame->DoShowSchematicSetupDialog();
  83. return 0;
  84. }
  85. int SCH_EDITOR_CONTROL::PageSetup( const TOOL_EVENT& aEvent )
  86. {
  87. PICKED_ITEMS_LIST undoCmd;
  88. WS_PROXY_UNDO_ITEM* undoItem = new WS_PROXY_UNDO_ITEM( m_frame );
  89. ITEM_PICKER wrapper( undoItem, UR_PAGESETTINGS );
  90. undoCmd.PushItem( wrapper );
  91. m_frame->SaveCopyInUndoList( undoCmd, UR_PAGESETTINGS );
  92. DIALOG_PAGES_SETTINGS dlg( m_frame, wxSize( MAX_PAGE_SIZE_MILS, MAX_PAGE_SIZE_MILS ) );
  93. dlg.SetWksFileName( BASE_SCREEN::m_PageLayoutDescrFileName );
  94. if( dlg.ShowModal() != wxID_OK )
  95. m_frame->RollbackSchematicFromUndo();
  96. return 0;
  97. }
  98. int SCH_EDITOR_CONTROL::Print( const TOOL_EVENT& aEvent )
  99. {
  100. m_frame->Print();
  101. return 0;
  102. }
  103. int SCH_EDITOR_CONTROL::Plot( const TOOL_EVENT& aEvent )
  104. {
  105. m_frame->PlotSchematic();
  106. return 0;
  107. }
  108. int SCH_EDITOR_CONTROL::Quit( const TOOL_EVENT& aEvent )
  109. {
  110. m_frame->Close( false );
  111. return 0;
  112. }
  113. // A dummy wxFindReplaceData signalling any marker should be found
  114. static wxFindReplaceData g_markersOnly;
  115. int SCH_EDITOR_CONTROL::FindAndReplace( const TOOL_EVENT& aEvent )
  116. {
  117. m_frame->ShowFindReplaceDialog( aEvent.IsAction( &ACTIONS::findAndReplace ));
  118. return UpdateFind( aEvent );
  119. }
  120. int SCH_EDITOR_CONTROL::NavigateHierarchy( const TOOL_EVENT& aEvent )
  121. {
  122. m_frame->UpdateHierarchyNavigator( true );
  123. return 0;
  124. }
  125. int SCH_EDITOR_CONTROL::UpdateFind( const TOOL_EVENT& aEvent )
  126. {
  127. wxFindReplaceData* data = m_frame->GetFindReplaceData();
  128. auto visit = [&] ( EDA_ITEM* aItem )
  129. {
  130. if( data && aItem->Matches( *data, nullptr ) )
  131. {
  132. aItem->SetForceVisible( true );
  133. m_selectionTool->BrightenItem( aItem );
  134. }
  135. else if( aItem->IsBrightened() )
  136. {
  137. aItem->SetForceVisible( false );
  138. m_selectionTool->UnbrightenItem( aItem );
  139. }
  140. };
  141. if( aEvent.IsAction( &ACTIONS::find ) || aEvent.IsAction( &ACTIONS::findAndReplace )
  142. || aEvent.IsAction( &ACTIONS::updateFind ) )
  143. {
  144. m_selectionTool->ClearSelection();
  145. for( SCH_ITEM* item : m_frame->GetScreen()->Items() )
  146. {
  147. visit( item );
  148. if( item->Type() == SCH_COMPONENT_T )
  149. {
  150. SCH_COMPONENT* cmp = static_cast<SCH_COMPONENT*>( item );
  151. for( SCH_FIELD& field : cmp->GetFields() )
  152. visit( &field );
  153. for( SCH_PIN* pin : cmp->GetSchPins() )
  154. visit( pin );
  155. }
  156. else if( item->Type() == SCH_SHEET_T )
  157. {
  158. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
  159. for( SCH_FIELD& field : sheet->GetFields() )
  160. visit( &field );
  161. for( SCH_SHEET_PIN* pin : sheet->GetPins() )
  162. visit( pin );
  163. }
  164. }
  165. }
  166. else if( aEvent.Matches( EVENTS::SelectedItemsModified ) )
  167. {
  168. for( EDA_ITEM* item : m_selectionTool->GetSelection() )
  169. visit( item );
  170. }
  171. getView()->UpdateItems();
  172. m_frame->GetCanvas()->Refresh();
  173. m_frame->UpdateTitle();
  174. return 0;
  175. }
  176. SCH_ITEM* SCH_EDITOR_CONTROL::nextMatch( SCH_SCREEN* aScreen, SCH_ITEM* aAfter,
  177. wxFindReplaceData* aData )
  178. {
  179. bool past_item = true;
  180. if( aAfter != nullptr )
  181. {
  182. past_item = false;
  183. if( aAfter->Type() == SCH_PIN_T || aAfter->Type() == SCH_FIELD_T )
  184. aAfter = static_cast<SCH_ITEM*>( aAfter->GetParent() );
  185. }
  186. for( SCH_ITEM* item : aScreen->Items() )
  187. {
  188. if( item == aAfter )
  189. {
  190. past_item = true;
  191. }
  192. else if( past_item )
  193. {
  194. if( aData == &g_markersOnly && item->Type() == SCH_MARKER_T )
  195. return item;
  196. if( item->Matches( *aData, nullptr ) )
  197. return item;
  198. if( item->Type() == SCH_COMPONENT_T )
  199. {
  200. SCH_COMPONENT* cmp = static_cast<SCH_COMPONENT*>( item );
  201. for( SCH_FIELD& field : cmp->GetFields() )
  202. {
  203. if( field.Matches( *aData, nullptr ) )
  204. return &field;
  205. }
  206. for( SCH_PIN* pin : cmp->GetSchPins() )
  207. {
  208. if( pin->Matches( *aData, nullptr ) )
  209. return pin;
  210. }
  211. }
  212. if( item->Type() == SCH_SHEET_T )
  213. {
  214. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
  215. for( SCH_FIELD& field : sheet->GetFields() )
  216. {
  217. if( field.Matches( *aData, nullptr ) )
  218. return &field;
  219. }
  220. for( SCH_SHEET_PIN* pin : sheet->GetPins() )
  221. {
  222. if( pin->Matches( *aData, nullptr ) )
  223. return pin;
  224. }
  225. }
  226. }
  227. }
  228. return nullptr;
  229. }
  230. int SCH_EDITOR_CONTROL::FindNext( const TOOL_EVENT& aEvent )
  231. {
  232. // A timer during which a subsequent FindNext will result in a wrap-around
  233. static wxTimer wrapAroundTimer;
  234. wxFindReplaceData* data = m_frame->GetFindReplaceData();
  235. if( aEvent.IsAction( &ACTIONS::findNextMarker ) )
  236. {
  237. if( data )
  238. g_markersOnly.SetFlags( data->GetFlags() );
  239. data = &g_markersOnly;
  240. }
  241. else if( !data )
  242. {
  243. return FindAndReplace( ACTIONS::find.MakeEvent() );
  244. }
  245. bool searchAllSheets = !( data->GetFlags() & FR_CURRENT_SHEET_ONLY );
  246. EE_SELECTION& selection = m_selectionTool->GetSelection();
  247. SCH_SCREEN* afterScreen = m_frame->GetScreen();
  248. SCH_ITEM* afterItem = dynamic_cast<SCH_ITEM*>( selection.Front() );
  249. SCH_ITEM* item = nullptr;
  250. if( wrapAroundTimer.IsRunning() )
  251. {
  252. afterScreen = nullptr;
  253. afterItem = nullptr;
  254. wrapAroundTimer.Stop();
  255. m_frame->ClearFindReplaceStatus();
  256. }
  257. m_selectionTool->ClearSelection();
  258. if( afterScreen || !searchAllSheets )
  259. item = nextMatch( m_frame->GetScreen(), afterItem, data );
  260. if( !item && searchAllSheets )
  261. {
  262. SCH_SHEET_LIST schematic( g_RootSheet );
  263. SCH_SCREENS screens;
  264. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  265. {
  266. if( afterScreen )
  267. {
  268. if( afterScreen == screen )
  269. afterScreen = nullptr;
  270. continue;
  271. }
  272. item = nextMatch( screen, nullptr, data );
  273. if( item )
  274. {
  275. SCH_SHEET_PATH* sheet = schematic.FindSheetForScreen( screen );
  276. wxCHECK_MSG( sheet, 0, "Sheet not found for " + screen->GetFileName() );
  277. *g_CurrentSheet = *sheet;
  278. g_CurrentSheet->UpdateAllScreenReferences();
  279. screen->SetZoom( m_frame->GetScreen()->GetZoom() );
  280. screen->TestDanglingEnds();
  281. m_frame->SetScreen( screen );
  282. UpdateFind( ACTIONS::updateFind.MakeEvent() );
  283. break;
  284. }
  285. }
  286. }
  287. if( item )
  288. {
  289. m_selectionTool->AddItemToSel( item );
  290. m_frame->FocusOnLocation( item->GetBoundingBox().GetCenter() );
  291. m_frame->GetCanvas()->Refresh();
  292. }
  293. else
  294. {
  295. wxString msg = searchAllSheets ? _( "Reached end of schematic." )
  296. : _( "Reached end of sheet." );
  297. m_frame->ShowFindReplaceStatus( msg + _( "\nFind again to wrap around to the start." ) );
  298. wrapAroundTimer.StartOnce( 4000 );
  299. }
  300. return 0;
  301. }
  302. bool SCH_EDITOR_CONTROL::HasMatch()
  303. {
  304. wxFindReplaceData* data = m_frame->GetFindReplaceData();
  305. EDA_ITEM* item = m_selectionTool->GetSelection().Front();
  306. return data && item && item->Matches( *data, nullptr );
  307. }
  308. int SCH_EDITOR_CONTROL::ReplaceAndFindNext( const TOOL_EVENT& aEvent )
  309. {
  310. wxFindReplaceData* data = m_frame->GetFindReplaceData();
  311. EDA_ITEM* item = m_selectionTool->GetSelection().Front();
  312. if( !data )
  313. return FindAndReplace( ACTIONS::find.MakeEvent() );
  314. if( item && item->Matches( *data, nullptr ) )
  315. {
  316. if( item->Replace( *data, g_CurrentSheet ) )
  317. {
  318. m_frame->RefreshItem( item );
  319. m_frame->OnModify();
  320. }
  321. FindNext( ACTIONS::findNext.MakeEvent() );
  322. }
  323. return 0;
  324. }
  325. int SCH_EDITOR_CONTROL::ReplaceAll( const TOOL_EVENT& aEvent )
  326. {
  327. wxFindReplaceData* data = m_frame->GetFindReplaceData();
  328. if( !data )
  329. return FindAndReplace( ACTIONS::find.MakeEvent() );
  330. SCH_SHEET_LIST schematic( g_RootSheet );
  331. SCH_SCREENS screens;
  332. for( SCH_SCREEN* screen = screens.GetFirst(); screen; screen = screens.GetNext() )
  333. {
  334. for( EDA_ITEM* item = nextMatch( screen, nullptr, data ); item; )
  335. {
  336. if( item->Replace( *data, schematic.FindSheetForScreen( screen ) ) )
  337. {
  338. m_frame->RefreshItem( item );
  339. m_frame->OnModify();
  340. }
  341. item = nextMatch( screen, dynamic_cast<SCH_ITEM*>( item ), data );
  342. }
  343. }
  344. return 0;
  345. }
  346. int SCH_EDITOR_CONTROL::CrossProbeToPcb( const TOOL_EVENT& aEvent )
  347. {
  348. doCrossProbeSchToPcb( aEvent, false );
  349. return 0;
  350. }
  351. int SCH_EDITOR_CONTROL::ExplicitCrossProbeToPcb( const TOOL_EVENT& aEvent )
  352. {
  353. doCrossProbeSchToPcb( aEvent, true );
  354. return 0;
  355. }
  356. void SCH_EDITOR_CONTROL::doCrossProbeSchToPcb( const TOOL_EVENT& aEvent, bool aForce )
  357. {
  358. // Don't get in an infinite loop SCH -> PCB -> SCH -> PCB -> SCH -> ...
  359. if( m_probingPcbToSch )
  360. return;
  361. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  362. SCH_ITEM* item = nullptr;
  363. SCH_COMPONENT* component = nullptr;
  364. if( aForce )
  365. {
  366. EE_SELECTION& selection = selTool->RequestSelection();
  367. if( selection.GetSize() >= 1 )
  368. item = (SCH_ITEM*) selection.Front();
  369. }
  370. else
  371. {
  372. EE_SELECTION& selection = selTool->GetSelection();
  373. if( selection.GetSize() >= 1 )
  374. item = (SCH_ITEM*) selection.Front();
  375. }
  376. if( !item )
  377. {
  378. if( aForce )
  379. m_frame->SendMessageToPCBNEW( nullptr, nullptr );
  380. return;
  381. }
  382. switch( item->Type() )
  383. {
  384. case SCH_FIELD_T:
  385. case LIB_FIELD_T:
  386. if( item->GetParent() && item->GetParent()->Type() == SCH_COMPONENT_T )
  387. {
  388. component = (SCH_COMPONENT*) item->GetParent();
  389. m_frame->SendMessageToPCBNEW( item, component );
  390. }
  391. break;
  392. case SCH_COMPONENT_T:
  393. component = (SCH_COMPONENT*) item;
  394. m_frame->SendMessageToPCBNEW( item, component );
  395. break;
  396. case SCH_PIN_T:
  397. component = (SCH_COMPONENT*) item->GetParent();
  398. m_frame->SendMessageToPCBNEW( static_cast<SCH_PIN*>( item ), component );
  399. break;
  400. case SCH_SHEET_T:
  401. if( aForce )
  402. m_frame->SendMessageToPCBNEW( item, nullptr );
  403. break;
  404. default:
  405. break;
  406. }
  407. }
  408. #ifdef KICAD_SPICE
  409. static KICAD_T wires[] = { SCH_LINE_LOCATE_WIRE_T, EOT };
  410. static KICAD_T wiresAndPins[] = { SCH_LINE_LOCATE_WIRE_T, SCH_PIN_T, SCH_SHEET_PIN_T, EOT };
  411. static KICAD_T fieldsAndComponents[] = { SCH_COMPONENT_T, SCH_FIELD_T, EOT };
  412. #define HITTEST_THRESHOLD_PIXELS 5
  413. int SCH_EDITOR_CONTROL::SimProbe( const TOOL_EVENT& aEvent )
  414. {
  415. auto picker = m_toolMgr->GetTool<PICKER_TOOL>();
  416. auto simFrame = (SIM_PLOT_FRAME*) m_frame->Kiway().Player( FRAME_SIMULATOR, false );
  417. if( !simFrame ) // Defensive coding; shouldn't happen.
  418. return 0;
  419. // Deactivate other tools; particularly important if another PICKER is currently running
  420. Activate();
  421. picker->SetCursor( SIM_CURSORS::GetCursor( SIM_CURSORS::VOLTAGE_PROBE ) );
  422. picker->SetClickHandler(
  423. [this, simFrame] ( const VECTOR2D& aPosition )
  424. {
  425. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  426. EDA_ITEM* item = selTool->SelectPoint( aPosition, wiresAndPins );
  427. if( !item )
  428. return false;
  429. if( item->IsType( wires ) )
  430. {
  431. std::unique_ptr<NETLIST_OBJECT_LIST> netlist( m_frame->BuildNetListBase() );
  432. for( NETLIST_OBJECT* obj : *netlist )
  433. {
  434. if( obj->m_Comp == item )
  435. {
  436. simFrame->AddVoltagePlot( UnescapeString( obj->GetNetName() ) );
  437. break;
  438. }
  439. }
  440. }
  441. else if( item->Type() == SCH_PIN_T )
  442. {
  443. SCH_PIN* pin = (SCH_PIN*) item;
  444. SCH_COMPONENT* comp = (SCH_COMPONENT*) item->GetParent();
  445. wxString param;
  446. wxString primitive;
  447. primitive = NETLIST_EXPORTER_PSPICE::GetSpiceField( SF_PRIMITIVE, comp, 0 );
  448. primitive.LowerCase();
  449. if( primitive == "c" || primitive == "l" || primitive == "r" )
  450. param = wxT( "I" );
  451. else if( primitive == "d" )
  452. param = wxT( "Id" );
  453. else
  454. param = wxString::Format( wxT( "I%s" ), pin->GetName().Lower() );
  455. simFrame->AddCurrentPlot( comp->GetRef( g_CurrentSheet ), param );
  456. }
  457. return true;
  458. } );
  459. picker->SetMotionHandler(
  460. [this, picker] ( const VECTOR2D& aPos )
  461. {
  462. EE_COLLECTOR collector;
  463. collector.m_Threshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) );
  464. collector.Collect( m_frame->GetScreen(), wiresAndPins, (wxPoint) aPos );
  465. EE_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  466. selectionTool->GuessSelectionCandidates( collector, aPos );
  467. EDA_ITEM* item = collector.GetCount() == 1 ? collector[ 0 ] : nullptr;
  468. SCH_LINE* wire = dynamic_cast<SCH_LINE*>( item );
  469. wxString netName;
  470. if( wire )
  471. {
  472. item = nullptr;
  473. if( wire->Connection( *g_CurrentSheet ) )
  474. netName = wire->Connection( *g_CurrentSheet )->Name();
  475. }
  476. if( item && item->Type() == SCH_PIN_T )
  477. picker->SetCursor( SIM_CURSORS::GetCursor( SIM_CURSORS::CURRENT_PROBE ) );
  478. else
  479. picker->SetCursor( SIM_CURSORS::GetCursor( SIM_CURSORS::VOLTAGE_PROBE ) );
  480. if( m_pickerItem != item )
  481. {
  482. if( m_pickerItem )
  483. selectionTool->UnbrightenItem( m_pickerItem );
  484. m_pickerItem = item;
  485. if( m_pickerItem )
  486. selectionTool->BrightenItem( m_pickerItem );
  487. }
  488. if( m_frame->GetSelectedNetName() != netName )
  489. {
  490. m_frame->SetSelectedNetName( netName );
  491. TOOL_EVENT dummyEvent;
  492. UpdateNetHighlighting( dummyEvent );
  493. }
  494. } );
  495. picker->SetFinalizeHandler(
  496. [this] ( const int& aFinalState )
  497. {
  498. if( m_pickerItem )
  499. m_toolMgr->GetTool<EE_SELECTION_TOOL>()->UnbrightenItem( m_pickerItem );
  500. if( !m_frame->GetSelectedNetName().IsEmpty() )
  501. {
  502. m_frame->SetSelectedNetName( wxEmptyString );
  503. TOOL_EVENT dummyEvent;
  504. UpdateNetHighlighting( dummyEvent );
  505. }
  506. } );
  507. std::string tool = aEvent.GetCommandStr().get();
  508. m_toolMgr->RunAction( ACTIONS::pickerTool, true, &tool );
  509. return 0;
  510. }
  511. int SCH_EDITOR_CONTROL::SimTune( const TOOL_EVENT& aEvent )
  512. {
  513. PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
  514. // Deactivate other tools; particularly important if another PICKER is currently running
  515. Activate();
  516. picker->SetCursor( SIM_CURSORS::GetCursor( SIM_CURSORS::CURSOR::TUNE ) );
  517. picker->SetClickHandler(
  518. [this] ( const VECTOR2D& aPosition )
  519. {
  520. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  521. EDA_ITEM* item = selTool->SelectPoint( aPosition, fieldsAndComponents );
  522. if( !item )
  523. return false;
  524. if( item->Type() != SCH_COMPONENT_T )
  525. {
  526. item = item->GetParent();
  527. if( item->Type() != SCH_COMPONENT_T )
  528. return false;
  529. }
  530. SIM_PLOT_FRAME* simFrame =
  531. (SIM_PLOT_FRAME*) m_frame->Kiway().Player( FRAME_SIMULATOR, false );
  532. if( simFrame )
  533. simFrame->AddTuner( static_cast<SCH_COMPONENT*>( item ) );
  534. return true;
  535. } );
  536. picker->SetMotionHandler(
  537. [this] ( const VECTOR2D& aPos )
  538. {
  539. EE_COLLECTOR collector;
  540. collector.m_Threshold = KiROUND( getView()->ToWorld( HITTEST_THRESHOLD_PIXELS ) );
  541. collector.Collect( m_frame->GetScreen(), fieldsAndComponents, (wxPoint) aPos );
  542. EE_SELECTION_TOOL* selectionTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  543. selectionTool->GuessSelectionCandidates( collector, aPos );
  544. EDA_ITEM* item = collector.GetCount() == 1 ? collector[ 0 ] : nullptr;
  545. if( m_pickerItem != item )
  546. {
  547. if( m_pickerItem )
  548. selectionTool->UnbrightenItem( m_pickerItem );
  549. m_pickerItem = item;
  550. if( m_pickerItem )
  551. selectionTool->BrightenItem( m_pickerItem );
  552. }
  553. } );
  554. picker->SetFinalizeHandler(
  555. [this] ( const int& aFinalState )
  556. {
  557. if( m_pickerItem )
  558. m_toolMgr->GetTool<EE_SELECTION_TOOL>()->UnbrightenItem( m_pickerItem );
  559. } );
  560. std::string tool = aEvent.GetCommandStr().get();
  561. m_toolMgr->RunAction( ACTIONS::pickerTool, true, &tool );
  562. return 0;
  563. }
  564. #endif /* KICAD_SPICE */
  565. // A singleton reference for clearing the highlight
  566. static VECTOR2D CLEAR;
  567. // TODO(JE) Probably use netcode rather than connection name here eventually
  568. static bool highlightNet( TOOL_MANAGER* aToolMgr, const VECTOR2D& aPosition )
  569. {
  570. SCH_EDIT_FRAME* editFrame = static_cast<SCH_EDIT_FRAME*>( aToolMgr->GetToolHolder() );
  571. EE_SELECTION_TOOL* selTool = aToolMgr->GetTool<EE_SELECTION_TOOL>();
  572. SCH_EDITOR_CONTROL* editorControl = aToolMgr->GetTool<SCH_EDITOR_CONTROL>();
  573. wxString netName;
  574. bool retVal = true;
  575. if( aPosition != CLEAR )
  576. {
  577. if( TestDuplicateSheetNames( false ) > 0 )
  578. {
  579. wxMessageBox( _( "Error: duplicate sub-sheet names found in current sheet." ) );
  580. retVal = false;
  581. }
  582. else
  583. {
  584. SCH_ITEM* item = static_cast<SCH_ITEM*>( selTool->GetNode( aPosition ) );
  585. SCH_COMPONENT* comp = nullptr;
  586. if( item )
  587. {
  588. if( item->Type() == SCH_FIELD_T )
  589. comp = dynamic_cast<SCH_COMPONENT*>( item->GetParent() );
  590. else
  591. comp = dynamic_cast<SCH_COMPONENT*>( item );
  592. }
  593. if( comp && comp->GetPartRef() && comp->GetPartRef()->IsPower() )
  594. netName = comp->GetPartRef()->GetName();
  595. else if( item && item->Connection( *g_CurrentSheet ) )
  596. netName = item->Connection( *g_CurrentSheet )->Name();
  597. }
  598. }
  599. if( netName.empty() )
  600. {
  601. editFrame->SetStatusText( wxT( "" ) );
  602. editFrame->SendCrossProbeClearHighlight();
  603. }
  604. else
  605. {
  606. editFrame->SendCrossProbeNetName( netName );
  607. editFrame->SetStatusText( wxString::Format( _( "Highlighted net: %s" ),
  608. UnescapeString( netName ) ) );
  609. }
  610. editFrame->SetSelectedNetName( netName );
  611. TOOL_EVENT dummy;
  612. editorControl->UpdateNetHighlighting( dummy );
  613. return retVal;
  614. }
  615. int SCH_EDITOR_CONTROL::HighlightNet( const TOOL_EVENT& aEvent )
  616. {
  617. KIGFX::VIEW_CONTROLS* controls = getViewControls();
  618. VECTOR2D cursorPos = controls->GetCursorPosition( !aEvent.Modifier( MD_ALT ) );
  619. highlightNet( m_toolMgr, cursorPos );
  620. return 0;
  621. }
  622. int SCH_EDITOR_CONTROL::ClearHighlight( const TOOL_EVENT& aEvent )
  623. {
  624. highlightNet( m_toolMgr, CLEAR );
  625. return 0;
  626. }
  627. int SCH_EDITOR_CONTROL::UpdateNetHighlighting( const TOOL_EVENT& aEvent )
  628. {
  629. SCH_SCREEN* screen = g_CurrentSheet->LastScreen();
  630. std::vector<EDA_ITEM*> itemsToRedraw;
  631. wxString selectedNetName = m_frame->GetSelectedNetName();
  632. if( !screen )
  633. return 0;
  634. for( SCH_ITEM* item : screen->Items() )
  635. {
  636. wxString itemConnectionName;
  637. SCH_COMPONENT* comp = nullptr;
  638. bool redraw = item->IsBrightened();
  639. if( item->Type() == SCH_COMPONENT_T )
  640. comp = static_cast<SCH_COMPONENT*>( item );
  641. if( comp && comp->GetPartRef() && comp->GetPartRef()->IsPower() )
  642. {
  643. itemConnectionName = comp->GetPartRef()->GetName();
  644. }
  645. else
  646. {
  647. SCH_CONNECTION* connection = item->Connection( *g_CurrentSheet );
  648. if( connection )
  649. itemConnectionName = connection->Name();
  650. }
  651. if( !selectedNetName.IsEmpty() && itemConnectionName == selectedNetName )
  652. item->SetBrightened();
  653. else
  654. item->ClearBrightened();
  655. redraw |= item->IsBrightened();
  656. if( item->Type() == SCH_COMPONENT_T )
  657. {
  658. redraw |= comp->HasBrightenedPins();
  659. comp->ClearBrightenedPins();
  660. std::vector<LIB_PIN*> pins;
  661. comp->GetPins( pins );
  662. for( LIB_PIN* pin : pins )
  663. {
  664. SCH_CONNECTION* pin_conn = comp->GetConnectionForPin( pin, *g_CurrentSheet );
  665. if( comp && pin_conn && pin_conn->Name( false ) == selectedNetName )
  666. {
  667. comp->BrightenPin( pin );
  668. redraw = true;
  669. }
  670. }
  671. if( comp->GetPartRef() && comp->GetPartRef()->IsPower() )
  672. {
  673. std::vector<SCH_FIELD>& fields = comp->GetFields();
  674. for( int id : { REFERENCE, VALUE } )
  675. {
  676. if( item->IsBrightened() && fields[id].IsVisible() )
  677. fields[id].SetBrightened();
  678. else
  679. fields[id].ClearBrightened();
  680. }
  681. }
  682. }
  683. else if( item->Type() == SCH_SHEET_T )
  684. {
  685. for( SCH_SHEET_PIN* pin : static_cast<SCH_SHEET*>( item )->GetPins() )
  686. {
  687. SCH_CONNECTION* pin_conn = pin->Connection( *g_CurrentSheet );
  688. bool redrawPin = pin->IsBrightened();
  689. if( pin_conn && pin_conn->Name() == selectedNetName )
  690. pin->SetBrightened();
  691. else
  692. pin->ClearBrightened();
  693. redrawPin |= pin->IsBrightened();
  694. if( redrawPin )
  695. itemsToRedraw.push_back( pin );
  696. }
  697. }
  698. if( redraw )
  699. itemsToRedraw.push_back( item );
  700. }
  701. // Be sure highlight change will be redrawn
  702. KIGFX::VIEW* view = getView();
  703. for( EDA_ITEM* redrawItem : itemsToRedraw )
  704. view->Update( (KIGFX::VIEW_ITEM*)redrawItem, KIGFX::VIEW_UPDATE_FLAGS::REPAINT );
  705. m_frame->GetCanvas()->Refresh();
  706. return 0;
  707. }
  708. int SCH_EDITOR_CONTROL::HighlightNetCursor( const TOOL_EVENT& aEvent )
  709. {
  710. // TODO(JE) remove once real-time connectivity is a given
  711. if( !ADVANCED_CFG::GetCfg().m_realTimeConnectivity || !CONNECTION_GRAPH::m_allowRealTime )
  712. m_frame->RecalculateConnections( NO_CLEANUP );
  713. std::string tool = aEvent.GetCommandStr().get();
  714. PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
  715. // Deactivate other tools; particularly important if another PICKER is currently running
  716. Activate();
  717. picker->SetCursor( wxStockCursor( wxCURSOR_BULLSEYE ) );
  718. picker->SetClickHandler(
  719. [this] ( const VECTOR2D& aPos )
  720. {
  721. return highlightNet( m_toolMgr, aPos );
  722. } );
  723. m_toolMgr->RunAction( ACTIONS::pickerTool, true, &tool );
  724. return 0;
  725. }
  726. int SCH_EDITOR_CONTROL::Undo( const TOOL_EVENT& aEvent )
  727. {
  728. if( m_frame->GetScreen()->GetUndoCommandCount() <= 0 )
  729. return 0;
  730. // Inform tools that undo command was issued
  731. m_toolMgr->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_PRE, AS_GLOBAL } );
  732. /* Get the old list */
  733. PICKED_ITEMS_LIST* List = m_frame->GetScreen()->PopCommandFromUndoList();
  734. /* Undo the command */
  735. m_frame->PutDataInPreviousState( List, false );
  736. /* Put the old list in RedoList */
  737. List->ReversePickersListOrder();
  738. m_frame->GetScreen()->PushCommandToRedoList( List );
  739. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  740. selTool->RebuildSelection();
  741. m_frame->SetSheetNumberAndCount();
  742. m_frame->TestDanglingEnds();
  743. m_frame->SyncView();
  744. m_frame->GetCanvas()->Refresh();
  745. m_frame->OnModify();
  746. return 0;
  747. }
  748. int SCH_EDITOR_CONTROL::Redo( const TOOL_EVENT& aEvent )
  749. {
  750. if( m_frame->GetScreen()->GetRedoCommandCount() == 0 )
  751. return 0;
  752. // Inform tools that undo command was issued
  753. m_toolMgr->ProcessEvent( { TC_MESSAGE, TA_UNDO_REDO_PRE, AS_GLOBAL } );
  754. /* Get the old list */
  755. PICKED_ITEMS_LIST* List = m_frame->GetScreen()->PopCommandFromRedoList();
  756. /* Redo the command: */
  757. m_frame->PutDataInPreviousState( List, true );
  758. /* Put the old list in UndoList */
  759. List->ReversePickersListOrder();
  760. m_frame->GetScreen()->PushCommandToUndoList( List );
  761. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  762. selTool->RebuildSelection();
  763. m_frame->SetSheetNumberAndCount();
  764. m_frame->TestDanglingEnds();
  765. m_frame->SyncView();
  766. m_frame->GetCanvas()->Refresh();
  767. m_frame->OnModify();
  768. return 0;
  769. }
  770. bool SCH_EDITOR_CONTROL::doCopy()
  771. {
  772. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  773. EE_SELECTION& selection = selTool->RequestSelection();
  774. if( !selection.GetSize() )
  775. return false;
  776. m_supplementaryClipboard.clear();
  777. for( EDA_ITEM* item : selection )
  778. {
  779. if( item->Type() == SCH_SHEET_T )
  780. {
  781. SCH_SHEET* sheet = (SCH_SHEET*) item;
  782. m_supplementaryClipboard[ sheet->GetFileName() ] = sheet->GetScreen();
  783. }
  784. }
  785. STRING_FORMATTER formatter;
  786. SCH_LEGACY_PLUGIN plugin;
  787. plugin.Format( &selection, &formatter );
  788. return m_toolMgr->SaveClipboard( formatter.GetString() );
  789. }
  790. bool SCH_EDITOR_CONTROL::searchSupplementaryClipboard( const wxString& aSheetFilename,
  791. SCH_SCREEN** aScreen )
  792. {
  793. if( m_supplementaryClipboard.count( aSheetFilename ) > 0 )
  794. {
  795. *aScreen = m_supplementaryClipboard[ aSheetFilename ];
  796. return true;
  797. }
  798. return false;
  799. }
  800. int SCH_EDITOR_CONTROL::Cut( const TOOL_EVENT& aEvent )
  801. {
  802. wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wxWindow::FindFocus() );
  803. if( textEntry )
  804. {
  805. textEntry->Cut();
  806. return 0;
  807. }
  808. if( doCopy() )
  809. m_toolMgr->RunAction( ACTIONS::doDelete, true );
  810. return 0;
  811. }
  812. int SCH_EDITOR_CONTROL::Copy( const TOOL_EVENT& aEvent )
  813. {
  814. wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wxWindow::FindFocus() );
  815. if( textEntry )
  816. {
  817. textEntry->Copy();
  818. return 0;
  819. }
  820. doCopy();
  821. return 0;
  822. }
  823. int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent )
  824. {
  825. wxTextEntry* textEntry = dynamic_cast<wxTextEntry*>( wxWindow::FindFocus() );
  826. if( textEntry )
  827. {
  828. textEntry->Paste();
  829. return 0;
  830. }
  831. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  832. std::string text = m_toolMgr->GetClipboard();
  833. if( text.empty() )
  834. return 0;
  835. STRING_LINE_READER reader( text, "Clipboard" );
  836. SCH_LEGACY_PLUGIN plugin;
  837. SCH_SCREEN paste_screen( &m_frame->GetScreen()->Kiway() );
  838. try
  839. {
  840. plugin.LoadContent( reader, &paste_screen );
  841. }
  842. catch( IO_ERROR& )
  843. {
  844. // If it wasn't content, then paste as text
  845. paste_screen.Append( new SCH_TEXT( wxPoint( 0, 0 ), text ) );
  846. }
  847. bool forceKeepAnnotations = false;
  848. bool forceDropAnnotations = false;
  849. bool dropAnnotations = false;
  850. if( aEvent.IsAction( &ACTIONS::pasteSpecial ) )
  851. {
  852. DIALOG_PASTE_SPECIAL dlg( m_frame, &forceKeepAnnotations, &forceDropAnnotations );
  853. if( dlg.ShowModal() == wxID_CANCEL )
  854. return 0;
  855. }
  856. if( forceDropAnnotations )
  857. dropAnnotations = true;
  858. // SCH_LEGACY_PLUGIN added the items to the paste screen, but not to the view or anything
  859. // else. Pull them back out to start with.
  860. //
  861. EDA_ITEMS loadedItems;
  862. bool sheetsPasted = false;
  863. SCH_SHEET_LIST hierarchy( g_RootSheet );
  864. wxFileName destFn = g_CurrentSheet->Last()->GetFileName();
  865. if( destFn.IsRelative() )
  866. destFn.MakeAbsolute( m_frame->Prj().GetProjectPath() );
  867. for( SCH_ITEM* item : paste_screen.Items() )
  868. {
  869. loadedItems.push_back( item );
  870. if( item->Type() == SCH_COMPONENT_T )
  871. {
  872. if( !dropAnnotations && !forceKeepAnnotations )
  873. {
  874. for( SCH_ITEM* existingItem : m_frame->GetScreen()->Items() )
  875. {
  876. if( item->m_Uuid == existingItem->m_Uuid )
  877. {
  878. dropAnnotations = true;
  879. break;
  880. }
  881. }
  882. }
  883. }
  884. else if( item->Type() == SCH_SHEET_T )
  885. {
  886. SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
  887. wxFileName srcFn = sheet->GetFileName();
  888. if( srcFn.IsRelative() )
  889. srcFn.MakeAbsolute( m_frame->Prj().GetProjectPath() );
  890. SCH_SHEET_LIST sheetHierarchy( sheet );
  891. if( hierarchy.TestForRecursion( sheetHierarchy, destFn.GetFullPath( wxPATH_UNIX ) ) )
  892. {
  893. auto msg = wxString::Format( _( "The pasted sheet \"%s\"\n"
  894. "was dropped because the destination already has "
  895. "the sheet or one of its subsheets as a parent." ),
  896. sheet->GetFileName() );
  897. DisplayError( m_frame, msg );
  898. loadedItems.pop_back();
  899. }
  900. }
  901. }
  902. // Remove the references from our temporary screen to prevent freeing on the DTOR
  903. paste_screen.Clear( false );
  904. // Now we can resolve the components and add everything to the screen, view, etc.
  905. SYMBOL_LIB_TABLE* symLibTable = m_frame->Prj().SchSymbolLibTable();
  906. for( unsigned i = 0; i < loadedItems.size(); ++i )
  907. {
  908. EDA_ITEM* item = loadedItems[i];
  909. if( item->Type() == SCH_COMPONENT_T )
  910. {
  911. SCH_COMPONENT* component = (SCH_COMPONENT*) item;
  912. if( dropAnnotations )
  913. {
  914. const_cast<KIID&>( component->m_Uuid ) = KIID();
  915. // clear the annotation, but preserve the selected unit
  916. int unit = component->GetUnit();
  917. component->ClearAnnotation( nullptr );
  918. component->SetUnit( unit );
  919. }
  920. LIB_PART* libSymbol = symLibTable->LoadSymbol( component->GetLibId() );
  921. if( libSymbol )
  922. {
  923. component->SetLibSymbol( new LIB_PART( *libSymbol ) );
  924. }
  925. else
  926. {
  927. DisplayError( m_frame,
  928. wxString::Format( _( "Symbol '%s' not found in library '%s'." ),
  929. component->GetLibId().GetLibItemName().wx_str(),
  930. component->GetLibId().GetLibNickname().wx_str() ) );
  931. }
  932. }
  933. else
  934. {
  935. // Everything else gets a new UUID
  936. const_cast<KIID&>( item->m_Uuid ) = KIID();
  937. }
  938. if( item->Type() == SCH_SHEET_T )
  939. {
  940. SCH_SHEET* sheet = (SCH_SHEET*) item;
  941. SCH_FIELD& nameField = sheet->GetFields()[SHEETNAME];
  942. wxFileName fn = sheet->GetFileName();
  943. SCH_SCREEN* existingScreen = nullptr;
  944. bool dropSheetAnnotations = false;
  945. wxString baseName = nameField.GetText();
  946. wxString candidateName = baseName;
  947. int uniquifier = 1;
  948. while( hierarchy.NameExists( candidateName ) )
  949. candidateName = wxString::Format( wxT( "%s%d" ), baseName, uniquifier++ );
  950. nameField.SetText( candidateName );
  951. sheet->SetParent( g_CurrentSheet->Last() );
  952. sheet->SetScreen( nullptr );
  953. sheetsPasted = true;
  954. if( !fn.IsAbsolute() )
  955. {
  956. wxFileName currentSheetFileName = g_CurrentSheet->LastScreen()->GetFileName();
  957. fn.Normalize( wxPATH_NORM_ALL, currentSheetFileName.GetPath() );
  958. }
  959. if( g_RootSheet->SearchHierarchy( fn.GetFullPath( wxPATH_UNIX ), &existingScreen ) )
  960. dropSheetAnnotations = !forceKeepAnnotations;
  961. else
  962. searchSupplementaryClipboard( sheet->GetFileName(), &existingScreen );
  963. if( existingScreen )
  964. {
  965. sheet->SetScreen( existingScreen );
  966. SCH_SHEET_PATH sheetpath = *g_CurrentSheet;
  967. sheetpath.push_back( sheet );
  968. // Clear annotation and create the AR for this path, if not exists,
  969. // when the screen is shared by sheet paths.
  970. // Otherwise ClearAnnotation do nothing, because the F1 field is used as
  971. // reference default value and takes the latest displayed value
  972. existingScreen->EnsureAlternateReferencesExist();
  973. if( forceDropAnnotations || dropSheetAnnotations )
  974. existingScreen->ClearAnnotation( &sheetpath );
  975. }
  976. else
  977. {
  978. if( !m_frame->LoadSheetFromFile( sheet, g_CurrentSheet, fn.GetFullPath() ) )
  979. m_frame->InitSheet( sheet, sheet->GetFileName() );
  980. }
  981. }
  982. item->SetFlags( IS_NEW | IS_PASTED | IS_MOVED );
  983. m_frame->AddItemToScreenAndUndoList( (SCH_ITEM*) item, i > 0 );
  984. // Reset flags for subsequent move operation
  985. item->SetFlags( IS_NEW | IS_PASTED | IS_MOVED );
  986. // Start out hidden so the pasted items aren't "ghosted" in their original location
  987. // before being moved to the current location.
  988. getView()->Hide( item, true );
  989. }
  990. if( sheetsPasted )
  991. {
  992. m_frame->SetSheetNumberAndCount();
  993. m_frame->UpdateHierarchyNavigator();
  994. }
  995. // Now clear the previous selection, select the pasted items, and fire up the "move"
  996. // tool.
  997. //
  998. m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
  999. m_toolMgr->RunAction( EE_ACTIONS::addItemsToSel, true, &loadedItems );
  1000. EE_SELECTION& selection = selTool->GetSelection();
  1001. if( !selection.Empty() )
  1002. {
  1003. SCH_ITEM* item = (SCH_ITEM*) selection.GetTopLeftItem();
  1004. selection.SetReferencePoint( item->GetPosition() );
  1005. m_toolMgr->RunAction( EE_ACTIONS::move, false );
  1006. }
  1007. return 0;
  1008. }
  1009. int SCH_EDITOR_CONTROL::EditWithLibEdit( const TOOL_EVENT& aEvent )
  1010. {
  1011. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  1012. EE_SELECTION& selection = selTool->RequestSelection( EE_COLLECTOR::ComponentsOnly );
  1013. SCH_COMPONENT* sym = nullptr;
  1014. LIB_EDIT_FRAME* libEdit;
  1015. if( selection.GetSize() >= 1 )
  1016. sym = (SCH_COMPONENT*) selection.Front();
  1017. if( !sym || sym->GetEditFlags() != 0 )
  1018. return 0;
  1019. m_toolMgr->RunAction( ACTIONS::showSymbolEditor, true );
  1020. libEdit = (LIB_EDIT_FRAME*) m_frame->Kiway().Player( FRAME_SCH_LIB_EDITOR, false );
  1021. if( libEdit )
  1022. libEdit->LoadComponentAndSelectLib( sym->GetLibId(), sym->GetUnit(), sym->GetConvert() );
  1023. return 0;
  1024. }
  1025. int SCH_EDITOR_CONTROL::Annotate( const TOOL_EVENT& aEvent )
  1026. {
  1027. wxCommandEvent dummy;
  1028. m_frame->OnAnnotate( dummy );
  1029. return 0;
  1030. }
  1031. int SCH_EDITOR_CONTROL::ShowCvpcb( const TOOL_EVENT& aEvent )
  1032. {
  1033. wxCommandEvent dummy;
  1034. m_frame->OnOpenCvpcb( dummy );
  1035. return 0;
  1036. }
  1037. int SCH_EDITOR_CONTROL::EditSymbolFields( const TOOL_EVENT& aEvent )
  1038. {
  1039. DIALOG_FIELDS_EDITOR_GLOBAL dlg( m_frame );
  1040. dlg.ShowQuasiModal();
  1041. return 0;
  1042. }
  1043. int SCH_EDITOR_CONTROL::EditSymbolLibraryLinks( const TOOL_EVENT& aEvent )
  1044. {
  1045. if( InvokeDialogEditComponentsLibId( m_frame ) )
  1046. m_frame->HardRedraw();
  1047. return 0;
  1048. }
  1049. int SCH_EDITOR_CONTROL::ShowPcbNew( const TOOL_EVENT& aEvent )
  1050. {
  1051. wxCommandEvent dummy;
  1052. m_frame->OnOpenPcbnew( dummy );
  1053. return 0;
  1054. }
  1055. int SCH_EDITOR_CONTROL::UpdatePCB( const TOOL_EVENT& aEvent )
  1056. {
  1057. wxCommandEvent dummy;
  1058. m_frame->OnUpdatePCB( dummy );
  1059. return 0;
  1060. }
  1061. int SCH_EDITOR_CONTROL::UpdateFromPCB( const TOOL_EVENT& aEvent )
  1062. {
  1063. DIALOG_UPDATE_FROM_PCB dlg( m_frame );
  1064. dlg.ShowModal();
  1065. return 0;
  1066. }
  1067. int SCH_EDITOR_CONTROL::ExportNetlist( const TOOL_EVENT& aEvent )
  1068. {
  1069. int result = NET_PLUGIN_CHANGE;
  1070. // If a plugin is removed or added, rebuild and reopen the new dialog
  1071. while( result == NET_PLUGIN_CHANGE )
  1072. result = InvokeDialogNetList( m_frame );
  1073. return 0;
  1074. }
  1075. int SCH_EDITOR_CONTROL::GenerateBOM( const TOOL_EVENT& aEvent )
  1076. {
  1077. InvokeDialogCreateBOM( m_frame );
  1078. return 0;
  1079. }
  1080. int SCH_EDITOR_CONTROL::DrawSheetOnClipboard( const TOOL_EVENT& aEvent )
  1081. {
  1082. m_frame->DrawCurrentSheetToClipboard();
  1083. return 0;
  1084. }
  1085. int SCH_EDITOR_CONTROL::ShowBusManager( const TOOL_EVENT& aEvent )
  1086. {
  1087. InvokeDialogBusManager( m_frame );
  1088. return 0;
  1089. }
  1090. int SCH_EDITOR_CONTROL::EnterSheet( const TOOL_EVENT& aEvent )
  1091. {
  1092. EE_SELECTION_TOOL* selTool = m_toolMgr->GetTool<EE_SELECTION_TOOL>();
  1093. const EE_SELECTION& selection = selTool->RequestSelection( EE_COLLECTOR::SheetsOnly );
  1094. if( selection.GetSize() == 1 )
  1095. {
  1096. SCH_SHEET* sheet = (SCH_SHEET*) selection.Front();
  1097. m_toolMgr->RunAction( ACTIONS::cancelInteractive, true );
  1098. m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
  1099. g_CurrentSheet->push_back( sheet );
  1100. m_frame->DisplayCurrentSheet();
  1101. m_frame->UpdateHierarchyNavigator();
  1102. }
  1103. return 0;
  1104. }
  1105. int SCH_EDITOR_CONTROL::LeaveSheet( const TOOL_EVENT& aEvent )
  1106. {
  1107. if( g_CurrentSheet->Last() != g_RootSheet )
  1108. {
  1109. m_toolMgr->RunAction( ACTIONS::cancelInteractive, true );
  1110. m_toolMgr->RunAction( EE_ACTIONS::clearSelection, true );
  1111. g_CurrentSheet->pop_back();
  1112. m_frame->DisplayCurrentSheet();
  1113. m_frame->UpdateHierarchyNavigator();
  1114. }
  1115. return 0;
  1116. }
  1117. int SCH_EDITOR_CONTROL::ToggleHiddenPins( const TOOL_EVENT& aEvent )
  1118. {
  1119. EESCHEMA_SETTINGS* cfg = m_frame->eeconfig();
  1120. cfg->m_Appearance.show_hidden_pins = !cfg->m_Appearance.show_hidden_pins;
  1121. KIGFX::SCH_PAINTER* painter = static_cast<KIGFX::SCH_PAINTER*>( getView()->GetPainter() );
  1122. painter->GetSettings()->m_ShowHiddenPins = m_frame->GetShowAllPins();
  1123. getView()->UpdateAllItems( KIGFX::REPAINT );
  1124. m_frame->GetCanvas()->Refresh();
  1125. return 0;
  1126. }
  1127. int SCH_EDITOR_CONTROL::ToggleHiddenFields( const TOOL_EVENT& aEvent )
  1128. {
  1129. EESCHEMA_SETTINGS* cfg = m_frame->eeconfig();
  1130. cfg->m_Appearance.show_hidden_fields = !cfg->m_Appearance.show_hidden_fields;
  1131. KIGFX::SCH_PAINTER* painter = static_cast<KIGFX::SCH_PAINTER*>( getView()->GetPainter() );
  1132. painter->GetSettings()->m_ShowHiddenText = cfg->m_Appearance.show_hidden_fields;
  1133. getView()->UpdateAllItems( KIGFX::REPAINT );
  1134. m_frame->GetCanvas()->Refresh();
  1135. return 0;
  1136. }
  1137. int SCH_EDITOR_CONTROL::ToggleForceHV( const TOOL_EVENT& aEvent )
  1138. {
  1139. m_frame->eeconfig()->m_Drawing.hv_lines_only = !m_frame->eeconfig()->m_Drawing.hv_lines_only;
  1140. return 0;
  1141. }
  1142. void SCH_EDITOR_CONTROL::setTransitions()
  1143. {
  1144. Go( &SCH_EDITOR_CONTROL::New, ACTIONS::doNew.MakeEvent() );
  1145. Go( &SCH_EDITOR_CONTROL::Open, ACTIONS::open.MakeEvent() );
  1146. Go( &SCH_EDITOR_CONTROL::Save, ACTIONS::save.MakeEvent() );
  1147. Go( &SCH_EDITOR_CONTROL::SaveAs, ACTIONS::saveAs.MakeEvent() );
  1148. Go( &SCH_EDITOR_CONTROL::ShowSchematicSetup, EE_ACTIONS::schematicSetup.MakeEvent() );
  1149. Go( &SCH_EDITOR_CONTROL::PageSetup, ACTIONS::pageSettings.MakeEvent() );
  1150. Go( &SCH_EDITOR_CONTROL::Print, ACTIONS::print.MakeEvent() );
  1151. Go( &SCH_EDITOR_CONTROL::Plot, ACTIONS::plot.MakeEvent() );
  1152. Go( &SCH_EDITOR_CONTROL::Quit, ACTIONS::quit.MakeEvent() );
  1153. Go( &SCH_EDITOR_CONTROL::FindAndReplace, ACTIONS::find.MakeEvent() );
  1154. Go( &SCH_EDITOR_CONTROL::FindAndReplace, ACTIONS::findAndReplace.MakeEvent() );
  1155. Go( &SCH_EDITOR_CONTROL::FindNext, ACTIONS::findNext.MakeEvent() );
  1156. Go( &SCH_EDITOR_CONTROL::FindNext, ACTIONS::findNextMarker.MakeEvent() );
  1157. Go( &SCH_EDITOR_CONTROL::ReplaceAndFindNext, ACTIONS::replaceAndFindNext.MakeEvent() );
  1158. Go( &SCH_EDITOR_CONTROL::ReplaceAll, ACTIONS::replaceAll.MakeEvent() );
  1159. Go( &SCH_EDITOR_CONTROL::UpdateFind, ACTIONS::updateFind.MakeEvent() );
  1160. Go( &SCH_EDITOR_CONTROL::UpdateFind, EVENTS::SelectedItemsModified );
  1161. Go( &SCH_EDITOR_CONTROL::CrossProbeToPcb, EVENTS::SelectedEvent );
  1162. Go( &SCH_EDITOR_CONTROL::CrossProbeToPcb, EVENTS::UnselectedEvent );
  1163. Go( &SCH_EDITOR_CONTROL::CrossProbeToPcb, EVENTS::ClearedEvent );
  1164. Go( &SCH_EDITOR_CONTROL::ExplicitCrossProbeToPcb, EE_ACTIONS::explicitCrossProbe.MakeEvent() );
  1165. #ifdef KICAD_SPICE
  1166. Go( &SCH_EDITOR_CONTROL::SimProbe, EE_ACTIONS::simProbe.MakeEvent() );
  1167. Go( &SCH_EDITOR_CONTROL::SimTune, EE_ACTIONS::simTune.MakeEvent() );
  1168. #endif /* KICAD_SPICE */
  1169. Go( &SCH_EDITOR_CONTROL::HighlightNet, EE_ACTIONS::highlightNet.MakeEvent() );
  1170. Go( &SCH_EDITOR_CONTROL::ClearHighlight, EE_ACTIONS::clearHighlight.MakeEvent() );
  1171. Go( &SCH_EDITOR_CONTROL::HighlightNetCursor, EE_ACTIONS::highlightNetTool.MakeEvent() );
  1172. Go( &SCH_EDITOR_CONTROL::UpdateNetHighlighting, EVENTS::SelectedItemsModified );
  1173. Go( &SCH_EDITOR_CONTROL::UpdateNetHighlighting, EE_ACTIONS::updateNetHighlighting.MakeEvent() );
  1174. Go( &SCH_EDITOR_CONTROL::ClearHighlight, ACTIONS::cancelInteractive.MakeEvent() );
  1175. Go( &SCH_EDITOR_CONTROL::Undo, ACTIONS::undo.MakeEvent() );
  1176. Go( &SCH_EDITOR_CONTROL::Redo, ACTIONS::redo.MakeEvent() );
  1177. Go( &SCH_EDITOR_CONTROL::Cut, ACTIONS::cut.MakeEvent() );
  1178. Go( &SCH_EDITOR_CONTROL::Copy, ACTIONS::copy.MakeEvent() );
  1179. Go( &SCH_EDITOR_CONTROL::Paste, ACTIONS::paste.MakeEvent() );
  1180. Go( &SCH_EDITOR_CONTROL::Paste, ACTIONS::pasteSpecial.MakeEvent() );
  1181. Go( &SCH_EDITOR_CONTROL::EditWithLibEdit, EE_ACTIONS::editWithLibEdit.MakeEvent() );
  1182. Go( &SCH_EDITOR_CONTROL::ShowCvpcb, EE_ACTIONS::assignFootprints.MakeEvent() );
  1183. Go( &SCH_EDITOR_CONTROL::ImportFPAssignments, EE_ACTIONS::importFPAssignments.MakeEvent() );
  1184. Go( &SCH_EDITOR_CONTROL::Annotate, EE_ACTIONS::annotate.MakeEvent() );
  1185. Go( &SCH_EDITOR_CONTROL::EditSymbolFields, EE_ACTIONS::editSymbolFields.MakeEvent() );
  1186. Go( &SCH_EDITOR_CONTROL::EditSymbolLibraryLinks,EE_ACTIONS::editSymbolLibraryLinks.MakeEvent() );
  1187. Go( &SCH_EDITOR_CONTROL::ShowPcbNew, EE_ACTIONS::showPcbNew.MakeEvent() );
  1188. Go( &SCH_EDITOR_CONTROL::UpdatePCB, ACTIONS::updatePcbFromSchematic.MakeEvent() );
  1189. Go( &SCH_EDITOR_CONTROL::UpdateFromPCB, ACTIONS::updateSchematicFromPcb.MakeEvent() );
  1190. Go( &SCH_EDITOR_CONTROL::ExportNetlist, EE_ACTIONS::exportNetlist.MakeEvent() );
  1191. Go( &SCH_EDITOR_CONTROL::GenerateBOM, EE_ACTIONS::generateBOM.MakeEvent() );
  1192. Go( &SCH_EDITOR_CONTROL::DrawSheetOnClipboard, EE_ACTIONS::drawSheetOnClipboard.MakeEvent() );
  1193. Go( &SCH_EDITOR_CONTROL::ShowBusManager, EE_ACTIONS::showBusManager.MakeEvent() );
  1194. Go( &SCH_EDITOR_CONTROL::EnterSheet, EE_ACTIONS::enterSheet.MakeEvent() );
  1195. Go( &SCH_EDITOR_CONTROL::LeaveSheet, EE_ACTIONS::leaveSheet.MakeEvent() );
  1196. Go( &SCH_EDITOR_CONTROL::NavigateHierarchy, EE_ACTIONS::navigateHierarchy.MakeEvent() );
  1197. Go( &SCH_EDITOR_CONTROL::ToggleHiddenPins, EE_ACTIONS::toggleHiddenPins.MakeEvent() );
  1198. Go( &SCH_EDITOR_CONTROL::ToggleHiddenFields, EE_ACTIONS::toggleHiddenFields.MakeEvent() );
  1199. Go( &SCH_EDITOR_CONTROL::ToggleForceHV, EE_ACTIONS::toggleForceHV.MakeEvent() );
  1200. }