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-2015 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 <fctsys.h>
30 #include <wxPcbStruct.h>
31 #include <kiface_i.h>
32 #include <pcbnew.h>
33 #include <class_board.h>
34 
35 
36 /* the dialog to create VRML files, derived from DIALOG_EXPORT_3DFILE_BASE,
37  * created by wxFormBuilder
38  */
39 #include <dialog_export_vrml_base.h> // the wxFormBuilder header file
40 
41 #define OPTKEY_OUTPUT_UNIT wxT( "VrmlExportUnit" )
42 #define OPTKEY_3DFILES_OPT wxT( "VrmlExportCopyFiles" )
43 #define OPTKEY_USE_RELATIVE_PATHS wxT( "VrmlUseRelativePaths" )
44 #define OPTKEY_USE_PLAIN_PCB wxT( "VrmlUsePlainPCB" )
45 #define OPTKEY_VRML_REF_UNITS wxT( "VrmlRefUnits" )
46 #define OPTKEY_VRML_REF_X wxT( "VrmlRefX" )
47 #define OPTKEY_VRML_REF_Y wxT( "VrmlRefY" )
48 
49 
51 {
52 private:
54  wxConfigBase* m_config;
55  int m_unitsOpt; // Remember last units option
56  bool m_copy3DFilesOpt; // Remember last copy model files option
57  bool m_useRelativePathsOpt; // Remember last use absolute paths option
58  bool m_usePlainPCBOpt; // Remember last Plain Board option
59  int m_RefUnits; // Remember last units for Reference Point
60  double m_XRef; // Remember last X Reference Point
61  double m_YRef; // Remember last Y Reference Point
62 
63 public:
66  {
67  m_parent = parent;
68  m_config = Kiface().KifaceSettings();
69  m_filePicker->SetFocus();
70  m_config->Read( OPTKEY_OUTPUT_UNIT, &m_unitsOpt, 1 );
71  m_config->Read( OPTKEY_3DFILES_OPT, &m_copy3DFilesOpt, false );
72  m_config->Read( OPTKEY_USE_RELATIVE_PATHS, &m_useRelativePathsOpt, false );
73  m_config->Read( OPTKEY_USE_PLAIN_PCB, &m_usePlainPCBOpt, false );
74  m_config->Read( OPTKEY_VRML_REF_UNITS, &m_RefUnits, 0 );
75  m_config->Read( OPTKEY_VRML_REF_X, &m_XRef, 0.0 );
76  m_config->Read( OPTKEY_VRML_REF_Y, &m_YRef, 0.0 );
77  m_rbSelectUnits->SetSelection( m_unitsOpt );
78  m_cbCopyFiles->SetValue( m_copy3DFilesOpt );
79  m_cbUseRelativePaths->SetValue( m_useRelativePathsOpt );
80  m_cbPlainPCB->SetValue( m_usePlainPCBOpt );
81  m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
82  wxString tmpStr;
83  tmpStr << m_XRef;
84  m_VRML_Xref->SetValue( tmpStr );
85  tmpStr = wxT( "" );
86  tmpStr << m_YRef;
87  m_VRML_Yref->SetValue( tmpStr );
88  m_sdbSizer1OK->SetDefault();
89 
90  // Now all widgets have the size fixed, call FinishDialogSettings
92 
93  Connect( ID_USE_ABS_PATH, wxEVT_UPDATE_UI,
94  wxUpdateUIEventHandler( DIALOG_EXPORT_3DFILE::OnUpdateUseRelativePath ) );
95  }
96 
98  {
99  m_unitsOpt = GetUnits();
100  m_copy3DFilesOpt = GetCopyFilesOption();
101  m_config->Write( OPTKEY_OUTPUT_UNIT, m_unitsOpt );
102  m_config->Write( OPTKEY_3DFILES_OPT, m_copy3DFilesOpt );
103  m_config->Write( OPTKEY_USE_RELATIVE_PATHS, m_useRelativePathsOpt );
104  m_config->Write( OPTKEY_USE_PLAIN_PCB, m_usePlainPCBOpt );
105  m_config->Write( OPTKEY_VRML_REF_UNITS, m_VRML_RefUnitChoice->GetSelection() );
106  m_config->Write( OPTKEY_VRML_REF_X, m_VRML_Xref->GetValue() );
107  m_config->Write( OPTKEY_VRML_REF_Y, m_VRML_Yref->GetValue() );
108  };
109 
110  void SetSubdir( const wxString & aDir )
111  {
112  m_SubdirNameCtrl->SetValue( aDir );
113  }
114 
115  wxString GetSubdir3Dshapes()
116  {
117  return m_SubdirNameCtrl->GetValue();
118  }
119 
120  wxFilePickerCtrl* FilePicker()
121  {
122  return m_filePicker;
123  }
124 
126  {
127  return m_VRML_RefUnitChoice->GetSelection();
128  }
129 
130  double GetXRef()
131  {
132  return DoubleValueFromString( UNSCALED_UNITS, m_VRML_Xref->GetValue() );
133  }
134 
135  double GetYRef()
136  {
137  return DoubleValueFromString( UNSCALED_UNITS, m_VRML_Yref->GetValue() );
138  }
139 
140  int GetUnits()
141  {
142  return m_unitsOpt = m_rbSelectUnits->GetSelection();
143  }
144 
146  {
147  return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
148  }
149 
151  {
152  return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
153  }
154 
156  {
157  return m_usePlainPCBOpt = m_cbPlainPCB->GetValue();
158  }
159 
160  void OnUpdateUseRelativePath( wxUpdateUIEvent& event )
161  {
162  // Making path relative or absolute has no meaning when VRML files are not copied.
163  event.Enable( m_cbCopyFiles->GetValue() );
164  }
165 
166  virtual void OnFileChanged( wxFileDirPickerEvent& event ) override
167  {
168  // Clicking on file picker button changes the focus to the file picker button which
169  // is not the behavior we want. Set the focus back to the OK button so the next enter
170  // key press dismisses this dialog with the OK action.
171  m_sdbSizer1OK->SetFocus();
172  }
173 
174 };
175 
176 
177 void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
178 {
179  // These variables are static to keep info during the session.
180  static wxString subDirFor3Dshapes;
181  static wxString last_brdName; // the last board name used to build the vrml filename
182  static wxString last_vrmlName; // the last wrml file name built
183 
184  // If the board name has changed since the last export,
185  // do not use the old path, initialized by an other board
186  if( last_brdName.IsEmpty() || last_brdName != GetBoard()->GetFileName() )
187  {
188  last_brdName = GetBoard()->GetFileName();
189  last_vrmlName = last_brdName;
190  }
191 
192  if( subDirFor3Dshapes.IsEmpty() )
193  {
194  subDirFor3Dshapes = wxT( "shapes3D" );
195  }
196 
197  // The general VRML scale factor
198  // Assuming the VRML default unit is the mm
199  // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
200  double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
201 
202  // Build default file name, to display in the file picker
203  wxFileName fn = last_vrmlName;
204  fn.SetExt( wxT( "wrl" ) );
205 
206  DIALOG_EXPORT_3DFILE dlg( this );
207  dlg.FilePicker()->SetPath( fn.GetFullPath() );
208  dlg.SetSubdir( subDirFor3Dshapes );
209 
210  if( dlg.ShowModal() != wxID_OK )
211  return;
212 
213  double aXRef = dlg.GetXRef();
214  double aYRef = dlg.GetYRef();
215 
216  if( dlg.GetRefUnitsChoice() == 1 )
217  {
218  // selected reference unit is in inches
219  aXRef *= 25.4;
220  aYRef *= 25.4;
221  }
222 
223  double scale = scaleList[dlg.GetUnits()]; // final scale export
224  bool export3DFiles = dlg.GetCopyFilesOption();
225  bool useRelativePaths = dlg.GetUseRelativePathsOption();
226  bool usePlainPCB = dlg.GetUsePlainPCBOption();
227 
228  last_vrmlName = dlg.FilePicker()->GetPath();
229  wxFileName modelPath = last_vrmlName;
230  wxBusyCursor dummy;
231 
232  subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
233  modelPath.AppendDir( subDirFor3Dshapes );
234 
235  if( export3DFiles && !modelPath.DirExists() )
236  {
237  modelPath.Mkdir();
238  }
239 
240  if( !ExportVRML_File( last_vrmlName, scale, export3DFiles, useRelativePaths,
241  usePlainPCB, modelPath.GetPath(), aXRef, aYRef ) )
242  {
243  wxString msg;
244  msg.Printf( _( "Unable to create file '%s'" ), GetChars( last_vrmlName ) );
245  wxMessageBox( msg );
246  return;
247  }
248 }
#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)
PCB_EDIT_FRAME * m_parent
void OnUpdateUseRelativePath(wxUpdateUIEvent &event)
#define OPTKEY_USE_PLAIN_PCB
const wxString & GetFileName() const
Definition: class_board.h:234
#define OPTKEY_VRML_REF_UNITS
virtual void OnFileChanged(wxFileDirPickerEvent &event) override
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.
#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