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