KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <base_units.h>
32#include <board.h>
33#include <confirm.h>
34#include <kiface_base.h>
35#include <pcb_edit_frame.h>
36#include <pcbnew_settings.h>
37#include <project/project_file.h> // LAST_PATH_TYPE
38
39
40/* the dialog to create VRML files, derived from DIALOG_EXPORT_3DFILE_BASE,
41 * created by wxFormBuilder
42 */
43#include <dialog_export_vrml_base.h> // the wxFormBuilder header file
44
45
47{
48private:
50 int m_unitsOpt; // Remember last units option
51 bool m_copy3DFilesOpt; // Remember last copy model files option
52 bool m_useRelativePathsOpt; // Remember last use absolute paths 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 int m_originMode; // Origin selection option
57 // (0 = user, 1 = board center)
58
59public:
61 DIALOG_EXPORT_3DFILE_BASE( parent ), m_parent( parent )
62 {
63 m_filePicker->SetFocus();
64
66
74
75
76 m_rbCoordOrigin->SetSelection( m_originMode );
77 m_rbSelectUnits->SetSelection( m_unitsOpt );
80 m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
81 wxString tmpStr;
82 tmpStr << m_XRef;
83 m_VRML_Xref->SetValue( tmpStr );
84 tmpStr = wxT( "" );
85 tmpStr << m_YRef;
86 m_VRML_Yref->SetValue( tmpStr );
87
89
90 // Now all widgets have the size fixed, call FinishDialogSettings
92 }
93
95 {
98
100
104 cfg->m_ExportVrml.ref_units = m_VRML_RefUnitChoice->GetSelection();
105 cfg->m_ExportVrml.origin_mode = m_rbCoordOrigin->GetSelection();
106
107 double val = 0.0;
108 m_VRML_Xref->GetValue().ToDouble( &val );
109 cfg->m_ExportVrml.ref_x = val;
110
111 m_VRML_Yref->GetValue().ToDouble( &val );
112 cfg->m_ExportVrml.ref_y = val;
113 };
114
115 void SetSubdir( const wxString & aDir )
116 {
117 m_SubdirNameCtrl->SetValue( aDir );
118 }
119
121 {
122 return m_SubdirNameCtrl->GetValue();
123 }
124
125 wxFilePickerCtrl* FilePicker()
126 {
127 return m_filePicker;
128 }
129
131 {
132 return m_VRML_RefUnitChoice->GetSelection();
133 }
134
136 {
137 return m_rbCoordOrigin->GetSelection();
138 }
139
140 double GetXRef()
141 {
143 }
144
145 double GetYRef()
146 {
148 }
149
151 {
152 return m_unitsOpt = m_rbSelectUnits->GetSelection();
153 }
154
156 {
157 return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
158 }
159
161 {
162 return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
163 }
164
165 void OnUpdateUseRelativePath( wxUpdateUIEvent& event ) override
166 {
167 // Making path relative or absolute has no meaning when VRML files are not copied.
168 event.Enable( m_cbCopyFiles->GetValue() );
169 }
170
171 bool TransferDataFromWindow() override;
172};
173
174
176{
177 wxFileName fn = m_filePicker->GetPath();
178
179 if( fn.Exists() )
180 {
181 if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
182 _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
183 return false;
184 }
185
186 return true;
187}
188
189
190void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
191{
192 // These variables are static to keep info during the session.
193 static wxString subDirFor3Dshapes;
194
195 // Build default output file name
196 wxString path = GetLastPath( LAST_PATH_VRML );
197
198 if( path.IsEmpty() )
199 {
200 wxFileName brdFile = GetBoard()->GetFileName();
201 brdFile.SetExt( wxT( "wrl" ) );
202 path = brdFile.GetFullPath();
203 }
204
205 if( subDirFor3Dshapes.IsEmpty() )
206 subDirFor3Dshapes = wxT( "shapes3D" );
207
208 // The general VRML scale factor
209 // Assuming the VRML default unit is the mm
210 // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
211 double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
212
213 DIALOG_EXPORT_3DFILE dlg( this );
214 dlg.FilePicker()->SetPath( path );
215 dlg.SetSubdir( subDirFor3Dshapes );
216
217 if( dlg.ShowModal() != wxID_OK )
218 return;
219
220 double aXRef = dlg.GetXRef();
221 double aYRef = dlg.GetYRef();
222
223 if( dlg.GetRefUnitsChoice() == 1 )
224 {
225 // selected reference unit is in inches
226 aXRef *= 25.4;
227 aYRef *= 25.4;
228 }
229
230 if( dlg.GetOriginChoice() == 1 )
231 {
232 // Origin = board center:
233 BOARD* pcb = GetBoard();
234 BOX2I bbox = pcb->ComputeBoundingBox( true );
235 aXRef = pcbIUScale.IUTomm( bbox.GetCenter().x );
236 aYRef = pcbIUScale.IUTomm( bbox.GetCenter().y );
237 }
238
239 double scale = scaleList[dlg.GetUnits()]; // final scale export
240 bool export3DFiles = dlg.GetCopyFilesOption();
241 bool useRelativePaths = dlg.GetUseRelativePathsOption();
242
243 path = dlg.FilePicker()->GetPath();
245 wxFileName modelPath = path;
246
247 wxBusyCursor dummy;
248
249 subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
250 modelPath.AppendDir( subDirFor3Dshapes );
251
252 if( export3DFiles && !modelPath.DirExists() )
253 {
254 if( !modelPath.Mkdir() )
255 {
256 wxString msg = wxString::Format( _( "Failed to create folder '%s'." ),
257 modelPath.GetPath() );
258 DisplayErrorMessage( this, msg );
259 return;
260 }
261 }
262
263 if( !ExportVRML_File( path, scale, export3DFiles, useRelativePaths,
264 modelPath.GetPath(), aXRef, aYRef ) )
265 {
266 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), path );
267 DisplayErrorMessage( this, msg );
268 return;
269 }
270}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1329
const wxString & GetFileName() const
Definition: board.h:308
const Vec GetCenter() const
Definition: box2.h:196
Class DIALOG_EXPORT_3DFILE_BASE.
bool TransferDataFromWindow() override
DIALOG_EXPORT_3DFILE(PCB_EDIT_FRAME *parent)
PCB_EDIT_FRAME * m_parent
void SetSubdir(const wxString &aDir)
void OnUpdateUseRelativePath(wxUpdateUIEvent &event) override
wxFilePickerCtrl * FilePicker()
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
DIALOG_EXPORT_VRML m_ExportVrml
PCBNEW_SETTINGS * GetPcbNewSettings() const
BOARD * GetBoard() const
The main frame for Pcbnew.
void SetLastPath(LAST_PATH_TYPE aType, const wxString &aLastPath)
Set the path of the last file successfully read.
wxString GetLastPath(LAST_PATH_TYPE aType)
Get the last path for a particular type.
void OnExportVRML(wxCommandEvent &event)
Export the current BOARD to a VRML file.
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.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
#define _(s)
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:445
@ LAST_PATH_VRML
Definition: project_file.h:52
const int scale
std::vector< FAB_LAYER_COLOR > dummy
constexpr double IUTomm(int iu) const
Definition: base_units.h:87