KiCad PCB EDA Suite
dialog_export_step.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) 2016 Cirilo Bernardo
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #include <wx/choicdlg.h>
29 #include <wx/stdpaths.h>
30 
31 #include "wxPcbStruct.h"
32 #include "kiface_i.h"
33 #include "pcbnew.h"
34 #include "class_board.h"
36 
37 #define OPTKEY_STEP_USE_DRILL_ORG "STEP_UseDrillOrigin"
38 #define OPTKEY_STEP_USE_AUX_ORG "STEP_UseAuxOrigin"
39 #define OPTKEY_STEP_USE_USER_ORG "STEP_UseUserOrigin"
40 #define OPTKEY_STEP_UORG_UNITS "STEP_UserOriginUnits"
41 #define OPTKEY_STEP_UORG_X "STEP_UserOriginX"
42 #define OPTKEY_STEP_UORG_Y "STEP_UserOriginY"
43 #define OPTKEY_STEP_NOVIRT "STEP_NoVirtual"
44 
45 
47 {
48 private:
50  wxConfigBase* m_config;
51  bool m_useDrillOrg; // remember last preference for Use Drill Origin
52  bool m_useAuxOrg; // remember last preference for Use Aux Origin
53  bool m_useUserOrg; // remember last preference for Use User Origin
54  bool m_noVirtual; // remember last preference for No Virtual Component
55  int m_OrgUnits; // remember last units for User Origin
56  double m_XOrg; // remember last User Origin X value
57  double m_YOrg; // remember last User Origin Y value
58 
59 public:
61  DIALOG_EXPORT_STEP_BASE( parent )
62  {
63  m_parent = parent;
64  m_config = Kiface().KifaceSettings();
65  SetFocus();
66  m_useDrillOrg = false;
67  m_config->Read( OPTKEY_STEP_USE_DRILL_ORG, &m_useDrillOrg );
68  m_cbDrillOrigin->SetValue( m_useDrillOrg );
69  m_useAuxOrg = false;
70  m_config->Read( OPTKEY_STEP_USE_AUX_ORG, &m_useAuxOrg );
71  m_cbAuxOrigin->SetValue( m_useAuxOrg );
72  m_useUserOrg = false;
73  m_config->Read( OPTKEY_STEP_USE_USER_ORG, &m_useUserOrg );
74  m_cbUserOrigin->SetValue( m_useUserOrg );
75  m_cbUserOrigin->Bind( wxEVT_CHECKBOX, &DIALOG_EXPORT_STEP::OnUserOriginSelect, this );
76  m_config->Read( OPTKEY_STEP_UORG_UNITS, &m_OrgUnits, 0 );
77  m_config->Read( OPTKEY_STEP_UORG_X, &m_XOrg, 0.0 );
78  m_config->Read( OPTKEY_STEP_UORG_Y, &m_YOrg, 0.0 );
79  m_config->Read( OPTKEY_STEP_NOVIRT, &m_noVirtual );
80  m_cbRemoveVirtual->SetValue( m_noVirtual );
81 
82  m_STEP_OrgUnitChoice->SetSelection( m_OrgUnits );
83  wxString tmpStr;
84  tmpStr << m_XOrg;
85  m_STEP_Xorg->SetValue( tmpStr );
86  tmpStr = "";
87  tmpStr << m_YOrg;
88  m_STEP_Yorg->SetValue( tmpStr );
89 
90  if( m_useUserOrg )
91  {
92  m_STEP_OrgUnitChoice->Enable( true );
93  m_STEP_Xorg->Enable( true );
94  m_STEP_Yorg->Enable( true );
95  }
96  else
97  {
98  m_STEP_OrgUnitChoice->Enable( false );
99  m_STEP_Xorg->Enable( false );
100  m_STEP_Yorg->Enable( false );
101  }
102 
103  m_sdbSizerOK->SetDefault();
104 
105  // Now all widgets have the size fixed, call FinishDialogSettings
107  }
108 
110  {
111  m_config->Write( OPTKEY_STEP_USE_DRILL_ORG, m_cbDrillOrigin->GetValue() );
112  m_config->Write( OPTKEY_STEP_USE_AUX_ORG, m_cbAuxOrigin->GetValue() );
113  m_config->Write( OPTKEY_STEP_USE_USER_ORG, m_cbUserOrigin->GetValue() );
114  m_config->Write( OPTKEY_STEP_NOVIRT, m_cbRemoveVirtual->GetValue() );
115 
116  m_config->Write( OPTKEY_STEP_UORG_UNITS, m_STEP_OrgUnitChoice->GetSelection() );
117  m_config->Write( OPTKEY_STEP_UORG_X, m_STEP_Xorg->GetValue() );
118  m_config->Write( OPTKEY_STEP_UORG_Y, m_STEP_Yorg->GetValue() );
119  }
120 
121  wxFilePickerCtrl* FilePicker()
122  {
123  return m_filePickerSTEP;
124  }
125 
127  {
128  return m_STEP_OrgUnitChoice->GetSelection();
129  }
130 
131  double GetXOrg()
132  {
133  return DoubleValueFromString( UNSCALED_UNITS, m_STEP_Xorg->GetValue() );
134  }
135 
136  double GetYOrg()
137  {
138  return DoubleValueFromString( UNSCALED_UNITS, m_STEP_Yorg->GetValue() );
139  }
140 
142  {
143  return m_cbDrillOrigin->GetValue();
144  }
145 
147  {
148  return m_cbAuxOrigin->GetValue();
149  }
150 
152  {
153  return m_cbUserOrigin->GetValue();
154  }
155 
157  {
158  return m_cbRemoveVirtual->GetValue();
159  }
160 
161  void OnUserOriginSelect( wxCommandEvent& event )
162  {
163  if( GetUserOrgOption() )
164  {
165  m_STEP_OrgUnitChoice->Enable( true );
166  m_STEP_Xorg->Enable( true );
167  m_STEP_Yorg->Enable( true );
168  }
169  else
170  {
171  m_STEP_OrgUnitChoice->Enable( false );
172  m_STEP_Xorg->Enable( false );
173  m_STEP_Yorg->Enable( false );
174  }
175 
176  event.Skip();
177  }
178 
179 };
180 
181 
182 void PCB_EDIT_FRAME::OnExportSTEP( wxCommandEvent& event )
183 {
184 
185  wxFileName brdFile = GetBoard()->GetFileName();
186  wxString brdName;
187 
188  if( GetScreen()->IsModify() || brdFile.GetFullPath().empty() )
189  {
190  if( !doAutoSave() )
191  {
192  wxMessageBox( _( "STEP export failed; please save the PCB and try again" ),
193  _( "STEP Export" ) );
194  return;
195  }
196 
197  brdFile = GetBoard()->GetFileName();
198  brdName = GetAutoSaveFilePrefix();
199  brdName.append( brdFile.GetName() );
200  brdFile.SetName( brdName );
201  }
202 
203  brdName = "\"";
204  brdName.Append( brdFile.GetFullPath() );
205  brdName.Append( "\"" );
206 
207  // Build default output file name
208  brdFile = GetBoard()->GetFileName();
209  wxString brdExt = brdFile.GetExt();
210  brdFile.SetExt( "stp" );
211 
212  DIALOG_EXPORT_STEP dlg( this );
213  dlg.FilePicker()->SetPath( brdFile.GetFullPath() );
214  bool fileOverwrite = false;
215  wxString outputFile;
216 
217  while( !fileOverwrite )
218  {
219  if ( dlg.ShowModal() != wxID_OK )
220  return;
221 
222  brdFile = dlg.FilePicker()->GetPath();
223  brdFile.SetExt( "stp" );
224  outputFile = brdFile.GetFullPath();
225 
226  if( wxFile::Exists( outputFile ) )
227  {
228  wxString msg( _( "File: " ) );
229  msg.append( outputFile );
230  msg.append( "\n" );
231  msg.append( _( "File exists, overwrite?" ) );
232  int resp = wxMessageBox( msg, _( "STEP Export" ), wxYES_NO | wxCANCEL, this );
233 
234  switch( resp )
235  {
236  case wxCANCEL:
237  return;
238 
239  case wxYES:
240  fileOverwrite = true;
241  break;
242 
243  default:
244  break;
245  }
246  }
247  else
248  {
249  fileOverwrite = true;
250  }
251  }
252 
253  outputFile.Prepend( "\"" );
254  outputFile.Append( "\"" );
255  bool aUseDrillOrg = dlg.GetDrillOrgOption();
256  bool aUseAuxOrg = dlg.GetAuxOrgOption();
257  bool aUseUserOrg = dlg.GetUserOrgOption();
258  bool aNoVirtual = dlg.GetNoVirtOption();
259  double aXOrg = 0.0;
260  double aYOrg = 0.0;
261 
262  if( aUseUserOrg )
263  {
264  aXOrg = dlg.GetXOrg();
265  aYOrg = dlg.GetYOrg();
266 
267  if( dlg.GetOrgUnitsChoice() == 1 )
268  {
269  // selected reference unit is in inches
270  aXOrg *= 25.4;
271  aYOrg *= 25.4;
272  }
273  }
274 
275  wxFileName appK2S( wxStandardPaths::Get().GetExecutablePath() );
276  appK2S.SetName( "kicad2step" );
277 
278  wxString cmdK2S = "\"";
279  cmdK2S.Append( appK2S.GetFullPath() );
280  cmdK2S.Append( "\"" );
281 
282  if( aNoVirtual )
283  cmdK2S.Append( " --no-virtual" );
284 
285  if( aUseDrillOrg )
286  cmdK2S.Append( " --drill-origin" );
287 
288  if( aUseAuxOrg )
289  cmdK2S.Append( " --grid-origin" );
290 
291  if( aUseUserOrg )
292  cmdK2S.Append( wxString::Format( " --user-origin %.6fx%.6f", aXOrg, aYOrg ) );
293 
294  cmdK2S.Append( " -f -o " );
295  cmdK2S.Append( outputFile );
296 
297  cmdK2S.Append( " " );
298  cmdK2S.Append( brdName );
299 
300  int result = 0;
301 
302  do
303  {
304  wxBusyCursor dummy;
305  result = wxExecute( cmdK2S, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE );
306  } while( 0 );
307 
308  if( result )
309  {
310  wxMessageBox(
311  _( "Unable to create STEP file; check that the board has a valid outline and models." ),
312  _( "STEP Export" ), wxOK );
313  }
314 
315  return;
316 }
#define OPTKEY_STEP_UORG_X
static wxString GetAutoSaveFilePrefix()
Function GetAutoSaveFilePrefix.
#define OPTKEY_STEP_NOVIRT
virtual bool doAutoSave() override
Function doAutoSave performs auto save when the board has been modified and not saved within the auto...
Class BOARD to handle a board.
void OnExportSTEP(wxCommandEvent &event)
Function OnExportSTEP Exports the current BOARD to a STEP assembly.
PCB_EDIT_FRAME * m_parent
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
#define OPTKEY_STEP_UORG_UNITS
BOARD * GetBoard() const
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
wxFilePickerCtrl * m_filePickerSTEP
const wxString & GetFileName() const
Definition: class_board.h:237
#define OPTKEY_STEP_USE_USER_ORG
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:101
void OnUserOriginSelect(wxCommandEvent &event)
#define OPTKEY_STEP_USE_AUX_ORG
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 when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
Class DIALOG_EXPORT_STEP_BASE.
PCB_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
#define OPTKEY_STEP_UORG_Y
DIALOG_EXPORT_STEP(PCB_EDIT_FRAME *parent)
#define OPTKEY_STEP_USE_DRILL_ORG
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301