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#include <dialog_export_vrml.h>
41
42
44 DIALOG_EXPORT_VRML_BASE( aEditFrame ),
45 m_editFrame( aEditFrame )
46{
47 m_filePicker->SetFocus();
48
50
60
61
62 m_rbCoordOrigin->SetSelection( m_originMode );
63 m_rbSelectUnits->SetSelection( m_unitsOpt );
65 m_cbRemoveDNP->SetValue( m_noDNP );
68 m_VRML_RefUnitChoice->SetSelection( m_RefUnits );
69 wxString tmpStr;
70 tmpStr << m_XRef;
71 m_VRML_Xref->SetValue( tmpStr );
72 tmpStr = wxT( "" );
73 tmpStr << m_YRef;
74 m_VRML_Yref->SetValue( tmpStr );
75
77
78 // Now all widgets have the size fixed, call FinishDialogSettings
80}
81
82
84{
89
90 PCBNEW_SETTINGS* cfg = nullptr;
91
92 try
93 {
95 }
96 catch( const std::runtime_error& e )
97 {
98 wxFAIL_MSG( e.what() );
99 }
100
101 if( cfg )
102 {
108 cfg->m_ExportVrml.ref_units = m_VRML_RefUnitChoice->GetSelection();
109 cfg->m_ExportVrml.origin_mode = m_rbCoordOrigin->GetSelection();
110
111 double val = 0.0;
112 m_VRML_Xref->GetValue().ToDouble( &val );
113 cfg->m_ExportVrml.ref_x = val;
114
115 m_VRML_Yref->GetValue().ToDouble( &val );
116 cfg->m_ExportVrml.ref_y = val;
117 }
118}
119
120
122{
123 wxFileName fn = m_filePicker->GetPath();
124
125 if( fn.Exists() )
126 {
127 if( wxMessageBox( _( "Are you sure you want to overwrite the existing file?" ),
128 _( "Warning" ), wxYES_NO | wxCENTER | wxICON_QUESTION, this ) == wxNO )
129 return false;
130 }
131
132 return true;
133}
134
135
137{
139}
140
141
143{
145}
146
147
148void PCB_EDIT_FRAME::OnExportVRML( wxCommandEvent& event )
149{
150 // These variables are static to keep info during the session.
151 static wxString subDirFor3Dshapes;
152
153 // Build default output file name
154 wxString path = GetLastPath( LAST_PATH_VRML );
155
156 if( path.IsEmpty() )
157 {
158 wxFileName brdFile = GetBoard()->GetFileName();
159 brdFile.SetExt( wxT( "wrl" ) );
160 path = brdFile.GetFullPath();
161 }
162
163 if( subDirFor3Dshapes.IsEmpty() )
164 subDirFor3Dshapes = wxT( "shapes3D" );
165
166 // The general VRML scale factor
167 // Assuming the VRML default unit is the mm
168 // this is the mm to VRML scaling factor for mm, 0.1 inch, and inch
169 double scaleList[4] = { 1.0, 0.001, 10.0/25.4, 1.0/25.4 };
170
171 DIALOG_EXPORT_VRML dlg( this );
172 dlg.FilePicker()->SetPath( path );
173 dlg.SetSubdir( subDirFor3Dshapes );
174
175 if( dlg.ShowModal() != wxID_OK )
176 return;
177
178 double aXRef = dlg.GetXRef();
179 double aYRef = dlg.GetYRef();
180
181 if( dlg.GetRefUnitsChoice() == 1 )
182 {
183 // selected reference unit is in inches
184 aXRef *= 25.4;
185 aYRef *= 25.4;
186 }
187
188 if( dlg.GetOriginChoice() == 1 )
189 {
190 // Origin = board center:
191 BOARD* pcb = GetBoard();
192 BOX2I bbox = pcb->ComputeBoundingBox( true );
193 aXRef = pcbIUScale.IUTomm( bbox.GetCenter().x );
194 aYRef = pcbIUScale.IUTomm( bbox.GetCenter().y );
195 }
196
197 double scale = scaleList[dlg.GetUnits()]; // final scale export
198 bool includeUnspecified = !dlg.GetNoUnspecifiedOption();
199 bool includeDNP = !dlg.GetNoDNPOption();
200 bool export3DFiles = dlg.GetCopyFilesOption();
201 bool useRelativePaths = dlg.GetUseRelativePathsOption();
202
203 path = dlg.FilePicker()->GetPath();
205 wxFileName modelPath = path;
206
207 wxBusyCursor dummy;
208
209 subDirFor3Dshapes = dlg.GetSubdir3Dshapes();
210 modelPath.AppendDir( subDirFor3Dshapes );
211
212 if( export3DFiles && !modelPath.DirExists() )
213 {
214 if( !modelPath.Mkdir() )
215 {
216 wxString msg = wxString::Format( _( "Failed to create folder '%s'." ),
217 modelPath.GetPath() );
218 DisplayErrorMessage( this, msg );
219 return;
220 }
221 }
222
223 if( !ExportVRML_File( path, scale, includeUnspecified, includeDNP, export3DFiles,
224 useRelativePaths, modelPath.GetPath(), aXRef, aYRef ) )
225 {
226 wxString msg = wxString::Format( _( "Failed to create file '%s'." ), path );
227 DisplayErrorMessage( this, msg );
228 return;
229 }
230}
constexpr EDA_IU_SCALE pcbIUScale
Definition: base_units.h:108
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
BOX2I ComputeBoundingBox(bool aBoardEdgesOnly=false) const
Calculate the bounding box containing all board items (or board edge segments).
Definition: board.cpp:1674
const wxString & GetFileName() const
Definition: board.h:327
constexpr const Vec GetCenter() const
Definition: box2.h:230
Class DIALOG_EXPORT_VRML_BASE.
wxFilePickerCtrl * FilePicker()
DIALOG_EXPORT_VRML(PCB_EDIT_FRAME *aEditFrame)
PCB_EDIT_FRAME * m_editFrame
wxString GetSubdir3Dshapes()
bool TransferDataFromWindow() override
void SetSubdir(const wxString &aDir)
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...
int ShowModal() override
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:53
const int scale
std::vector< FAB_LAYER_COLOR > dummy
constexpr double IUTomm(int iu) const
Definition: base_units.h:86