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.

304 lines
8.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <dialogs/dialog_export_2581.h>
  20. #include <board.h>
  21. #include <footprint.h>
  22. #include <kiway_holder.h>
  23. #include <pcb_edit_frame.h>
  24. #include <pcbnew_settings.h>
  25. #include <pgm_base.h>
  26. #include <project.h>
  27. #include <project/board_project_settings.h>
  28. #include <project/project_file.h>
  29. #include <settings/settings_manager.h>
  30. #include <widgets/std_bitmap_button.h>
  31. #include <set>
  32. #include <vector>
  33. #include <wx/filedlg.h>
  34. static wxString s_oemColumn = wxEmptyString;
  35. DIALOG_EXPORT_2581::DIALOG_EXPORT_2581( PCB_EDIT_FRAME* aParent ) :
  36. DIALOG_EXPORT_2581_BASE( aParent ), m_parent( aParent )
  37. {
  38. m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
  39. SetupStandardButtons( { { wxID_OK, _( "Export" ) },
  40. { wxID_CANCEL, _( "Close" ) } } );
  41. wxString path = m_parent->GetLastPath( LAST_PATH_2581 );
  42. if( path.IsEmpty() )
  43. {
  44. wxFileName brdFile( m_parent->GetBoard()->GetFileName() );
  45. brdFile.SetExt( wxT( "xml" ) );
  46. path = brdFile.GetFullPath();
  47. }
  48. m_outputFileName->SetValue( path );
  49. m_textDistributor->SetSize( m_choiceDistPN->GetSize() );
  50. // Now all widgets have the size fixed, call FinishDialogSettings
  51. finishDialogSettings();
  52. }
  53. void DIALOG_EXPORT_2581::onBrowseClicked( wxCommandEvent& event )
  54. {
  55. // Build the absolute path of current output directory to preselect it in the file browser.
  56. wxString path = ExpandEnvVarSubstitutions( m_outputFileName->GetValue(), &Prj() );
  57. wxFileName fn( Prj().AbsolutePath( path ) );
  58. wxString ipc_files = _( "IPC-2581 Files (*.xml)|*.xml" );
  59. wxString compressed_files = _( "IPC-2581 Compressed Files (*.zip)|*.zip" );
  60. wxFileDialog dlg( this, _( "Export IPC-2581 File" ), fn.GetPath(), fn.GetFullName(),
  61. m_cbCompress->IsChecked() ? compressed_files : ipc_files,
  62. wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  63. if( dlg.ShowModal() == wxID_CANCEL )
  64. return;
  65. m_outputFileName->SetValue( dlg.GetPath() );
  66. }
  67. void DIALOG_EXPORT_2581::onOKClick( wxCommandEvent& event )
  68. {
  69. m_parent->SetLastPath( LAST_PATH_2581, m_outputFileName->GetValue() );
  70. event.Skip();
  71. }
  72. void DIALOG_EXPORT_2581::onCompressCheck( wxCommandEvent& event )
  73. {
  74. if( m_cbCompress->GetValue() )
  75. {
  76. wxFileName fn = m_outputFileName->GetValue();
  77. fn.SetExt( "zip" );
  78. m_outputFileName->SetValue( fn.GetFullPath() );
  79. }
  80. else
  81. {
  82. wxFileName fn = m_outputFileName->GetValue();
  83. fn.SetExt( "xml" );
  84. m_outputFileName->SetValue( fn.GetFullPath() );
  85. }
  86. }
  87. void DIALOG_EXPORT_2581::onMfgPNChange( wxCommandEvent& event )
  88. {
  89. if( event.GetSelection() == 0 )
  90. {
  91. m_choiceMfg->Enable( false );
  92. }
  93. else
  94. {
  95. m_choiceMfg->Enable( true );
  96. // Don't try to guess the manufacturer if the user has already selected one
  97. if( m_choiceMfg->GetSelection() > 0 )
  98. return;
  99. int it = 0;
  100. if( it = m_choiceMfg->FindString( wxT( "manufacturer" ) ); it != wxNOT_FOUND )
  101. {
  102. m_choiceMfg->Select( it );
  103. }
  104. else if( it = m_choiceMfg->FindString( _( "manufacturer" ) ); it != wxNOT_FOUND )
  105. {
  106. m_choiceMfg->Select( it );
  107. }
  108. else if( it = m_choiceMfg->FindString( wxT( "mfg" ) ); it != wxNOT_FOUND )
  109. {
  110. m_choiceMfg->Select( it );
  111. }
  112. else if( it = m_choiceMfg->FindString( _( "mfg" ) ); it != wxNOT_FOUND )
  113. {
  114. m_choiceMfg->Select( it );
  115. }
  116. }
  117. }
  118. void DIALOG_EXPORT_2581::onDistPNChange( wxCommandEvent& event )
  119. {
  120. if( event.GetSelection() == 0 )
  121. {
  122. m_textDistributor->Enable( false );
  123. m_textDistributor->SetValue( _( "N/A" ) );
  124. }
  125. else
  126. {
  127. m_textDistributor->Enable( true );
  128. // Don't try to guess the distributor if the user has already selected one
  129. if( m_textDistributor->GetValue() != _( "N/A" ) )
  130. return;
  131. wxString dist = m_choiceDistPN->GetStringSelection();
  132. dist.MakeUpper();
  133. // Try to guess the distributor from the part number column
  134. if( dist.Contains( wxT( "DIGIKEY" ) ) )
  135. {
  136. m_textDistributor->SetValue( wxT( "Digi-Key" ) );
  137. }
  138. else if( dist.Contains( wxT( "DIGI-KEY" ) ) )
  139. {
  140. m_textDistributor->SetValue( wxT( "Digi-Key" ) );
  141. }
  142. else if( dist.Contains( wxT( "MOUSER" ) ) )
  143. {
  144. m_textDistributor->SetValue( wxT( "Mouser" ) );
  145. }
  146. else if( dist.Contains( wxT( "NEWARK" ) ) )
  147. {
  148. m_textDistributor->SetValue( wxT( "Newark" ) );
  149. }
  150. else if( dist.Contains( wxT( "RS COMPONENTS" ) ) )
  151. {
  152. m_textDistributor->SetValue( wxT( "RS Components" ) );
  153. }
  154. else if( dist.Contains( wxT( "FARNELL" ) ) )
  155. {
  156. m_textDistributor->SetValue( wxT( "Farnell" ) );
  157. }
  158. else if( dist.Contains( wxT( "ARROW" ) ) )
  159. {
  160. m_textDistributor->SetValue( wxT( "Arrow" ) );
  161. }
  162. else if( dist.Contains( wxT( "AVNET" ) ) )
  163. {
  164. m_textDistributor->SetValue( wxT( "Avnet" ) );
  165. }
  166. else if( dist.Contains( wxT( "TME" ) ) )
  167. {
  168. m_textDistributor->SetValue( wxT( "TME" ) );
  169. }
  170. else if( dist.Contains( wxT( "LCSC" ) ) )
  171. {
  172. m_textDistributor->SetValue( wxT( "LCSC" ) );
  173. }
  174. }
  175. }
  176. bool DIALOG_EXPORT_2581::TransferDataToWindow()
  177. {
  178. PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
  179. std::set<wxString> options;
  180. BOARD* board = m_parent->GetBoard();
  181. for( FOOTPRINT* fp : board->Footprints() )
  182. {
  183. for( PCB_FIELD* field : fp->GetFields() )
  184. options.insert( field->GetName() );
  185. }
  186. m_choiceUnits->SetSelection( cfg->m_Export2581.units );
  187. m_precision->SetValue( cfg->m_Export2581.precision );
  188. m_versionChoice->SetSelection( cfg->m_Export2581.version );
  189. m_cbCompress->SetValue( cfg->m_Export2581.compress );
  190. wxCommandEvent dummy;
  191. onCompressCheck( dummy );
  192. std::vector<wxString> items( options.begin(), options.end() );
  193. m_oemRef->Append( items );
  194. m_choiceMPN->Append( items );
  195. m_choiceMfg->Append( items );
  196. m_choiceDistPN->Append( items );
  197. m_oemRef->SetStringSelection( s_oemColumn );
  198. PROJECT_FILE& prj = Prj().GetProjectFile();
  199. if( !m_choiceMPN->SetStringSelection( prj.m_IP2581Bom.id ) )
  200. m_choiceMPN->SetSelection( 0 );
  201. if( m_choiceMPN->SetStringSelection( prj.m_IP2581Bom.MPN ) )
  202. {
  203. m_choiceMfg->Enable( true );
  204. if( !m_choiceMfg->SetStringSelection( prj.m_IP2581Bom.mfg ) )
  205. m_choiceMfg->SetSelection( 0 );
  206. }
  207. else
  208. {
  209. m_choiceMPN->SetSelection( 0 );
  210. m_choiceMfg->SetSelection( 0 );
  211. m_choiceMfg->Enable( false );
  212. }
  213. if( m_choiceDistPN->SetStringSelection( prj.m_IP2581Bom.distPN ) )
  214. {
  215. m_textDistributor->Enable( true );
  216. // The combo box selection can be fixed, so any value can be entered
  217. if( !prj.m_IP2581Bom.distPN.empty() )
  218. {
  219. m_textDistributor->SetValue( prj.m_IP2581Bom.dist );
  220. }
  221. else
  222. {
  223. wxCommandEvent evt;
  224. onDistPNChange( evt );
  225. }
  226. }
  227. else
  228. {
  229. m_choiceDistPN->SetSelection( 0 );
  230. m_textDistributor->SetValue( _( "N/A" ) );
  231. m_textDistributor->Enable( false );
  232. }
  233. return true;
  234. }
  235. bool DIALOG_EXPORT_2581::TransferDataFromWindow()
  236. {
  237. PCBNEW_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings<PCBNEW_SETTINGS>();
  238. cfg->m_Export2581.units = m_choiceUnits->GetSelection();
  239. cfg->m_Export2581.precision = m_precision->GetValue();
  240. cfg->m_Export2581.version = m_versionChoice->GetSelection();
  241. cfg->m_Export2581.compress = m_cbCompress->GetValue();
  242. PROJECT_FILE& prj = Prj().GetProjectFile();
  243. wxString empty;
  244. prj.m_IP2581Bom.id = GetOEM();
  245. prj.m_IP2581Bom.mfg = GetMfg();
  246. prj.m_IP2581Bom.MPN = GetMPN();
  247. prj.m_IP2581Bom.distPN = GetDistPN();
  248. prj.m_IP2581Bom.dist = GetDist();
  249. s_oemColumn = m_oemRef->GetStringSelection();
  250. return true;
  251. }