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 <pcbnew.h>
28 #include <class_board.h>
29 #include <convert_to_biu.h>
30 #include <widgets/text_ctrl_eval.h>
31 #include <dialog_export_idf_base.h>
32 #include <pcb_edit_frame.h>
33 #include <confirm.h>
34 
35 #define OPTKEY_IDF_THOU wxT( "IDFExportThou" )
36 #define OPTKEY_IDF_REF_AUTOADJ wxT( "IDFRefAutoAdj" )
37 #define OPTKEY_IDF_REF_UNITS wxT( "IDFRefUnits" )
38 #define OPTKEY_IDF_REF_X wxT( "IDFRefX" )
39 #define OPTKEY_IDF_REF_Y wxT( "IDFRefY" )
40 
41 
43 {
44 private:
45  wxConfigBase* m_config;
46  bool m_idfThouOpt; // remember last preference for units in THOU
47  bool m_AutoAdjust; // remember last Reference Point AutoAdjust setting
48  int m_RefUnits; // remember last units for Reference Point
49  double m_XRef; // remember last X Reference Point
50  double m_YRef; // remember last Y Reference Point
51 
52 public:
54  DIALOG_EXPORT_IDF3_BASE( parent )
55  {
57  SetFocus();
58  m_idfThouOpt = false;
60  m_rbUnitSelection->SetSelection( m_idfThouOpt ? 1 : 0 );
63  m_config->Read( OPTKEY_IDF_REF_X, &m_XRef, 0.0 );
64  m_config->Read( OPTKEY_IDF_REF_Y, &m_YRef, 0.0 );
65 
67  m_cbAutoAdjustOffset->Bind( wxEVT_CHECKBOX, &DIALOG_EXPORT_IDF3::OnAutoAdjustOffset, this );
68 
69  m_IDF_RefUnitChoice->SetSelection( m_RefUnits );
70  wxString tmpStr;
71  tmpStr << m_XRef;
72  m_IDF_Xref->SetValue( tmpStr );
73  tmpStr = wxT( "" );
74  tmpStr << m_YRef;
75  m_IDF_Yref->SetValue( tmpStr );
76 
77  if( m_AutoAdjust )
78  {
79  m_IDF_RefUnitChoice->Enable( false );
80  m_IDF_Xref->Enable( false );
81  m_IDF_Yref->Enable( false );
82  }
83  else
84  {
85  m_IDF_RefUnitChoice->Enable( true );
86  m_IDF_Xref->Enable( true );
87  m_IDF_Yref->Enable( true );
88  }
89 
90  m_sdbSizerOK->SetDefault();
91 
92  // Now all widgets have the size fixed, call FinishDialogSettings
94  }
95 
97  {
98  m_idfThouOpt = m_rbUnitSelection->GetSelection() == 1;
101  m_config->Write( OPTKEY_IDF_REF_UNITS, m_IDF_RefUnitChoice->GetSelection() );
102  m_config->Write( OPTKEY_IDF_REF_X, m_IDF_Xref->GetValue() );
103  m_config->Write( OPTKEY_IDF_REF_Y, m_IDF_Yref->GetValue() );
104  }
105 
107  {
108  return m_rbUnitSelection->GetSelection() == 1;
109  }
110 
111  wxFilePickerCtrl* FilePicker()
112  {
113  return m_filePickerIDF;
114  }
115 
117  {
118  return m_IDF_RefUnitChoice->GetSelection();
119  }
120 
121  double GetXRef()
122  {
123  return DoubleValueFromString( UNSCALED_UNITS, m_IDF_Xref->GetValue() );
124  }
125 
126  double GetYRef()
127  {
128  return DoubleValueFromString( UNSCALED_UNITS, m_IDF_Yref->GetValue() );
129  }
130 
132  {
133  return m_cbAutoAdjustOffset->GetValue();
134  }
135 
136  void OnAutoAdjustOffset( wxCommandEvent& event )
137  {
138  if( GetAutoAdjustOffset() )
139  {
140  m_IDF_RefUnitChoice->Enable( false );
141  m_IDF_Xref->Enable( false );
142  m_IDF_Yref->Enable( false );
143  }
144  else
145  {
146  m_IDF_RefUnitChoice->Enable( true );
147  m_IDF_Xref->Enable( true );
148  m_IDF_Yref->Enable( true );
149  }
150 
151  event.Skip();
152  }
153 
154  bool TransferDataFromWindow() override;
155 };
156 
157 
159 {
160  wxFileName fn = m_filePickerIDF->GetPath();
161 
162  if( fn.FileExists() )
163  {
164  wxString msg = wxString::Format( _( "File %s already exists." ), fn.GetPath() );
165  KIDIALOG dlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
166  dlg.SetOKLabel( _( "Overwrite" ) );
167  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
168 
169  return ( dlg.ShowModal() == wxID_OK );
170  }
171 
172  return true;
173 }
174 
175 
176 void PCB_EDIT_FRAME::OnExportIDF3( wxCommandEvent& event )
177 {
178  // Build default output file name
179  wxString path = GetLastPath( LAST_PATH_IDF );
180 
181  if( path.IsEmpty() )
182  {
183  wxFileName brdFile = GetBoard()->GetFileName();
184  brdFile.SetExt( "emn" );
185  path = brdFile.GetFullPath();
186  }
187 
188  DIALOG_EXPORT_IDF3 dlg( this );
189  dlg.FilePicker()->SetPath( path );
190 
191  if ( dlg.ShowModal() != wxID_OK )
192  return;
193 
194  bool thou = dlg.GetThouOption();
195  double aXRef;
196  double aYRef;
197 
198  if( dlg.GetAutoAdjustOffset() )
199  {
201 
202  aXRef = bbox.Centre().x * MM_PER_IU;
203  aYRef = bbox.Centre().y * MM_PER_IU;
204  }
205  else
206  {
207  aXRef = dlg.GetXRef();
208  aYRef = dlg.GetYRef();
209 
210  if( dlg.GetRefUnitsChoice() == 1 )
211  {
212  // selected reference unit is in inches
213  aXRef *= 25.4;
214  aYRef *= 25.4;
215  }
216 
217  }
218 
219  wxBusyCursor dummy;
220 
221  wxString fullFilename = dlg.FilePicker()->GetPath();
222  SetLastPath( LAST_PATH_IDF, fullFilename );
223 
224  if( !Export_IDF3( GetBoard(), fullFilename, thou, aXRef, aYRef ) )
225  {
226  wxString msg = _( "Unable to create " ) + fullFilename;
227  wxMessageBox( msg );
228  return;
229  }
230 }
231 
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
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:792
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
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
#define OPTKEY_IDF_REF_X
const wxString & GetFileName() const
Definition: class_board.h:216
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.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
bool TransferDataFromWindow() override
#define OPTKEY_IDF_REF_UNITS
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:314
Class DIALOG_EXPORT_IDF3_BASE.
#define OPTKEY_IDF_REF_Y
#define _(s)
wxFilePickerCtrl * FilePicker()
wxConfigBase * m_config
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.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class 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
#define OPTKEY_IDF_REF_AUTOADJ
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.