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
65
66
67 m_rbCoordOrigin->SetSelection( m_originMode );
68 m_rbSelectUnits->SetSelection( m_unitsOpt );
70 m_cbRemoveDNP->SetValue( m_noDNP );
73 m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
74 wxString tmpStr;
75 tmpStr << m_XRef;
76 m_VRML_Xref->SetValue( tmpStr );
77 tmpStr = wxT( "" );
78 tmpStr << m_YRef;
79 m_VRML_Yref->SetValue( tmpStr );
80
82
83 // Now all widgets have the size fixed, call FinishDialogSettings
85 }
86
88 {
93
94 PCBNEW_SETTINGS* cfg = nullptr;
95
96 try
97 {
99 }
100 catch( const std::runtime_error& e )
101 {
102 wxFAIL_MSG( e.what() );
103 }
104
105 if( cfg )
106 {
112 cfg->m_ExportVrml.ref_units = m_VRML_RefUnitChoice->GetSelection();
113 cfg->m_ExportVrml.origin_mode = m_rbCoordOrigin->GetSelection();
114
115 double val = 0.0;
116 m_VRML_Xref->GetValue().ToDouble( &val );
117 cfg->m_ExportVrml.ref_x = val;
118
119 m_VRML_Yref->GetValue().ToDouble( &val );
120 cfg->m_ExportVrml.ref_y = val;
121 }
122 };
123
124 void SetSubdir( const wxString & aDir )
125 {
126 m_SubdirNameCtrl->SetValue( aDir );
127 }
128
130 {
131 return m_SubdirNameCtrl->GetValue();
132 }
133
134 wxFilePickerCtrl* FilePicker()
135 {
136 return m_filePicker;
137 }
138
140 {
141 return m_VRML_RefUnitChoice->GetSelection();
142 }
143
145 {
146 return m_rbCoordOrigin->GetSelection();
147 }
148
149 double GetXRef()
150 {
152 }
153
154 double GetYRef()
155 {
157 }
158
160 {
161 return m_unitsOpt = m_rbSelectUnits->GetSelection();
162 }
163
165 {
166 return m_cbRemoveUnspecified->GetValue();
167 }
168
170 {
171 return m_cbRemoveDNP->GetValue();
172 }
173
175 {
176 return m_copy3DFilesOpt = m_cbCopyFiles->GetValue();
177 }
178
180 {
181 return m_useRelativePathsOpt = m_cbUseRelativePaths->GetValue();
182 }
183
184 void OnUpdateUseRelativePath( wxUpdateUIEvent& event ) override
185 {
186 // Making path relative or absolute has no meaning when VRML files are not copied.
187 event.Enable( m_cbCopyFiles->GetValue() );
188 }
189
190 bool TransferDataFromWindow() override;
191
192private:
194 int m_unitsOpt; // Remember last units option
195 bool m_noUnspecified; // Remember last No Unspecified Component option
196 bool m_noDNP; // Remember last No DNP Component option
197 bool m_copy3DFilesOpt; // Remember last copy model files option
198 bool m_useRelativePathsOpt; // Remember last use absolute paths option
199 int m_RefUnits; // Remember last units for Reference Point
200 double m_XRef; // Remember last X Reference Point
201 double m_YRef; // Remember last Y Reference Point
202 int m_originMode; // Origin selection option
203 // (0 = user, 1 = board center)
204};
205
206
208{
209 wxFileName fn = m_filePicker->GetPath();
210
211 if( fn.Exists() )
212 {
213 if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
214 _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
215 return false;
216 }
217
218 return true;
219}
220
221
222void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
223{
224 // These variables are static to keep info during the session.
225 static wxString subDirFor3Dshapes;
226
227 // Build default output file name
228 wxString path = GetLastPath( LAST_PATH_VRML );
229
230 if( path.IsEmpty() )
231 {
232 wxFileName brdFile = GetBoard()->GetFileName();
233 brdFile.SetExt( wxT( "wrl" ) );
234 path = brdFile.GetFullPath();
235 }
236
237 if( subDirFor3Dshapes.IsEmpty() )
238 subDirFor3Dshapes = wxT( "shapes3D" );
239
240 // The general VRML scale factor
241 // Assuming the VRML default unit is the mm
242 // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
243 double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
244
245 DIALOG_EXPORT_3DFILE dlg( this );
246 dlg.FilePicker()->SetPath( path );
247 dlg.SetSubdir( subDirFor3Dshapes );
248
249 if( dlg.ShowModal() != wxID_OK )
250 return;
251
252 double aXRef = dlg.GetXRef();
253 double aYRef = dlg.GetYRef();
254
255 if( dlg.GetRefUnitsChoice() == 1 )
256 {
257 // selected reference unit is in inches
258 aXRef *= 25.4;
259 aYRef *= 25.4;
260 }
261
262 if( dlg.GetOriginChoice() == 1 )
263 {
264 // Origin = board center:
265 BOARD* pcb = GetBoard();
266 BOX2I bbox = pcb->ComputeBoundingBox( true );
267 aXRef = pcbIUScale.IUTomm( bbox.GetCenter().x );
268 aYRef = pcbIUScale.IUTomm( bbox.GetCenter().y );
269 }
270
271 double scale = scaleList[dlg.GetUnits()]; // final scale export
272 bool includeUnspecified = !dlg.GetNoUnspecifiedOption();
273 bool includeDNP = !dlg.GetNoDNPOption();
274 bool export3DFiles = dlg.GetCopyFilesOption();
275 bool useRelativePaths = dlg.GetUseRelativePathsOption();
276
277 path = dlg.FilePicker()->GetPath();
279 wxFileName modelPath = path;
280
281 wxBusyCursor dummy;
282
283 subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
284 modelPath.AppendDir( subDirFor3Dshapes );
285
286 if( export3DFiles && !modelPath.DirExists() )
287 {
288 if( !modelPath.Mkdir() )
289 {
290 wxString msg = wxString::Format( _( "Failed to create folder '%s'." ),
291 modelPath.GetPath() );
292 DisplayErrorMessage( this, msg );
293 return;
294 }
295 }
296
297 if( !ExportVRML_File( path, scale, includeUnspecified, includeDNP, export3DFiles,
298 useRelativePaths, modelPath.GetPath(), aXRef, aYRef ) )
299 {
300 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), path );
301 DisplayErrorMessage( this, msg );
302 return;
303 }
304}
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:1425
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 aIncludeUnspecified, bool aIncludeDNP, 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