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-2023 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{
48public:
50 DIALOG_EXPORT_3DFILE_BASE( parent ), m_parent( parent )
51 {
52 m_filePicker->SetFocus();
53
55
63
64
65 m_rbCoordOrigin->SetSelection( m_originMode );
66 m_rbSelectUnits->SetSelection( m_unitsOpt );
69 m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
70 wxString tmpStr;
71 tmpStr << m_XRef;
72 m_VRML_Xref->SetValue( tmpStr );
73 tmpStr = wxT( "" );
74 tmpStr << m_YRef;
75 m_VRML_Yref->SetValue( tmpStr );
76
78
79 // Now all widgets have the size fixed, call FinishDialogSettings
81 }
82
84 {
87
88 PCBNEW_SETTINGS* cfg = nullptr;
89
90 try
91 {
93 }
94 catch( const std::runtime_error& e )
95 {
96 wxFAIL_MSG( e.what() );
97 }
98
99 if( cfg )
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
116 void SetSubdir( const wxString & aDir )
117 {
118 m_SubdirNameCtrl->SetValue( aDir );
119 }
120
122 {
123 return m_SubdirNameCtrl->GetValue();
124 }
125
126 wxFilePickerCtrl* FilePicker()
127 {
128 return m_filePicker;
129 }
130
132 {
133 return m_VRML_RefUnitChoice->GetSelection();
134 }
135
137 {
138 return m_rbCoordOrigin->GetSelection();
139 }
140
141 double GetXRef()
142 {
144 }
145
146 double GetYRef()
147 {
149 }
150
152 {
153 return m_unitsOpt = m_rbSelectUnits->GetSelection();
154 }
155
157 {
158 return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
159 }
160
162 {
163 return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
164 }
165
166 void OnUpdateUseRelativePath( wxUpdateUIEvent& event ) override
167 {
168 // Making path relative or absolute has no meaning when VRML files are not copied.
169 event.Enable( m_cbCopyFiles->GetValue() );
170 }
171
172 bool TransferDataFromWindow() override;
173
174private:
176 int m_unitsOpt; // Remember last units option
177 bool m_copy3DFilesOpt; // Remember last copy model files option
178 bool m_useRelativePathsOpt; // Remember last use absolute paths option
179 int m_RefUnits; // Remember last units for Reference Point
180 double m_XRef; // Remember last X Reference Point
181 double m_YRef; // Remember last Y Reference Point
182 int m_originMode; // Origin selection option
183 // (0 = user, 1 = board center)
184};
185
186
188{
189 wxFileName fn = m_filePicker->GetPath();
190
191 if( fn.Exists() )
192 {
193 if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
194 _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
195 return false;
196 }
197
198 return true;
199}
200
201
202void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
203{
204 // These variables are static to keep info during the session.
205 static wxString subDirFor3Dshapes;
206
207 // Build default output file name
208 wxString path = GetLastPath( LAST_PATH_VRML );
209
210 if( path.IsEmpty() )
211 {
212 wxFileName brdFile = GetBoard()->GetFileName();
213 brdFile.SetExt( wxT( "wrl" ) );
214 path = brdFile.GetFullPath();
215 }
216
217 if( subDirFor3Dshapes.IsEmpty() )
218 subDirFor3Dshapes = wxT( "shapes3D" );
219
220 // The general VRML scale factor
221 // Assuming the VRML default unit is the mm
222 // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
223 double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
224
225 DIALOG_EXPORT_3DFILE dlg( this );
226 dlg.FilePicker()->SetPath( path );
227 dlg.SetSubdir( subDirFor3Dshapes );
228
229 if( dlg.ShowModal() != wxID_OK )
230 return;
231
232 double aXRef = dlg.GetXRef();
233 double aYRef = dlg.GetYRef();
234
235 if( dlg.GetRefUnitsChoice() == 1 )
236 {
237 // selected reference unit is in inches
238 aXRef *= 25.4;
239 aYRef *= 25.4;
240 }
241
242 if( dlg.GetOriginChoice() == 1 )
243 {
244 // Origin = board center:
245 BOARD* pcb = GetBoard();
246 BOX2I bbox = pcb->ComputeBoundingBox( true );
247 aXRef = pcbIUScale.IUTomm( bbox.GetCenter().x );
248 aYRef = pcbIUScale.IUTomm( bbox.GetCenter().y );
249 }
250
251 double scale = scaleList[dlg.GetUnits()]; // final scale export
252 bool export3DFiles = dlg.GetCopyFilesOption();
253 bool useRelativePaths = dlg.GetUseRelativePathsOption();
254
255 path = dlg.FilePicker()->GetPath();
257 wxFileName modelPath = path;
258
259 wxBusyCursor dummy;
260
261 subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
262 modelPath.AppendDir( subDirFor3Dshapes );
263
264 if( export3DFiles && !modelPath.DirExists() )
265 {
266 if( !modelPath.Mkdir() )
267 {
268 wxString msg = wxString::Format( _( "Failed to create folder '%s'." ),
269 modelPath.GetPath() );
270 DisplayErrorMessage( this, msg );
271 return;
272 }
273 }
274
275 if( !ExportVRML_File( path, scale, export3DFiles, useRelativePaths,
276 modelPath.GetPath(), aXRef, aYRef ) )
277 {
278 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), path );
279 DisplayErrorMessage( this, msg );
280 return;
281 }
282}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:109
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1390
const wxString & GetFileName() const
Definition: board.h:313
const Vec GetCenter() const
Definition: box2.h:196
Class DIALOG_EXPORT_3DFILE_BASE.
bool TransferDataFromWindow() override
DIALOG_EXPORT_3DFILE(PCB_EDIT_FRAME *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:565
@ 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