KiCad PCB EDA Suite
dialog_export_vrml.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2009-2013 Lorenzo Mercantonio
5  * Copyright (C) 2013 Jean-Pierre Charras jp.charras at wanadoo.fr
6  * Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
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  int m_RefUnits; // Remember last units for Reference Point
53  double m_XRef; // Remember last X Reference Point
54  double m_YRef; // Remember last Y Reference Point
55  int m_originMode; // Origin selection option
56  // (0 = user, 1 = board center)
57 
58 public:
60  DIALOG_EXPORT_3DFILE_BASE( parent ), m_parent( parent )
61  {
62  m_filePicker->SetFocus();
63 
65 
70  m_XRef = cfg->m_ExportVrml.ref_x;
71  m_YRef = cfg->m_ExportVrml.ref_y;
73 
74 
75  m_rbCoordOrigin->SetSelection( m_originMode );
76  m_rbSelectUnits->SetSelection( m_unitsOpt );
77  m_cbCopyFiles->SetValue( m_copy3DFilesOpt );
79  m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
80  wxString tmpStr;
81  tmpStr << m_XRef;
82  m_VRML_Xref->SetValue( tmpStr );
83  tmpStr = wxT( "" );
84  tmpStr << m_YRef;
85  m_VRML_Yref->SetValue( tmpStr );
86  m_sdbSizerOK->SetDefault();
87 
88  // Now all widgets have the size fixed, call FinishDialogSettings
90  }
91 
93  {
94  m_unitsOpt = GetUnits();
96 
98 
102  cfg->m_ExportVrml.ref_units = m_VRML_RefUnitChoice->GetSelection();
103  cfg->m_ExportVrml.origin_mode = m_rbCoordOrigin->GetSelection();
104 
105  double val = 0.0;
106  m_VRML_Xref->GetValue().ToDouble( &val );
107  cfg->m_ExportVrml.ref_x = val;
108 
109  m_VRML_Yref->GetValue().ToDouble( &val );
110  cfg->m_ExportVrml.ref_y = val;
111  };
112 
113  void SetSubdir( const wxString & aDir )
114  {
115  m_SubdirNameCtrl->SetValue( aDir );
116  }
117 
118  wxString GetSubdir3Dshapes()
119  {
120  return m_SubdirNameCtrl->GetValue();
121  }
122 
123  wxFilePickerCtrl* FilePicker()
124  {
125  return m_filePicker;
126  }
127 
129  {
130  return m_VRML_RefUnitChoice->GetSelection();
131  }
132 
134  {
135  return m_rbCoordOrigin->GetSelection();
136  }
137 
138  double GetXRef()
139  {
141  }
142 
143  double GetYRef()
144  {
146  }
147 
148  int GetUnits()
149  {
150  return m_unitsOpt = m_rbSelectUnits->GetSelection();
151  }
152 
154  {
155  return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
156  }
157 
159  {
160  return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
161  }
162 
163  void OnUpdateUseRelativePath( wxUpdateUIEvent& event ) override
164  {
165  // Making path relative or absolute has no meaning when VRML files are not copied.
166  event.Enable( m_cbCopyFiles->GetValue() );
167  }
168 
169  bool TransferDataFromWindow() override;
170 };
171 
172 
174 {
175  wxFileName fn = m_filePicker->GetPath();
176 
177  if( fn.Exists() )
178  {
179  if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
180  _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
181  return false;
182  }
183 
184  return true;
185 }
186 
187 
188 void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
189 {
190  // These variables are static to keep info during the session.
191  static wxString subDirFor3Dshapes;
192 
193  // Build default output file name
194  wxString path = GetLastPath( LAST_PATH_VRML );
195 
196  if( path.IsEmpty() )
197  {
198  wxFileName brdFile = GetBoard()->GetFileName();
199  brdFile.SetExt( "wrl" );
200  path = brdFile.GetFullPath();
201  }
202 
203  if( subDirFor3Dshapes.IsEmpty() )
204  subDirFor3Dshapes = wxT( "shapes3D" );
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  DIALOG_EXPORT_3DFILE dlg( this );
212  dlg.FilePicker()->SetPath( path );
213  dlg.SetSubdir( subDirFor3Dshapes );
214 
215  if( dlg.ShowModal() != wxID_OK )
216  return;
217 
218  double aXRef = dlg.GetXRef();
219  double aYRef = dlg.GetYRef();
220 
221  if( dlg.GetRefUnitsChoice() == 1 )
222  {
223  // selected reference unit is in inches
224  aXRef *= 25.4;
225  aYRef *= 25.4;
226  }
227 
228  if( dlg.GetOriginChoice() == 1 )
229  {
230  // Origin = board center:
231  BOARD* pcb = GetBoard();
232  wxPoint center = pcb->GetBoundingBox().GetCenter();
233  aXRef = Iu2Millimeter( center.x );
234  aYRef = Iu2Millimeter( center.y );
235  }
236 
237  double scale = scaleList[dlg.GetUnits()]; // final scale export
238  bool export3DFiles = dlg.GetCopyFilesOption();
239  bool useRelativePaths = dlg.GetUseRelativePathsOption();
240 
241  path = dlg.FilePicker()->GetPath();
243  wxFileName modelPath = path;
244 
245  wxBusyCursor dummy;
246 
247  subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
248  modelPath.AppendDir( subDirFor3Dshapes );
249 
250  if( export3DFiles && !modelPath.DirExists() )
251  {
252  if( !modelPath.Mkdir() )
253  {
254  wxString msg = wxString::Format(
255  _( "Unable to create directory \"%s\"" ), modelPath.GetPath() );
256  DisplayErrorMessage( this, msg );
257  return;
258  }
259  }
260 
261  if( !ExportVRML_File( path, scale, export3DFiles, useRelativePaths,
262  modelPath.GetPath(), aXRef, aYRef ) )
263  {
264  wxString msg = wxString::Format( _( "Unable to create file \"%s\"" ), path );
265  DisplayErrorMessage( this, msg );
266  return;
267  }
268 }
const EDA_RECT GetBoundingBox() const override
Return the orthogonal bounding box of this object for display purposes.
Definition: board.h:728
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:265
This file is part of the common library.
wxFilePickerCtrl * FilePicker()
bool ExportVRML_File(const wxString &aFullFileName, double aMMtoWRMLunit, bool aExport3DFiles, bool aUseRelativePaths, const wxString &a3D_Subdir, double aXRef, double aYRef)
Create the file(s) exporting current BOARD to a VRML file.
DIALOG_EXPORT_VRML m_ExportVrml
const wxString & GetFileName() const
Definition: board.h:228
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
DIALOG_EXPORT_3DFILE(PCB_EDIT_FRAME *parent)
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:70
PCB_EDIT_FRAME * m_parent
Class DIALOG_EXPORT_3DFILE_BASE.
void SetSubdir(const wxString &aDir)
void OnUpdateUseRelativePath(wxUpdateUIEvent &event) override
#define _(s)
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
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
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
const wxPoint GetCenter() const
Definition: eda_rect.h:104
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:306
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.