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 
35 // IDF export header generated by wxFormBuilder
36 #include <dialog_export_idf_base.h>
37 #include <wxPcbStruct.h>
38 
39 #define OPTKEY_IDF_THOU wxT( "IDFExportThou" )
40 #define OPTKEY_IDF_REF_AUTOADJ wxT( "IDFRefAutoAdj" )
41 #define OPTKEY_IDF_REF_UNITS wxT( "IDFRefUnits" )
42 #define OPTKEY_IDF_REF_X wxT( "IDFRefX" )
43 #define OPTKEY_IDF_REF_Y wxT( "IDFRefY" )
44 
45 
47 {
48 private:
49  wxConfigBase* m_config;
50  bool m_idfThouOpt; // remember last preference for units in THOU
51  bool m_AutoAdjust; // remember last Reference Point AutoAdjust setting
52  int m_RefUnits; // remember last units for Reference Point
53  double m_XRef; // remember last X Reference Point
54  double m_YRef; // remember last Y Reference Point
55 
56 public:
58  DIALOG_EXPORT_IDF3_BASE( parent )
59  {
60  m_config = Kiface().KifaceSettings();
61  SetFocus();
62  m_idfThouOpt = false;
63  m_config->Read( OPTKEY_IDF_THOU, &m_idfThouOpt );
64  m_rbUnitSelection->SetSelection( m_idfThouOpt ? 1 : 0 );
65  m_config->Read( OPTKEY_IDF_REF_AUTOADJ, &m_AutoAdjust, false );
66  m_config->Read( OPTKEY_IDF_REF_UNITS, &m_RefUnits, 0 );
67  m_config->Read( OPTKEY_IDF_REF_X, &m_XRef, 0.0 );
68  m_config->Read( OPTKEY_IDF_REF_Y, &m_YRef, 0.0 );
69 
70  m_cbAutoAdjustOffset->SetValue( m_AutoAdjust );
71  m_cbAutoAdjustOffset->Bind( wxEVT_CHECKBOX, &DIALOG_EXPORT_IDF3::OnAutoAdjustOffset, this );
72 
73  m_IDF_RefUnitChoice->SetSelection( m_RefUnits );
74  wxString tmpStr;
75  tmpStr << m_XRef;
76  m_IDF_Xref->SetValue( tmpStr );
77  tmpStr = wxT( "" );
78  tmpStr << m_YRef;
79  m_IDF_Yref->SetValue( tmpStr );
80 
81  if( m_AutoAdjust )
82  {
83  m_IDF_RefUnitChoice->Enable( false );
84  m_IDF_Xref->Enable( false );
85  m_IDF_Yref->Enable( false );
86  }
87  else
88  {
89  m_IDF_RefUnitChoice->Enable( true );
90  m_IDF_Xref->Enable( true );
91  m_IDF_Yref->Enable( true );
92  }
93 
94  m_sdbSizerOK->SetDefault();
95 
96  // Now all widgets have the size fixed, call FinishDialogSettings
98  }
99 
101  {
102  m_idfThouOpt = m_rbUnitSelection->GetSelection() == 1;
103  m_config->Write( OPTKEY_IDF_THOU, m_idfThouOpt );
104  m_config->Write( OPTKEY_IDF_REF_AUTOADJ, GetAutoAdjustOffset() );
105  m_config->Write( OPTKEY_IDF_REF_UNITS, m_IDF_RefUnitChoice->GetSelection() );
106  m_config->Write( OPTKEY_IDF_REF_X, m_IDF_Xref->GetValue() );
107  m_config->Write( OPTKEY_IDF_REF_Y, m_IDF_Yref->GetValue() );
108  }
109 
111  {
112  return m_rbUnitSelection->GetSelection() == 1;
113  }
114 
115  wxFilePickerCtrl* FilePicker()
116  {
117  return m_filePickerIDF;
118  }
119 
121  {
122  return m_IDF_RefUnitChoice->GetSelection();
123  }
124 
125  double GetXRef()
126  {
127  return DoubleValueFromString( UNSCALED_UNITS, m_IDF_Xref->GetValue() );
128  }
129 
130  double GetYRef()
131  {
132  return DoubleValueFromString( UNSCALED_UNITS, m_IDF_Yref->GetValue() );
133  }
134 
136  {
137  return m_cbAutoAdjustOffset->GetValue();
138  }
139 
140  void OnAutoAdjustOffset( wxCommandEvent& event )
141  {
142  if( GetAutoAdjustOffset() )
143  {
144  m_IDF_RefUnitChoice->Enable( false );
145  m_IDF_Xref->Enable( false );
146  m_IDF_Yref->Enable( false );
147  }
148  else
149  {
150  m_IDF_RefUnitChoice->Enable( true );
151  m_IDF_Xref->Enable( true );
152  m_IDF_Yref->Enable( true );
153  }
154 
155  event.Skip();
156  }
157 
158  bool TransferDataFromWindow() override;
159 };
160 
161 
163 {
164  wxFileName fn = m_filePickerIDF->GetPath();
165 
166  if( fn.FileExists() )
167  {
168  if( wxMessageBox( _( "Are you sure you want to overwrite the exiting file?" ),
169  _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
170  return false;
171  }
172 
173  return true;
174 }
175 
176 
177 void PCB_EDIT_FRAME::OnExportIDF3( wxCommandEvent& event )
178 {
179  wxFileName fn;
180 
181  // Build default file name
182  fn = GetBoard()->GetFileName();
183  fn.SetExt( wxT( "emn" ) );
184 
185  DIALOG_EXPORT_IDF3 dlg( this );
186  dlg.FilePicker()->SetPath( fn.GetFullPath() );
187 
188  if ( dlg.ShowModal() != wxID_OK )
189  return;
190 
191  bool thou = dlg.GetThouOption();
192  double aXRef;
193  double aYRef;
194 
195  if( dlg.GetAutoAdjustOffset() )
196  {
198 
199  aXRef = bbox.Centre().x * MM_PER_IU;
200  aYRef = bbox.Centre().y * MM_PER_IU;
201  }
202  else
203  {
204  aXRef = dlg.GetXRef();
205  aYRef = dlg.GetYRef();
206 
207  if( dlg.GetRefUnitsChoice() == 1 )
208  {
209  // selected reference unit is in inches
210  aXRef *= 25.4;
211  aYRef *= 25.4;
212  }
213 
214  }
215 
216  wxBusyCursor dummy;
217 
218  wxString fullFilename = dlg.FilePicker()->GetPath();
219 
220  if( !Export_IDF3( GetBoard(), fullFilename, thou, aXRef, aYRef ) )
221  {
222  wxString msg = _( "Unable to create " ) + fullFilename;
223  wxMessageBox( msg );
224  return;
225  }
226 }
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:301