KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_export_2581.cpp
Go to the documentation of this file.
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
21
22#include <board.h>
23#include <footprint.h>
24#include <kiway_holder.h>
25#include <pcb_edit_frame.h>
26#include <pcbnew_settings.h>
27#include <pgm_base.h>
28#include <project.h>
34
35#include <set>
36#include <vector>
37#include <wx/filedlg.h>
38
39static wxString s_oemColumn = wxEmptyString;
40
42 DIALOG_EXPORT_2581_BASE( aParent ),
43 m_parent( aParent ),
44 m_job( nullptr )
45{
46 m_browseButton->SetBitmap( KiBitmapBundle( BITMAPS::small_folder ) );
47
48 SetupStandardButtons( { { wxID_OK, _( "Export" ) },
49 { wxID_CANCEL, _( "Close" ) } } );
50
52
53 if( path.IsEmpty() )
54 {
55 wxFileName brdFile( m_parent->GetBoard()->GetFileName() );
56 brdFile.SetExt( wxT( "xml" ) );
57 path = brdFile.GetFullPath();
58 }
59
60 m_outputFileName->SetValue( path );
61
62 m_textDistributor->SetSize( m_choiceDistPN->GetSize() );
63
64 // Fill wxChoice (and others) items with data before calling finishDialogSettings()
65 // to calculate suitable widgets sizes
66 Init();
67
68 // Now all widgets have the size fixed, call FinishDialogSettings
70}
71
72
74 wxWindow* aParent ) :
75 DIALOG_EXPORT_2581_BASE( aParent ),
76 m_parent( aEditFrame ),
77 m_job( aJob )
78{
79 m_browseButton->Hide();
80
81 SetupStandardButtons( { { wxID_OK, _( "Save" ) }, { wxID_CANCEL, _( "Close" ) } } );
82
83 m_outputFileName->SetValue( m_job->GetOutputPath() );
84
85 m_textDistributor->SetSize( m_choiceDistPN->GetSize() );
86
87 // Fill wxChoice (and others) items with data before calling finishDialogSettings()
88 // to calculate suitable widgets sizes
89 Init();
90
91 // Now all widgets have the size fixed, call FinishDialogSettings
93}
94
95
96void DIALOG_EXPORT_2581::onBrowseClicked( wxCommandEvent& event )
97{
98 // Build the absolute path of current output directory to preselect it in the file browser.
99 wxString path = ExpandEnvVarSubstitutions( m_outputFileName->GetValue(), &Prj() );
100 wxFileName fn( Prj().AbsolutePath( path ) );
101 wxString ipc_files = _( "IPC-2581 Files (*.xml)|*.xml" );
102 wxString compressed_files = _( "IPC-2581 Compressed Files (*.zip)|*.zip" );
103
104 wxFileDialog dlg( this, _( "Export IPC-2581 File" ), fn.GetPath(), fn.GetFullName(),
105 m_cbCompress->IsChecked() ? compressed_files : ipc_files,
106 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
107
108 if( dlg.ShowModal() == wxID_CANCEL )
109 return;
110
111 m_outputFileName->SetValue( dlg.GetPath() );
112
113}
114
115void DIALOG_EXPORT_2581::onOKClick( wxCommandEvent& event )
116{
117 if( !m_job )
118 {
120 }
121
122 event.Skip();
123}
124
125
126void DIALOG_EXPORT_2581::onCompressCheck( wxCommandEvent& event )
127{
128 if( m_cbCompress->GetValue() )
129 {
130 wxFileName fn = m_outputFileName->GetValue();
131
132 fn.SetExt( "zip" );
133 m_outputFileName->SetValue( fn.GetFullPath() );
134 }
135 else
136 {
137 wxFileName fn = m_outputFileName->GetValue();
138
139 fn.SetExt( "xml" );
140 m_outputFileName->SetValue( fn.GetFullPath() );
141 }
142}
143
144
145void DIALOG_EXPORT_2581::onMfgPNChange( wxCommandEvent& event )
146{
147 if( event.GetSelection() == 0 )
148 {
149 m_choiceMfg->Enable( false );
150 }
151 else
152 {
153 m_choiceMfg->Enable( true );
154
155 // Don't try to guess the manufacturer if the user has already selected one
156 if( m_choiceMfg->GetSelection() > 0 )
157 return;
158
159 int it = 0;
160
161 if( it = m_choiceMfg->FindString( wxT( "manufacturer" ) ); it != wxNOT_FOUND )
162 {
163 m_choiceMfg->Select( it );
164 }
165 else if( it = m_choiceMfg->FindString( _( "manufacturer" ) ); it != wxNOT_FOUND )
166 {
167 m_choiceMfg->Select( it );
168 }
169 else if( it = m_choiceMfg->FindString( wxT( "mfg" ) ); it != wxNOT_FOUND )
170 {
171 m_choiceMfg->Select( it );
172 }
173 else if( it = m_choiceMfg->FindString( _( "mfg" ) ); it != wxNOT_FOUND )
174 {
175 m_choiceMfg->Select( it );
176 }
177 }
178}
179
180
181void DIALOG_EXPORT_2581::onDistPNChange( wxCommandEvent& event )
182{
183 if( event.GetSelection() == 0 )
184 {
185 m_textDistributor->Enable( false );
186 m_textDistributor->SetValue( _( "N/A" ) );
187 }
188 else
189 {
190 m_textDistributor->Enable( true );
191
192 // Don't try to guess the distributor if the user has already selected one
193 if( m_textDistributor->GetValue() != _( "N/A" ) )
194 return;
195
196 wxString dist = m_choiceDistPN->GetStringSelection();
197 dist.MakeUpper();
198
199 // Try to guess the distributor from the part number column
200
201 if( dist.Contains( wxT( "DIGIKEY" ) ) )
202 {
203 m_textDistributor->SetValue( wxT( "Digi-Key" ) );
204 }
205 else if( dist.Contains( wxT( "DIGI-KEY" ) ) )
206 {
207 m_textDistributor->SetValue( wxT( "Digi-Key" ) );
208 }
209 else if( dist.Contains( wxT( "MOUSER" ) ) )
210 {
211 m_textDistributor->SetValue( wxT( "Mouser" ) );
212 }
213 else if( dist.Contains( wxT( "NEWARK" ) ) )
214 {
215 m_textDistributor->SetValue( wxT( "Newark" ) );
216 }
217 else if( dist.Contains( wxT( "RS COMPONENTS" ) ) )
218 {
219 m_textDistributor->SetValue( wxT( "RS Components" ) );
220 }
221 else if( dist.Contains( wxT( "FARNELL" ) ) )
222 {
223 m_textDistributor->SetValue( wxT( "Farnell" ) );
224 }
225 else if( dist.Contains( wxT( "ARROW" ) ) )
226 {
227 m_textDistributor->SetValue( wxT( "Arrow" ) );
228 }
229 else if( dist.Contains( wxT( "AVNET" ) ) )
230 {
231 m_textDistributor->SetValue( wxT( "Avnet" ) );
232 }
233 else if( dist.Contains( wxT( "TME" ) ) )
234 {
235 m_textDistributor->SetValue( wxT( "TME" ) );
236 }
237 else if( dist.Contains( wxT( "LCSC" ) ) )
238 {
239 m_textDistributor->SetValue( wxT( "LCSC" ) );
240 }
241 }
242}
243
244
246{
248 PCBNEW_SETTINGS* cfg = mgr.GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
249
250 std::set<wxString> options;
251 BOARD* board = m_parent->GetBoard();
252
253 for( FOOTPRINT* fp : board->Footprints() )
254 {
255 for( PCB_FIELD* field : fp->GetFields() )
256 options.insert( field->GetName() );
257 }
258
259 if( !m_job )
260 {
261 m_choiceUnits->SetSelection( cfg->m_Export2581.units );
262 m_precision->SetValue( cfg->m_Export2581.precision );
263 m_versionChoice->SetSelection( cfg->m_Export2581.version );
264 m_cbCompress->SetValue( cfg->m_Export2581.compress );
265 }
266 else
267 {
268 m_choiceUnits->SetSelection( static_cast<int>( m_job->m_units ) );
269 m_precision->SetValue( static_cast<int>( m_job->m_precision ) );
271 m_cbCompress->SetValue( m_job->m_compress );
272 }
273
274 wxCommandEvent dummy;
276
277 std::vector<wxString> items( options.begin(), options.end() );
278 m_oemRef->Append( items );
279 m_choiceMPN->Append( items );
280 m_choiceMfg->Append( items );
281 m_choiceDistPN->Append( items );
282
283 m_oemRef->SetStringSelection( s_oemColumn );
284
286
287 wxString internalIdCol;
288 wxString mpnCol;
289 wxString distPnCol;
290 wxString mfgCol;
291 wxString distCol;
292
293 if( !m_job )
294 {
295 internalIdCol = prj.m_IP2581Bom.id;
296 mpnCol = prj.m_IP2581Bom.MPN;
297 distPnCol = prj.m_IP2581Bom.distPN;
298 mfgCol = prj.m_IP2581Bom.mfg;
299 distCol = prj.m_IP2581Bom.dist;
300 }
301 else
302 {
303 internalIdCol = m_job->m_colInternalId;
304 mpnCol = m_job->m_colMfgPn;
305 distPnCol = m_job->m_colDistPn;
306 mfgCol = m_job->m_colMfg;
307 distCol = m_job->m_colDist;
308 }
309
310 if( !m_choiceMPN->SetStringSelection( internalIdCol ) )
311 m_choiceMPN->SetSelection( 0 );
312
313 if( m_choiceMPN->SetStringSelection( mpnCol ) )
314 {
315 m_choiceMfg->Enable( true );
316
317 if( !m_choiceMfg->SetStringSelection( mfgCol ) )
318 m_choiceMfg->SetSelection( 0 );
319 }
320 else
321 {
322 m_choiceMPN->SetSelection( 0 );
323 m_choiceMfg->SetSelection( 0 );
324 m_choiceMfg->Enable( false );
325 }
326
327 if( m_choiceDistPN->SetStringSelection( distPnCol ) )
328 {
329 m_textDistributor->Enable( true );
330
331 // The combo box selection can be fixed, so any value can be entered
332 if( !prj.m_IP2581Bom.distPN.empty() )
333 {
334 m_textDistributor->SetValue( distCol );
335 }
336 else
337 {
338 wxCommandEvent evt;
339 onDistPNChange( evt );
340 }
341 }
342 else
343 {
344 m_choiceDistPN->SetSelection( 0 );
345 m_textDistributor->SetValue( _( "N/A" ) );
346 m_textDistributor->Enable( false );
347 }
348
349 return true;
350}
351
353{
354 if( !m_job )
355 {
357 PCBNEW_SETTINGS* cfg = mgr.GetAppSettings<PCBNEW_SETTINGS>( "pcbnew" );
358
359 cfg->m_Export2581.units = m_choiceUnits->GetSelection();
360 cfg->m_Export2581.precision = m_precision->GetValue();
361 cfg->m_Export2581.version = m_versionChoice->GetSelection();
362 cfg->m_Export2581.compress = m_cbCompress->GetValue();
363
365 wxString empty;
366
367 prj.m_IP2581Bom.id = GetOEM();
368 prj.m_IP2581Bom.mfg = GetMfg();
369 prj.m_IP2581Bom.MPN = GetMPN();
371 prj.m_IP2581Bom.dist = GetDist();
372
373 s_oemColumn = m_oemRef->GetStringSelection();
374 }
375 else
376 {
377 m_job->SetOutputPath( m_outputFileName->GetValue() );
378
382 m_job->m_colMfg = GetMfg();
384
389 m_job->m_precision = m_precision->GetValue();
391 }
392
393 return true;
394}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
const FOOTPRINTS & Footprints() const
Definition: board.h:331
const wxString & GetFileName() const
Definition: board.h:327
Class DIALOG_EXPORT_2581_BASE.
STD_BITMAP_BUTTON * m_browseButton
void onMfgPNChange(wxCommandEvent &event) override
wxString GetOEM() const
bool GetCompress() const
void onCompressCheck(wxCommandEvent &event) override
void onOKClick(wxCommandEvent &event) override
wxString GetMPN() const
void onDistPNChange(wxCommandEvent &event) override
bool TransferDataFromWindow() override
wxString GetDistPN() const
DIALOG_EXPORT_2581(PCB_EDIT_FRAME *aParent)
PCB_EDIT_FRAME * m_parent
wxString GetDist() const
wxString GetMfg() const
void onBrowseClicked(wxCommandEvent &event) override
wxString GetUnitsString() const
JOB_EXPORT_PCB_IPC2581 * m_job
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void SetOutputPath(const wxString &aPath)
Definition: job.cpp:129
wxString GetOutputPath() const
Definition: job.h:119
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
DIALOG_EXPORT_2581 m_Export2581
BOARD * GetBoard() const
The main frame for Pcbnew.
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:142
The backing store for a PROJECT, in JSON format.
Definition: project_file.h:72
struct IP2581_BOM m_IP2581Bom
Layer pair list for the board.
Definition: project_file.h:186
virtual PROJECT_FILE & GetProjectFile() const
Definition: project.h:200
T * GetAppSettings(const wxString &aFilename)
Returns a handle to the a given settings by type If the settings have already been loaded,...
void SetBitmap(const wxBitmapBundle &aBmp)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:348
static wxString s_oemColumn
static bool empty(const wxTextEntryBase *aCtrl)
#define _(s)
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: pgm_base.cpp:1060
see class PGM_BASE
@ LAST_PATH_2581
Definition: project_file.h:59
std::vector< FAB_LAYER_COLOR > dummy
wxString mfg
Manufacturer name column.
wxString MPN
Manufacturer part number column.
wxString id
Internal ID column.
wxString dist
Distributor name column.
wxString distPN
Distributor part number column.