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  * Copyright (C) 2016-2018 KiCad Developers, see AUTHORS.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 <wx/choicdlg.h>
30 #include <wx/stdpaths.h>
31 
32 #include "pcb_edit_frame.h"
33 #include "kiface_i.h"
34 #include "confirm.h"
35 #include "reporter.h"
36 
37 #include "pcbnew.h"
38 #include "class_board.h"
40 #include <widgets/text_ctrl_eval.h>
41 #include <wx_html_report_panel.h>
42 
43 #define OPTKEY_STEP_ORIGIN_OPT "STEP_Origin_Opt"
44 #define OPTKEY_STEP_UORG_UNITS "STEP_UserOriginUnits"
45 #define OPTKEY_STEP_UORG_X "STEP_UserOriginX"
46 #define OPTKEY_STEP_UORG_Y "STEP_UserOriginY"
47 #define OPTKEY_STEP_NOVIRT "STEP_NoVirtual"
48 
49 
51 {
52 public:
54  {
55  STEP_ORG_0, // absolute coordinates
56  STEP_ORG_PLOT_AXIS, // origin is plot/drill axis origin
57  STEP_ORG_GRID_AXIS, // origin is grid origin
58  STEP_ORG_BOARD_CENTER, // origin is board center
59  STEP_ORG_USER, // origin is entered by user
60  };
61 
62 private:
64  wxConfigBase* m_config;
65  // The last preference for STEP Origin:
67  bool m_noVirtual; // remember last preference for No Virtual Component
68  int m_OrgUnits; // remember last units for User Origin
69  double m_XOrg; // remember last User Origin X value
70  double m_YOrg; // remember last User Origin Y value
71  wxString m_boardPath; // path to the exported board file
72 
73 protected:
74  void onUpdateUnits( wxUpdateUIEvent& aEvent ) override;
75  void onUpdateXPos( wxUpdateUIEvent& aEvent ) override;
76  void onUpdateYPos( wxUpdateUIEvent& aEvent ) override;
77  void onExportButton( wxCommandEvent& aEvent ) override;
78 
79  int GetOrgUnitsChoice() const
80  {
81  return m_STEP_OrgUnitChoice->GetSelection();
82  }
83 
84  double GetXOrg() const
85  {
86  return DoubleValueFromString( UNSCALED_UNITS, m_STEP_Xorg->GetValue() );
87  }
88 
89  double GetYOrg()
90  {
91  return DoubleValueFromString( UNSCALED_UNITS, m_STEP_Yorg->GetValue() );
92  }
93 
95 
97  {
98  return m_cbRemoveVirtual->GetValue();
99  }
100 
101 public:
102  DIALOG_EXPORT_STEP( PCB_EDIT_FRAME* aParent, const wxString& aBoardPath );
103 
105  {
106  GetOriginOption(); // Update m_STEP_org_opt member.
107  m_config->Write( OPTKEY_STEP_ORIGIN_OPT, (int)m_STEP_org_opt );
108  m_config->Write( OPTKEY_STEP_NOVIRT, m_cbRemoveVirtual->GetValue() );
109  m_config->Write( OPTKEY_STEP_UORG_UNITS, m_STEP_OrgUnitChoice->GetSelection() );
110  m_config->Write( OPTKEY_STEP_UORG_X, m_STEP_Xorg->GetValue() );
111  m_config->Write( OPTKEY_STEP_UORG_Y, m_STEP_Yorg->GetValue() );
112  }
113 };
114 
115 
116 DIALOG_EXPORT_STEP::DIALOG_EXPORT_STEP( PCB_EDIT_FRAME* aParent, const wxString& aBoardPath ) :
117  DIALOG_EXPORT_STEP_BASE( aParent )
118 {
119  m_parent = aParent;
120  m_boardPath = aBoardPath;
122 
123  // Build default output file name
124  wxFileName brdFile = m_parent->GetBoard()->GetFileName();
125  brdFile.SetExt( "stp" );
126  m_filePickerSTEP->SetPath( brdFile.GetFullPath() );
127 
128  SetFocus();
129 
131  int tmp = STEP_ORG_0;
132 
133  if( m_config->Read( OPTKEY_STEP_ORIGIN_OPT, &tmp ) )
135 
136  switch( m_STEP_org_opt )
137  {
138  default: break;
139  case STEP_ORG_PLOT_AXIS: m_rbDrillAndPlotOrigin->SetValue( true ); break;
140  case STEP_ORG_GRID_AXIS: m_rbGridOrigin->SetValue( true ); break;
141  case STEP_ORG_USER: m_rbUserDefinedOrigin->SetValue( true ); break;
142  case STEP_ORG_BOARD_CENTER: m_rbBoardCenterOrigin->SetValue( true ); break;
143  }
144 
146  m_config->Read( OPTKEY_STEP_UORG_X, &m_XOrg, 0.0 );
147  m_config->Read( OPTKEY_STEP_UORG_Y, &m_YOrg, 0.0 );
149  m_cbRemoveVirtual->SetValue( m_noVirtual );
150 
151  m_STEP_OrgUnitChoice->SetSelection( m_OrgUnits );
152  wxString tmpStr;
153  tmpStr << m_XOrg;
154  m_STEP_Xorg->SetValue( tmpStr );
155  tmpStr = "";
156  tmpStr << m_YOrg;
157  m_STEP_Yorg->SetValue( tmpStr );
158 
159  // Now all widgets have the size fixed, call FinishDialogSettings
161 }
162 
163 
165 {
167 
168  if( m_rbDrillAndPlotOrigin->GetValue() )
170  else if( m_rbGridOrigin->GetValue() )
172  else if( m_rbUserDefinedOrigin->GetValue() )
174  else if( m_rbBoardCenterOrigin->GetValue() )
176 
177  return m_STEP_org_opt;
178 }
179 
180 
181 void PCB_EDIT_FRAME::OnExportSTEP( wxCommandEvent& event )
182 {
183  wxFileName brdFile = GetBoard()->GetFileName();
184 
185  if( GetScreen()->IsModify() || brdFile.GetFullPath().empty() )
186  {
187  if( !doAutoSave() )
188  {
189  DisplayErrorMessage( this,
190  _( "STEP export failed! Please save the PCB and try again" ) );
191  return;
192  }
193 
194  // Use auto-saved board for export
195  brdFile.SetName( GetAutoSaveFilePrefix() + brdFile.GetName() );
196  }
197 
198  DIALOG_EXPORT_STEP dlg( this, brdFile.GetFullPath() );
199  dlg.ShowModal();
200 }
201 
202 
203 void DIALOG_EXPORT_STEP::onUpdateUnits( wxUpdateUIEvent& aEvent )
204 {
205  aEvent.Enable( m_rbUserDefinedOrigin->GetValue() );
206 }
207 
208 
209 void DIALOG_EXPORT_STEP::onUpdateXPos( wxUpdateUIEvent& aEvent )
210 {
211  aEvent.Enable( m_rbUserDefinedOrigin->GetValue() );
212 }
213 
214 
215 void DIALOG_EXPORT_STEP::onUpdateYPos( wxUpdateUIEvent& aEvent )
216 {
217  aEvent.Enable( m_rbUserDefinedOrigin->GetValue() );
218 }
219 
220 
221 void DIALOG_EXPORT_STEP::onExportButton( wxCommandEvent& aEvent )
222 {
223  SHAPE_POLY_SET outline;
224  wxString msg;
225 
226  // Check if the board outline is continuous
227  // TODO the check below is more forgiving than kicad2step, needs to be more strict
228  if( !m_parent->GetBoard()->GetBoardPolygonOutlines( outline, &msg ) )
229  {
230  DisplayErrorMessage( this, _( "Cannot determine the board outline." ), msg );
231  return;
232  }
233 
234  wxFileName fn = m_filePickerSTEP->GetFileName();
235 
236  if( fn.FileExists() )
237  {
238  msg.Printf( _( "File '%s' already exists. Do you want overwrite this file?" ),
239  fn.GetFullPath().GetData() );
240 
241  if( wxMessageBox( msg, _( "STEP Export" ), wxYES_NO | wxICON_QUESTION, this ) == wxNO )
242  return;
243  }
244 
246  double xOrg = 0.0;
247  double yOrg = 0.0;
248 
249  wxFileName appK2S( wxStandardPaths::Get().GetExecutablePath() );
250  appK2S.SetName( "kicad2step" );
251 
252  wxString cmdK2S = "\"";
253  cmdK2S.Append( appK2S.GetFullPath() );
254  cmdK2S.Append( "\"" );
255 
256  if( GetNoVirtOption() )
257  cmdK2S.Append( " --no-virtual" );
258 
259  switch( orgOpt )
260  {
262  break;
263 
265  cmdK2S.Append( " --drill-origin" );
266  break;
267 
269  cmdK2S.Append( " --grid-origin" );
270  break;
271 
273  {
274  xOrg = GetXOrg();
275  yOrg = GetYOrg();
276 
277  if( GetOrgUnitsChoice() == 1 )
278  {
279  // selected reference unit is in inches, and STEP units are mm
280  xOrg *= 25.4;
281  yOrg *= 25.4;
282  }
283 
285  cmdK2S.Append( wxString::Format( " --user-origin %.6fx%.6f", xOrg, yOrg ) );
286  }
287  break;
288 
290  {
291  EDA_RECT bbox = m_parent->GetBoard()->ComputeBoundingBox( true );
292  xOrg = Iu2Millimeter( bbox.GetCenter().x );
293  yOrg = Iu2Millimeter( bbox.GetCenter().y );
295  cmdK2S.Append( wxString::Format( " --user-origin %.6fx%.6f", xOrg, yOrg ) );
296  }
297  break;
298  }
299 
300  cmdK2S.Append( " -f -o " );
301  cmdK2S.Append( wxString::Format("\"%s\"", m_filePickerSTEP->GetPath() ) ); // input file path
302 
303  cmdK2S.Append( " " );
304  cmdK2S.Append( wxString::Format("\"%s\"", m_boardPath ) ); // output file path
305 
306  int result = 0;
307  bool success = false;
308  wxArrayString output, errors;
309  REPORTER& reporter = m_messagesPanel->Reporter();
310  reporter.Report( wxString::Format( _( "Executing '%s'" ), cmdK2S ), REPORTER::RPT_ACTION );
311 
312  {
313  wxBusyCursor dummy;
314  result = wxExecute( cmdK2S, output, errors, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE );
315  }
316 
317  // Check the output log for an indication of success,
318  // the value returned by wxExecute is not conclusive
319  for( auto& l : output )
320  {
321  if( l.Contains( "Done" ) )
322  {
323  success = true;
324  break;
325  }
326  }
327 
328  for( auto& err : errors )
329  reporter.Report( err, REPORTER::RPT_WARNING );
330 
331  if( result ) // Any troubles?
332  {
333  if( !success )
334  {
335  reporter.Report( _( "Unable to create STEP file. Check that the board has a "
336  "valid outline and models." ), REPORTER::RPT_ERROR );
337  }
338  else
339  {
340  reporter.Report( _( "STEP file has been created, but there are warnings." ),
342  }
343  }
344  else
345  {
346  reporter.Report( _( "STEP file has been created succesfully." ), REPORTER::RPT_INFO );
347  }
348 }
#define OPTKEY_STEP_UORG_X
double GetXOrg() const
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:199
void onUpdateXPos(wxUpdateUIEvent &aEvent) override
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
#define OPTKEY_STEP_NOVIRT
This file is part of the common library.
bool GetBoardPolygonOutlines(SHAPE_POLY_SET &aOutlines, wxString *aErrorText=NULL)
Function GetBoardPolygonOutlines Extracts the board outlines and build a closed polygon from lines...
EDA_RECT ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Function ComputeBoundingBox calculates the bounding box containing all board items (or board edge seg...
Class BOARD to handle a board.
void OnExportSTEP(wxCommandEvent &event)
Function OnExportSTEP Exports the current BOARD to a STEP assembly.
wxRadioButton * m_rbDrillAndPlotOrigin
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
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
#define OPTKEY_STEP_ORIGIN_OPT
void SetValue(const wxString &aValue) override
Set a new value in evaluator buffer, and display it in the wxTextCtrl.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
STEP_ORG_OPT GetOriginOption()
void onExportButton(wxCommandEvent &aEvent) override
wxFilePickerCtrl * m_filePickerSTEP
const wxString & GetFileName() const
Definition: class_board.h:234
Class SHAPE_POLY_SET.
REPORTER & Reporter()
returns the reporter object that reports to this panel
void onUpdateUnits(wxUpdateUIEvent &aEvent) override
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
string & err
Definition: json11.cpp:598
WX_HTML_REPORT_PANEL * m_messagesPanel
BOARD * GetBoard()
int GetOrgUnitsChoice() const
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.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)=0
Function Report is a pure virtual function to override in the derived object.
void onUpdateYPos(wxUpdateUIEvent &aEvent) override
#define OPTKEY_STEP_UORG_Y
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:303
DIALOG_EXPORT_STEP(PCB_EDIT_FRAME *aParent, const wxString &aBoardPath)
const wxPoint GetCenter() const
Definition: eda_rect.h:115