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.

365 lines
10 KiB

3 years ago
4 years ago
3 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2022 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-2.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. #include <board_commit.h>
  24. #include <confirm.h>
  25. #include <dialog_footprint_properties_fp_editor.h>
  26. #include <footprint_edit_frame.h>
  27. #include <footprint_tree_pane.h>
  28. #include <fp_lib_table.h>
  29. #include <functional>
  30. #include <kiway_express.h>
  31. #include <pcb_marker.h>
  32. #include <pad.h>
  33. #include <zone.h>
  34. #include <settings/color_settings.h>
  35. #include <tool/tool_manager.h>
  36. #include <tools/pcb_actions.h>
  37. #include <tools/footprint_editor_control.h>
  38. #include <widgets/appearance_controls.h>
  39. #include <widgets/lib_tree.h>
  40. #include <pcb_layer_box_selector.h>
  41. #include <pcb_dimension.h>
  42. #include <dialogs/dialog_dimension_properties.h>
  43. using namespace std::placeholders;
  44. void FOOTPRINT_EDIT_FRAME::OnLoadFootprintFromBoard( wxCommandEvent& event )
  45. {
  46. LoadFootprintFromBoard( nullptr );
  47. }
  48. void FOOTPRINT_EDIT_FRAME::LoadFootprintFromLibrary( LIB_ID aFPID )
  49. {
  50. bool is_last_fp_from_brd = IsCurrentFPFromBoard();
  51. FOOTPRINT* footprint = LoadFootprint( aFPID );
  52. if( !footprint )
  53. return;
  54. if( !Clear_Pcb( true ) )
  55. return;
  56. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  57. AddFootprintToBoard( footprint );
  58. footprint->ClearFlags();
  59. // if either m_Reference or m_Value are gone, reinstall them -
  60. // otherwise you cannot see what you are doing on board
  61. FP_TEXT* ref = &footprint->Reference();
  62. FP_TEXT* val = &footprint->Value();
  63. if( val && ref )
  64. {
  65. ref->SetType( FP_TEXT::TEXT_is_REFERENCE ); // just in case ...
  66. if( ref->GetLength() == 0 )
  67. ref->SetText( wxT( "Ref**" ) );
  68. val->SetType( FP_TEXT::TEXT_is_VALUE ); // just in case ...
  69. if( val->GetLength() == 0 )
  70. val->SetText( wxT( "Val**" ) );
  71. }
  72. Zoom_Automatique( false );
  73. Update3DView( true, true );
  74. GetScreen()->SetContentModified( false );
  75. UpdateView();
  76. GetCanvas()->Refresh();
  77. // Update the save items if needed.
  78. if( is_last_fp_from_brd )
  79. {
  80. ReCreateMenuBar();
  81. ReCreateHToolbar();
  82. }
  83. m_treePane->GetLibTree()->ExpandLibId( aFPID );
  84. m_centerItemOnIdle = aFPID;
  85. Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  86. m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
  87. }
  88. void FOOTPRINT_EDIT_FRAME::centerItemIdleHandler( wxIdleEvent& aEvent )
  89. {
  90. m_treePane->GetLibTree()->CenterLibId( m_centerItemOnIdle );
  91. Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  92. }
  93. void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
  94. {
  95. SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
  96. if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
  97. GetCanvas()->Refresh();
  98. }
  99. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintToBoard( wxCommandEvent& event )
  100. {
  101. SaveFootprintToBoard( true );
  102. }
  103. class BASIC_FOOTPRINT_INFO : public FOOTPRINT_INFO
  104. {
  105. public:
  106. BASIC_FOOTPRINT_INFO( FOOTPRINT* aFootprint )
  107. {
  108. wxASSERT( aFootprint );
  109. m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
  110. m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
  111. m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
  112. m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
  113. m_keywords = aFootprint->GetKeywords();
  114. m_doc = aFootprint->GetDescription();
  115. m_loaded = true;
  116. }
  117. };
  118. void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
  119. FOOTPRINT* aFootprint )
  120. {
  121. wxCHECK( aFootprint, /* void */ );
  122. BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
  123. if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  124. // from file therefore not yet in tree.
  125. {
  126. static_cast<LIB_TREE_NODE_LIB_ID*>( aTreeItem.GetID() )->Update( &footprintInfo );
  127. m_treePane->GetLibTree()->RefreshLibTree();
  128. }
  129. }
  130. void FOOTPRINT_EDIT_FRAME::editFootprintProperties( FOOTPRINT* aFootprint )
  131. {
  132. LIB_ID oldFPID = aFootprint->GetFPID();
  133. DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
  134. dialog.ShowModal();
  135. // Update library tree and title in case of a name change
  136. wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
  137. UpdateLibraryTree( treeItem, aFootprint );
  138. UpdateTitle();
  139. UpdateMsgPanel();
  140. }
  141. void FOOTPRINT_EDIT_FRAME::OnEditItemRequest( BOARD_ITEM* aItem )
  142. {
  143. switch( aItem->Type() )
  144. {
  145. case PCB_BITMAP_T:
  146. ShowBitmapPropertiesDialog( aItem );
  147. break;
  148. case PCB_PAD_T:
  149. ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
  150. break;
  151. case PCB_FOOTPRINT_T:
  152. editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
  153. GetCanvas()->Refresh();
  154. break;
  155. case PCB_FP_TEXT_T:
  156. ShowTextPropertiesDialog( aItem );
  157. break;
  158. case PCB_FP_TEXTBOX_T:
  159. ShowTextBoxPropertiesDialog( aItem );
  160. break;
  161. case PCB_FP_SHAPE_T :
  162. ShowGraphicItemPropertiesDialog( aItem );
  163. break;
  164. case PCB_FP_DIM_ALIGNED_T:
  165. case PCB_FP_DIM_CENTER_T:
  166. case PCB_FP_DIM_RADIAL_T:
  167. case PCB_FP_DIM_ORTHOGONAL_T:
  168. case PCB_FP_DIM_LEADER_T:
  169. {
  170. DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
  171. dlg.ShowQuasiModal();
  172. break;
  173. }
  174. case PCB_FP_ZONE_T:
  175. {
  176. ZONE* zone = static_cast<ZONE*>( aItem );
  177. bool success = false;
  178. ZONE_SETTINGS zoneSettings;
  179. zoneSettings << *static_cast<ZONE*>( aItem );
  180. if( zone->GetIsRuleArea() )
  181. {
  182. success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
  183. }
  184. else if( zone->IsOnCopperLayer() )
  185. {
  186. success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  187. }
  188. else
  189. {
  190. success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  191. }
  192. if( success )
  193. {
  194. BOARD_COMMIT commit( this );
  195. commit.Modify( zone );
  196. commit.Push( _( "Edit Zone" ) );
  197. zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
  198. }
  199. break;
  200. }
  201. case PCB_GROUP_T:
  202. m_toolManager->RunAction( PCB_ACTIONS::groupProperties, true, aItem );
  203. break;
  204. case PCB_MARKER_T:
  205. m_toolManager->GetTool<FOOTPRINT_EDITOR_CONTROL>()->CrossProbe( static_cast<PCB_MARKER*>( aItem ) );
  206. break;
  207. default:
  208. wxFAIL_MSG( wxT( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type " )
  209. + aItem->GetClass() );
  210. break;
  211. }
  212. }
  213. COLOR4D FOOTPRINT_EDIT_FRAME::GetGridColor()
  214. {
  215. return GetColorSettings()->GetColor( LAYER_GRID );
  216. }
  217. void FOOTPRINT_EDIT_FRAME::SetActiveLayer( PCB_LAYER_ID aLayer )
  218. {
  219. PCB_BASE_FRAME::SetActiveLayer( aLayer );
  220. m_appearancePanel->OnLayerChanged();
  221. m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
  222. GetCanvas()->SetFocus(); // allow capture of hotkeys
  223. GetCanvas()->SetHighContrastLayer( aLayer );
  224. GetCanvas()->Refresh();
  225. }
  226. bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
  227. {
  228. if( !Clear_Pcb( true ) )
  229. return false; // this command is aborted
  230. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  231. ImportFootprint( aFileSet[ 0 ] );
  232. if( GetBoard()->GetFirstFootprint() )
  233. GetBoard()->GetFirstFootprint()->ClearFlags();
  234. GetScreen()->SetContentModified( false );
  235. Zoom_Automatique( false );
  236. GetCanvas()->Refresh();
  237. return true;
  238. }
  239. void FOOTPRINT_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  240. {
  241. const std::string& payload = mail.GetPayload();
  242. switch( mail.Command() )
  243. {
  244. case MAIL_FP_EDIT:
  245. if( !payload.empty() )
  246. {
  247. wxFileName fpFileName( payload );
  248. wxString libNickname;
  249. wxString msg;
  250. FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
  251. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
  252. if( !libTableRow )
  253. {
  254. msg.Printf( _( "The current configuration does not include a library named '%s'.\n"
  255. "Use Manage Footprint Libraries to edit the configuration." ),
  256. fpFileName.GetPath() );
  257. DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
  258. msg );
  259. break;
  260. }
  261. libNickname = libTableRow->GetNickName();
  262. if( !libTable->HasLibrary( libNickname, true ) )
  263. {
  264. msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
  265. "Use Manage Footprint Libraries to edit the configuration." ),
  266. libNickname );
  267. DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
  268. break;
  269. }
  270. LIB_ID fpId( libNickname, fpFileName.GetName() );
  271. if( m_treePane )
  272. {
  273. m_treePane->GetLibTree()->SelectLibId( fpId );
  274. wxCommandEvent event( SYMBOL_SELECTED );
  275. wxPostEvent( m_treePane, event );
  276. }
  277. }
  278. break;
  279. default:
  280. break;
  281. }
  282. }