KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_easyedapro_plugin.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) 2023 Alex Shvartzkop <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
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
29
30#include <board.h>
31#include <footprint.h>
32#include <progress_reporter.h>
33#include <common.h>
34#include <macros.h>
35
36#include <fstream>
37#include <wx/txtstrm.h>
38#include <wx/wfstream.h>
39#include <wx/mstream.h>
40#include <wx/zipstrm.h>
41#include <wx/log.h>
42
43#include <nlohmann/json.hpp>
45#include <core/map_helpers.h>
46#include <string_utf8_map.h>
47
48
50{
51 std::map<wxString, std::unique_ptr<FOOTPRINT>> m_Footprints;
52 std::map<wxString, EASYEDAPRO::BLOB> m_Blobs;
53 std::map<wxString, std::multimap<wxString, EASYEDAPRO::POURED>> m_Poured;
54};
55
56
58{
59 m_board = nullptr;
60 m_props = nullptr;
61}
62
63
65{
66 if( m_projectData )
67 delete m_projectData;
68}
69
70
71bool EASYEDAPRO_PLUGIN::CanReadBoard( const wxString& aFileName ) const
72{
73 if( aFileName.Lower().EndsWith( wxS( ".epro" ) ) )
74 {
75 return true;
76 }
77 else if( aFileName.Lower().EndsWith( wxS( ".zip" ) ) )
78 {
79 std::shared_ptr<wxZipEntry> entry;
80 wxFFileInputStream in( aFileName );
81 wxZipInputStream zip( in );
82
83 if( !zip.IsOk() )
84 return false;
85
86 while( entry.reset( zip.GetNextEntry() ), entry.get() != NULL )
87 {
88 wxString name = entry->GetName();
89
90 if( name == wxS( "project.json" ) )
91 return true;
92 }
93
94 return false;
95 }
96
97 return false;
98}
99
100
101BOARD* EASYEDAPRO_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
102 const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
103 PROGRESS_REPORTER* aProgressReporter )
104{
105 m_props = aProperties;
106
107 m_board = aAppendToMe ? aAppendToMe : new BOARD();
108
109 // Give the filename to the board if it's new
110 if( !aAppendToMe )
111 m_board->SetFileName( aFileName );
112
113 if( aProgressReporter )
114 {
115 aProgressReporter->Report( wxString::Format( _( "Loading %s..." ), aFileName ) );
116
117 if( !aProgressReporter->KeepRefreshing() )
118 THROW_IO_ERROR( _( "Open cancelled by user." ) );
119 }
120
121 PCB_EASYEDAPRO_PARSER parser( nullptr, nullptr );
122
123 wxFileName fname( aFileName );
124
125 if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
126 {
127 nlohmann::json project = EASYEDAPRO::ReadProjectFile( aFileName );
128
129 wxString pcbToLoad;
130
131 if( m_props && m_props->Exists( "pcb_id" ) )
132 {
133 pcbToLoad = wxString::FromUTF8( aProperties->at( "pcb_id" ) );
134 }
135 else
136 {
137 std::map<wxString, wxString> prjPcbNames = project.at( "pcbs" );
138
139 if( prjPcbNames.size() == 1 )
140 {
141 pcbToLoad = prjPcbNames.begin()->first;
142 }
143 else
144 {
145 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler(
147
148 if( chosen.size() > 0 )
149 pcbToLoad = chosen[0].PCBId;
150 }
151 }
152
153 if( pcbToLoad.empty() )
154 return nullptr;
155
156 LoadAllDataFromProject( aFileName, project );
157
158 if( !m_projectData )
159 return nullptr;
160
161 auto cb = [&]( const wxString& name, const wxString& pcbUuid, wxInputStream& zip ) -> bool
162 {
163 if( !name.EndsWith( wxS( ".epcb" ) ) )
165
166 if( pcbUuid != pcbToLoad )
168
169 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
170
171 wxString boardKey = pcbUuid + wxS( "_0" );
172 wxScopedCharBuffer cb = boardKey.ToUTF8();
173 wxString boardPouredKey = wxBase64Encode( cb.data(), cb.length() );
174
175 const std::multimap<wxString, EASYEDAPRO::POURED>& boardPoured =
176 get_def( m_projectData->m_Poured, boardPouredKey );
177
179 m_projectData->m_Blobs, boardPoured, lines,
180 EASYEDAPRO::ShortenLibName( fname.GetName() ) );
181
183 };
184 EASYEDAPRO::IterateZipFiles( aFileName, cb );
185 }
186
187 return m_board;
188}
189
190
191long long EASYEDAPRO_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
192{
193 return 0;
194}
195
196
197void EASYEDAPRO_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames,
198 const wxString& aLibraryPath, bool aBestEfforts,
199 const STRING_UTF8_MAP* aProperties )
200{
201 wxFileName fname( aLibraryPath );
202
203 if( fname.GetExt() == wxS( "efoo" ) )
204 {
205 wxFFileInputStream ffis( aLibraryPath );
206 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
207
208 while( ffis.CanRead() )
209 {
210 wxString line = txt.ReadLine();
211
212 if( !line.Contains( wxS( "ATTR" ) ) )
213 continue; // Don't bother parsing
214
215 nlohmann::json js = nlohmann::json::parse( line );
216 if( js.at( 0 ) == "ATTR" && js.at( 7 ) == "Footprint" )
217 {
218 aFootprintNames.Add( js.at( 8 ).get<wxString>() );
219 }
220 }
221 }
222 else if( fname.GetExt() == wxS( "epro" ) || fname.GetExt() == wxS( "zip" ) )
223 {
224 nlohmann::json project = EASYEDAPRO::ReadProjectFile( aLibraryPath );
225 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
226
227 for( auto& [key, value] : footprintMap )
228 aFootprintNames.Add( value.at( "title" ) );
229 }
230}
231
232
233void EASYEDAPRO_PLUGIN::LoadAllDataFromProject( const wxString& aProjectPath,
234 const nlohmann::json& aProject )
235{
236 if( m_projectData )
237 delete m_projectData;
238
239 m_projectData = new PRJ_DATA();
240
241 PCB_EASYEDAPRO_PARSER parser( nullptr, nullptr );
242 wxFileName fname( aProjectPath );
243 wxString fpLibName = EASYEDAPRO::ShortenLibName( fname.GetName() );
244
245 std::map<wxString, std::unique_ptr<FOOTPRINT>> result;
246
247 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
248 {
249 if( !name.EndsWith( wxS( ".efoo" ) ) && !name.EndsWith( wxS( ".eblob" ) )
250 && !name.EndsWith( wxS( ".ecop" ) ) )
251 {
253 }
254
255 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
256
257 if( name.EndsWith( wxS( ".efoo" ) ) )
258 {
259 nlohmann::json fpData = aProject.at( "footprints" ).at( baseName );
260 wxString fpTitle = fpData.at( "title" );
261
262 FOOTPRINT* footprint = parser.ParseFootprint( aProject, baseName, lines );
263
264 if( !footprint )
266
267 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( fpLibName, fpTitle );
268 footprint->SetFPID( fpID );
269
270 m_projectData->m_Footprints.emplace( baseName, footprint );
271 }
272 else if( name.EndsWith( wxS( ".eblob" ) ) )
273 {
274 for( const nlohmann::json& line : lines )
275 {
276 if( line.at( 0 ) == "BLOB" )
277 {
278 EASYEDAPRO::BLOB blob = line;
279 m_projectData->m_Blobs[blob.objectId] = blob;
280 }
281 }
282 }
283 else if( name.EndsWith( wxS( ".ecop" ) ) && EASYEDAPRO::IMPORT_POURED )
284 {
285 for( const nlohmann::json& line : lines )
286 {
287 if( line.at( 0 ) == "POURED" )
288 {
289 if( !line.at( 2 ).is_string() )
290 continue; // Unknown type of POURED
291
292 EASYEDAPRO::POURED poured = line;
293 m_projectData->m_Poured[baseName].emplace( poured.parentId, poured );
294 }
295 }
296 }
298 };
299 EASYEDAPRO::IterateZipFiles( aProjectPath, cb );
300}
301
302
303FOOTPRINT* EASYEDAPRO_PLUGIN::FootprintLoad( const wxString& aLibraryPath,
304 const wxString& aFootprintName, bool aKeepUUID,
305 const STRING_UTF8_MAP* aProperties )
306{
307 PCB_EASYEDAPRO_PARSER parser( nullptr, nullptr );
308 FOOTPRINT* footprint = nullptr;
309
310 wxFileName libFname( aLibraryPath );
311
312 if( libFname.GetExt() == wxS( "efoo" ) )
313 {
314 wxFFileInputStream ffis( aLibraryPath );
315 wxTextInputStream txt( ffis, wxS( " " ), wxConvUTF8 );
316
317 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( ffis, aLibraryPath );
318
319 for( const nlohmann::json& js : lines )
320 {
321 if( js.at( 0 ) == "ATTR" )
322 {
323 EASYEDAPRO::PCB_ATTR attr = js;
324
325 if( attr.key == wxS( "Footprint" ) && attr.value != aFootprintName )
326 return nullptr;
327 }
328 }
329
330 footprint = parser.ParseFootprint( nlohmann::json(), wxEmptyString, lines );
331
332 if( !footprint )
333 {
334 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
335 aFootprintName, aLibraryPath ) );
336 }
337
338 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
339 footprint->SetFPID( fpID );
340
341 footprint->Reference().SetVisible( true );
342 footprint->Value().SetText( aFootprintName );
343 footprint->Value().SetVisible( true );
344 footprint->AutoPositionFields();
345 }
346 else if( libFname.GetExt() == wxS( "epro" ) || libFname.GetExt() == wxS( "zip" ) )
347 {
348 nlohmann::json project = EASYEDAPRO::ReadProjectFile( aLibraryPath );
349
350 wxString fpUuid;
351
352 std::map<wxString, nlohmann::json> footprintMap = project.at( "footprints" );
353 for( auto& [uuid, data] : footprintMap )
354 {
355 wxString title = data.at( "title" );
356
357 if( title == aFootprintName )
358 {
359 fpUuid = uuid;
360 break;
361 }
362 }
363
364 if( !fpUuid )
365 {
366 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in project '%s'" ),
367 aFootprintName, aLibraryPath ) );
368 }
369
370 auto cb = [&]( const wxString& name, const wxString& baseName, wxInputStream& zip ) -> bool
371 {
372 if( !name.EndsWith( wxS( ".efoo" ) ) )
374
375 if( baseName != fpUuid )
377
378 std::vector<nlohmann::json> lines = EASYEDAPRO::ParseJsonLines( zip, name );
379
380 footprint = parser.ParseFootprint( project, fpUuid, lines );
381
382 if( !footprint )
383 {
384 THROW_IO_ERROR( wxString::Format( _( "Cannot load footprint '%s' from '%s'" ),
385 aFootprintName, aLibraryPath ) );
386 }
387
388 LIB_ID fpID = EASYEDAPRO::ToKiCadLibID( wxEmptyString, aFootprintName );
389 footprint->SetFPID( fpID );
390
391 footprint->Reference().SetVisible( true );
392 footprint->Value().SetText( aFootprintName );
393 footprint->Value().SetVisible( true );
394 footprint->AutoPositionFields();
395
397 };
398 EASYEDAPRO::IterateZipFiles( aLibraryPath, cb );
399 }
400
401 return footprint;
402}
403
404
406{
407 std::vector<FOOTPRINT*> result;
408
409 if( !m_projectData )
410 return result;
411
412 for( auto& [fpUuid, footprint] : m_projectData->m_Footprints )
413 {
414 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
415 }
416
417 return result;
418}
const char * name
Definition: DXF_plotter.cpp:57
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:276
void SetFileName(const wxString &aFileName)
Definition: board.h:311
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
const STRING_UTF8_MAP * m_props
void LoadAllDataFromProject(const wxString &aLibraryPath, const nlohmann::json &aProject)
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PL...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PLUGIN can read the specified board file.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Load information from some input file format that this PLUGIN implementation knows about into either ...
virtual void SetVisible(bool aVisible)
Definition: eda_text.cpp:229
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:183
void SetFPID(const LIB_ID &aFPID)
Definition: footprint.h:231
PCB_FIELD & Value()
read/write accessors:
Definition: footprint.h:592
PCB_FIELD & Reference()
Definition: footprint.h:593
void AutoPositionFields()
Position Reference and Value fields at the top and bottom of footprint's bounding box.
Definition: footprint.cpp:2244
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
FOOTPRINT * ParseFootprint(const nlohmann::json &aProject, const wxString &aFpUuid, const std::vector< nlohmann::json > &aLines)
void ParseBoard(BOARD *aBoard, const nlohmann::json &aProject, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, const std::map< wxString, EASYEDAPRO::BLOB > &aBlobMap, const std::multimap< wxString, EASYEDAPRO::POURED > &aPouredMap, const std::vector< nlohmann::json > &aLines, const wxString &aFpLibName)
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
Definition: project.h:62
A name/value tuple with unique names and optional values.
bool Exists(const std::string &aProperty) const
The common library.
#define EASY_IT_BREAK
#define EASY_IT_CONTINUE
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
This file contains miscellaneous commonly used macros and functions.
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
Definition: map_helpers.h:64
LIB_ID ToKiCadLibID(const wxString &aLibName, const wxString &aLibReference)
nlohmann::json ReadProjectFile(const wxString &aZipFileName)
void IterateZipFiles(const wxString &aFileName, std::function< bool(const wxString &, const wxString &, wxInputStream &)> aCallback)
std::vector< nlohmann::json > ParseJsonLines(wxInputStream &aInput, const wxString &aSource)
static const bool IMPORT_POURED
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
wxString ShortenLibName(wxString aProjectName)
std::map< wxString, std::multimap< wxString, EASYEDAPRO::POURED > > m_Poured
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_Footprints
std::map< wxString, EASYEDAPRO::BLOB > m_Blobs