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 <board.h>
32 #include <confirm.h>
33 #include <kiface_i.h>
34 #include <pcb_edit_frame.h>
35 #include <pcbnew_settings.h>
36 #include <project/project_file.h> // LAST_PATH_TYPE
37 
38 
39 /* the dialog to create VRML files, derived from DIALOG_EXPORT_3DFILE_BASE,
40  * created by wxFormBuilder
41  */
42 #include <dialog_export_vrml_base.h> // the wxFormBuilder header file
43 
44 
46 {
47 private:
49  int m_unitsOpt; // Remember last units option
50  bool m_copy3DFilesOpt; // Remember last copy model files option
51  bool m_useRelativePathsOpt; // Remember last use absolute paths option
52  bool m_usePlainPCBOpt; // Remember last Plain Board option
53  int m_RefUnits; // Remember last units for Reference Point
54  double m_XRef; // Remember last X Reference Point
55  double m_YRef; // Remember last Y Reference Point
56 
57 public:
59  DIALOG_EXPORT_3DFILE_BASE( parent ), m_parent( parent )
60  {
61  m_filePicker->SetFocus();
62 
63  auto cfg = m_parent->GetPcbNewSettings();
64 
65  m_unitsOpt = cfg->m_ExportVrml.units;
66  m_copy3DFilesOpt = cfg->m_ExportVrml.copy_3d_models;
67  m_useRelativePathsOpt = cfg->m_ExportVrml.use_relative_paths;
68  m_usePlainPCBOpt = cfg->m_ExportVrml.use_plain_pcb;
69  m_RefUnits = cfg->m_ExportVrml.ref_units;
70  m_XRef = cfg->m_ExportVrml.ref_x;
71  m_YRef = cfg->m_ExportVrml.ref_y;
72 
73  m_rbSelectUnits->SetSelection( m_unitsOpt );
74  m_cbCopyFiles->SetValue( m_copy3DFilesOpt );
76  m_cbPlainPCB->SetValue( m_usePlainPCBOpt );
77  m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
78  wxString tmpStr;
79  tmpStr << m_XRef;
80  m_VRML_Xref->SetValue( tmpStr );
81  tmpStr = wxT( "" );
82  tmpStr << m_YRef;
83  m_VRML_Yref->SetValue( tmpStr );
84  m_sdbSizer1OK->SetDefault();
85 
86  // Now all widgets have the size fixed, call FinishDialogSettings
88 
89  Connect( ID_USE_ABS_PATH, wxEVT_UPDATE_UI,
90  wxUpdateUIEventHandler( DIALOG_EXPORT_3DFILE::OnUpdateUseRelativePath ) );
91  }
92 
94  {
95  m_unitsOpt = GetUnits();
97 
98  auto cfg = m_parent->GetPcbNewSettings();
99 
101  cfg->m_ExportVrml.copy_3d_models = m_copy3DFilesOpt;
102  cfg->m_ExportVrml.use_relative_paths = m_useRelativePathsOpt;
103  cfg->m_ExportVrml.use_plain_pcb = m_usePlainPCBOpt;
104  cfg->m_ExportVrml.ref_units = m_VRML_RefUnitChoice->GetSelection();
105 
106  double val = 0.0;
107  m_VRML_Xref->GetValue().ToDouble( &val );
108  cfg->m_ExportVrml.ref_x = val;
109 
110  m_VRML_Yref->GetValue().ToDouble( &val );
111  cfg->m_ExportVrml.ref_y = val;
112  };
113 
114  void SetSubdir( const wxString & aDir )
115  {
116  m_SubdirNameCtrl->SetValue( aDir );
117  }
118 
119  wxString GetSubdir3Dshapes()
120  {
121  return m_SubdirNameCtrl->GetValue();
122  }
123 
124  wxFilePickerCtrl* FilePicker()
125  {
126  return m_filePicker;
127  }
128 
130  {
131  return m_VRML_RefUnitChoice->GetSelection();
132  }
133 
134  double GetXRef()
135  {
137  }
138 
139  double GetYRef()
140  {
142  }
143 
144  int GetUnits()
145  {
146  return m_unitsOpt = m_rbSelectUnits->GetSelection();
147  }
148 
150  {
151  return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
152  }
153 
155  {
156  return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
157  }
158 
160  {
161  return m_usePlainPCBOpt = m_cbPlainPCB->GetValue();
162  }
163 
164  void OnUpdateUseRelativePath( wxUpdateUIEvent& event )
165  {
166  // Making path relative or absolute has no meaning when VRML files are not copied.
167  event.Enable( m_cbCopyFiles->GetValue() );
168  }
169 
170  bool TransferDataFromWindow() override;
171 };
172 
173 
175 {
176  wxFileName fn = m_filePicker->GetPath();
177 
178  if( fn.Exists() )
179  {
180  if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
181  _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
182  return false;
183  }
184 
185  return true;
186 }
187 
188 
189 void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
190 {
191  // These variables are static to keep info during the session.
192  static wxString subDirFor3Dshapes;
193 
194  // Build default output file name
195  wxString path = GetLastPath( LAST_PATH_VRML );
196 
197  if( path.IsEmpty() )
198  {
199  wxFileName brdFile = GetBoard()->GetFileName();
200  brdFile.SetExt( "wrl" );
201  path = brdFile.GetFullPath();
202  }
203 
204  if( subDirFor3Dshapes.IsEmpty() )
205  subDirFor3Dshapes = wxT( "shapes3D" );
206 
207  // The general VRML scale factor
208  // Assuming the VRML default unit is the mm
209  // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
210  double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
211 
212  DIALOG_EXPORT_3DFILE dlg( this );
213  dlg.FilePicker()->SetPath( path );
214  dlg.SetSubdir( subDirFor3Dshapes );
215 
216  if( dlg.ShowModal() != wxID_OK )
217  return;
218 
219  double aXRef = dlg.GetXRef();
220  double aYRef = dlg.GetYRef();
221 
222  if( dlg.GetRefUnitsChoice() == 1 )
223  {
224  // selected reference unit is in inches
225  aXRef *= 25.4;
226  aYRef *= 25.4;
227  }
228 
229  double scale = scaleList[dlg.GetUnits()]; // final scale export
230  bool export3DFiles = dlg.GetCopyFilesOption();
231  bool useRelativePaths = dlg.GetUseRelativePathsOption();
232  bool usePlainPCB = dlg.GetUsePlainPCBOption();
233 
234  path = dlg.FilePicker()->GetPath();
235  SetLastPath( LAST_PATH_VRML, path );
236  wxFileName modelPath = path;
237 
238  wxBusyCursor dummy;
239 
240  subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
241  modelPath.AppendDir( subDirFor3Dshapes );
242 
243  if( export3DFiles && !modelPath.DirExists() )
244  {
245  if( !modelPath.Mkdir() )
246  {
247  wxString msg = wxString::Format(
248  _( "Unable to create directory \"%s\"" ), modelPath.GetPath() );
249  DisplayErrorMessage( this, msg );
250  return;
251  }
252  }
253 
254  if( !ExportVRML_File( path, scale, export3DFiles, useRelativePaths,
255  usePlainPCB, modelPath.GetPath(), aXRef, aYRef ) )
256  {
257  wxString msg = wxString::Format( _( "Unable to create file \"%s\"" ), path );
258  DisplayErrorMessage( this, msg );
259  return;
260  }
261 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:266
This file is part of the common library.
wxFilePickerCtrl * FilePicker()
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
DIALOG_EXPORT_VRML m_ExportVrml
const wxString & GetFileName() const
Definition: board.h:298
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)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
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)
Creates the file(s) exporting current BOARD to a VRML file.
#define _(s)
Definition: 3d_actions.cpp:33
void OnExportVRML(wxCommandEvent &event)
Export the current BOARD to a VRML file.
The main frame for Pcbnew.
PCBNEW_SETTINGS * GetPcbNewSettings() const
bool TransferDataFromWindow() override
BOARD * GetBoard() const
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:338
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.