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 <fctsys.h>
32 #include <pcb_edit_frame.h>
33 #include <kiface_i.h>
34 
35 #include <pcbnew.h>
36 #include <class_board.h>
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 #define OPTKEY_OUTPUT_UNIT wxT( "VrmlExportUnit" )
45 #define OPTKEY_3DFILES_OPT wxT( "VrmlExportCopyFiles" )
46 #define OPTKEY_USE_RELATIVE_PATHS wxT( "VrmlUseRelativePaths" )
47 #define OPTKEY_USE_PLAIN_PCB wxT( "VrmlUsePlainPCB" )
48 #define OPTKEY_VRML_REF_UNITS wxT( "VrmlRefUnits" )
49 #define OPTKEY_VRML_REF_X wxT( "VrmlRefX" )
50 #define OPTKEY_VRML_REF_Y wxT( "VrmlRefY" )
51 
52 
54 {
55 private:
56  wxConfigBase* m_config;
57  int m_unitsOpt; // Remember last units option
58  bool m_copy3DFilesOpt; // Remember last copy model files option
59  bool m_useRelativePathsOpt; // Remember last use absolute paths option
60  bool m_usePlainPCBOpt; // Remember last Plain Board option
61  int m_RefUnits; // Remember last units for Reference Point
62  double m_XRef; // Remember last X Reference Point
63  double m_YRef; // Remember last Y Reference Point
64 
65 public:
68  {
70  m_filePicker->SetFocus();
76  m_config->Read( OPTKEY_VRML_REF_X, &m_XRef, 0.0 );
77  m_config->Read( OPTKEY_VRML_REF_Y, &m_YRef, 0.0 );
78  m_rbSelectUnits->SetSelection( m_unitsOpt );
79  m_cbCopyFiles->SetValue( m_copy3DFilesOpt );
81  m_cbPlainPCB->SetValue( m_usePlainPCBOpt );
82  m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
83  wxString tmpStr;
84  tmpStr << m_XRef;
85  m_VRML_Xref->SetValue( tmpStr );
86  tmpStr = wxT( "" );
87  tmpStr << m_YRef;
88  m_VRML_Yref->SetValue( tmpStr );
89  m_sdbSizer1OK->SetDefault();
90 
91  // Now all widgets have the size fixed, call FinishDialogSettings
93 
94  Connect( ID_USE_ABS_PATH, wxEVT_UPDATE_UI,
95  wxUpdateUIEventHandler( DIALOG_EXPORT_3DFILE::OnUpdateUseRelativePath ) );
96  }
97 
99  {
100  m_unitsOpt = GetUnits();
106  m_config->Write( OPTKEY_VRML_REF_UNITS, m_VRML_RefUnitChoice->GetSelection() );
107  m_config->Write( OPTKEY_VRML_REF_X, m_VRML_Xref->GetValue() );
108  m_config->Write( OPTKEY_VRML_REF_Y, m_VRML_Yref->GetValue() );
109  };
110 
111  void SetSubdir( const wxString & aDir )
112  {
113  m_SubdirNameCtrl->SetValue( aDir );
114  }
115 
116  wxString GetSubdir3Dshapes()
117  {
118  return m_SubdirNameCtrl->GetValue();
119  }
120 
121  wxFilePickerCtrl* FilePicker()
122  {
123  return m_filePicker;
124  }
125 
127  {
128  return m_VRML_RefUnitChoice->GetSelection();
129  }
130 
131  double GetXRef()
132  {
133  return DoubleValueFromString( UNSCALED_UNITS, m_VRML_Xref->GetValue() );
134  }
135 
136  double GetYRef()
137  {
138  return DoubleValueFromString( UNSCALED_UNITS, m_VRML_Yref->GetValue() );
139  }
140 
141  int GetUnits()
142  {
143  return m_unitsOpt = m_rbSelectUnits->GetSelection();
144  }
145 
147  {
148  return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
149  }
150 
152  {
153  return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
154  }
155 
157  {
158  return m_usePlainPCBOpt = m_cbPlainPCB->GetValue();
159  }
160 
161  void OnUpdateUseRelativePath( wxUpdateUIEvent& event )
162  {
163  // Making path relative or absolute has no meaning when VRML files are not copied.
164  event.Enable( m_cbCopyFiles->GetValue() );
165  }
166 
167  bool TransferDataFromWindow() override;
168 };
169 
170 
172 {
173  wxFileName fn = m_filePicker->GetPath();
174 
175  if( fn.Exists() )
176  {
177  if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
178  _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
179  return false;
180  }
181 
182  return true;
183 }
184 
185 
186 void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
187 {
188  // These variables are static to keep info during the session.
189  static wxString subDirFor3Dshapes;
190 
191  // Build default output file name
192  wxString path = GetLastPath( LAST_PATH_VRML );
193 
194  if( path.IsEmpty() )
195  {
196  wxFileName brdFile = GetBoard()->GetFileName();
197  brdFile.SetExt( "wrl" );
198  path = brdFile.GetFullPath();
199  }
200 
201  if( subDirFor3Dshapes.IsEmpty() )
202  subDirFor3Dshapes = wxT( "shapes3D" );
203 
204  // The general VRML scale factor
205  // Assuming the VRML default unit is the mm
206  // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
207  double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
208 
209  DIALOG_EXPORT_3DFILE dlg( this );
210  dlg.FilePicker()->SetPath( path );
211  dlg.SetSubdir( subDirFor3Dshapes );
212 
213  if( dlg.ShowModal() != wxID_OK )
214  return;
215 
216  double aXRef = dlg.GetXRef();
217  double aYRef = dlg.GetYRef();
218 
219  if( dlg.GetRefUnitsChoice() == 1 )
220  {
221  // selected reference unit is in inches
222  aXRef *= 25.4;
223  aYRef *= 25.4;
224  }
225 
226  double scale = scaleList[dlg.GetUnits()]; // final scale export
227  bool export3DFiles = dlg.GetCopyFilesOption();
228  bool useRelativePaths = dlg.GetUseRelativePathsOption();
229  bool usePlainPCB = dlg.GetUsePlainPCBOption();
230 
231  path = dlg.FilePicker()->GetPath();
232  SetLastPath( LAST_PATH_VRML, path );
233  wxFileName modelPath = path;
234 
235  wxBusyCursor dummy;
236 
237  subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
238  modelPath.AppendDir( subDirFor3Dshapes );
239 
240  if( export3DFiles && !modelPath.DirExists() )
241  {
242  modelPath.Mkdir();
243  }
244 
245  if( !ExportVRML_File( path, scale, export3DFiles, useRelativePaths,
246  usePlainPCB, modelPath.GetPath(), aXRef, aYRef ) )
247  {
248  wxString msg;
249  msg.Printf( _( "Unable to create file \"%s\"" ), path );
250  wxMessageBox( msg );
251  return;
252  }
253 }
#define OPTKEY_OUTPUT_UNIT
#define OPTKEY_3DFILES_OPT
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxFilePickerCtrl * FilePicker()
const wxString & GetFileName() const
Definition: class_board.h:216
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:51
DIALOG_EXPORT_3DFILE(PCB_EDIT_FRAME *parent)
void OnUpdateUseRelativePath(wxUpdateUIEvent &event)
#define OPTKEY_USE_PLAIN_PCB
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue, bool aUseMils)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:314
#define OPTKEY_VRML_REF_UNITS
Class DIALOG_EXPORT_3DFILE_BASE.
void SetSubdir(const wxString &aDir)
#define _(s)
const int scale
#define OPTKEY_VRML_REF_X
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.
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
#define OPTKEY_USE_RELATIVE_PATHS
void OnExportVRML(wxCommandEvent &event)
Function OnExportVRML will export the current BOARD to a VRML file.
Class PCB_EDIT_FRAME is the main frame for Pcbnew.
bool TransferDataFromWindow() override
#define OPTKEY_VRML_REF_Y
BOARD * GetBoard() const
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.