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  m_sdbSizerCancel->SetLabel( _( "Close" ) );
123  m_sdbSizer->Layout();
124 
125  // Build default output file name
126  wxFileName brdFile = m_parent->GetBoard()->GetFileName();
127  brdFile.SetExt( "step" );
128  m_filePickerSTEP->SetPath( brdFile.GetFullPath() );
129 
130  SetFocus();
131 
133  int tmp = STEP_ORG_0;
134 
135  if( m_config->Read( OPTKEY_STEP_ORIGIN_OPT, &tmp ) )
137 
138  switch( m_STEP_org_opt )
139  {
140  default: break;
141  case STEP_ORG_PLOT_AXIS: m_rbDrillAndPlotOrigin->SetValue( true ); break;
142  case STEP_ORG_GRID_AXIS: m_rbGridOrigin->SetValue( true ); break;
143  case STEP_ORG_USER: m_rbUserDefinedOrigin->SetValue( true ); break;
144  case STEP_ORG_BOARD_CENTER: m_rbBoardCenterOrigin->SetValue( true ); break;
145  }
146 
148  m_config->Read( OPTKEY_STEP_UORG_X, &m_XOrg, 0.0 );
149  m_config->Read( OPTKEY_STEP_UORG_Y, &m_YOrg, 0.0 );
151  m_cbRemoveVirtual->SetValue( m_noVirtual );
152 
153  m_STEP_OrgUnitChoice->SetSelection( m_OrgUnits );
154  wxString tmpStr;
155  tmpStr << m_XOrg;
156  m_STEP_Xorg->SetValue( tmpStr );
157  tmpStr = "";
158  tmpStr << m_YOrg;
159  m_STEP_Yorg->SetValue( tmpStr );
160 
161  // Now all widgets have the size fixed, call FinishDialogSettings
163 }
164 
165 
167 {
169 
170  if( m_rbDrillAndPlotOrigin->GetValue() )
172  else if( m_rbGridOrigin->GetValue() )
174  else if( m_rbUserDefinedOrigin->GetValue() )
176  else if( m_rbBoardCenterOrigin->GetValue() )
178 
179  return m_STEP_org_opt;
180 }
181 
182 
183 void PCB_EDIT_FRAME::OnExportSTEP( wxCommandEvent& event )
184 {
185  wxFileName brdFile = GetBoard()->GetFileName();
186 
187  if( GetScreen()->IsModify() || brdFile.GetFullPath().empty() )
188  {
189  if( !doAutoSave() )
190  {
191  DisplayErrorMessage( this,
192  _( "STEP export failed! Please save the PCB and try again" ) );
193  return;
194  }
195 
196  // Use auto-saved board for export
197  brdFile.SetName( GetAutoSaveFilePrefix() + brdFile.GetName() );
198  }
199 
200  DIALOG_EXPORT_STEP dlg( this, brdFile.GetFullPath() );
201  dlg.ShowModal();
202 }
203 
204 
205 void DIALOG_EXPORT_STEP::onUpdateUnits( wxUpdateUIEvent& aEvent )
206 {
207  aEvent.Enable( m_rbUserDefinedOrigin->GetValue() );
208 }
209 
210 
211 void DIALOG_EXPORT_STEP::onUpdateXPos( wxUpdateUIEvent& aEvent )
212 {
213  aEvent.Enable( m_rbUserDefinedOrigin->GetValue() );
214 }
215 
216 
217 void DIALOG_EXPORT_STEP::onUpdateYPos( wxUpdateUIEvent& aEvent )
218 {
219  aEvent.Enable( m_rbUserDefinedOrigin->GetValue() );
220 }
221 
222 extern bool BuildBoardPolygonOutlines( BOARD* aBoard, SHAPE_POLY_SET& aOutlines,
223  wxString* aErrorText, unsigned int aTolerance );
224 
225 void DIALOG_EXPORT_STEP::onExportButton( wxCommandEvent& aEvent )
226 {
227  SHAPE_POLY_SET outline;
228  wxString msg;
229 
230  // Check if the board outline is continuous
231  if( !BuildBoardPolygonOutlines( m_parent->GetBoard(), outline, &msg, Millimeter2iu( 0.01 ) ) )
232  {
233  DisplayErrorMessage( this, _( "Cannot determine the board outline." ), msg );
234  return;
235  }
236 
237  wxFileName fn = m_filePickerSTEP->GetFileName();
238 
239  if( fn.FileExists() )
240  {
241  msg.Printf( _( "File '%s' already exists. Do you want overwrite this file?" ),
242  fn.GetFullPath().GetData() );
243 
244  if( wxMessageBox( msg, _( "STEP Export" ), wxYES_NO | wxICON_QUESTION, this ) == wxNO )
245  return;
246  }
247 
249  double xOrg = 0.0;
250  double yOrg = 0.0;
251 
252  wxFileName appK2S( wxStandardPaths::Get().GetExecutablePath() );
253 
254 #ifdef __WXMAC__
255  // On macOS, we have standalone applications inside the main bundle, so we handle that here:
256  if( appK2S.GetPath().find( "/Contents/Applications/pcbnew.app/Contents/MacOS" ) != wxNOT_FOUND )
257  {
258  appK2S.AppendDir( ".." );
259  appK2S.AppendDir( ".." );
260  appK2S.AppendDir( ".." );
261  appK2S.AppendDir( ".." );
262  appK2S.AppendDir( "MacOS" );
263  }
264 #endif
265 
266  appK2S.SetName( "kicad2step" );
267 
268  wxString cmdK2S = "\"";
269  cmdK2S.Append( appK2S.GetFullPath() );
270  cmdK2S.Append( "\"" );
271 
272  if( GetNoVirtOption() )
273  cmdK2S.Append( " --no-virtual" );
274 
275  switch( orgOpt )
276  {
278  break;
279 
281  cmdK2S.Append( " --drill-origin" );
282  break;
283 
285  cmdK2S.Append( " --grid-origin" );
286  break;
287 
289  {
290  xOrg = GetXOrg();
291  yOrg = GetYOrg();
292 
293  if( GetOrgUnitsChoice() == 1 )
294  {
295  // selected reference unit is in inches, and STEP units are mm
296  xOrg *= 25.4;
297  yOrg *= 25.4;
298  }
299 
301  cmdK2S.Append( wxString::Format( " --user-origin %.6fx%.6f", xOrg, yOrg ) );
302  }
303  break;
304 
306  {
307  EDA_RECT bbox = m_parent->GetBoard()->ComputeBoundingBox( true );
308  xOrg = Iu2Millimeter( bbox.GetCenter().x );
309  yOrg = Iu2Millimeter( bbox.GetCenter().y );
311  cmdK2S.Append( wxString::Format( " --user-origin %.6fx%.6f", xOrg, yOrg ) );
312  }
313  break;
314  }
315 
316  cmdK2S.Append( " -f -o " );
317  cmdK2S.Append( wxString::Format("\"%s\"", m_filePickerSTEP->GetPath() ) ); // input file path
318 
319  cmdK2S.Append( " " );
320  cmdK2S.Append( wxString::Format("\"%s\"", m_boardPath ) ); // output file path
321 
322  int result = 0;
323  bool success = false;
324  wxArrayString output, errors;
325  REPORTER& reporter = m_messagesPanel->Reporter();
326  reporter.ReportHead( wxString::Format( _( "Executing '%s'" ), cmdK2S ), REPORTER::RPT_ACTION );
327 
328  {
329  wxBusyCursor dummy;
330  result = wxExecute( cmdK2S, output, errors, wxEXEC_SYNC | wxEXEC_HIDE_CONSOLE );
331  }
332 
333  // Check the output log for an indication of success,
334  // the value returned by wxExecute is not conclusive
335  for( auto& l : output )
336  {
337  if( l.Contains( "Done" ) )
338  {
339  success = true;
340  break;
341  }
342  }
343 
344  for( auto& err : errors )
345  reporter.Report( err, REPORTER::RPT_WARNING );
346 
347  if( result ) // Any troubles?
348  {
349  if( !success )
350  {
351  reporter.ReportTail( _( "Unable to create STEP file. Check that the board has a "
352  "valid outline and models." ), REPORTER::RPT_ERROR );
353  }
354  else
355  {
356  reporter.ReportTail( _( "STEP file has been created, but there are warnings." ),
358  }
359  }
360  else
361  {
362  reporter.ReportTail( _( "STEP file has been created successfully." ), REPORTER::RPT_INFO );
363  }
364 }
#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
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:167
#define OPTKEY_STEP_NOVIRT
This file is part of the common library.
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.
bool BuildBoardPolygonOutlines(BOARD *aBoard, SHAPE_POLY_SET &aOutlines, wxString *aErrorText, unsigned int aTolerance)
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
virtual REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)
Function ReportHead Places the report at the beginning of the list for objects that support ordering...
Definition: reporter.h:118
virtual REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_UNDEFINED)
Function ReportTail Places the report at the end of the list, for objects that support report orderin...
Definition: reporter.h:109
const wxString & GetFileName() const
Definition: class_board.h:236
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
wxStdDialogButtonSizer * m_sdbSizer
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 BOARD holds information pertinent to a Pcbnew printed circuit board.
Definition: class_board.h:169
Class DIALOG_EXPORT_STEP_BASE.
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
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