KiCad PCB EDA Suite
Loading...
Searching...
No Matches
import_proj.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) 2019 CERN
5 * Copyright (C) 2019-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "import_proj.h"
23#include <macros.h>
24#include <string_utils.h>
25#include <richio.h>
26
27#include <wx/msgdlg.h>
28
29#include <kiway.h>
30#include <kiway_player.h>
31#include <kicad_manager_frame.h>
32#include <pcb_edit_frame.h>
33#include <sch_edit_frame.h>
34
35#include <sch_io/sch_io_mgr.h>
36#include <pcb_io/pcb_io_mgr.h>
37
42
43
45 const std::vector<wxString>& aSchFileExtensions,
46 const std::vector<wxString>& aPcbFileExtensions ) :
47 m_frame( aFrame ),
48 m_schExtenstions( aSchFileExtensions ), m_pcbExtenstions( aPcbFileExtensions )
49{
50}
51
52
54{
55 // Append a new directory with the same name of the project file
56 // Keep iterating until we find an empty directory
57 wxString newDir = m_TargetProj.GetName();
58 int attempt = 0;
59
60 m_TargetProj.AppendDir( newDir );
61
62 while( m_TargetProj.DirExists() )
63 {
64 m_TargetProj.RemoveLastDir();
65 wxString suffix = wxString::Format( "_%d", ++attempt );
66 m_TargetProj.AppendDir( newDir + suffix );
67 }
68}
69
70
71void IMPORT_PROJ_HELPER::OutputCopyError( const wxFileName& aSrc, const wxFileName& aFileCopy )
72{
73 wxString msg;
74 msg.Printf( _( "Cannot copy file '%s'\n"
75 "to '%s'\n"
76 "The project cannot be imported." ),
77 aSrc.GetFullPath(), aFileCopy.GetFullPath() );
78
79 wxMessageDialog fileCopyErrorDlg( m_frame, msg, _( "Error" ), wxOK_DEFAULT | wxICON_ERROR );
80 fileCopyErrorDlg.ShowModal();
81}
82
83
85{
86 wxString m_file;
87
88public:
89 SCOPED_FILE_REMOVER( const wxString& aFile ) : m_file( aFile ) {}
90 ~SCOPED_FILE_REMOVER() { wxRemoveFile( m_file ); }
91};
92
93
94void IMPORT_PROJ_HELPER::ImportIndividualFile( KICAD_T aFT, int aImportedFileType )
95{
96 FRAME_T frame_type;
97 wxString appImportFile;
98 std::vector<wxString> neededExts;
99
100 switch( aFT )
101 {
102 case SCHEMATIC_T:
103 neededExts = m_schExtenstions;
104 frame_type = FRAME_SCH;
105 break;
106
107 case PCB_T:
108 neededExts = m_pcbExtenstions;
109 frame_type = FRAME_PCB_EDITOR;
110 break;
111
112 default: return;
113 }
114
115 std::vector<SCOPED_FILE_REMOVER> copiedFiles;
116
117 for( wxString ext : neededExts )
118 {
119 if( ext == wxS( "INPUT" ) )
120 ext = m_InputFile.GetExt();
121
122 wxFileName candidate = m_InputFile;
123 candidate.SetExt( ext );
124
125 if( !candidate.FileExists() )
126 continue;
127
128 wxFileName targetFile( m_TargetProj.GetPath(), candidate.GetName(), candidate.GetExt() );
129
130 if( !targetFile.FileExists() )
131 {
132 bool copied = wxCopyFile( candidate.GetFullPath(), targetFile.GetFullPath(), false );
133
134 if( copied )
135 {
136 // Will be auto-removed
137 copiedFiles.emplace_back( targetFile.GetFullPath() );
138 }
139 }
140
141 // Pick the first file to pass to application
142 if( appImportFile.empty() && targetFile.FileExists() )
143 appImportFile = targetFile.GetFullPath();
144 }
145
146 if( appImportFile.empty() )
147 return;
148
149 KIWAY_PLAYER* frame = m_frame->Kiway().Player( frame_type, true );
150
151 std::stringstream ss;
152 ss << aImportedFileType << '\n' << TO_UTF8( appImportFile );
153
154 for( const auto& [key, value] : m_properties )
155 ss << '\n' << key << '\n' << value.wx_str();
156
157 std::string packet = ss.str();
158 frame->Kiway().ExpressMail( frame_type, MAIL_IMPORT_FILE, packet, m_frame );
159
160 if( !frame->IsShownOnScreen() )
161 frame->Show( true );
162
163 // On Windows, Raise() does not bring the window on screen, when iconized
164 if( frame->IsIconized() )
165 frame->Iconize( false );
166
167 frame->Raise();
168}
169
170
172{
173 wxFileName fname = m_InputFile;
174
175 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
176 {
177 nlohmann::json project = EASYEDAPRO::ReadProjectOrDeviceFile( fname.GetFullPath() );
178
179 std::map<wxString, EASYEDAPRO::PRJ_SCHEMATIC> prjSchematics = project.at( "schematics" );
180 std::map<wxString, EASYEDAPRO::PRJ_BOARD> prjBoards = project.at( "boards" );
181 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
182
183 std::vector<IMPORT_PROJECT_DESC> toImport =
185
186 if( toImport.size() > 1 )
187 {
189 }
190
191 if( toImport.size() == 1 )
192 {
193 const IMPORT_PROJECT_DESC& desc = toImport[0];
194
195 m_properties["pcb_id"] = desc.PCBId;
196 m_properties["sch_id"] = desc.SchematicId;
197 }
198 else
199 {
200 m_properties["pcb_id"] = "";
201 m_properties["sch_id"] = "";
202 }
203 }
204}
205
206
207void IMPORT_PROJ_HELPER::ImportFiles( int aImportedSchFileType, int aImportedPcbFileType )
208{
209 m_properties.clear();
210
211 if( aImportedSchFileType == SCH_IO_MGR::SCH_EASYEDAPRO
212 || aImportedPcbFileType == PCB_IO_MGR::EASYEDAPRO )
213 {
215 }
216
217 ImportIndividualFile( SCHEMATIC_T, aImportedSchFileType );
218 ImportIndividualFile( PCB_T, aImportedPcbFileType );
219}
static std::vector< IMPORT_PROJECT_DESC > GetSelectionsModal(wxWindow *aParent, const std::vector< IMPORT_PROJECT_DESC > &aProjectDesc)
Create and show a dialog (modal) and returns the data from it after completion.
wxFileName m_TargetProj
Definition: import_proj.h:35
void EasyEDAProProjectHandler()
IMPORT_PROJ_HELPER(KICAD_MANAGER_FRAME *aframe, const std::vector< wxString > &aSchFileExtensions, const std::vector< wxString > &aPcbFileExtensions)
Definition: import_proj.cpp:44
void FindEmptyTargetDir()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
Definition: import_proj.cpp:53
std::vector< wxString > m_pcbExtenstions
Definition: import_proj.h:46
KICAD_MANAGER_FRAME * m_frame
Definition: import_proj.h:38
void ImportFiles(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
void OutputCopyError(const wxFileName &aSrc, const wxFileName &aFileCopy)
Definition: import_proj.cpp:71
void ImportIndividualFile(KICAD_T aKicad_T, int aImportedFileType)
Definition: import_proj.cpp:94
wxFileName m_InputFile
Definition: import_proj.h:34
std::vector< wxString > m_schExtenstions
Definition: import_proj.h:45
STRING_UTF8_MAP m_properties
Definition: import_proj.h:40
The main KiCad project manager frame.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:55
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
Definition: kiway_player.h:67
virtual KIWAY_PLAYER * Player(FRAME_T aFrameType, bool doCreate=true, wxTopLevelWindow *aParent=nullptr)
Return the KIWAY_PLAYER* given a FRAME_T.
Definition: kiway.cpp:406
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr)
Send aPayload to aDestination from aSource.
Definition: kiway.cpp:527
SCOPED_FILE_REMOVER(const wxString &aFile)
Definition: import_proj.cpp:89
#define _(s)
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition: frame_type.h:33
@ FRAME_PCB_EDITOR
Definition: frame_type.h:42
@ FRAME_SCH
Definition: frame_type.h:34
This file contains miscellaneous commonly used macros and functions.
@ MAIL_IMPORT_FILE
Definition: mail_type.h:48
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:391
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition: typeinfo.h:78
@ PCB_T
Definition: typeinfo.h:82
@ SCHEMATIC_T
Definition: typeinfo.h:203
Definition of file extensions used in Kicad.