KiCad PCB EDA Suite
dialog_export_vrml.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) 2009-2013 Lorenzo Mercantonio
9  * Copyright (C) 2013 Jean-Pierre Charras jp.charras at wanadoo.fr
10  * Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, you may find one here:
24  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
25  * or you may search the http://www.gnu.org website for the version 2 license,
26  * or you may write to the Free Software Foundation, Inc.,
27  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 #include <wx/dir.h>
30 
31 #include <class_board.h>
32 #include <confirm.h>
33 #include <fctsys.h>
34 #include <kiface_i.h>
35 #include <pcb_edit_frame.h>
36 #include <pcbnew_settings.h>
37 #include <pcbnew.h>
38 
39 
40 /* the dialog to create VRML files, derived from DIALOG_EXPORT_3DFILE_BASE,
41  * created by wxFormBuilder
42  */
43 #include <dialog_export_vrml_base.h> // the wxFormBuilder header file
44 
45 
47 {
48 private:
50  int m_unitsOpt; // Remember last units option
51  bool m_copy3DFilesOpt; // Remember last copy model files option
52  bool m_useRelativePathsOpt; // Remember last use absolute paths option
53  bool m_usePlainPCBOpt; // Remember last Plain Board option
54  int m_RefUnits; // Remember last units for Reference Point
55  double m_XRef; // Remember last X Reference Point
56  double m_YRef; // Remember last Y Reference Point
57 
58 public:
60  DIALOG_EXPORT_3DFILE_BASE( parent ), m_parent( parent )
61  {
62  m_filePicker->SetFocus();
63 
64  auto cfg = m_parent->GetSettings();
65 
66  m_unitsOpt = cfg->m_ExportVrml.units;
67  m_copy3DFilesOpt = cfg->m_ExportVrml.copy_3d_models;
68  m_useRelativePathsOpt = cfg->m_ExportVrml.use_relative_paths;
69  m_usePlainPCBOpt = cfg->m_ExportVrml.use_plain_pcb;
70  m_RefUnits = cfg->m_ExportVrml.ref_units;
71  m_XRef = cfg->m_ExportVrml.ref_x;
72  m_YRef = cfg->m_ExportVrml.ref_y;
73 
74  m_rbSelectUnits->SetSelection( m_unitsOpt );
75  m_cbCopyFiles->SetValue( m_copy3DFilesOpt );
77  m_cbPlainPCB->SetValue( m_usePlainPCBOpt );
78  m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
79  wxString tmpStr;
80  tmpStr << m_XRef;
81  m_VRML_Xref->SetValue( tmpStr );
82  tmpStr = wxT( "" );
83  tmpStr << m_YRef;
84  m_VRML_Yref->SetValue( tmpStr );
85  m_sdbSizer1OK->SetDefault();
86 
87  // Now all widgets have the size fixed, call FinishDialogSettings
89 
90  Connect( ID_USE_ABS_PATH, wxEVT_UPDATE_UI,
91  wxUpdateUIEventHandler( DIALOG_EXPORT_3DFILE::OnUpdateUseRelativePath ) );
92  }
93 
95  {
96  m_unitsOpt = GetUnits();
98 
99  auto cfg = m_parent->GetSettings();
100 
102  cfg->m_ExportVrml.copy_3d_models = m_copy3DFilesOpt;
103  cfg->m_ExportVrml.use_relative_paths = m_useRelativePathsOpt;
104  cfg->m_ExportVrml.use_plain_pcb = m_usePlainPCBOpt;
105  cfg->m_ExportVrml.ref_units = m_VRML_RefUnitChoice->GetSelection();
106 
107  double val = 0.0;
108  m_VRML_Xref->GetValue().ToDouble( &val );
109  cfg->m_ExportVrml.ref_x = val;
110 
111  m_VRML_Yref->GetValue().ToDouble( &val );
112  cfg->m_ExportVrml.ref_y = val;
113  };
114 
115  void SetSubdir( const wxString & aDir )
116  {
117  m_SubdirNameCtrl->SetValue( aDir );
118  }
119 
120  wxString GetSubdir3Dshapes()
121  {
122  return m_SubdirNameCtrl->GetValue();
123  }
124 
125  wxFilePickerCtrl* FilePicker()
126  {
127  return m_filePicker;
128  }
129 
131  {
132  return m_VRML_RefUnitChoice->GetSelection();
133  }
134 
135  double GetXRef()
136  {
138  }
139 
140  double GetYRef()
141  {
143  }
144 
145  int GetUnits()
146  {
147  return m_unitsOpt = m_rbSelectUnits->GetSelection();
148  }
149 
151  {
152  return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
153  }
154 
156  {
157  return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
158  }
159 
161  {
162  return m_usePlainPCBOpt = m_cbPlainPCB->GetValue();
163  }
164 
165  void OnUpdateUseRelativePath( wxUpdateUIEvent& event )
166  {
167  // Making path relative or absolute has no meaning when VRML files are not copied.
168  event.Enable( m_cbCopyFiles->GetValue() );
169  }
170 
171  bool TransferDataFromWindow() override;
172 };
173 
174 
176 {
177  wxFileName fn = m_filePicker->GetPath();
178 
179  if( fn.Exists() )
180  {
181  if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
182  _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
183  return false;
184  }
185 
186  return true;
187 }
188 
189 
190 void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
191 {
192  // These variables are static to keep info during the session.
193  static wxString subDirFor3Dshapes;
194 
195  // Build default output file name
196  wxString path = GetLastPath( LAST_PATH_VRML );
197 
198  if( path.IsEmpty() )
199  {
200  wxFileName brdFile = GetBoard()->GetFileName();
201  brdFile.SetExt( "wrl" );
202  path = brdFile.GetFullPath();
203  }
204 
205  if( subDirFor3Dshapes.IsEmpty() )
206  subDirFor3Dshapes = wxT( "shapes3D" );
207 
208  // The general VRML scale factor
209  // Assuming the VRML default unit is the mm
210  // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
211  double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
212 
213  DIALOG_EXPORT_3DFILE dlg( this );
214  dlg.FilePicker()->SetPath( path );
215  dlg.SetSubdir( subDirFor3Dshapes );
216 
217  if( dlg.ShowModal() != wxID_OK )
218  return;
219 
220  double aXRef = dlg.GetXRef();
221  double aYRef = dlg.GetYRef();
222 
223  if( dlg.GetRefUnitsChoice() == 1 )
224  {
225  // selected reference unit is in inches
226  aXRef *= 25.4;
227  aYRef *= 25.4;
228  }
229 
230  double scale = scaleList[dlg.GetUnits()]; // final scale export
231  bool export3DFiles = dlg.GetCopyFilesOption();
232  bool useRelativePaths = dlg.GetUseRelativePathsOption();
233  bool usePlainPCB = dlg.GetUsePlainPCBOption();
234 
235  path = dlg.FilePicker()->GetPath();
236  SetLastPath( LAST_PATH_VRML, path );
237  wxFileName modelPath = path;
238 
239  wxBusyCursor dummy;
240 
241  subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
242  modelPath.AppendDir( subDirFor3Dshapes );
243 
244  if( export3DFiles && !modelPath.DirExists() )
245  {
246  if( !modelPath.Mkdir() )
247  {
248  wxString msg = wxString::Format(
249  _( "Unable to create directory \"%s\"" ), modelPath.GetPath() );
250  DisplayErrorMessage( this, msg );
251  return;
252  }
253  }
254 
255  if( !ExportVRML_File( path, scale, export3DFiles, useRelativePaths,
256  usePlainPCB, modelPath.GetPath(), aXRef, aYRef ) )
257  {
258  wxString msg = wxString::Format( _( "Unable to create file \"%s\"" ), path );
259  DisplayErrorMessage( this, msg );
260  return;
261  }
262 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:252
This file is part of the common library.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxFilePickerCtrl * FilePicker()
DIALOG_EXPORT_VRML m_ExportVrml
const wxString & GetFileName() const
Definition: class_board.h:218
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
DIALOG_EXPORT_3DFILE(PCB_EDIT_FRAME *parent)
PCB_EDIT_FRAME * m_parent
void OnUpdateUseRelativePath(wxUpdateUIEvent &event)
Class DIALOG_EXPORT_3DFILE_BASE.
void SetSubdir(const wxString &aDir)
const int scale
bool ExportVRML_File(const wxString &aFullFileName, double aMMtoWRMLunit, bool aExport3DFiles, bool aUseRelativePaths, bool aUsePlainPCB, const wxString &a3D_Subdir, double aXRef, double aYRef)
Function ExportVRML_File Creates the file(s) exporting current BOARD to a VRML file.
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.
#define _(s)
Definition: 3d_actions.cpp:33
void OnExportVRML(wxCommandEvent &event)
Function OnExportVRML will export the current BOARD to a VRML file.
PCB_EDIT_FRAME is the main frame for Pcbnew.
bool TransferDataFromWindow() override
BOARD * GetBoard() const
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:331
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
PCBNEW_SETTINGS * GetSettings()