KiCad PCB EDA Suite
kicad/import_project.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) 2017-2020 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * @author Russell Oliver <roliver8143@gmail.com>
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  */
25 
32 #include <wx/filename.h>
33 #include <wx/dir.h>
34 #include <wx/log.h>
35 #include <wx/stdpaths.h>
36 #include <wx/string.h>
37 
38 #include <common.h>
39 #include <confirm.h>
40 #include <hotkeys_basic.h>
41 #include <kiway.h>
42 #include <richio.h>
44 #include <systemdirsappend.h>
45 #include <kiway_player.h>
46 #include <stdexcept>
47 #include "pgm_kicad.h"
48 
49 #include <io_mgr.h>
50 #include <sch_io_mgr.h>
51 
52 #include "kicad_manager_frame.h"
53 
54 void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
55 {
56  // Close other windows.
57  if( !Kiway().PlayersClose( false ) )
58  return;
59 
60 
61  wxString title = _( "Import Eagle Project Files" );
62  int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
63  wxString default_dir = GetMruPath();
64 
65  ClearMsg();
66 
67  wxFileDialog schdlg( this, title, default_dir, wxEmptyString,
68  EagleFilesWildcard(), style );
69 
70  if( schdlg.ShowModal() == wxID_CANCEL )
71  return;
72 
73 
74  wxFileName sch( schdlg.GetPath() );
75 
76  sch.SetExt( LegacySchematicFileExtension );
77 
78  wxFileName pro = sch;
79 
80  pro.SetExt( ProjectFileExtension );
81 
82  wxString protitle = _( "KiCad Project Destination" );
83 
84  // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
85  // file is actually selected.
86  wxDirDialog prodlg( this, protitle, pro.GetPath(), wxDD_DEFAULT_STYLE );
87 
88  if( prodlg.ShowModal() == wxID_CANCEL )
89  return;
90 
91  pro.SetPath( prodlg.GetPath() );
92 
93  // Check if the project directory is empty
94  wxDir directory( pro.GetPath() );
95 
96  if( directory.HasFiles() )
97  {
98  wxString msg = _( "The selected directory is not empty. We recommend you "
99  "create projects in their own clean directory.\n\nDo you "
100  "want to create a new empty directory for the project?" );
101 
102  KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
103  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
104 
105  if( dlg.ShowModal() == wxID_YES )
106  {
107  // Append a new directory with the same name of the project file
108  // and try to create it
109  pro.AppendDir( pro.GetName() );
110 
111  if( !wxMkdir( pro.GetPath() ) )
112  // There was a problem, undo
113  pro.RemoveLastDir();
114  }
115  }
116 
117  wxFileName pcb( sch );
118  pro.SetExt( ProjectFileExtension ); // enforce extension
119  pcb.SetExt( LegacyPcbFileExtension ); // enforce extension
120 
121  if( !pro.IsAbsolute() )
122  pro.MakeAbsolute();
123 
124  SetProjectFileName( pro.GetFullPath() );
125  wxString prj_filename = GetProjectFileName();
126 
127  if( sch.FileExists() )
128  {
129  KIWAY_PLAYER* schframe = Kiway().Player( FRAME_SCH, false );
130 
131  if( !schframe )
132  {
133  try // SCH frame was not available, try to start it
134  {
135  schframe = Kiway().Player( FRAME_SCH, true );
136  }
137  catch( const IO_ERROR& err )
138  {
139  wxMessageBox( _( "Eeschema failed to load:\n" ) + err.What(),
140  _( "KiCad Error" ), wxOK | wxICON_ERROR, this );
141  return;
142  }
143  }
144 
145  std::string packet = StrPrintf( "%d\n%s", SCH_IO_MGR::SCH_EAGLE,
146  TO_UTF8( sch.GetFullPath() ) );
147  schframe->Kiway().ExpressMail( FRAME_SCH, MAIL_IMPORT_FILE, packet, this );
148 
149  if( !schframe->IsShown() ) // the frame exists, (created by the dialog field editor)
150  // but no project loaded.
151  {
152  schframe->Show( true );
153  }
154 
155  if( schframe->IsIconized() )
156  schframe->Iconize( false );
157 
158  schframe->Raise();
159  }
160 
161 
162  if( pcb.FileExists() )
163  {
164  KIWAY_PLAYER* pcbframe = Kiway().Player( FRAME_PCB_EDITOR, false );
165 
166  if( !pcbframe )
167  {
168  try // PCB frame was not available, try to start it
169  {
170  pcbframe = Kiway().Player( FRAME_PCB_EDITOR, true );
171  }
172  catch( const IO_ERROR& err )
173  {
174  wxMessageBox( _( "Pcbnew failed to load:\n" ) + err.What(), _( "KiCad Error" ),
175  wxOK | wxICON_ERROR, this );
176  return;
177  }
178  }
179 
180  // a pcb frame can be already existing, but not yet used.
181  // this is the case when running the footprint editor, or the footprint viewer first
182  // if the frame is not visible, the board is not yet loaded
183  if( !pcbframe->IsVisible() )
184  {
185  pcbframe->Show( true );
186  }
187 
188  std::string packet = StrPrintf( "%d\n%s", IO_MGR::EAGLE,
189  TO_UTF8( pcb.GetFullPath() ) );
190  pcbframe->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_IMPORT_FILE, packet, this );
191 
192  // On Windows, Raise() does not bring the window on screen, when iconized
193  if( pcbframe->IsIconized() )
194  pcbframe->Iconize( false );
195 
196  pcbframe->Raise();
197  }
198 
199  ReCreateTreePrj();
200  m_active_project = true;
201 }
KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of ...
Definition: kiway_player.h:59
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:53
KIWAY & Kiway() const
Function Kiway returns a reference to the KIWAY that this object has an opportunity to participate in...
Definition: kiway_holder.h:56
const wxString GetProjectFileName() const
int StrPrintf(std::string *aResult, const char *aFormat,...)
Function StrPrintf is like sprintf() but the output is appended to a std::string instead of to a char...
Definition: richio.cpp:74
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition: confirm.h:44
This file is part of the common library.
const std::string ProjectFileExtension
const std::string LegacyPcbFileExtension
void OnImportEagleFiles(wxCommandEvent &event)
Open dialog to import Eagle schematic and board files.
System directories search utilities.
wxString GetMruPath() const
VTBL_ENTRY KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=NULL)
Function Player returns the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:341
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
Definition of file extensions used in Kicad.
const std::string LegacySchematicFileExtension
void SetProjectFileName(const wxString &aFullProjectProFileName)
VTBL_ENTRY void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=NULL)
Function ExpressMail send aPayload to aDestination from aSource.
Definition: kiway.cpp:427
Import a different format file.
Definition: mail_type.h:45
#define _(s)
Definition: 3d_actions.cpp:33
#define TO_UTF8(wxstring)
The common library.
int ShowModal() override
Definition: confirm.cpp:95
wxString EagleFilesWildcard()
void ClearMsg()
Erase the text panel.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76