KiCad PCB EDA Suite
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-2018 KiCad Developers, see AUTHORS.txt for contributors.
5  * @author Russell Oliver <roliver8143@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
31 #include <wx/filename.h>
32 #include <wx/dir.h>
33 #include <wx/log.h>
34 #include <wx/stdpaths.h>
35 #include <wx/string.h>
36 
37 #include <common.h>
38 #include <confirm.h>
39 #include <hotkeys_basic.h>
40 #include <kiway.h>
41 #include <richio.h>
43 #include <systemdirsappend.h>
44 #include <kiway_player.h>
45 #include <stdexcept>
46 #include "pgm_kicad.h"
47 
48 #include <io_mgr.h>
49 #include <sch_io_mgr.h>
50 
51 #include "kicad_manager_frame.h"
52 
53 void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
54 {
55  // Close other windows.
56  if( !Kiway().PlayersClose( false ) )
57  return;
58 
59 
60  wxString title = _( "Import Eagle Project Files" );
61  int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
62  wxString default_dir = GetMruPath();
63 
64  ClearMsg();
65 
66  wxFileDialog schdlg( this, title, default_dir, wxEmptyString,
67  EagleFilesWildcard(), style );
68 
69  if( schdlg.ShowModal() == wxID_CANCEL )
70  return;
71 
72 
73  wxFileName sch( schdlg.GetPath() );
74 
75  sch.SetExt( SchematicFileExtension );
76 
77  wxFileName pro = sch;
78 
79  pro.SetExt( ProjectFileExtension );
80 
81  wxString protitle = _( "KiCad Project Destination" );
82 
83  // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
84  // file is actually selected.
85  wxDirDialog prodlg( this, protitle, pro.GetPath(), wxDD_DEFAULT_STYLE );
86 
87  if( prodlg.ShowModal() == wxID_CANCEL )
88  return;
89 
90  pro.SetPath( prodlg.GetPath() );
91 
92  // Check if the project directory is empty
93  wxDir directory( pro.GetPath() );
94 
95  if( directory.HasFiles() )
96  {
97  wxString msg = _( "The selected directory is not empty. We recommend you "
98  "create projects in their own clean directory.\n\nDo you "
99  "want to create a new empty directory for the project?" );
100 
101  KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
102  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
103 
104  if( dlg.ShowModal() == wxID_YES )
105  {
106  // Append a new directory with the same name of the project file
107  // and try to create it
108  pro.AppendDir( pro.GetName() );
109 
110  if( !wxMkdir( pro.GetPath() ) )
111  // There was a problem, undo
112  pro.RemoveLastDir();
113  }
114  }
115 
116  wxFileName pcb( sch );
117  pro.SetExt( ProjectFileExtension ); // enforce extension
118  pcb.SetExt( LegacyPcbFileExtension ); // enforce extension
119 
120  if( !pro.IsAbsolute() )
121  pro.MakeAbsolute();
122 
123  SetProjectFileName( pro.GetFullPath() );
124  wxString prj_filename = GetProjectFileName();
125 
126  if( sch.FileExists() )
127  {
128  KIWAY_PLAYER* schframe = Kiway().Player( FRAME_SCH, false );
129 
130  if( !schframe )
131  {
132  try // SCH frame was not available, try to start it
133  {
134  schframe = Kiway().Player( FRAME_SCH, true );
135  }
136  catch( const IO_ERROR& err )
137  {
138  wxMessageBox( _( "Eeschema failed to load:\n" ) + err.What(),
139  _( "KiCad Error" ), wxOK | wxICON_ERROR, this );
140  return;
141  }
142  }
143 
144  std::string packet = StrPrintf( "%d\n%s", SCH_IO_MGR::SCH_EAGLE,
145  TO_UTF8( sch.GetFullPath() ) );
146  schframe->Kiway().ExpressMail( FRAME_SCH, MAIL_IMPORT_FILE, packet, this );
147 
148  if( !schframe->IsShown() ) // the frame exists, (created by the dialog field editor)
149  // but no project loaded.
150  {
151  schframe->Show( true );
152  }
153 
154  if( schframe->IsIconized() )
155  schframe->Iconize( false );
156 
157  schframe->Raise();
158  }
159 
160 
161  if( pcb.FileExists() )
162  {
163  KIWAY_PLAYER* pcbframe = Kiway().Player( FRAME_PCB_EDITOR, false );
164 
165  if( !pcbframe )
166  {
167  try // PCB frame was not available, try to start it
168  {
169  pcbframe = Kiway().Player( FRAME_PCB_EDITOR, true );
170  }
171  catch( const IO_ERROR& err )
172  {
173  wxMessageBox( _( "Pcbnew failed to load:\n" ) + err.What(), _( "KiCad Error" ),
174  wxOK | wxICON_ERROR, this );
175  return;
176  }
177  }
178 
179  // a pcb frame can be already existing, but not yet used.
180  // this is the case when running the footprint editor, or the footprint viewer first
181  // if the frame is not visible, the board is not yet loaded
182  if( !pcbframe->IsVisible() )
183  {
184  pcbframe->Show( true );
185  }
186 
187  std::string packet = StrPrintf( "%d\n%s", IO_MGR::EAGLE,
188  TO_UTF8( pcb.GetFullPath() ) );
189  pcbframe->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_IMPORT_FILE, packet, this );
190 
191  // On Windows, Raise() does not bring the window on screen, when iconized
192  if( pcbframe->IsIconized() )
193  pcbframe->Iconize( false );
194 
195  pcbframe->Raise();
196  }
197 
198  ReCreateTreePrj();
199  m_active_project = true;
200 }
Class KIWAY_PLAYER is a wxFrame capable of the OpenProjectFiles function, meaning it can load a porti...
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
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.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
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
const std::string SchematicFileExtension
Definition of file extensions used in Kicad.
#define _(s)
string & err
Definition: json11.cpp:650
const wxString GetProjectFileName()
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:44
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