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 <board.h>
27 #include <widgets/text_ctrl_eval.h>
28 #include <dialog_export_idf_base.h>
29 #include <pcbnew_settings.h>
30 #include <project/project_file.h> // LAST_PATH_TYPE
31 #include <confirm.h>
32 
33 
34 class DIALOG_EXPORT_IDF3: public DIALOG_EXPORT_IDF3_BASE
35 {
36 private:
37     bool   m_idfThouOpt;    // remember last preference for units in THOU
38     bool   m_AutoAdjust;    // remember last Reference Point AutoAdjust setting
39     int    m_RefUnits;      // remember last units for Reference Point
40     double m_XRef;          // remember last X Reference Point
41     double m_YRef;          // remember last Y Reference Point
42 
43     PCB_EDIT_FRAME* m_parent;
44 
45 public:
DIALOG_EXPORT_IDF3(PCB_EDIT_FRAME * parent)46     DIALOG_EXPORT_IDF3( PCB_EDIT_FRAME* parent ) :
47             DIALOG_EXPORT_IDF3_BASE( parent ), m_parent( parent )
48     {
49         SetFocus();
50 
51         auto cfg = m_parent->GetPcbNewSettings();
52 
53         m_idfThouOpt = cfg->m_ExportIdf.units_mils;
54         m_rbUnitSelection->SetSelection( m_idfThouOpt ? 1 : 0 );
55         m_AutoAdjust = cfg->m_ExportIdf.auto_adjust;
56         m_RefUnits   = cfg->m_ExportIdf.ref_units;
57         m_XRef       = cfg->m_ExportIdf.ref_x;
58         m_YRef       = cfg->m_ExportIdf.ref_y;
59 
60         m_cbAutoAdjustOffset->SetValue( m_AutoAdjust );
61         m_cbAutoAdjustOffset->Bind( wxEVT_CHECKBOX, &DIALOG_EXPORT_IDF3::OnAutoAdjustOffset, this );
62 
63         m_IDF_RefUnitChoice->SetSelection( m_RefUnits );
64         wxString tmpStr;
65         tmpStr << m_XRef;
66         m_IDF_Xref->SetValue( tmpStr );
67         tmpStr = wxT( "" );
68         tmpStr << m_YRef;
69         m_IDF_Yref->SetValue( tmpStr );
70 
71         if( m_AutoAdjust )
72         {
73             m_IDF_RefUnitChoice->Enable( false );
74             m_IDF_Xref->Enable( false );
75             m_IDF_Yref->Enable( false );
76         }
77         else
78         {
79             m_IDF_RefUnitChoice->Enable( true );
80             m_IDF_Xref->Enable( true );
81             m_IDF_Yref->Enable( true );
82         }
83 
84         m_sdbSizerOK->SetDefault();
85 
86         // Now all widgets have the size fixed, call FinishDialogSettings
87         finishDialogSettings();
88     }
89 
~DIALOG_EXPORT_IDF3()90     ~DIALOG_EXPORT_IDF3()
91     {
92         m_idfThouOpt = m_rbUnitSelection->GetSelection() == 1;
93 
94         auto cfg = m_parent->GetPcbNewSettings();
95 
96         cfg->m_ExportIdf.units_mils  = m_idfThouOpt;
97         cfg->m_ExportIdf.auto_adjust = m_AutoAdjust;
98         cfg->m_ExportIdf.ref_units   = m_RefUnits;
99         cfg->m_ExportIdf.ref_x       = m_XRef;
100         cfg->m_ExportIdf.ref_y       = m_YRef;
101     }
102 
GetThouOption()103     bool GetThouOption()
104     {
105         return m_rbUnitSelection->GetSelection() == 1;
106     }
107 
FilePicker()108     wxFilePickerCtrl* FilePicker()
109     {
110         return m_filePickerIDF;
111     }
112 
GetRefUnitsChoice()113     int GetRefUnitsChoice()
114     {
115         return m_IDF_RefUnitChoice->GetSelection();
116     }
117 
GetXRef()118     double GetXRef()
119     {
120         return DoubleValueFromString( EDA_UNITS::UNSCALED, m_IDF_Xref->GetValue() );
121     }
122 
GetYRef()123     double GetYRef()
124     {
125         return DoubleValueFromString( EDA_UNITS::UNSCALED, m_IDF_Yref->GetValue() );
126     }
127 
GetAutoAdjustOffset()128     bool GetAutoAdjustOffset()
129     {
130         return m_cbAutoAdjustOffset->GetValue();
131     }
132 
OnAutoAdjustOffset(wxCommandEvent & event)133     void OnAutoAdjustOffset( wxCommandEvent& event )
134     {
135         if( GetAutoAdjustOffset() )
136         {
137             m_IDF_RefUnitChoice->Enable( false );
138             m_IDF_Xref->Enable( false );
139             m_IDF_Yref->Enable( false );
140         }
141         else
142         {
143             m_IDF_RefUnitChoice->Enable( true );
144             m_IDF_Xref->Enable( true );
145             m_IDF_Yref->Enable( true );
146         }
147 
148         event.Skip();
149     }
150 
151     bool TransferDataFromWindow() override;
152 };
153 
154 
TransferDataFromWindow()155 bool DIALOG_EXPORT_IDF3::TransferDataFromWindow()
156 {
157     wxFileName fn = m_filePickerIDF->GetPath();
158 
159     if( fn.FileExists() )
160     {
161         wxString msg = wxString::Format( _( "File %s already exists." ), fn.GetPath() );
162         KIDIALOG dlg( this, msg, _( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
163         dlg.SetOKLabel( _( "Overwrite" ) );
164         dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
165 
166         return ( dlg.ShowModal() == wxID_OK );
167     }
168 
169     return true;
170 }
171 
172 
OnExportIDF3(wxCommandEvent & event)173 void PCB_EDIT_FRAME::OnExportIDF3( wxCommandEvent& event )
174 {
175     // Build default output file name
176     wxString path = GetLastPath( LAST_PATH_IDF );
177 
178     if( path.IsEmpty() )
179     {
180         wxFileName brdFile = GetBoard()->GetFileName();
181         brdFile.SetExt( "emn" );
182         path = brdFile.GetFullPath();
183     }
184 
185     DIALOG_EXPORT_IDF3 dlg( this );
186     dlg.FilePicker()->SetPath( path );
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     {
197         EDA_RECT bbox = GetBoard()->GetBoardEdgesBoundingBox();
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     SetLastPath( LAST_PATH_IDF, fullFilename );
220 
221     if( !Export_IDF3( GetBoard(), fullFilename, thou, aXRef, aYRef ) )
222     {
223         wxString msg = wxString::Format( _( "Failed to create file '%s'." ), fullFilename );
224         wxMessageBox( msg );
225         return;
226     }
227 }
228 
229