2020-07-12 16:58:35 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-09-06 17:00:02 +00:00
|
|
|
* Copyright (C) 2020 Roberto Fernandez Bautista <roberto.fer.bau@gmail.com>
|
2020-07-12 16:58:35 +00:00
|
|
|
* Copyright (C) 2020 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file cadstar_pcb_archive_plugin.cpp
|
|
|
|
* @brief Pcbnew PLUGIN for CADSTAR PCB Archive (*.cpa) format: an ASCII format
|
|
|
|
* based on S-expressions.
|
|
|
|
*/
|
|
|
|
|
2020-08-29 18:09:48 +00:00
|
|
|
#include <cadstar_pcb_archive_loader.h>
|
2020-07-12 16:58:35 +00:00
|
|
|
#include <cadstar_pcb_archive_plugin.h>
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2021-01-31 21:55:51 +00:00
|
|
|
#include <footprint.h>
|
2022-11-06 16:51:52 +00:00
|
|
|
#include <string_utf8_map.h>
|
2020-07-12 16:58:35 +00:00
|
|
|
|
|
|
|
|
2020-10-09 16:58:21 +00:00
|
|
|
std::map<wxString, PCB_LAYER_ID> CADSTAR_PCB_ARCHIVE_PLUGIN::DefaultLayerMappingCallback(
|
2020-10-01 22:47:38 +00:00
|
|
|
const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
|
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
std::map<wxString, PCB_LAYER_ID> retval;
|
2020-10-01 22:47:38 +00:00
|
|
|
|
|
|
|
// Just return a the auto-mapped layers
|
|
|
|
for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
|
|
|
|
{
|
|
|
|
retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CADSTAR_PCB_ARCHIVE_PLUGIN::RegisterLayerMappingCallback(
|
|
|
|
LAYER_MAPPING_HANDLER aLayerMappingHandler )
|
|
|
|
{
|
2020-10-09 16:58:21 +00:00
|
|
|
LAYER_REMAPPABLE_PLUGIN::RegisterLayerMappingCallback( aLayerMappingHandler );
|
2020-10-01 22:47:38 +00:00
|
|
|
m_show_layer_mapping_warnings = false; // only show warnings with default callback
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-12 16:58:35 +00:00
|
|
|
CADSTAR_PCB_ARCHIVE_PLUGIN::CADSTAR_PCB_ARCHIVE_PLUGIN()
|
|
|
|
{
|
2020-10-01 22:47:38 +00:00
|
|
|
m_board = nullptr;
|
|
|
|
m_props = nullptr;
|
|
|
|
m_show_layer_mapping_warnings = true;
|
2020-10-09 16:58:21 +00:00
|
|
|
LAYER_REMAPPABLE_PLUGIN::RegisterLayerMappingCallback(
|
|
|
|
CADSTAR_PCB_ARCHIVE_PLUGIN::DefaultLayerMappingCallback );
|
2020-07-12 16:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CADSTAR_PCB_ARCHIVE_PLUGIN::~CADSTAR_PCB_ARCHIVE_PLUGIN()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 21:55:51 +00:00
|
|
|
void CADSTAR_PCB_ARCHIVE_PLUGIN::clearLoadedFootprints()
|
|
|
|
{
|
|
|
|
for( FOOTPRINT* fp : m_loaded_footprints )
|
|
|
|
{
|
|
|
|
delete fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_loaded_footprints.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-12 16:58:35 +00:00
|
|
|
const wxString CADSTAR_PCB_ARCHIVE_PLUGIN::PluginName() const
|
|
|
|
{
|
|
|
|
return wxT( "CADSTAR PCB Archive" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const wxString CADSTAR_PCB_ARCHIVE_PLUGIN::GetFileExtension() const
|
|
|
|
{
|
|
|
|
return wxT( "cpa" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 21:55:51 +00:00
|
|
|
std::vector<FOOTPRINT*> CADSTAR_PCB_ARCHIVE_PLUGIN::GetImportedCachedLibraryFootprints()
|
|
|
|
{
|
|
|
|
std::vector<FOOTPRINT*> retval;
|
|
|
|
|
|
|
|
for( FOOTPRINT* fp : m_loaded_footprints )
|
|
|
|
{
|
|
|
|
retval.push_back( static_cast<FOOTPRINT*>( fp->Clone() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-18 15:40:13 +00:00
|
|
|
BOARD* CADSTAR_PCB_ARCHIVE_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
|
2021-06-23 22:53:08 +00:00
|
|
|
PROGRESS_REPORTER* aProgressReporter )
|
2020-07-12 16:58:35 +00:00
|
|
|
{
|
|
|
|
m_props = aProperties;
|
|
|
|
m_board = aAppendToMe ? aAppendToMe : new BOARD();
|
2021-01-31 21:55:51 +00:00
|
|
|
clearLoadedFootprints();
|
2020-07-12 16:58:35 +00:00
|
|
|
|
2021-09-09 21:15:38 +00:00
|
|
|
CADSTAR_PCB_ARCHIVE_LOADER tempPCB( aFileName, m_layer_mapping_handler,
|
|
|
|
m_show_layer_mapping_warnings, aProgressReporter );
|
2020-12-13 17:28:47 +00:00
|
|
|
tempPCB.Load( m_board, aProject );
|
2020-07-12 16:58:35 +00:00
|
|
|
|
2020-08-31 22:09:29 +00:00
|
|
|
//center the board:
|
|
|
|
if( aProperties )
|
|
|
|
{
|
|
|
|
UTF8 page_width;
|
|
|
|
UTF8 page_height;
|
|
|
|
|
|
|
|
if( aProperties->Value( "page_width", &page_width )
|
|
|
|
&& aProperties->Value( "page_height", &page_height ) )
|
|
|
|
{
|
2022-08-30 23:28:18 +00:00
|
|
|
BOX2I bbbox = m_board->GetBoardEdgesBoundingBox();
|
2020-08-31 22:09:29 +00:00
|
|
|
|
|
|
|
int w = atoi( page_width.c_str() );
|
|
|
|
int h = atoi( page_height.c_str() );
|
|
|
|
|
|
|
|
int desired_x = ( w - bbbox.GetWidth() ) / 2;
|
|
|
|
int desired_y = ( h - bbbox.GetHeight() ) / 2;
|
|
|
|
|
2022-01-11 00:49:49 +00:00
|
|
|
m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
|
2020-08-31 22:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 19:45:49 +00:00
|
|
|
// Need to set legacy loading so that netclassess and design rules are loaded correctly
|
|
|
|
m_board->m_LegacyNetclassesLoaded = true;
|
|
|
|
m_board->m_LegacyDesignSettingsLoaded = true;
|
|
|
|
|
2021-01-31 21:55:51 +00:00
|
|
|
m_loaded_footprints = tempPCB.GetLoadedLibraryFootpints();
|
|
|
|
|
2020-07-12 16:58:35 +00:00
|
|
|
return m_board;
|
|
|
|
}
|
2023-03-13 21:21:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
void CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames,
|
|
|
|
const wxString& aLibraryPath,
|
|
|
|
bool aBestEfforts,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
ensureLoadedLibrary( aLibraryPath );
|
|
|
|
|
|
|
|
if( !m_cache.count( aLibraryPath ) )
|
|
|
|
return; // not found
|
|
|
|
|
|
|
|
for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
|
|
|
|
aFootprintNames.Add( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const FOOTPRINT*
|
|
|
|
CADSTAR_PCB_ARCHIVE_PLUGIN::GetEnumeratedFootprint( const wxString& aLibraryPath,
|
|
|
|
const wxString& aFootprintName,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
ensureLoadedLibrary( aLibraryPath );
|
|
|
|
|
|
|
|
if( !m_cache.count( aLibraryPath ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return m_cache.at( aLibraryPath ).at( aFootprintName ).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintExists( const wxString& aLibraryPath,
|
|
|
|
const wxString& aFootprintName,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
ensureLoadedLibrary( aLibraryPath );
|
|
|
|
|
|
|
|
if( !m_cache.count( aLibraryPath ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT* CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintLoad( const wxString& aLibraryPath,
|
|
|
|
const wxString& aFootprintName,
|
|
|
|
bool aKeepUUID,
|
|
|
|
const STRING_UTF8_MAP* aProperties )
|
|
|
|
{
|
|
|
|
ensureLoadedLibrary( aLibraryPath );
|
|
|
|
|
|
|
|
if( !m_cache.count( aLibraryPath ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long long CADSTAR_PCB_ARCHIVE_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
|
|
|
|
{
|
|
|
|
wxFileName fn( aLibraryPath );
|
|
|
|
|
|
|
|
if( fn.IsFileReadable() )
|
|
|
|
return fn.GetModificationTime().GetValue().GetValue();
|
|
|
|
else
|
|
|
|
return wxDateTime( 0.0 ).GetValue().GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CADSTAR_PCB_ARCHIVE_PLUGIN::ensureLoadedLibrary( const wxString& aLibraryPath )
|
|
|
|
{
|
|
|
|
if( m_cache.count( aLibraryPath ) )
|
|
|
|
{
|
|
|
|
wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
|
|
|
|
|
|
|
|
if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CADSTAR_PCB_ARCHIVE_LOADER csLoader( aLibraryPath, m_layer_mapping_handler,
|
|
|
|
false /*don't log stackup warnings*/, nullptr );
|
|
|
|
|
|
|
|
NAME_TO_FOOTPRINT_MAP footprintMap;
|
|
|
|
std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
|
|
|
|
|
|
|
|
for( std::unique_ptr<FOOTPRINT>& fp : footprints )
|
|
|
|
{
|
|
|
|
footprintMap.insert( { fp->GetFPID().GetLibItemName(), std::move( fp ) } );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
|
|
|
|
m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
|
|
|
|
}
|