KiCad PCB EDA Suite
Loading...
Searching...
No Matches
command_pcb_import.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "command_pcb_import.h"
21#include <cli/exit_codes.h>
22#include <jobs/job_pcb_import.h>
23#include <kiface_base.h>
24#include <string_utils.h>
25#include <macros.h>
26#include <wx/crt.h>
27#include <wx/filename.h>
28
29
30#define ARG_FORMAT "--format"
31#define ARG_LAYER_MAP "--layer-map"
32#define ARG_AUTO_MAP "--auto-map"
33#define ARG_REPORT_FORMAT "--report-format"
34#define ARG_REPORT_FILE "--report-file"
35
36
38{
40
41 m_argParser.add_description(
42 UTF8STDSTR( _( "Import a non-KiCad PCB file to KiCad format" ) ) );
43
44 m_argParser.add_argument( ARG_FORMAT )
45 .default_value( std::string( "auto" ) )
46 .help( UTF8STDSTR( _( "Input format hint: auto, pads, altium, eagle, cadstar, "
47 "fabmaster, pcad, solidworks (default: auto)" ) ) )
48 .metavar( "FORMAT" );
49
50 m_argParser.add_argument( ARG_LAYER_MAP )
51 .default_value( std::string( "" ) )
52 .help( UTF8STDSTR( _( "JSON file with layer name mappings" ) ) )
53 .metavar( "FILE" );
54
55 m_argParser.add_argument( ARG_AUTO_MAP )
56 .help( UTF8STDSTR( _( "Use automatic layer mapping (default behavior)" ) ) )
57 .flag();
58
59 m_argParser.add_argument( ARG_REPORT_FORMAT )
60 .default_value( std::string( "none" ) )
61 .help( UTF8STDSTR( _( "Import report format: none, json, text (default: none)" ) ) )
62 .metavar( "FORMAT" );
63
64 m_argParser.add_argument( ARG_REPORT_FILE )
65 .default_value( std::string( "" ) )
66 .help( UTF8STDSTR( _( "File path for import report (default: stdout)" ) ) )
67 .metavar( "FILE" );
68}
69
70
72{
73 std::unique_ptr<JOB_PCB_IMPORT> importJob = std::make_unique<JOB_PCB_IMPORT>();
74
75 importJob->m_inputFile = m_argInput;
76 importJob->SetConfiguredOutputPath( m_argOutput );
77
78 wxString format = From_UTF8( m_argParser.get<std::string>( ARG_FORMAT ).c_str() );
79
80 if( format == wxS( "auto" ) )
81 {
82 importJob->m_format = JOB_PCB_IMPORT::FORMAT::AUTO;
83 }
84 else if( format == wxS( "pads" ) )
85 {
86 importJob->m_format = JOB_PCB_IMPORT::FORMAT::PADS;
87 }
88 else if( format == wxS( "altium" ) )
89 {
90 importJob->m_format = JOB_PCB_IMPORT::FORMAT::ALTIUM;
91 }
92 else if( format == wxS( "eagle" ) )
93 {
94 importJob->m_format = JOB_PCB_IMPORT::FORMAT::EAGLE;
95 }
96 else if( format == wxS( "cadstar" ) )
97 {
98 importJob->m_format = JOB_PCB_IMPORT::FORMAT::CADSTAR;
99 }
100 else if( format == wxS( "fabmaster" ) )
101 {
102 importJob->m_format = JOB_PCB_IMPORT::FORMAT::FABMASTER;
103 }
104 else if( format == wxS( "pcad" ) )
105 {
106 importJob->m_format = JOB_PCB_IMPORT::FORMAT::PCAD;
107 }
108 else if( format == wxS( "solidworks" ) )
109 {
110 importJob->m_format = JOB_PCB_IMPORT::FORMAT::SOLIDWORKS;
111 }
112 else
113 {
114 wxFprintf( stderr, _( "Invalid format: %s\n" ), format );
116 }
117
118 wxString layerMapFile = From_UTF8( m_argParser.get<std::string>( ARG_LAYER_MAP ).c_str() );
119 importJob->m_layerMapFile = layerMapFile;
120
121 importJob->m_autoMap = m_argParser.get<bool>( ARG_AUTO_MAP ) || layerMapFile.IsEmpty();
122
123 wxString reportFormat = From_UTF8( m_argParser.get<std::string>( ARG_REPORT_FORMAT ).c_str() );
124
125 if( reportFormat == wxS( "none" ) )
126 {
127 importJob->m_reportFormat = JOB_PCB_IMPORT::REPORT_FORMAT::NONE;
128 }
129 else if( reportFormat == wxS( "json" ) )
130 {
131 importJob->m_reportFormat = JOB_PCB_IMPORT::REPORT_FORMAT::JSON;
132 }
133 else if( reportFormat == wxS( "text" ) )
134 {
135 importJob->m_reportFormat = JOB_PCB_IMPORT::REPORT_FORMAT::TEXT;
136 }
137 else
138 {
139 wxFprintf( stderr, _( "Invalid report format: %s\n" ), reportFormat );
141 }
142
143 wxString reportFile = From_UTF8( m_argParser.get<std::string>( ARG_REPORT_FILE ).c_str() );
144 importJob->m_reportFile = reportFile;
145
146 int exitCode = aKiway.ProcessJob( KIWAY::FACE_PCB, importJob.get() );
147
148 return exitCode;
149}
argparse::ArgumentParser m_argParser
Definition command.h:113
COMMAND(const std::string &aName)
Define a new COMMAND instance.
Definition command.cpp:30
void addCommonArgs(bool aInput, bool aOutput, IO_TYPE aInputType, IO_TYPE aOutputType)
Set up the most common of args used across cli.
Definition command.cpp:132
wxString m_argOutput
Value of the output arg if configured.
Definition command.h:148
wxString m_argInput
Value of the common input arg if configured.
Definition command.h:143
int doPerform(KIWAY &aKiway) override
The internal handler that should be overloaded to implement command specific processing and work.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:295
int ProcessJob(KIWAY::FACE_T aFace, JOB *aJob, REPORTER *aReporter=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr)
Definition kiway.cpp:744
@ FACE_PCB
pcbnew DSO
Definition kiway.h:303
#define UTF8STDSTR(s)
Definition command.h:27
#define ARG_FORMAT
#define ARG_LAYER_MAP
#define ARG_AUTO_MAP
#define ARG_REPORT_FILE
#define ARG_REPORT_FORMAT
#define _(s)
This file contains miscellaneous commonly used macros and functions.
static const int ERR_ARGS
Definition exit_codes.h:31
wxString From_UTF8(const char *cstring)