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 <wxPcbStruct.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  {
69  m_config = Kiface().KifaceSettings();
70  m_filePicker->SetFocus();
71  m_config->Read( OPTKEY_OUTPUT_UNIT, &m_unitsOpt, 1 );
72  m_config->Read( OPTKEY_3DFILES_OPT, &m_copy3DFilesOpt, false );
73  m_config->Read( OPTKEY_USE_RELATIVE_PATHS, &m_useRelativePathsOpt, false );
74  m_config->Read( OPTKEY_USE_PLAIN_PCB, &m_usePlainPCBOpt, false );
75  m_config->Read( OPTKEY_VRML_REF_UNITS, &m_RefUnits, 0 );
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 );
80  m_cbUseRelativePaths->SetValue( m_useRelativePathsOpt );
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();
101  m_copy3DFilesOpt = GetCopyFilesOption();
102  m_config->Write( OPTKEY_OUTPUT_UNIT, m_unitsOpt );
103  m_config->Write( OPTKEY_3DFILES_OPT, m_copy3DFilesOpt );
104  m_config->Write( OPTKEY_USE_RELATIVE_PATHS, m_useRelativePathsOpt );
105  m_config->Write( OPTKEY_USE_PLAIN_PCB, m_usePlainPCBOpt );
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 exiting file(s)?" ),
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  static wxString last_brdName; // the last board name used to build the vrml filename
191  static wxString last_vrmlName; // the last wrml file name built
192 
193  // If the board name has changed since the last export,
194  // do not use the old path, initialized by an other board
195  if( last_brdName.IsEmpty() || last_brdName != GetBoard()->GetFileName() )
196  {
197  last_brdName = GetBoard()->GetFileName();
198  last_vrmlName = last_brdName;
199  }
200 
201  if( subDirFor3Dshapes.IsEmpty() )
202  {
203  subDirFor3Dshapes = wxT( "shapes3D" );
204  }
205 
206  // The general VRML scale factor
207  // Assuming the VRML default unit is the mm
208  // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
209  double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
210 
211  // Build default file name, to display in the file picker
212  wxFileName fn = last_vrmlName;
213  fn.SetExt( wxT( "wrl" ) );
214 
215  DIALOG_EXPORT_3DFILE dlg( this );
216  dlg.FilePicker()->SetPath( fn.GetFullPath() );
217  dlg.SetSubdir( subDirFor3Dshapes );
218 
219  if( dlg.ShowModal() != wxID_OK )
220  return;
221 
222  double aXRef = dlg.GetXRef();
223  double aYRef = dlg.GetYRef();
224 
225  if( dlg.GetRefUnitsChoice() == 1 )
226  {
227  // selected reference unit is in inches
228  aXRef *= 25.4;
229  aYRef *= 25.4;
230  }
231 
232  double scale = scaleList[dlg.GetUnits()]; // final scale export
233  bool export3DFiles = dlg.GetCopyFilesOption();
234  bool useRelativePaths = dlg.GetUseRelativePathsOption();
235  bool usePlainPCB = dlg.GetUsePlainPCBOption();
236 
237  last_vrmlName = dlg.FilePicker()->GetPath();
238  wxFileName modelPath = last_vrmlName;
239  wxBusyCursor dummy;
240 
241  subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
242  modelPath.AppendDir( subDirFor3Dshapes );
243 
244  if( export3DFiles && !modelPath.DirExists() )
245  {
246  modelPath.Mkdir();
247  }
248 
249  if( !ExportVRML_File( last_vrmlName, scale, export3DFiles, useRelativePaths,
250  usePlainPCB, modelPath.GetPath(), aXRef, aYRef ) )
251  {
252  wxString msg;
253  msg.Printf( _( "Unable to create file '%s'" ), GetChars( last_vrmlName ) );
254  wxMessageBox( msg );
255  return;
256  }
257 }
#define OPTKEY_OUTPUT_UNIT
#define OPTKEY_3DFILES_OPT
Class BOARD to handle a board.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxFilePickerCtrl * FilePicker()
BOARD * GetBoard() const
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
DIALOG_EXPORT_3DFILE(PCB_EDIT_FRAME *parent)
void OnUpdateUseRelativePath(wxUpdateUIEvent &event)
#define OPTKEY_USE_PLAIN_PCB
const wxString & GetFileName() const
Definition: class_board.h:234
#define OPTKEY_VRML_REF_UNITS
Class DIALOG_EXPORT_3DFILE_BASE.
void SetSubdir(const wxString &aDir)
wxConfigBase * KifaceSettings() const
Definition: kiface_i.h:103
const int scale
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
#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 when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
#define OPTKEY_USE_RELATIVE_PATHS
void OnExportVRML(wxCommandEvent &event)
Function OnExportVRML will export the current BOARD to a VRML file.
bool TransferDataFromWindow() override
#define OPTKEY_VRML_REF_Y
double DoubleValueFromString(EDA_UNITS_T aUnits, const wxString &aTextValue)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:301