2022-10-26 21:59:33 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mark Roszko <mark.roszko@gmail.com>
|
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "eeschema_jobs_handler.h"
|
2023-03-22 02:14:50 +00:00
|
|
|
#include <pgm_base.h>
|
2022-10-26 21:59:33 +00:00
|
|
|
#include <cli/exit_codes.h>
|
2023-03-22 02:14:50 +00:00
|
|
|
#include <sch_plotter.h>
|
2023-03-15 00:03:57 +00:00
|
|
|
#include <jobs/job_export_sch_bom.h>
|
2022-12-01 00:56:59 +00:00
|
|
|
#include <jobs/job_export_sch_pythonbom.h>
|
2022-11-10 03:37:47 +00:00
|
|
|
#include <jobs/job_export_sch_netlist.h>
|
2023-03-22 02:14:50 +00:00
|
|
|
#include <jobs/job_export_sch_plot.h>
|
2022-12-13 03:42:22 +00:00
|
|
|
#include <jobs/job_sym_export_svg.h>
|
2022-11-29 04:54:29 +00:00
|
|
|
#include <jobs/job_sym_upgrade.h>
|
2022-10-26 21:59:33 +00:00
|
|
|
#include <schematic.h>
|
|
|
|
#include <wx/crt.h>
|
2022-12-13 03:42:22 +00:00
|
|
|
#include <wx/dir.h>
|
|
|
|
#include <wx/file.h>
|
2022-10-26 21:59:33 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <connection_graph.h>
|
|
|
|
#include "eeschema_helpers.h"
|
|
|
|
#include <sch_painter.h>
|
2022-12-13 03:42:22 +00:00
|
|
|
#include <locale_io.h>
|
2022-11-10 03:37:47 +00:00
|
|
|
#include <erc.h>
|
|
|
|
#include <wildcards_and_files_ext.h>
|
2022-12-13 03:42:22 +00:00
|
|
|
#include <plotters/plotters_pslike.h>
|
2023-01-14 19:07:49 +00:00
|
|
|
#include <drawing_sheet/ds_data_model.h>
|
2022-10-26 21:59:33 +00:00
|
|
|
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
|
2022-11-29 04:54:29 +00:00
|
|
|
#include <sch_file_versions.h>
|
|
|
|
#include <sch_plugins/kicad/sch_sexpr_lib_plugin_cache.h>
|
|
|
|
|
2022-11-10 03:37:47 +00:00
|
|
|
#include <netlist.h>
|
|
|
|
#include <netlist_exporter_base.h>
|
|
|
|
#include <netlist_exporter_orcadpcb2.h>
|
|
|
|
#include <netlist_exporter_cadstar.h>
|
|
|
|
#include <netlist_exporter_spice.h>
|
|
|
|
#include <netlist_exporter_spice_model.h>
|
|
|
|
#include <netlist_exporter_kicad.h>
|
|
|
|
#include <netlist_exporter_xml.h>
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
#include <fields_data_model.h>
|
|
|
|
|
2022-10-26 21:59:33 +00:00
|
|
|
|
|
|
|
EESCHEMA_JOBS_HANDLER::EESCHEMA_JOBS_HANDLER()
|
|
|
|
{
|
2023-03-15 00:03:57 +00:00
|
|
|
Register( "bom",
|
|
|
|
std::bind( &EESCHEMA_JOBS_HANDLER::JobExportBom, this, std::placeholders::_1 ) );
|
2022-12-01 00:56:59 +00:00
|
|
|
Register( "pythonbom",
|
|
|
|
std::bind( &EESCHEMA_JOBS_HANDLER::JobExportPythonBom, this, std::placeholders::_1 ) );
|
2022-11-10 03:37:47 +00:00
|
|
|
Register( "netlist",
|
|
|
|
std::bind( &EESCHEMA_JOBS_HANDLER::JobExportNetlist, this, std::placeholders::_1 ) );
|
2023-03-22 02:14:50 +00:00
|
|
|
Register( "plot",
|
|
|
|
std::bind( &EESCHEMA_JOBS_HANDLER::JobExportPlot, this, std::placeholders::_1 ) );
|
2022-11-29 04:54:29 +00:00
|
|
|
Register( "symupgrade",
|
2022-12-13 03:42:22 +00:00
|
|
|
std::bind( &EESCHEMA_JOBS_HANDLER::JobSymUpgrade, this, std::placeholders::_1 ) );
|
|
|
|
Register( "symsvg",
|
|
|
|
std::bind( &EESCHEMA_JOBS_HANDLER::JobSymExportSvg, this, std::placeholders::_1 ) );
|
2022-10-26 21:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EESCHEMA_JOBS_HANDLER::InitRenderSettings( KIGFX::SCH_RENDER_SETTINGS* aRenderSettings,
|
|
|
|
const wxString& aTheme, SCHEMATIC* aSch )
|
|
|
|
{
|
|
|
|
COLOR_SETTINGS* cs = Pgm().GetSettingsManager().GetColorSettings( aTheme );
|
|
|
|
aRenderSettings->LoadColors( cs );
|
|
|
|
|
|
|
|
aRenderSettings->SetDefaultPenWidth( aSch->Settings().m_DefaultLineWidth );
|
|
|
|
aRenderSettings->m_LabelSizeRatio = aSch->Settings().m_LabelSizeRatio;
|
|
|
|
aRenderSettings->m_TextOffsetRatio = aSch->Settings().m_TextOffsetRatio;
|
|
|
|
aRenderSettings->m_PinSymbolSize = aSch->Settings().m_PinSymbolSize;
|
|
|
|
|
|
|
|
aRenderSettings->SetDashLengthRatio( aSch->Settings().m_DashedLineDashRatio );
|
|
|
|
aRenderSettings->SetGapLengthRatio( aSch->Settings().m_DashedLineGapRatio );
|
2023-01-14 19:07:49 +00:00
|
|
|
|
|
|
|
// Load the drawing sheet from the filename stored in BASE_SCREEN::m_DrawingSheetFileName.
|
|
|
|
// If empty, or not existing, the default drawing sheet is loaded.
|
2023-01-20 04:47:09 +00:00
|
|
|
wxString filename = DS_DATA_MODEL::ResolvePath( aSch->Settings().m_SchDrawingSheetFileName,
|
2023-01-14 19:07:49 +00:00
|
|
|
aSch->Prj().GetProjectPath() );
|
|
|
|
|
|
|
|
if( !DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( filename ) )
|
|
|
|
wxFprintf( stderr, _( "Error loading drawing sheet." ) );
|
2022-10-26 21:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-18 15:27:53 +00:00
|
|
|
REPORTER& EESCHEMA_JOBS_HANDLER::Report( const wxString& aText, SEVERITY aSeverity )
|
|
|
|
{
|
|
|
|
if( aSeverity == RPT_SEVERITY_ERROR )
|
2023-01-30 12:11:36 +00:00
|
|
|
wxFprintf( stderr, wxS( "%s\n" ), aText );
|
2022-11-18 15:27:53 +00:00
|
|
|
else
|
2023-01-30 12:11:36 +00:00
|
|
|
wxPrintf( wxS( "%s\n" ), aText );
|
2022-11-18 15:27:53 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-22 02:14:50 +00:00
|
|
|
int EESCHEMA_JOBS_HANDLER::JobExportPlot( JOB* aJob )
|
2022-10-26 21:59:33 +00:00
|
|
|
{
|
2023-03-22 02:14:50 +00:00
|
|
|
JOB_EXPORT_SCH_PLOT* aPlotJob = dynamic_cast<JOB_EXPORT_SCH_PLOT*>( aJob );
|
2022-10-26 21:59:33 +00:00
|
|
|
|
2023-03-22 02:14:50 +00:00
|
|
|
if( !aPlotJob )
|
2022-11-30 20:30:49 +00:00
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
2023-03-22 02:14:50 +00:00
|
|
|
SCHEMATIC* sch = EESCHEMA_HELPERS::LoadSchematic( aPlotJob->m_filename, SCH_IO_MGR::SCH_KICAD );
|
2022-11-13 23:47:00 +00:00
|
|
|
|
|
|
|
if( sch == nullptr )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Failed to load schematic file\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:59:33 +00:00
|
|
|
std::unique_ptr<KIGFX::SCH_RENDER_SETTINGS> renderSettings =
|
|
|
|
std::make_unique<KIGFX::SCH_RENDER_SETTINGS>();
|
2023-03-22 02:14:50 +00:00
|
|
|
InitRenderSettings( renderSettings.get(), aPlotJob->settings.m_theme, sch );
|
2022-10-26 21:59:33 +00:00
|
|
|
|
|
|
|
std::unique_ptr<SCH_PLOTTER> schPlotter = std::make_unique<SCH_PLOTTER>( sch );
|
2023-03-22 02:14:50 +00:00
|
|
|
schPlotter->Plot( aPlotJob->m_plotFormat, aPlotJob->settings, renderSettings.get(), this );
|
2022-10-26 21:59:33 +00:00
|
|
|
|
2022-11-10 03:37:47 +00:00
|
|
|
return CLI::EXIT_CODES::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int EESCHEMA_JOBS_HANDLER::JobExportNetlist( JOB* aJob )
|
|
|
|
{
|
|
|
|
JOB_EXPORT_SCH_NETLIST* aNetJob = dynamic_cast<JOB_EXPORT_SCH_NETLIST*>( aJob );
|
|
|
|
|
2022-11-30 20:30:49 +00:00
|
|
|
if( !aNetJob )
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
2022-11-10 03:37:47 +00:00
|
|
|
SCHEMATIC* sch = EESCHEMA_HELPERS::LoadSchematic( aNetJob->m_filename, SCH_IO_MGR::SCH_KICAD );
|
|
|
|
|
2022-11-13 23:47:00 +00:00
|
|
|
if( sch == nullptr )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Failed to load schematic file\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
2022-11-10 03:37:47 +00:00
|
|
|
// Annotation warning check
|
|
|
|
SCH_REFERENCE_LIST referenceList;
|
|
|
|
sch->GetSheets().GetSymbols( referenceList );
|
|
|
|
if( referenceList.GetCount() > 0 )
|
|
|
|
{
|
|
|
|
if( referenceList.CheckAnnotation( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) > 0 )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Warning: schematic has annotation errors, please use the schematic editor to fix them\n" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test duplicate sheet names:
|
|
|
|
ERC_TESTER erc( sch );
|
|
|
|
|
|
|
|
if( erc.TestDuplicateSheetNames( false ) > 0 )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Warning: duplicate sheet names.\n" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<NETLIST_EXPORTER_BASE> helper;
|
2022-12-08 08:44:48 +00:00
|
|
|
unsigned netlistOption = 0;
|
2022-11-10 03:37:47 +00:00
|
|
|
|
|
|
|
wxString fileExt;
|
|
|
|
|
|
|
|
switch( aNetJob->format )
|
|
|
|
{
|
|
|
|
case JOB_EXPORT_SCH_NETLIST::FORMAT::KICADSEXPR:
|
|
|
|
fileExt = NetlistFileExtension;
|
|
|
|
helper = std::make_unique<NETLIST_EXPORTER_KICAD>( sch );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOB_EXPORT_SCH_NETLIST::FORMAT::ORCADPCB2:
|
|
|
|
fileExt = OrCadPcb2NetlistFileExtension;
|
|
|
|
helper = std::make_unique<NETLIST_EXPORTER_ORCADPCB2>( sch );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOB_EXPORT_SCH_NETLIST::FORMAT::CADSTAR:
|
|
|
|
fileExt = CadstarNetlistFileExtension;
|
|
|
|
helper = std::make_unique<NETLIST_EXPORTER_CADSTAR>( sch );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOB_EXPORT_SCH_NETLIST::FORMAT::SPICE:
|
|
|
|
fileExt = SpiceFileExtension;
|
2022-12-08 08:44:48 +00:00
|
|
|
netlistOption = NETLIST_EXPORTER_SPICE::OPTION_SIM_COMMAND;
|
2022-11-10 03:37:47 +00:00
|
|
|
helper = std::make_unique<NETLIST_EXPORTER_SPICE>( sch );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOB_EXPORT_SCH_NETLIST::FORMAT::SPICEMODEL:
|
|
|
|
fileExt = SpiceFileExtension;
|
|
|
|
helper = std::make_unique<NETLIST_EXPORTER_SPICE_MODEL>( sch );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JOB_EXPORT_SCH_NETLIST::FORMAT::KICADXML:
|
|
|
|
fileExt = wxS( "xml" );
|
|
|
|
helper = std::make_unique<NETLIST_EXPORTER_XML>( sch );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
wxFprintf( stderr, _( "Unknown netlist format.\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( aNetJob->m_outputFile.IsEmpty() )
|
|
|
|
{
|
|
|
|
wxFileName fn = sch->GetFileName();
|
|
|
|
fn.SetName( fn.GetName() );
|
|
|
|
fn.SetExt( fileExt );
|
|
|
|
|
|
|
|
aNetJob->m_outputFile = fn.GetFullName();
|
|
|
|
}
|
|
|
|
|
2022-12-08 08:44:48 +00:00
|
|
|
bool res = helper->WriteNetlist( aNetJob->m_outputFile, netlistOption, *this );
|
2022-11-10 03:37:47 +00:00
|
|
|
|
|
|
|
if(!res)
|
|
|
|
{
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CLI::EXIT_CODES::OK;
|
2022-11-13 02:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
int EESCHEMA_JOBS_HANDLER::JobExportBom( JOB* aJob )
|
|
|
|
{
|
|
|
|
JOB_EXPORT_SCH_BOM* aBomJob = dynamic_cast<JOB_EXPORT_SCH_BOM*>( aJob );
|
|
|
|
|
|
|
|
if( !aBomJob )
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
|
|
|
SCHEMATIC* sch = EESCHEMA_HELPERS::LoadSchematic( aBomJob->m_filename, SCH_IO_MGR::SCH_KICAD );
|
|
|
|
|
|
|
|
if( sch == nullptr )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Failed to load schematic file\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Annotation warning check
|
|
|
|
SCH_REFERENCE_LIST referenceList;
|
|
|
|
sch->GetSheets().GetSymbols( referenceList, false, false );
|
|
|
|
|
|
|
|
if( referenceList.GetCount() > 0 )
|
|
|
|
{
|
|
|
|
SCH_REFERENCE_LIST copy = referenceList;
|
|
|
|
|
|
|
|
// Check annotation splits references...
|
|
|
|
if( copy.CheckAnnotation( []( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* ) {} ) > 0 )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Warning: schematic has annotation errors, please use the schematic "
|
|
|
|
"editor to fix them\n" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test duplicate sheet names:
|
|
|
|
ERC_TESTER erc( sch );
|
|
|
|
|
|
|
|
if( erc.TestDuplicateSheetNames( false ) > 0 )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Warning: duplicate sheet names.\n" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build our data model
|
|
|
|
FIELDS_EDITOR_GRID_DATA_MODEL dataModel( referenceList );
|
|
|
|
|
|
|
|
// Mandatory fields + quantity virtual field first
|
|
|
|
for( int i = 0; i < MANDATORY_FIELDS; ++i )
|
|
|
|
dataModel.AddColumn( TEMPLATE_FIELDNAME::GetDefaultFieldName( i ),
|
|
|
|
TEMPLATE_FIELDNAME::GetDefaultFieldName( i, true ), false );
|
|
|
|
|
|
|
|
dataModel.AddColumn( wxS( "Quantity" ), _( "Qty" ), false );
|
|
|
|
|
|
|
|
// User field names in symbols second
|
|
|
|
std::set<wxString> userFieldNames;
|
|
|
|
|
|
|
|
for( size_t i = 0; i < referenceList.GetCount(); ++i )
|
|
|
|
{
|
|
|
|
SCH_SYMBOL* symbol = referenceList[i].GetSymbol();
|
|
|
|
|
|
|
|
for( int j = MANDATORY_FIELDS; j < symbol->GetFieldCount(); ++j )
|
|
|
|
userFieldNames.insert( symbol->GetFields()[j].GetName() );
|
|
|
|
}
|
|
|
|
|
|
|
|
for( const wxString& fieldName : userFieldNames )
|
|
|
|
dataModel.AddColumn( fieldName, fieldName, true );
|
|
|
|
|
|
|
|
// Add any templateFieldNames which aren't already present in the userFieldNames
|
|
|
|
for( const TEMPLATE_FIELDNAME& templateFieldname :
|
|
|
|
sch->Settings().m_TemplateFieldNames.GetTemplateFieldNames() )
|
|
|
|
{
|
|
|
|
if( userFieldNames.count( templateFieldname.m_Name ) == 0 )
|
|
|
|
dataModel.AddColumn( templateFieldname.m_Name, templateFieldname.m_Name, false );
|
|
|
|
}
|
|
|
|
|
|
|
|
BOM_PRESET preset;
|
2023-03-27 20:48:37 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for( wxString fieldName : aBomJob->m_fieldsOrdered )
|
|
|
|
{
|
|
|
|
struct BOM_FIELD field;
|
|
|
|
|
|
|
|
field.name = fieldName;
|
|
|
|
field.show = true;
|
|
|
|
field.groupBy = std::find( aBomJob->m_fieldsGroupBy.begin(), aBomJob->m_fieldsGroupBy.end(),
|
|
|
|
field.name )
|
|
|
|
!= aBomJob->m_fieldsGroupBy.end();
|
|
|
|
field.label =
|
|
|
|
( ( aBomJob->m_fieldsLabels.size() < i ) && !aBomJob->m_fieldsLabels[i].IsEmpty() )
|
|
|
|
? aBomJob->m_fieldsLabels[i]
|
|
|
|
: field.name;
|
|
|
|
|
|
|
|
preset.fieldsOrdered.emplace_back( field );
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2023-03-15 00:03:57 +00:00
|
|
|
preset.sortAsc = aBomJob->m_sortAsc;
|
|
|
|
preset.sortField = aBomJob->m_sortField;
|
|
|
|
preset.groupSymbols = aBomJob->m_groupSymbols;
|
|
|
|
preset.filterString = aBomJob->m_filterString;
|
|
|
|
|
|
|
|
dataModel.ApplyBomPreset( preset );
|
|
|
|
|
|
|
|
if( aBomJob->m_outputFile.IsEmpty() )
|
|
|
|
{
|
|
|
|
wxFileName fn = sch->GetFileName();
|
|
|
|
fn.SetName( fn.GetName() );
|
|
|
|
fn.SetExt( CsvFileExtension );
|
|
|
|
|
|
|
|
aBomJob->m_outputFile = fn.GetFullName();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFile f;
|
|
|
|
if( !f.Open( aBomJob->m_outputFile, wxFile::write ) )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Unable to open destination '%s'" ), aBomJob->m_outputFile );
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOM_FMT_PRESET fmt;
|
|
|
|
fmt.fieldDelimiter = aBomJob->m_fieldDelimiter;
|
|
|
|
fmt.stringDelimiter = aBomJob->m_stringDelimiter;
|
|
|
|
fmt.refDelimiter = aBomJob->m_refDelimiter;
|
|
|
|
fmt.refRangeDelimiter = aBomJob->m_refRangeDelimiter;
|
|
|
|
fmt.keepTabs = aBomJob->m_keepTabs;
|
|
|
|
fmt.keepLineBreaks = aBomJob->m_keepLineBreaks;
|
|
|
|
|
|
|
|
bool res = f.Write( dataModel.Export( fmt ) );
|
|
|
|
|
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CLI::EXIT_CODES::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
int EESCHEMA_JOBS_HANDLER::JobExportPythonBom( JOB* aJob )
|
2022-11-13 02:35:33 +00:00
|
|
|
{
|
2022-12-01 00:56:59 +00:00
|
|
|
JOB_EXPORT_SCH_PYTHONBOM* aNetJob = dynamic_cast<JOB_EXPORT_SCH_PYTHONBOM*>( aJob );
|
2022-11-13 02:35:33 +00:00
|
|
|
|
2023-02-20 08:16:33 +00:00
|
|
|
if( !aNetJob )
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
2022-11-13 02:35:33 +00:00
|
|
|
SCHEMATIC* sch = EESCHEMA_HELPERS::LoadSchematic( aNetJob->m_filename, SCH_IO_MGR::SCH_KICAD );
|
|
|
|
|
2022-11-13 23:47:00 +00:00
|
|
|
if( sch == nullptr )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Failed to load schematic file\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
2022-11-13 02:35:33 +00:00
|
|
|
// Annotation warning check
|
|
|
|
SCH_REFERENCE_LIST referenceList;
|
|
|
|
sch->GetSheets().GetSymbols( referenceList );
|
|
|
|
if( referenceList.GetCount() > 0 )
|
|
|
|
{
|
|
|
|
if( referenceList.CheckAnnotation(
|
|
|
|
[]( ERCE_T, const wxString&, SCH_REFERENCE*, SCH_REFERENCE* )
|
|
|
|
{
|
|
|
|
} )
|
|
|
|
> 0 )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Warning: schematic has annotation errors, please use the schematic "
|
|
|
|
"editor to fix them\n" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test duplicate sheet names:
|
|
|
|
ERC_TESTER erc( sch );
|
|
|
|
|
|
|
|
if( erc.TestDuplicateSheetNames( false ) > 0 )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Warning: duplicate sheet names.\n" ) );
|
|
|
|
}
|
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
std::unique_ptr<NETLIST_EXPORTER_XML> xmlNetlist =
|
|
|
|
std::make_unique<NETLIST_EXPORTER_XML>( sch );
|
2022-11-13 02:35:33 +00:00
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
if( aNetJob->m_outputFile.IsEmpty() )
|
|
|
|
{
|
|
|
|
wxFileName fn = sch->GetFileName();
|
|
|
|
fn.SetName( fn.GetName() + "-bom" );
|
2022-12-13 03:42:22 +00:00
|
|
|
fn.SetExt( XmlFileExtension );
|
2022-11-13 02:35:33 +00:00
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
aNetJob->m_outputFile = fn.GetFullName();
|
|
|
|
}
|
2022-11-13 02:35:33 +00:00
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
bool res = xmlNetlist->WriteNetlist( aNetJob->m_outputFile, GNL_OPT_BOM, *this );
|
2022-11-13 02:35:33 +00:00
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
2022-11-13 02:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 00:56:59 +00:00
|
|
|
return CLI::EXIT_CODES::OK;
|
2022-11-29 04:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-13 03:42:22 +00:00
|
|
|
int EESCHEMA_JOBS_HANDLER::doSymExportSvg( JOB_SYM_EXPORT_SVG* aSvgJob,
|
|
|
|
KIGFX::SCH_RENDER_SETTINGS* aRenderSettings,
|
|
|
|
LIB_SYMBOL* symbol )
|
|
|
|
{
|
|
|
|
wxASSERT( symbol != nullptr );
|
|
|
|
|
|
|
|
if( symbol == nullptr )
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
2023-01-09 03:18:50 +00:00
|
|
|
LIB_SYMBOL* symbolToPlot = symbol;
|
|
|
|
|
|
|
|
// if the symbol is an alias, then the draw items are stored in the parent
|
|
|
|
if( symbol->IsAlias() )
|
|
|
|
{
|
|
|
|
LIB_SYMBOL_SPTR parent = symbol->GetParent().lock();
|
|
|
|
symbolToPlot = parent.get();
|
|
|
|
}
|
|
|
|
|
2023-01-09 03:37:48 +00:00
|
|
|
if( aSvgJob->m_includeHiddenPins )
|
|
|
|
{
|
|
|
|
// horrible hack, TODO overhaul the Plot method to handle this
|
|
|
|
for( LIB_ITEM& item : symbolToPlot->GetDrawItems() )
|
|
|
|
{
|
|
|
|
if( item.Type() != LIB_PIN_T )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LIB_PIN& pin = static_cast<LIB_PIN&>( item );
|
|
|
|
pin.SetVisible( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 03:42:22 +00:00
|
|
|
// iterate from unit 1, unit 0 would be "all units" which we don't want
|
|
|
|
for( int unit = 1; unit < symbol->GetUnitCount() + 1; unit++ )
|
|
|
|
{
|
|
|
|
int convert = 0;
|
|
|
|
|
|
|
|
wxFileName fn;
|
|
|
|
|
|
|
|
fn.SetPath( aSvgJob->m_outputDirectory );
|
|
|
|
fn.SetExt( SVGFileExtension );
|
|
|
|
|
|
|
|
//simplify the name if its single unit
|
|
|
|
if( symbol->IsMulti() )
|
|
|
|
{
|
|
|
|
fn.SetName( wxString::Format( "%s_%d", symbol->GetName().Lower(), unit ) );
|
|
|
|
wxPrintf( _( "Plotting symbol '%s' unit %d to '%s'\n" ), symbol->GetName(), unit,
|
|
|
|
fn.GetFullPath() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fn.SetName( symbol->GetName().Lower() );
|
|
|
|
wxPrintf( _( "Plotting symbol '%s' to '%s'\n" ), symbol->GetName(), fn.GetFullPath() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the symbol bounding box to fit the plot page to it
|
2022-12-13 03:47:56 +00:00
|
|
|
BOX2I symbolBB = symbol->Flatten()->GetUnitBoundingBox( unit, convert, false );
|
2022-12-13 03:42:22 +00:00
|
|
|
PAGE_INFO pageInfo( PAGE_INFO::Custom );
|
|
|
|
pageInfo.SetHeightMils( schIUScale.IUToMils( symbolBB.GetHeight() * 1.2 ) );
|
|
|
|
pageInfo.SetWidthMils( schIUScale.IUToMils( symbolBB.GetWidth() * 1.2 ) );
|
|
|
|
|
|
|
|
SVG_PLOTTER* plotter = new SVG_PLOTTER();
|
|
|
|
plotter->SetRenderSettings( aRenderSettings );
|
|
|
|
plotter->SetPageSettings( pageInfo );
|
|
|
|
plotter->SetColorMode( !aSvgJob->m_blackAndWhite );
|
|
|
|
|
2023-02-19 03:40:07 +00:00
|
|
|
VECTOR2I plot_offset;
|
2022-12-13 03:42:22 +00:00
|
|
|
const double scale = 1.0;
|
|
|
|
|
|
|
|
// Currently, plot units are in decimil
|
|
|
|
plotter->SetViewport( plot_offset, schIUScale.IU_PER_MILS / 10, scale, false );
|
|
|
|
|
|
|
|
plotter->SetCreator( wxT( "Eeschema-SVG" ) );
|
|
|
|
|
|
|
|
if( !plotter->OpenFile( fn.GetFullPath() ) )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Unable to open destination '%s'" ), fn.GetFullPath() );
|
|
|
|
|
|
|
|
delete plotter;
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_INPUT_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCALE_IO toggle;
|
|
|
|
|
|
|
|
plotter->StartPlot( wxT( "1" ) );
|
|
|
|
|
2023-01-09 03:18:50 +00:00
|
|
|
bool background = true;
|
|
|
|
TRANSFORM temp; // Uses default transform
|
|
|
|
VECTOR2I plotPos;
|
2022-12-13 03:42:22 +00:00
|
|
|
|
2023-01-09 03:18:50 +00:00
|
|
|
plotPos.x = pageInfo.GetWidthIU( schIUScale.IU_PER_MILS ) / 2;
|
|
|
|
plotPos.y = pageInfo.GetHeightIU( schIUScale.IU_PER_MILS ) / 2;
|
2022-12-13 03:42:22 +00:00
|
|
|
|
2023-01-09 03:18:50 +00:00
|
|
|
// note, we want to use the fields from the original symbol pointer (in case of non-alias)
|
|
|
|
symbolToPlot->Plot( plotter, unit, convert, background, plotPos, temp, false );
|
2023-01-09 03:37:48 +00:00
|
|
|
symbol->PlotLibFields( plotter, unit, convert, background, plotPos, temp, false, aSvgJob->m_includeHiddenFields );
|
2022-12-13 03:42:22 +00:00
|
|
|
|
2023-01-09 03:18:50 +00:00
|
|
|
symbolToPlot->Plot( plotter, unit, convert, !background, plotPos, temp, false );
|
2023-01-09 03:37:48 +00:00
|
|
|
symbol->PlotLibFields( plotter, unit, convert, !background, plotPos, temp, false, aSvgJob->m_includeHiddenFields );
|
2022-12-13 03:42:22 +00:00
|
|
|
|
|
|
|
plotter->EndPlot();
|
|
|
|
delete plotter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CLI::EXIT_CODES::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int EESCHEMA_JOBS_HANDLER::JobSymExportSvg( JOB* aJob )
|
|
|
|
{
|
|
|
|
JOB_SYM_EXPORT_SVG* svgJob = dynamic_cast<JOB_SYM_EXPORT_SVG*>( aJob );
|
|
|
|
|
2023-02-20 08:16:33 +00:00
|
|
|
if( !svgJob )
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
2023-01-06 04:32:24 +00:00
|
|
|
wxFileName fn( svgJob->m_libraryPath );
|
|
|
|
fn.MakeAbsolute();
|
|
|
|
|
|
|
|
SCH_SEXPR_PLUGIN_CACHE schLibrary( fn.GetFullPath() );
|
2022-12-13 03:42:22 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
schLibrary.Load();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Unable to load library\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIB_SYMBOL* symbol = nullptr;
|
|
|
|
if( !svgJob->m_symbol.IsEmpty() )
|
|
|
|
{
|
|
|
|
// See if the selected symbol exists
|
|
|
|
symbol = schLibrary.GetSymbol( svgJob->m_symbol );
|
|
|
|
if( !symbol )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "There is no symbol selected to save." ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_ARGS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !svgJob->m_outputDirectory.IsEmpty() && !wxDir::Exists( svgJob->m_outputDirectory ) )
|
|
|
|
{
|
|
|
|
wxFileName::Mkdir( svgJob->m_outputDirectory );
|
|
|
|
}
|
|
|
|
|
|
|
|
KIGFX::SCH_RENDER_SETTINGS renderSettings;
|
|
|
|
COLOR_SETTINGS* cs = Pgm().GetSettingsManager().GetColorSettings( svgJob->m_colorTheme );
|
|
|
|
renderSettings.LoadColors( cs );
|
|
|
|
renderSettings.SetDefaultPenWidth( DEFAULT_LINE_WIDTH_MILS * schIUScale.IU_PER_MILS );
|
|
|
|
|
|
|
|
int exitCode = CLI::EXIT_CODES::OK;
|
|
|
|
if( symbol )
|
|
|
|
{
|
|
|
|
exitCode = doSymExportSvg( svgJob, &renderSettings, symbol );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just plot all the symbols we can
|
|
|
|
const LIB_SYMBOL_MAP& libSymMap = schLibrary.GetSymbolMap();
|
|
|
|
|
|
|
|
for( const std::pair<const wxString, LIB_SYMBOL*>& entry : libSymMap )
|
|
|
|
{
|
|
|
|
exitCode = doSymExportSvg( svgJob, &renderSettings, entry.second );
|
|
|
|
if( exitCode != CLI::EXIT_CODES::OK )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int EESCHEMA_JOBS_HANDLER::JobSymUpgrade( JOB* aJob )
|
2022-11-29 04:54:29 +00:00
|
|
|
{
|
|
|
|
JOB_SYM_UPGRADE* upgradeJob = dynamic_cast<JOB_SYM_UPGRADE*>( aJob );
|
|
|
|
|
2023-02-20 08:16:33 +00:00
|
|
|
if( !upgradeJob )
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
|
2023-01-06 03:26:26 +00:00
|
|
|
wxFileName fn( upgradeJob->m_libraryPath );
|
|
|
|
fn.MakeAbsolute();
|
|
|
|
|
|
|
|
SCH_SEXPR_PLUGIN_CACHE schLibrary( fn.GetFullPath() );
|
2022-11-29 04:54:29 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
schLibrary.Load();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Unable to load library\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2022-12-07 02:20:19 +00:00
|
|
|
if( !upgradeJob->m_outputLibraryPath.IsEmpty() )
|
|
|
|
{
|
|
|
|
if( wxFile::Exists( upgradeJob->m_outputLibraryPath ) )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Output path must not conflict with existing path\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_INVALID_OUTPUT_CONFLICT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 04:54:29 +00:00
|
|
|
bool shouldSave = upgradeJob->m_force
|
|
|
|
|| schLibrary.GetFileFormatVersionAtLoad() < SEXPR_SYMBOL_LIB_FILE_VERSION;
|
|
|
|
|
|
|
|
if( shouldSave )
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Saving symbol library in updated format\n" ) );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-12-07 02:20:19 +00:00
|
|
|
if( !upgradeJob->m_outputLibraryPath.IsEmpty() )
|
|
|
|
{
|
|
|
|
schLibrary.SetFileName( upgradeJob->m_outputLibraryPath );
|
|
|
|
}
|
|
|
|
|
2022-11-29 04:54:29 +00:00
|
|
|
schLibrary.SetModified();
|
|
|
|
schLibrary.Save();
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
wxFprintf( stderr, _( "Unable to save library\n" ) );
|
|
|
|
return CLI::EXIT_CODES::ERR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxPrintf( _( "Symbol library was not updated\n" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return CLI::EXIT_CODES::OK;
|
2022-11-30 20:30:49 +00:00
|
|
|
}
|