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 
35 #include <common.h>
36 #include <confirm.h>
37 #include <kiway.h>
38 #include <richio.h>
40 #include <kiway_player.h>
41 #include <stdexcept>
42 #include "pgm_kicad.h"
43 
44 #include <io_mgr.h>
45 #include <sch_io_mgr.h>
46 
47 #include "kicad_manager_frame.h"
48 
49 void KICAD_MANAGER_FRAME::OnImportEagleFiles( wxCommandEvent& event )
50 {
51  // Close other windows.
52  if( !Kiway().PlayersClose( false ) )
53  return;
54 
55 
56  wxString title = _( "Import Eagle Project Files" );
57  int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;
58  wxString default_dir = GetMruPath();
59 
60  ClearMsg();
61 
62  wxFileDialog schdlg( this, title, default_dir, wxEmptyString,
63  EagleFilesWildcard(), style );
64 
65  if( schdlg.ShowModal() == wxID_CANCEL )
66  return;
67 
68 
69  wxFileName sch( schdlg.GetPath() );
70 
71  sch.SetExt( LegacySchematicFileExtension );
72 
73  wxFileName pro = sch;
74 
75  pro.SetExt( ProjectFileExtension );
76 
77  wxString protitle = _( "KiCad Project Destination" );
78 
79  // Don't use wxFileDialog here. On GTK builds, the default path is returned unless a
80  // file is actually selected.
81  wxDirDialog prodlg( this, protitle, pro.GetPath(), wxDD_DEFAULT_STYLE );
82 
83  if( prodlg.ShowModal() == wxID_CANCEL )
84  return;
85 
86  pro.SetPath( prodlg.GetPath() );
87 
88  // Check if the project directory is empty
89  wxDir directory( pro.GetPath() );
90 
91  if( directory.HasFiles() )
92  {
93  wxString msg = _( "The selected directory is not empty. We recommend you "
94  "create projects in their own clean directory.\n\nDo you "
95  "want to create a new empty directory for the project?" );
96 
97  KIDIALOG dlg( this, msg, _( "Confirmation" ), wxYES_NO | wxICON_WARNING );
98  dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
99 
100  if( dlg.ShowModal() == wxID_YES )
101  {
102  // Append a new directory with the same name of the project file
103  // and try to create it
104  pro.AppendDir( pro.GetName() );
105 
106  if( !wxMkdir( pro.GetPath() ) )
107  // There was a problem, undo
108  pro.RemoveLastDir();
109  }
110  }
111 
112  wxFileName pcb( sch );
113  std::string packet;
114  pro.SetExt( ProjectFileExtension );
115  pcb.SetExt( LegacyPcbFileExtension ); // enforce extension
116 
117  if( !pro.IsAbsolute() )
118  pro.MakeAbsolute();
119 
120  SetProjectFileName( pro.GetFullPath() );
121  wxString prj_filename = GetProjectFileName();
122 
123  if( sch.FileExists() )
124  {
125  KIWAY_PLAYER* schframe = Kiway().Player( FRAME_SCH, true );
126 
127  packet = StrPrintf( "%d\n%s", SCH_IO_MGR::SCH_EAGLE, TO_UTF8( sch.GetFullPath() ) );
128  schframe->Kiway().ExpressMail( FRAME_SCH, MAIL_IMPORT_FILE, packet, this );
129 
130  if( !schframe->IsShown() )
131  schframe->Show( true );
132 
133  // On Windows, Raise() does not bring the window on screen, when iconized
134  if( schframe->IsIconized() )
135  schframe->Iconize( false );
136 
137  schframe->Raise();
138  }
139 
140  if( pcb.FileExists() )
141  {
142  KIWAY_PLAYER* pcbframe = Kiway().Player( FRAME_PCB_EDITOR, true );
143 
144  if( !pcbframe->IsVisible() )
145  pcbframe->Show( true );
146 
147  packet = StrPrintf( "%d\n%s", IO_MGR::EAGLE, TO_UTF8( pcb.GetFullPath() ) );
148  pcbframe->Kiway().ExpressMail( FRAME_PCB_EDITOR, MAIL_IMPORT_FILE, packet, this );
149 
150  // On Windows, Raise() does not bring the window on screen, when iconized
151  if( pcbframe->IsIconized() )
152  pcbframe->Iconize( false );
153 
154  pcbframe->Raise();
155  }
156 
157  ReCreateTreePrj();
158  m_active_project = true;
159 }
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.
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:342
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:432
#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.