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 The 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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include "import_proj.h"
22
23#include <list>
24
26#include <confirm.h>
27#include <kidialog.h>
29#include <macros.h>
30#include <string_utils.h>
31#include <richio.h>
32
33#include <wx/fileconf.h>
34#include <wx/filedlg.h>
35#include <wx/msgdlg.h>
36#include <wx/tokenzr.h>
37#include <wx/dir.h>
38#include <wx/textfile.h>
39#include <wx/wfstream.h>
40
41#include <kiway.h>
42#include <kiway_player.h>
43#include <kicad_manager_frame.h>
44#include <pcb_edit_frame.h>
45#include <sch_edit_frame.h>
46
47#include <sch_io/sch_io_mgr.h>
48#include <pcb_io/pcb_io_mgr.h>
49#include <project_sch.h>
50#include <project_pcb.h>
51
56#include <io/pads/pads_common.h>
60
61#include <wx/log.h>
62
63
65 const std::vector<wxString>& aSchFileExtensions,
66 const std::vector<wxString>& aPcbFileExtensions ) :
67 m_frame( aFrame ),
68 m_schExtenstions( aSchFileExtensions ), m_pcbExtenstions( aPcbFileExtensions )
69{
70}
71
72
74{
75 // Append a new directory with the same name of the project file
76 // Keep iterating until we find an empty directory
77 wxString newDir = m_TargetProj.GetName();
78 int attempt = 0;
79
80 m_TargetProj.AppendDir( newDir );
81
82 while( m_TargetProj.DirExists() )
83 {
84 m_TargetProj.RemoveLastDir();
85 wxString suffix = wxString::Format( "_%d", ++attempt );
86 m_TargetProj.AppendDir( newDir + suffix );
87 }
88}
89
90
92{
93public:
94 SCOPED_FILE_REMOVER( const wxString& aFile ) :
95 m_file( aFile )
96 {
97 }
98
100 {
101 wxRemoveFile( m_file );
102 }
103
104 // No copy operations allowed or the destructor might remove files multiple times.
107
108private:
109 wxString m_file;
110};
111
112
113void IMPORT_PROJ_HELPER::ImportIndividualFile( KICAD_T aFT, int aImportedFileType )
114{
115 FRAME_T frame_type;
116 wxString appImportFile;
117 std::vector<wxString> neededExts;
118
119 switch( aFT )
120 {
121 case SCHEMATIC_T:
122 neededExts = m_schExtenstions;
123 frame_type = FRAME_SCH;
124 break;
125
126 case PCB_T:
127 neededExts = m_pcbExtenstions;
128 frame_type = FRAME_PCB_EDITOR;
129 break;
130
131 default: return;
132 }
133
134 std::list<SCOPED_FILE_REMOVER> copiedFiles;
135
136 for( wxString ext : neededExts )
137 {
138 if( ext == wxS( "INPUT" ) )
139 ext = m_InputFile.GetExt();
140
141 wxFileName candidate = m_InputFile;
142
143 // Selected file often the one wanted (e.g. schematic in schematic-only
144 // import); keep on-disk name so uppercase ext still resolves on
145 // case-sensitive fs. Rewrite ext only when seeking differently-typed sibling
146 if( m_InputFile.GetExt().CmpNoCase( ext ) != 0 )
147 candidate.SetExt( ext );
148
149 if( !candidate.FileExists() )
150 continue;
151
152 wxFileName targetFile( m_TargetProj.GetPath(), candidate.GetName(), candidate.GetExt() );
153
154 if( !targetFile.FileExists() )
155 {
156 bool copied = wxCopyFile( candidate.GetFullPath(), targetFile.GetFullPath(), false );
157
158 if( copied )
159 {
160 // Will be auto-removed
161 copiedFiles.emplace_back( targetFile.GetFullPath() );
162 }
163 }
164
165 // Pick the first file to pass to application
166 if( appImportFile.empty() && targetFile.FileExists() )
167 appImportFile = targetFile.GetFullPath();
168 }
169
170 if( appImportFile.empty() )
171 return;
172
173 doImport( appImportFile, frame_type, aImportedFileType );
174}
175
176
177void IMPORT_PROJ_HELPER::doImport( const wxString& aFile, FRAME_T aFrameType, int aImportedFileType )
178{
179 if( KIWAY_PLAYER* frame = m_frame->Kiway().Player( aFrameType, true ) )
180 {
181 std::stringstream ss;
182 ss << aImportedFileType << '\n' << TO_UTF8( aFile );
183
184 for( const auto& [key, value] : m_properties )
185 ss << '\n' << key << '\n' << value.wx_str();
186
187 std::string packet = ss.str();
188 frame->Kiway().ExpressMail( aFrameType, MAIL_IMPORT_FILE, packet, m_frame );
189
190 // Editors reply with an empty packet when the import fails
191 if( !packet.empty() )
192 m_importedFrames.insert( aFrameType );
193
194 if( aFrameType == FRAME_SCH && aImportedFileType == SCH_IO_MGR::SCH_ORCAD
197
198 if( !frame->IsShownOnScreen() )
199 frame->Show( true );
200
201 // On Windows, Raise() does not bring the window on screen, when iconized
202 if( frame->IsIconized() )
203 frame->Iconize( false );
204
205 frame->Raise();
206 }
207}
208
209
211{
212 for( FRAME_T frameType : m_importedFrames )
213 {
214 std::string payload;
215 MAIL_T mail = frameType == FRAME_SCH ? MAIL_SCH_SAVE : MAIL_PCB_SAVE;
216
217 m_frame->Kiway().ExpressMail( frameType, mail, payload, m_frame );
218 }
219}
220
221
222void IMPORT_PROJ_HELPER::EasyEDAProProjectHandler( int aImportedSchFileType, int aImportedPcbFileType )
223{
224 const bool isV3 =
225 aImportedSchFileType == SCH_IO_MGR::SCH_EASYEDAPRO_V3 || aImportedPcbFileType == PCB_IO_MGR::EASYEDAPRO_V3;
226
227 // Sch/PCB importers both assign FPIDs under ShortenLibName( archive ); advertise that
228 // nickname as the source footprint lib so post-import reconciler keeps it (instead of
229 // rewriting schematic Footprint fields onto the generated *-import-fps cache).
230 wxArrayString sourceFpLibs;
231 sourceFpLibs.Add( EASYEDAPRO::ShortenLibName( m_InputFile.GetName() ) );
233
234 nlohmann::json project;
235
236 if( isV3 )
237 {
238 // For v3, build a minimal legacy-format project from raw documents
239 EASYEDAPRO::V3_DOC_PARSER adapter( m_InputFile.GetFullPath() );
240 adapter.Load();
242 }
243 else
244 {
246 }
247
248 std::vector<IMPORT_PROJECT_DESC> toImport = EASYEDAPRO::ProjectToSelectorDialog( project, false, false );
249
250 if( toImport.size() > 1 )
251 toImport = DIALOG_IMPORT_CHOOSE_PROJECT::RunModal( m_frame, toImport );
252
253 if( toImport.size() == 1 )
254 {
255 const IMPORT_PROJECT_DESC& desc = toImport[0];
256 wxString pcbId = desc.PCBId;
257 wxString schId = desc.SchematicId;
258
259 if( pcbId.empty() && project.contains( "pcbs" ) && project.at( "pcbs" ).is_object()
260 && project.at( "pcbs" ).size() == 1 )
261 {
262 pcbId = wxString::FromUTF8( project.at( "pcbs" ).begin().key() );
263 }
264
265 if( schId.empty() && project.contains( "schematics" ) && project.at( "schematics" ).is_object()
266 && project.at( "schematics" ).size() == 1 )
267 {
268 schId = wxString::FromUTF8( project.at( "schematics" ).begin().key() );
269 }
270
271 if( !pcbId.empty() )
272 m_properties["pcb_id"] = pcbId;
273
274 if( !schId.empty() )
275 m_properties["sch_id"] = schId;
276 }
277 else
278 {
279 m_properties["pcb_id"] = "";
280 m_properties["sch_id"] = "";
281 }
282}
283
284
286{
287 // fresh empty target dir, so a stem-derived name is collision-free; reconciler still guards on-disk
288 wxString stem = m_TargetProj.GetName();
289
290 if( stem.IsEmpty() )
291 stem = m_InputFile.GetName();
292
295}
296
297
298wxString IMPORT_PROJ_HELPER::joinSourceLibNicknames( const std::set<wxString>& aPaths ) const
299{
300 wxArrayString nicks;
301
302 for( const wxString& path : aPaths )
303 {
304 wxString nick = wxFileName( path ).GetName();
305
306 if( !nick.IsEmpty() )
307 nicks.Add( nick );
308 }
309
310 return IMPORT_PROJ_PROPS::JoinList( nicks );
311}
312
313
314void IMPORT_PROJ_HELPER::addLocalLibraries( const std::set<wxString>& aNames, FRAME_T aFrameType )
315{
316 KIWAY_PLAYER* frame = m_frame->Kiway().Player( aFrameType, true );
317
318 if( !frame )
319 return;
320
321 std::stringstream ss;
322
323 // First line is the source project directory so the handler can distinguish
324 // project-local libraries from external fixed-path references.
325 ss << TO_UTF8( m_InputFile.GetPath() ) << '\n';
326
327 for( const wxString& name : aNames )
328 {
329 wxFileName fname( name );
330 fname.MakeAbsolute( m_InputFile.GetPath() );
331 ss << TO_UTF8( fname.GetFullPath() ) << '\n';
332 }
333
334 std::string packet = ss.str();
335 frame->Kiway().ExpressMail( aFrameType, MAIL_ADD_LOCAL_LIB, packet, m_frame );
336}
337
338
340{
341 wxFileConfig config( wxEmptyString, wxEmptyString, wxEmptyString, m_InputFile.GetFullPath(),
342 wxCONFIG_USE_NO_ESCAPE_CHARACTERS );
343
344 wxString groupname;
345 long groupid;
346
347 std::set<wxString> sch_file;
348 std::set<wxString> sch_libs;
349 std::set<wxString> pcb_libs;
350
351 wxString pcb_file;
352 int pcb_file_type = PCB_IO_MGR::ALTIUM_DESIGNER;
353
354 auto matchPcbExt = []( const wxString& aExt ) -> int
355 {
356 if( !aExt.CmpNoCase( wxS( "PcbDoc" ) ) )
358
359 if( !aExt.CmpNoCase( wxS( "CSPcbDoc" ) ) )
361
362 if( !aExt.CmpNoCase( wxS( "CMPcbDoc" ) ) )
364
365 if( !aExt.CmpNoCase( wxS( "SWPcbDoc" ) ) )
367
368 return -1;
369 };
370
371 for( bool more = config.GetFirstGroup( groupname, groupid ); more;
372 more = config.GetNextGroup( groupname, groupid ) )
373 {
374 if( !groupname.StartsWith( wxS( "Document" ) ) )
375 continue;
376
377 wxString number = groupname.Mid( 8 );
378 long docNumber;
379
380 if( !number.ToLong( &docNumber ) )
381 continue;
382
383 wxString path = config.Read( groupname + wxS( "/DocumentPath" ), wxEmptyString );
384
385 if( path.empty() )
386 continue;
387
388 wxFileName fname( path, wxPATH_WIN );
389
390 if( !fname.IsAbsolute() )
391 fname.MakeAbsolute( m_InputFile.GetPath() );
392
393 int pcbType = matchPcbExt( fname.GetExt() );
394
395 if( pcbType >= 0 && pcb_file.empty() )
396 {
397 pcb_file = fname.GetFullPath();
398 pcb_file_type = pcbType;
399 }
400
401 if( !fname.GetExt().CmpNoCase( "SCHDOC" ) )
402 sch_file.insert( fname.GetFullPath() );
403
404 if( !fname.GetExt().CmpNoCase( "PCBLIB" ) )
405 pcb_libs.insert( fname.GetFullPath() );
406
407 if( !fname.GetExt().CmpNoCase( "SCHLIB" ) )
408 sch_libs.insert( fname.GetFullPath() );
409 }
410
411 addLocalLibraries( sch_libs, FRAME_SCH );
413
414 // pass source fp libs to both imports so found footprints relink to source, not the cache
416
417 m_properties["project_file"] = m_InputFile.GetFullPath();
418
419 int ii = 0;
420
421 for( auto& path : sch_file )
422 {
423 std::string key = "sch" + std::to_string( ii++ );
424 m_properties[key] = path.ToStdString();
425 }
426
427 if( !sch_file.empty() )
428 doImport( "", FRAME_SCH, SCH_IO_MGR::SCH_ALTIUM );
429
430 if( !pcb_file.empty() )
431 doImport( pcb_file, FRAME_PCB_EDITOR, pcb_file_type );
432}
433
434
436{
437 m_properties.clear();
438
440
441 if( !related.HasPcb() && !related.HasSchematic() )
442 return;
443
444 std::list<SCOPED_FILE_REMOVER> copiedFiles;
445
446 auto copyAndImport = [&]( const wxString& sourceFile, FRAME_T frameType, int fileType )
447 {
448 if( sourceFile.IsEmpty() )
449 return;
450
451 wxFileName srcFn( sourceFile );
452 wxFileName targetFile( m_TargetProj.GetPath(), srcFn.GetName(), srcFn.GetExt() );
453
454 if( !targetFile.FileExists() )
455 {
456 bool copied = wxCopyFile( srcFn.GetFullPath(), targetFile.GetFullPath(), false );
457
458 if( copied )
459 copiedFiles.emplace_back( targetFile.GetFullPath() );
460 }
461
462 if( targetFile.FileExists() )
463 doImport( targetFile.GetFullPath(), frameType, fileType );
464 };
465
466 copyAndImport( related.schematicFile, FRAME_SCH, SCH_IO_MGR::SCH_PADS );
467 copyAndImport( related.pcbFile, FRAME_PCB_EDITOR, PCB_IO_MGR::PADS );
468}
469
470
472{
473 wxFileName prjFile = m_InputFile;
474
475 if( prjFile.GetExt().CmpNoCase( "prj" ) != 0 )
476 return;
477
478 wxTextFile file;
479
480 if( !file.Open( prjFile.GetFullPath() ) )
481 {
482 wxLogWarning( _( "Could not open gEDA / Lepton EDA project file '%s'." ), prjFile.GetFullPath() );
483 return;
484 }
485
486 std::vector<wxFileName> schFiles;
487 wxString outputName;
488 wxString elementsDir;
489
490 for( size_t i = 0; i < file.GetLineCount(); ++i )
491 {
492 wxString line = file.GetLine( i );
493
494 line.Trim( true );
495 line.Trim( false );
496
497 if( line.IsEmpty() )
498 continue;
499
500 if( line.StartsWith( wxT( "#" ) ) || line.StartsWith( wxT( ";" ) ) )
501 continue;
502
503 int commentPos = line.Find( wxT( "#" ) );
504
505 if( commentPos != wxNOT_FOUND )
506 {
507 line = line.Left( commentPos );
508 line.Trim( true );
509 line.Trim( false );
510 }
511
512 if( line.IsEmpty() )
513 continue;
514
515 wxStringTokenizer tok( line );
516
517 if( !tok.HasMoreTokens() )
518 continue;
519
520 wxString keyword = tok.GetNextToken();
521
522 if( keyword.CmpNoCase( "schematics" ) == 0 )
523 {
524 while( tok.HasMoreTokens() )
525 {
526 wxString schToken = tok.GetNextToken();
527 wxFileName schFile( schToken );
528
529 if( !schFile.IsAbsolute() )
530 schFile.MakeAbsolute( prjFile.GetPath() );
531
532 schFiles.push_back( schFile );
533 }
534 }
535 else if( keyword.CmpNoCase( "output-name" ) == 0 )
536 {
537 wxString rest = line.Mid( keyword.length() );
538 rest.Trim( true );
539 rest.Trim( false );
540 outputName = rest;
541 }
542 else if( keyword.CmpNoCase( "elements-dir" ) == 0 )
543 {
544 wxString rest = line.Mid( keyword.length() );
545 rest.Trim( true );
546 rest.Trim( false );
547 elementsDir = rest;
548 }
549 }
550
551 if( !elementsDir.IsEmpty() )
552 {
553 wxFileName elementsPath( elementsDir );
554
555 if( !elementsPath.IsAbsolute() )
556 elementsPath.MakeAbsolute( prjFile.GetPath() );
557
558 m_properties["elements_dir"] = elementsPath.GetFullPath().ToStdString();
559 }
560
561 if( schFiles.empty() )
562 {
563 wxFileName candidate = prjFile;
564 candidate.SetExt( wxS( "sch" ) );
565
566 if( candidate.FileExists() )
567 schFiles.push_back( candidate );
568 }
569
570 auto promptForMissingFile =
571 [&]( const wxString& aTitle, const wxString& aWildcard, wxFileName& aFile ) -> bool
572 {
573 wxString defaultDir = aFile.GetPath();
574
575 if( defaultDir.IsEmpty() )
576 defaultDir = prjFile.GetPath();
577
578 wxFileDialog dlg( m_frame, aTitle, defaultDir, wxEmptyString, aWildcard,
579 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
580
581 if( dlg.ShowModal() == wxID_OK )
582 {
583 aFile.Assign( dlg.GetPath() );
584 return true;
585 }
586
587 aFile.Clear();
588 return false;
589 };
590
591 std::vector<wxFileName> resolvedSchFiles;
592 const wxString schWildcard = _( "gEDA / Lepton EDA schematic files" ) + wxS( " (*.sch)|*.sch" );
593
594 for( wxFileName schFile : schFiles )
595 {
596 if( !schFile.FileExists() )
597 {
598 if( !promptForMissingFile( _( "Locate gEDA / Lepton EDA Schematic" ), schWildcard, schFile ) )
599 continue;
600 }
601
602 resolvedSchFiles.push_back( schFile );
603 }
604
605 if( schFiles.empty() && resolvedSchFiles.empty() )
606 {
607 wxFileName schFile;
608
609 if( promptForMissingFile( _( "Locate gEDA / Lepton EDA Schematic" ), schWildcard, schFile ) )
610 resolvedSchFiles.push_back( schFile );
611 }
612
613 if( resolvedSchFiles.size() > 1 )
614 {
615 // Pass additional schematic files as a semicolon-delimited property so the
616 // importer can create sub-sheets for each additional page within a single
617 // KiCad project hierarchy.
618 wxString additionalFiles;
619
620 for( size_t i = 1; i < resolvedSchFiles.size(); i++ )
621 {
622 if( !additionalFiles.IsEmpty() )
623 additionalFiles += wxT( ";" );
624
625 additionalFiles += resolvedSchFiles[i].GetFullPath();
626 }
627
628 m_properties["additional_schematics"] = additionalFiles.ToStdString();
629 }
630
631 // Auto-discover subdirectories containing .sym files and pass them as
632 // additional search paths for the importer's symbol resolution.
633 auto discoverSymDirs = [&]( const wxString& aBaseDir, wxString& aSymPaths )
634 {
635 wxDir dir( aBaseDir );
636
637 if( !dir.IsOpened() )
638 return;
639
640 wxString subdir;
641 bool cont = dir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS );
642
643 while( cont )
644 {
645 wxString subdirPath = aBaseDir + wxFileName::GetPathSeparator() + subdir;
646 wxDir childDir( subdirPath );
647
648 if( childDir.IsOpened() && childDir.HasFiles( wxT( "*.sym" ) ) )
649 {
650 if( !aSymPaths.IsEmpty() )
651 aSymPaths += wxT( "\n" );
652
653 aSymPaths += subdirPath;
654 }
655
656 cont = dir.GetNext( &subdir );
657 }
658 };
659
660 wxString symPaths;
661 discoverSymDirs( prjFile.GetPath(), symPaths );
662
663 if( !resolvedSchFiles.empty() )
664 {
665 wxString schDir = resolvedSchFiles[0].GetPath();
666
667 if( schDir != prjFile.GetPath() )
668 discoverSymDirs( schDir, symPaths );
669 }
670
671 if( !symPaths.IsEmpty() )
672 m_properties["sym_search_paths"] = symPaths.ToStdString();
673
674 if( !resolvedSchFiles.empty() )
675 doImport( resolvedSchFiles[0].GetFullPath(), FRAME_SCH, SCH_IO_MGR::SCH_GEDA );
676
677 wxFileName layoutDir( prjFile.GetPath(), wxEmptyString );
678 layoutDir.RemoveLastDir();
679 layoutDir.AppendDir( wxS( "layout" ) );
680
681 wxFileName pcbFile;
682
683 if( !outputName.IsEmpty() )
684 {
685 wxFileName candidate( layoutDir.GetPath(), outputName, wxS( "pcb" ) );
686
687 if( candidate.FileExists() )
688 pcbFile = candidate;
689 }
690
691 if( !pcbFile.FileExists() )
692 {
693 wxFileName candidate( prjFile.GetPath(), prjFile.GetName(), wxS( "pcb" ) );
694
695 if( candidate.FileExists() )
696 pcbFile = candidate;
697 }
698
699 if( !pcbFile.FileExists() )
700 {
701 wxFileName candidate( layoutDir.GetPath(), prjFile.GetName(), wxS( "pcb" ) );
702
703 if( candidate.FileExists() )
704 pcbFile = candidate;
705 }
706
707 const wxString pcbWildcard = _( "gEDA / Lepton EDA PCB files" ) + wxS( " (*.pcb)|*.pcb" );
708
709 if( !pcbFile.FileExists() )
710 promptForMissingFile( _( "Locate gEDA / Lepton EDA PCB" ), pcbWildcard, pcbFile );
711
712 if( pcbFile.FileExists() )
713 doImport( pcbFile.GetFullPath(), FRAME_PCB_EDITOR, PCB_IO_MGR::GEDA_PCB );
714}
715
716
717static wxFileName findSiblingByExt( const wxFileName& aBase, const wxString& aExt )
718{
719 wxDir dir( aBase.GetPath() );
720
721 if( dir.IsOpened() )
722 {
723 wxString found;
724
725 for( bool more = dir.GetFirst( &found, wxEmptyString, wxDIR_FILES ); more;
726 more = dir.GetNext( &found ) )
727 {
728 wxFileName fn( aBase.GetPath(), found );
729
730 if( fn.GetName().CmpNoCase( aBase.GetName() ) == 0 && fn.GetExt().CmpNoCase( aExt ) == 0 )
731 return fn;
732 }
733 }
734
735 return wxFileName();
736}
737
738
740{
742
743 // Import selected OrCAD Capture schematic (.dsn) first
744 ImportIndividualFile( SCHEMATIC_T, SCH_IO_MGR::SCH_ORCAD );
745
746 // Only a successful schematic import replies with a net-name map, even an empty one.
748 return;
749
750 // OrCAD designs pair w/ Cadence Allegro board; offer import, defaulting to
751 // sibling board (matched case-insensitively) next to schematic
752 wxFileName brdFile = findSiblingByExt( m_InputFile, wxS( "brd" ) );
753 bool haveSibling = brdFile.FileExists();
754
755 wxString msg = haveSibling
756 ? wxString::Format( _( "Import the associated Allegro board '%s'?" ),
757 brdFile.GetFullName() )
758 : _( "Import an associated Allegro board file?" );
759
760 KIDIALOG dlg( m_frame, msg, _( "Import Allegro Board" ), wxOK | wxCANCEL | wxICON_QUESTION );
761 dlg.SetOKCancelLabels( _( "OK" ), _( "Skip" ) );
762
763 if( dlg.ShowModal() != wxID_OK )
764 return;
765
766 if( !haveSibling )
767 {
768 wxFileDialog fileDlg( m_frame, _( "Locate Allegro Board" ), m_InputFile.GetPath(),
769 wxEmptyString, FILEEXT::AllegroPcbFilesWildcard(),
770 wxFD_OPEN | wxFD_FILE_MUST_EXIST );
771
772 if( fileDlg.ShowModal() != wxID_OK )
773 return;
774
775 brdFile.Assign( fileDlg.GetPath() );
776 }
777
778 // ExpressMail dispatches synchronously and loaded target project sets generated
779 // board path, so import source board directly, no staging copy
780 if( brdFile.FileExists() )
781 doImport( brdFile.GetFullPath(), FRAME_PCB_EDITOR, PCB_IO_MGR::ALLEGRO );
782}
783
784
786{
787 wxFileName schFile;
788 wxFileName pcbFile;
789 wxString projectPcb;
790
791 if( m_InputFile.GetExt().CmpNoCase( wxS( "prj" ) ) == 0 )
792 {
793 schFile = m_InputFile;
794
795 try
796 {
797 std::unique_ptr<EASYPC::PROJECT_DOCUMENT> doc = EASYPC::LoadProject( m_InputFile.GetFullPath() );
798 const EASYPC::SOURCE_PROJECT& project = *doc->Project;
799
800 if( !project.Boards.empty() )
801 {
802 projectPcb = project.Boards.front()->Name;
803 pcbFile.Assign( EASYPC::ResolveProjectItem( m_InputFile.GetFullPath(), projectPcb ) );
804 }
805 }
806 catch( const IO_ERROR& e )
807 {
808 DisplayErrorMessage( m_frame, _( "Cannot read the Easy-PC project." ), e.What() );
809 return;
810 }
811 }
812 else if( m_InputFile.GetExt().CmpNoCase( wxS( "pcb" ) ) == 0 )
813 {
814 pcbFile = m_InputFile;
815 schFile = findSiblingByExt( m_InputFile, wxS( "sch" ) );
816 }
817 else
818 {
819 schFile = m_InputFile;
820 pcbFile = findSiblingByExt( m_InputFile, wxS( "pcb" ) );
821 }
822
823 // Sources are imported in place: a project's sheets are found relative to the .prj
824 if( schFile.IsOk() && schFile.FileExists() )
825 doImport( schFile.GetFullPath(), FRAME_SCH, SCH_IO_MGR::SCH_EASYPC );
826
827 if( pcbFile.IsOk() && pcbFile.FileExists() )
828 {
829 doImport( pcbFile.GetFullPath(), FRAME_PCB_EDITOR, PCB_IO_MGR::EASYPC );
830 }
831 else if( !projectPcb.IsEmpty() )
832 {
833 DisplayErrorMessage( m_frame, wxString::Format( _( "The board '%s' named by the Easy-PC project was not "
834 "found." ),
835 projectPcb ) );
836 }
837}
838
839
840void IMPORT_PROJ_HELPER::ImportFiles( int aImportedSchFileType, int aImportedPcbFileType )
841{
842 m_properties.clear();
843
844 // both imports must agree on the cache nickname up front
846
847 int importedSchFileType = aImportedSchFileType;
848 int importedPcbFileType = aImportedPcbFileType;
849
850 if( importedSchFileType == SCH_IO_MGR::SCH_EASYEDAPRO || importedSchFileType == SCH_IO_MGR::SCH_EASYEDAPRO_V3
851 || importedPcbFileType == PCB_IO_MGR::EASYEDAPRO || importedPcbFileType == PCB_IO_MGR::EASYEDAPRO_V3 )
852 {
853 bool isV3 = EASYEDAPRO::V3_DOC_PARSER::IsV3Archive( m_InputFile.GetFullPath() );
854
855 importedSchFileType = isV3 ? SCH_IO_MGR::SCH_EASYEDAPRO_V3 : SCH_IO_MGR::SCH_EASYEDAPRO;
856 importedPcbFileType = isV3 ? PCB_IO_MGR::EASYEDAPRO_V3 : PCB_IO_MGR::EASYEDAPRO;
857
858 EasyEDAProProjectHandler( importedSchFileType, importedPcbFileType );
859 }
860 else if( importedSchFileType == SCH_IO_MGR::SCH_ALTIUM || importedPcbFileType == PCB_IO_MGR::ALTIUM_DESIGNER )
861 {
863 return;
864 }
865 else if( aImportedSchFileType == SCH_IO_MGR::SCH_ORCAD )
866 {
868 return;
869 }
870 else if( aImportedSchFileType == SCH_IO_MGR::SCH_EASYPC )
871 {
873 return;
874 }
875 else if( aImportedSchFileType == SCH_IO_MGR::SCH_GEDA )
876 {
877 if( m_InputFile.GetExt().CmpNoCase( "prj" ) == 0 )
878 {
880 }
881 else if( m_InputFile.GetExt().CmpNoCase( "pcb" ) == 0 )
882 {
883 ImportIndividualFile( PCB_T, aImportedPcbFileType );
884 }
885 else
886 {
887 // When importing a bare .sch file, pass the original source directory
888 // so the importer can find symbols in subdirectories relative to the
889 // gEDA / Lepton EDA schematic, even though the file may be copied to a new location
890 // for the KiCad project.
891 wxString sourceDir = m_InputFile.GetPath();
892 wxString symPaths = sourceDir;
893
894 auto discoverSymDirs = [&]( const wxString& aBaseDir )
895 {
896 wxDir dir( aBaseDir );
897
898 if( !dir.IsOpened() )
899 return;
900
901 wxString subdir;
902 bool cont = dir.GetFirst( &subdir, wxEmptyString, wxDIR_DIRS );
903
904 while( cont )
905 {
906 wxString subdirPath =
907 aBaseDir + wxFileName::GetPathSeparator() + subdir;
908 wxDir childDir( subdirPath );
909
910 if( childDir.IsOpened() && childDir.HasFiles( wxT( "*.sym" ) ) )
911 symPaths += wxT( "\n" ) + subdirPath;
912
913 cont = dir.GetNext( &subdir );
914 }
915 };
916
917 discoverSymDirs( sourceDir );
918 m_properties["sym_search_paths"] = symPaths.ToStdString();
919
920 doImport( m_InputFile.GetFullPath(), FRAME_SCH, SCH_IO_MGR::SCH_GEDA );
921 ImportIndividualFile( PCB_T, aImportedPcbFileType );
922 }
923
924 return;
925 }
926
927 ImportIndividualFile( SCHEMATIC_T, importedSchFileType );
928 ImportIndividualFile( PCB_T, importedPcbFileType );
929}
const char * name
static std::vector< IMPORT_PROJECT_DESC > RunModal(wxWindow *aParent, const std::vector< IMPORT_PROJECT_DESC > &aProjectDesc)
Create and show a dialog (modal) and returns the data from it after completion.
Parses EasyEDA Pro v3 .epro2 archives.
void Load()
Load all v3 docs from the archive.
static bool IsV3Archive(const wxString &aFileName)
Return true if aFileName looks like an EasyEDA Pro v3 archive.
void SaveImportedFiles()
Saves every design an editor imported, so the new project exists on disk.
void OrcadProjectHandler()
Imports the OrCAD schematic, then offers to import an associated Allegro board.
wxFileName m_TargetProj
Definition import_proj.h:68
wxString joinSourceLibNicknames(const std::set< wxString > &aPaths) const
Join the basenames of the given library paths into a source-library property value.
IMPORT_PROJ_HELPER(KICAD_MANAGER_FRAME *aframe, const std::vector< wxString > &aSchFileExtensions, const std::vector< wxString > &aPcbFileExtensions)
std::map< std::string, UTF8 > m_properties
Definition import_proj.h:73
void FindEmptyTargetDir()
Appends a new directory with the name of the project file Keep iterating until an empty directory is ...
void EasyEDAProProjectHandler(int aImportedSchFileType, int aImportedPcbFileType)
std::vector< wxString > m_pcbExtenstions
Definition import_proj.h:81
std::set< FRAME_T > m_importedFrames
editors whose import succeeded
Definition import_proj.h:75
void setImportCacheNickname()
Pre-commit one collision-free generated footprint-cache nickname into m_properties so the schematic a...
KICAD_MANAGER_FRAME * m_frame
Definition import_proj.h:71
void ImportFiles(int aImportedSchFileType, int aImportedPcbFileType)
Converts imported files to kicad type files.
void EasyPcProjectHandler()
Imports an Easy-PC / DesignSpark project, or a schematic and board pair.
void addLocalLibraries(const std::set< wxString > &aLibName, FRAME_T aFrameType)
void ImportIndividualFile(KICAD_T aKicad_T, int aImportedFileType)
wxFileName m_InputFile
Definition import_proj.h:67
std::vector< wxString > m_schExtenstions
Definition import_proj.h:80
void ImportPadsFiles()
Converts PADS ASCII schematic and PCB files to KiCad type files.
void doImport(const wxString &aFile, FRAME_T aFrameType, int aImportedFileType)
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
The main KiCad project manager frame.
Helper class to create more flexible dialogs, including 'do not show again' checkbox handling.
Definition kidialog.h:38
bool SetOKCancelLabels(const ButtonLabel &ok, const ButtonLabel &cancel) override
Definition kidialog.h:48
int ShowModal() override
Definition kidialog.cpp:89
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
virtual void ExpressMail(FRAME_T aDestination, MAIL_T aCommand, std::string &aPayload, wxWindow *aSource=nullptr, bool aFromOtherThread=false)
Send aPayload to aDestination from aSource.
Definition kiway.cpp:511
@ EASYPC
Easy-PC / DesignSpark PCB.
Definition pcb_io_mgr.h:76
@ GEDA_PCB
Geda PCB file formats.
Definition pcb_io_mgr.h:66
@ ALTIUM_DESIGNER
Definition pcb_io_mgr.h:59
@ ALTIUM_CIRCUIT_MAKER
Definition pcb_io_mgr.h:57
@ ALTIUM_CIRCUIT_STUDIO
Definition pcb_io_mgr.h:58
SCOPED_FILE_REMOVER & operator=(SCOPED_FILE_REMOVER const &)=delete
SCOPED_FILE_REMOVER(SCOPED_FILE_REMOVER const &)=delete
SCOPED_FILE_REMOVER(const wxString &aFile)
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
The project file (.prj), a bare archive stream.
Loading designs, library items and projects.
#define _(s)
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ FRAME_PCB_EDITOR
Definition frame_type.h:38
@ FRAME_SCH
Definition frame_type.h:30
static wxString AllegroPcbFilesWildcard()
static wxFileName findSiblingByExt(const wxFileName &aBase, const wxString &aExt)
This file contains miscellaneous commonly used macros and functions.
MAIL_T
The set of mail types sendable via KIWAY::ExpressMail() and supplied as the aCommand parameter to tha...
Definition mail_type.h:34
@ MAIL_IMPORT_FILE
Definition mail_type.h:44
@ MAIL_SCH_SAVE
Definition mail_type.h:38
@ MAIL_ADD_LOCAL_LIB
Definition mail_type.h:50
@ MAIL_PCB_SAVE
Definition mail_type.h:39
nlohmann::json BuildV3ProjectIndexFromRawDocs(const V3_DOC_PARSER &aParser, bool aIncludeLibraryMetadata=true)
Build a minimal legacy-style project index from parsed v3 raw documents.
std::vector< IMPORT_PROJECT_DESC > ProjectToSelectorDialog(const nlohmann::json &aProject, bool aPcbOnly=false, bool aSchOnly=false)
List the project's board entries, then the PCBs and schematics no entry names.
nlohmann::json ReadProjectOrDeviceFile(const wxString &aZipFileName)
wxString ShortenLibName(wxString aProjectName)
wxString ResolveProjectItem(const wxString &aProjectPath, const wxString &aItemName)
The file a project item names, as Easy-PC opens it: the Windows path relative to the project's direct...
std::unique_ptr< PROJECT_DOCUMENT > LoadProject(const wxString &aPath)
constexpr char SOURCE_FP_LIBS[]
constexpr char SYM_CACHE_NICKNAME[]
constexpr char FP_CACHE_NICKNAME[]
wxString JoinList(const wxArrayString &aItems)
Encode library nicknames into a single list-property value.
wxString MakeCacheNickname(const wxString &aStem)
Derive the generated footprint-cache nickname from a project or file stem.
wxString MakeSymbolCacheNickname(const wxString &aStem)
Derive the generated symbol-cache nickname from a project or file stem.
std::optional< std::map< wxString, wxString > > SplitNetNameMap(const wxString &aValue)
Decode a net-name map property value, or nothing when the value is not one.
constexpr char NET_NAME_MAP[]
RELATED_FILES FindRelatedPadsFiles(const wxString &aFilePath)
Find the related PADS project file (schematic for a PCB source, or vice versa) in the same directory,...
MODEL3D_FORMAT_TYPE fileType(const char *aFileName)
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
bool copied
std::string path
KICAD_T
The set of class identification values stored in EDA_ITEM::m_structType.
Definition typeinfo.h:70
@ PCB_T
Definition typeinfo.h:74
@ SCHEMATIC_T
Definition typeinfo.h:200
Definition of file extensions used in Kicad.