36#include <wx/filename.h>
43#define ARG_LAYER_MAP "--layer-map"
49 _(
"Import non-KiCad board and/or schematic files into a new KiCad project" ) ) );
52 .
help(
UTF8STDSTR(
_(
"Input file(s) to import; each is autodetected as a board or "
54 .nargs( argparse::nargs_pattern::at_least_one )
55 .metavar(
"INPUT_FILES" );
58 .default_value( std::string() )
59 .
help(
UTF8STDSTR(
_(
"Output project path stem; produces <stem>.kicad_pro plus "
60 "imported board and/or schematic files beside it. Defaults to the first "
61 "input's name in the current directory." ) ) )
62 .metavar(
"PROJECT" );
65 .default_value( std::string(
"" ) )
66 .
help(
UTF8STDSTR(
_(
"JSON file mapping source layer names to KiCad layer names for "
67 "imported boards; unmapped layers use the automatic best-guess" ) ) )
77 if( rawInputs.empty() )
79 wxFprintf( stderr,
_(
"At least one input file is required\n" ) );
83 std::vector<wxString> inputFiles;
85 for(
const std::string& raw : rawInputs )
86 inputFiles.push_back(
From_UTF8( raw.c_str() ) );
88 std::map<wxString, wxString> layerMap;
91 if( !layerMapFile.IsEmpty() )
97 wxFprintf( stderr, wxS(
"%s\n" ), error );
102 wxFileName projectFn;
104 if( !outputArg.IsEmpty() )
106 projectFn.Assign( outputArg );
110 wxFileName firstInput( inputFiles.front() );
111 projectFn.AssignDir( wxFileName::GetCwd() );
112 projectFn.SetName( firstInput.GetName() );
116 projectFn.MakeAbsolute();
118 if( !projectFn.DirExists()
119 && !wxFileName::Mkdir( projectFn.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
121 wxFprintf( stderr,
_(
"Could not create output directory: %s\n" ), projectFn.GetPath() );
125 wxString stem = projectFn.GetName();
126 wxString dir = projectFn.GetPath();
141 wxFprintf( stderr,
_(
"Could not create project: %s\n" ), projectFn.GetFullPath() );
146 bool haveBoard =
false;
147 bool haveSch =
false;
148 std::vector<FILE_INFO_PAIR> boards;
151 std::map<wxString, wxString> netNameMap;
155 for(
const wxString& input : inputFiles )
169 if( !sourcePcbs.empty() )
171 const bool hasMultipleBoards = sourcePcbs.size() > 1;
172 std::set<wxString> usedBoardNames;
174 for(
const auto& [pcbId, sourceName] : sourcePcbs )
176 wxFileName outputFn = boardFn;
178 if( hasMultipleBoards )
182 if( boardName.empty() )
185 wxString uniqueBoardName = boardName;
188 while( !usedBoardNames.insert( uniqueBoardName ).second )
190 uniqueBoardName = wxString::Format( wxS(
"%s-%u" ), boardName,
duplicate );
194 outputFn.SetName( uniqueBoardName );
211 boards.emplace_back(
KIID(), outputFn.GetFullName() );
221 boards.emplace_back(
KIID(), boardFn.GetFullName() );
255 if( haveBoard && haveSch )
257 wxFprintf( stderr,
_(
"Could not import '%s': a board and a schematic have already "
258 "been imported for this project\n" ), input );
262 wxFprintf( stderr,
_(
"No board or schematic importer recognizes the file: %s\n" ), input );
282 wxFprintf( stderr,
_(
"No inputs could be imported\n" ) );
291 std::vector<FILE_INFO_PAIR>& projectBoards =
project->GetProjectFile().GetBoards();
292 projectBoards = boards;
297 reporter.Report( wxString::Format(
_(
"Created project '%s'\n" ),
298 projectFn.GetFullPath() ),
303 wxFprintf( stderr,
_(
"Failed to write project file: %s\n" ),
304 projectFn.GetFullPath() );
argparse::ArgumentParser m_argParser
COMMAND(const std::string &aName)
Define a new COMMAND instance.
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
static CLI_REPORTER & GetInstance()
Job to import a non-KiCad PCB file to KiCad format.
wxString m_importPcbId
EasyEDA Pro PCB document identifier selected by project-level import.
std::map< wxString, wxString > m_layerMap
Explicit overrides from source layer name to KiCad layer name (canonical board-file name,...
std::map< wxString, wxString > m_netNameMap
bool m_isPartOfMultiBoardProject
Avoid promoting an individual board's source settings into shared project settings.
std::vector< std::pair< wxString, wxString > > m_projectBoards
PCB document identifier and source name pairs found during a probe.
Job to import a non-KiCad schematic file to KiCad format.
std::map< wxString, wxString > m_netNameMap
void SetConfiguredOutputPath(const wxString &aPath)
Sets the configured output path for the job, this path is always saved to file.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
virtual SETTINGS_MANAGER & GetSettingsManager() const
Container for project specific data.
A pure virtual class used to derive REPORTER objects from.
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
static const std::string ProjectFileExtension
static const std::string KiCadSchematicFileExtension
static const std::string KiCadPcbFileExtension
static wxString From_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
bool LoadLayerMapFile(const wxString &aFile, std::map< wxString, wxString > &aMap, wxString &aError)
Load an explicit layer-mapping file into aMap.
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
static const int ERR_INVALID_INPUT_FILE
static const int ERR_INVALID_OUTPUT_CONFLICT
static const int ERR_UNKNOWN_FILE_FORMAT
No plugin for the requested face recognized the input file format.
static const int ERR_UNKNOWN
PGM_BASE & Pgm()
The global program "get" accessor.
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
IbisParser parser & reporter
Definition of file extensions used in Kicad.