KiCad PCB EDA Suite
dialog_export_idf.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) 2013-2015 Cirilo Bernardo
5  * Copyright (C) 2013-2019 KiCad Developers, see change_log.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 
25 #include <pcb_edit_frame.h>
26 #include <kiface_i.h>
27 #include <class_board.h>
28 #include <convert_to_biu.h>
29 #include <widgets/text_ctrl_eval.h>
30 #include <dialog_export_idf_base.h>
31 #include <pcb_edit_frame.h>
32 #include <pcbnew_settings.h>
33 #include <confirm.h>
34 
35 
37 {
38 private:
39  bool m_idfThouOpt; // remember last preference for units in THOU
40  bool m_AutoAdjust; // remember last Reference Point AutoAdjust setting
41  int m_RefUnits; // remember last units for Reference Point
42  double m_XRef; // remember last X Reference Point
43  double m_YRef; // remember last Y Reference Point
44 
46 
47 public:
49  DIALOG_EXPORT_IDF3_BASE( parent ), m_parent( parent )
50  {
51  SetFocus();
52 
53  auto cfg = m_parent->GetSettings();
54 
55  m_idfThouOpt = cfg->m_ExportIdf.units_mils;
56  m_rbUnitSelection->SetSelection( m_idfThouOpt ? 1 : 0 );
57  m_AutoAdjust = cfg->m_ExportIdf.auto_adjust;
58  m_RefUnits = cfg->m_ExportIdf.ref_units;
59  m_XRef = cfg->m_ExportIdf.ref_x;
60  m_YRef = cfg->m_ExportIdf.ref_y;
61 
63  m_cbAutoAdjustOffset->Bind( wxEVT_CHECKBOX, &DIALOG_EXPORT_IDF3::OnAutoAdjustOffset, this );
64 
65  m_IDF_RefUnitChoice->SetSelection( m_RefUnits );
66  wxString tmpStr;
67  tmpStr << m_XRef;
68  m_IDF_Xref->SetValue( tmpStr );
69  tmpStr = wxT( "" );
70  tmpStr << m_YRef;
71  m_IDF_Yref->SetValue( tmpStr );
72 
73  if( m_AutoAdjust )
74  {
75  m_IDF_RefUnitChoice->Enable( false );
76  m_IDF_Xref->Enable( false );
77  m_IDF_Yref->Enable( false );
78  }
79  else
80  {
81  m_IDF_RefUnitChoice->Enable( true );
82  m_IDF_Xref->Enable( true );
83  m_IDF_Yref->Enable( true );
84  }
85 
86  m_sdbSizerOK->SetDefault();
87 
88  // Now all widgets have the size fixed, call FinishDialogSettings
90  }
91 
93  {
94  m_idfThouOpt = m_rbUnitSelection->GetSelection() == 1;
95 
96  auto cfg = m_parent->GetSettings();
97 
99  cfg->m_ExportIdf.auto_adjust = m_AutoAdjust;
100  cfg->m_ExportIdf.ref_units = m_RefUnits;
101  cfg->m_ExportIdf.ref_x = m_XRef;
102  cfg->m_ExportIdf.ref_y = m_YRef;
103  }
104 
106  {
107  return m_rbUnitSelection->GetSelection() == 1;
108  }
109 
110  wxFilePickerCtrl* FilePicker()
111  {
112  return m_filePickerIDF;
113  }
114 
116  {
117  return m_IDF_RefUnitChoice->GetSelection();
118  }
119 
120  double GetXRef()
121  {
123  }
124 
125  double GetYRef()
126  {
128  }
129 
131  {
132  return m_cbAutoAdjustOffset->GetValue();
133  }
134 
135  void OnAutoAdjustOffset( wxCommandEvent& event )
136  {
137  if( GetAutoAdjustOffset() )
138  {
139  m_IDF_RefUnitChoice->Enable( false );
140  m_IDF_Xref->Enable( false );
141  m_IDF_Yref->Enable( false );
142  }
143  else
144  {
145  m_IDF_RefUnitChoice->Enable( true );
146  m_IDF_Xref->Enable( true );
147  m_IDF_Yref->Enable( true );
148  }
149 
150  event.Skip();
151  }
152 
153  bool TransferDataFromWindow() override;
154 };
155 
156 
158 {
159  wxFileName fn = m_filePickerIDF->GetPath();
160 
161  if( fn.FileExists() )
162  {
163  wxString msg = wxString::Format( _( "File %s already exists." ), fn.GetPath() );
164  KIDIALOG dlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
165  dlg.SetOKLabel( _( "Overwrite" ) );
166  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
167 
168  return ( dlg.ShowModal() == wxID_OK );
169  }
170 
171  return true;
172 }
173 
174 
175 void PCB_EDIT_FRAME::OnExportIDF3( wxCommandEvent& event )
176 {
177  // Build default output file name
178  wxString path = GetLastPath( LAST_PATH_IDF );
179 
180  if( path.IsEmpty() )
181  {
182  wxFileName brdFile = GetBoard()->GetFileName();
183  brdFile.SetExt( "emn" );
184  path = brdFile.GetFullPath();
185  }
186 
187  DIALOG_EXPORT_IDF3 dlg( this );
188  dlg.FilePicker()->SetPath( path );
189 
190  if ( dlg.ShowModal() != wxID_OK )
191  return;
192 
193  bool thou = dlg.GetThouOption();
194  double aXRef;
195  double aYRef;
196 
197  if( dlg.GetAutoAdjustOffset() )
198  {
200 
201  aXRef = bbox.Centre().x * MM_PER_IU;
202  aYRef = bbox.Centre().y * MM_PER_IU;
203  }
204  else
205  {
206  aXRef = dlg.GetXRef();
207  aYRef = dlg.GetYRef();
208 
209  if( dlg.GetRefUnitsChoice() == 1 )
210  {
211  // selected reference unit is in inches
212  aXRef *= 25.4;
213  aYRef *= 25.4;
214  }
215 
216  }
217 
218  wxBusyCursor dummy;
219 
220  wxString fullFilename = dlg.FilePicker()->GetPath();
221  SetLastPath( LAST_PATH_IDF, fullFilename );
222 
223  if( !Export_IDF3( GetBoard(), fullFilename, thou, aXRef, aYRef ) )
224  {
225  wxString msg = _( "Unable to create " ) + fullFilename;
226  wxMessageBox( msg );
227  return;
228  }
229 }
230 
void OnExportIDF3(wxCommandEvent &event)
Function OnExportIDF3 will export the current BOARD to a IDFv3 board and lib files.
wxFilePickerCtrl * m_filePickerIDF
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:44
This file is part of the common library.
void OnAutoAdjustOffset(wxCommandEvent &event)
const EDA_RECT GetBoardEdgesBoundingBox() const
Function GetBoardEdgesBoundingBox Returns the board bounding box calculated using exclusively the boa...
Definition: class_board.h:767
DIALOG_EXPORT_IDF m_ExportIdf
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
bool Export_IDF3(BOARD *aPcb, const wxString &aFullFileName, bool aUseThou, double aXRef, double aYRef)
Function Export_IDF3 Creates an IDF3 compliant BOARD (*.emn) and LIBRARY (*.emp) file.
const wxString & GetFileName() const
Definition: class_board.h:218
void SetValue(const wxString &aValue) override
Set a new value in evaluator buffer, and display it in the wxTextCtrl.
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
bool TransferDataFromWindow() override
Class DIALOG_EXPORT_IDF3_BASE.
wxFilePickerCtrl * FilePicker()
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define _(s)
Definition: 3d_actions.cpp:33
EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
PCB_EDIT_FRAME is the main frame for Pcbnew.
wxPoint Centre() const
Definition: eda_rect.h:62
DIALOG_EXPORT_IDF3(PCB_EDIT_FRAME *parent)
BOARD * GetBoard() const
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:331
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
PCB_EDIT_FRAME * m_parent
PCBNEW_SETTINGS * GetSettings()