KiCad PCB EDA Suite
dialog_export_idf.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2013-2015 Cirilo Bernardo
9  * Copyright (C) 2013-2017 KiCad Developers, see change_log.txt for contributors.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <wxPcbStruct.h>
30 #include <kiface_i.h>
31 #include <pcbnew.h>
32 #include <class_board.h>
33 #include <convert_to_biu.h>
34 #include <widgets/text_ctrl_eval.h>
35 
36 // IDF export header generated by wxFormBuilder
37 #include <dialog_export_idf_base.h>
38 #include <wxPcbStruct.h>
39 
40 #define OPTKEY_IDF_THOU wxT( "IDFExportThou" )
41 #define OPTKEY_IDF_REF_AUTOADJ wxT( "IDFRefAutoAdj" )
42 #define OPTKEY_IDF_REF_UNITS wxT( "IDFRefUnits" )
43 #define OPTKEY_IDF_REF_X wxT( "IDFRefX" )
44 #define OPTKEY_IDF_REF_Y wxT( "IDFRefY" )
45 
46 
48 {
49 private:
50  wxConfigBase* m_config;
51  bool m_idfThouOpt; // remember last preference for units in THOU
52  bool m_AutoAdjust; // remember last Reference Point AutoAdjust setting
53  int m_RefUnits; // remember last units for Reference Point
54  double m_XRef; // remember last X Reference Point
55  double m_YRef; // remember last Y Reference Point
56 
57 public:
59  DIALOG_EXPORT_IDF3_BASE( parent )
60  {
61  m_config = Kiface().KifaceSettings();
62  SetFocus();
63  m_idfThouOpt = false;
64  m_config->Read( OPTKEY_IDF_THOU, &m_idfThouOpt );
65  m_rbUnitSelection->SetSelection( m_idfThouOpt ? 1 : 0 );
66  m_config->Read( OPTKEY_IDF_REF_AUTOADJ, &m_AutoAdjust, false );
67  m_config->Read( OPTKEY_IDF_REF_UNITS, &m_RefUnits, 0 );
68  m_config->Read( OPTKEY_IDF_REF_X, &m_XRef, 0.0 );
69  m_config->Read( OPTKEY_IDF_REF_Y, &m_YRef, 0.0 );
70 
71  m_cbAutoAdjustOffset->SetValue( m_AutoAdjust );
72  m_cbAutoAdjustOffset->Bind( wxEVT_CHECKBOX, &DIALOG_EXPORT_IDF3::OnAutoAdjustOffset, this );
73 
74  m_IDF_RefUnitChoice->SetSelection( m_RefUnits );
75  wxString tmpStr;
76  tmpStr << m_XRef;
77  m_IDF_Xref->SetValue( tmpStr );
78  tmpStr = wxT( "" );
79  tmpStr << m_YRef;
80  m_IDF_Yref->SetValue( tmpStr );
81 
82  if( m_AutoAdjust )
83  {
84  m_IDF_RefUnitChoice->Enable( false );
85  m_IDF_Xref->Enable( false );
86  m_IDF_Yref->Enable( false );
87  }
88  else
89  {
90  m_IDF_RefUnitChoice->Enable( true );
91  m_IDF_Xref->Enable( true );
92  m_IDF_Yref->Enable( true );
93  }
94 
95  m_sdbSizerOK->SetDefault();
96 
97  // Now all widgets have the size fixed, call FinishDialogSettings
99  }
100 
102  {
103  m_idfThouOpt = m_rbUnitSelection->GetSelection() == 1;
104  m_config->Write( OPTKEY_IDF_THOU, m_idfThouOpt );
105  m_config->Write( OPTKEY_IDF_REF_AUTOADJ, GetAutoAdjustOffset() );
106  m_config->Write( OPTKEY_IDF_REF_UNITS, m_IDF_RefUnitChoice->GetSelection() );
107  m_config->Write( OPTKEY_IDF_REF_X, m_IDF_Xref->GetValue() );
108  m_config->Write( OPTKEY_IDF_REF_Y, m_IDF_Yref->GetValue() );
109  }
110 
112  {
113  return m_rbUnitSelection->GetSelection() == 1;
114  }
115 
116  wxFilePickerCtrl* FilePicker()
117  {
118  return m_filePickerIDF;
119  }
120 
122  {
123  return m_IDF_RefUnitChoice->GetSelection();
124  }
125 
126  double GetXRef()
127  {
128  return DoubleValueFromString( UNSCALED_UNITS, m_IDF_Xref->GetValue() );
129  }
130 
131  double GetYRef()
132  {
133  return DoubleValueFromString( UNSCALED_UNITS, m_IDF_Yref->GetValue() );
134  }
135 
137  {
138  return m_cbAutoAdjustOffset->GetValue();
139  }
140 
141  void OnAutoAdjustOffset( wxCommandEvent& event )
142  {
143  if( GetAutoAdjustOffset() )
144  {
145  m_IDF_RefUnitChoice->Enable( false );
146  m_IDF_Xref->Enable( false );
147  m_IDF_Yref->Enable( false );
148  }
149  else
150  {
151  m_IDF_RefUnitChoice->Enable( true );
152  m_IDF_Xref->Enable( true );
153  m_IDF_Yref->Enable( true );
154  }
155 
156  event.Skip();
157  }
158 
159  bool TransferDataFromWindow() override;
160 };
161 
162 
164 {
165  wxFileName fn = m_filePickerIDF->GetPath();
166 
167  if( fn.FileExists() )
168  {
169  if( wxMessageBox( _( "Are you sure you want to overwrite the exiting file?" ),
170  _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
171  return false;
172  }
173 
174  return true;
175 }
176 
177 
178 void PCB_EDIT_FRAME::OnExportIDF3( wxCommandEvent& event )
179 {
180  wxFileName fn;
181 
182  // Build default file name
183  fn = GetBoard()->GetFileName();
184  fn.SetExt( wxT( "emn" ) );
185 
186  DIALOG_EXPORT_IDF3 dlg( this );
187  dlg.FilePicker()->SetPath( fn.GetFullPath() );
188 
189  if ( dlg.ShowModal() != wxID_OK )
190  return;
191 
192  bool thou = dlg.GetThouOption();
193  double aXRef;
194  double aYRef;
195 
196  if( dlg.GetAutoAdjustOffset() )
197  {
199 
200  aXRef = bbox.Centre().x * MM_PER_IU;
201  aYRef = bbox.Centre().y * MM_PER_IU;
202  }
203  else
204  {
205  aXRef = dlg.GetXRef();
206  aYRef = dlg.GetYRef();
207 
208  if( dlg.GetRefUnitsChoice() == 1 )
209  {
210  // selected reference unit is in inches
211  aXRef *= 25.4;
212  aYRef *= 25.4;
213  }
214 
215  }
216 
217  wxBusyCursor dummy;
218 
219  wxString fullFilename = dlg.FilePicker()->GetPath();
220 
221  if( !Export_IDF3( GetBoard(), fullFilename, thou, aXRef, aYRef ) )
222  {
223  wxString msg = _( "Unable to create " ) + fullFilename;
224  wxMessageBox( msg );
225  return;
226  }
227 }
void OnExportIDF3(wxCommandEvent &event)
Function OnExportIDF3 will export the current BOARD to a IDFv3 board and lib files.
#define OPTKEY_IDF_THOU
wxFilePickerCtrl * m_filePickerIDF
void OnAutoAdjustOffset(wxCommandEvent &event)
Class BOARD to handle a board.
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.
Definition: export_idf.cpp:553
BOARD * GetBoard() const
#define OPTKEY_IDF_REF_X
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
const EDA_RECT GetBoardEdgesBoundingBox() const
Function GetBoardEdgesBoundingBox Returns the board bounding box calculated using exclusively the boa...
Definition: class_board.h:797
bool TransferDataFromWindow() override
const wxString & GetFileName() const
Definition: class_board.h:234
#define OPTKEY_IDF_REF_UNITS
Class DIALOG_EXPORT_IDF3_BASE.
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
#define OPTKEY_IDF_REF_Y
wxPoint Centre() const
wxFilePickerCtrl * FilePicker()
wxConfigBase * m_config
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
Class EDA_RECT handles the component boundary box.
DIALOG_EXPORT_IDF3(PCB_EDIT_FRAME *parent)
#define OPTKEY_IDF_REF_AUTOADJ
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:302