2020-04-03 23:22:24 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Thomas Pointhuber <thomas.pointhuber@gmx.at>
|
2023-07-22 03:55:49 +00:00
|
|
|
* Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-04-03 23:22:24 +00:00
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Pcbnew PLUGIN for Altium *.PcbDoc format.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <wx/string.h>
|
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
#include <pcb_io_altium_designer.h>
|
2020-04-03 23:22:24 +00:00
|
|
|
#include <altium_pcb.h>
|
2023-12-19 17:39:26 +00:00
|
|
|
#include <io/io_utils.h>
|
2024-02-04 17:14:19 +00:00
|
|
|
#include <io/altium/altium_binary_parser.h>
|
2023-12-19 17:39:26 +00:00
|
|
|
#include <pcb_io/pcb_io.h>
|
2024-05-01 01:21:48 +00:00
|
|
|
#include <reporter.h>>
|
2020-04-03 23:22:24 +00:00
|
|
|
|
2020-11-12 20:19:22 +00:00
|
|
|
#include <board.h>
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
|
|
#include <compoundfilereader.h>
|
|
|
|
#include <utf.h>
|
|
|
|
|
2023-12-24 04:31:22 +00:00
|
|
|
PCB_IO_ALTIUM_DESIGNER::PCB_IO_ALTIUM_DESIGNER() : PCB_IO( wxS( "Altium Designer" ) )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
2024-05-01 01:21:48 +00:00
|
|
|
m_reporter = &WXLOG_REPORTER::GetInstance();
|
2020-04-03 23:22:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
PCB_IO_ALTIUM_DESIGNER::~PCB_IO_ALTIUM_DESIGNER()
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
bool PCB_IO_ALTIUM_DESIGNER::checkFileHeader( const wxString& aFileName )
|
2023-08-18 12:46:59 +00:00
|
|
|
{
|
|
|
|
// Compound File Binary Format header
|
2023-12-19 17:39:26 +00:00
|
|
|
return IO_UTILS::fileStartsWithBinaryHeader( aFileName, IO_UTILS::COMPOUND_FILE_HEADER );
|
2023-08-18 12:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
bool PCB_IO_ALTIUM_DESIGNER::CanReadBoard( const wxString& aFileName ) const
|
2023-08-18 12:46:59 +00:00
|
|
|
{
|
2023-12-19 17:39:26 +00:00
|
|
|
if( !PCB_IO::CanReadBoard( aFileName ) )
|
2023-08-18 12:46:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return checkFileHeader( aFileName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-27 00:25:41 +00:00
|
|
|
bool PCB_IO_ALTIUM_DESIGNER::CanReadLibrary( const wxString& aFileName ) const
|
2023-08-18 12:46:59 +00:00
|
|
|
{
|
2023-12-27 00:25:41 +00:00
|
|
|
if( !PCB_IO::CanReadLibrary( aFileName ) )
|
2023-08-18 12:46:59 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return checkFileHeader( aFileName );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
BOARD* PCB_IO_ALTIUM_DESIGNER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
|
2023-12-27 17:06:23 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
|
2020-04-03 23:22:24 +00:00
|
|
|
{
|
|
|
|
m_props = aProperties;
|
|
|
|
|
|
|
|
m_board = aAppendToMe ? aAppendToMe : new BOARD();
|
|
|
|
|
|
|
|
// Give the filename to the board if it's new
|
|
|
|
if( !aAppendToMe )
|
|
|
|
m_board->SetFileName( aFileName );
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
const std::map<ALTIUM_PCB_DIR, std::string> mapping = {
|
|
|
|
{ ALTIUM_PCB_DIR::FILE_HEADER, "FileHeader" },
|
2022-02-19 15:45:55 +00:00
|
|
|
{ ALTIUM_PCB_DIR::ARCS6, "Arcs6" },
|
|
|
|
{ ALTIUM_PCB_DIR::BOARD6, "Board6" },
|
|
|
|
{ ALTIUM_PCB_DIR::BOARDREGIONS, "BoardRegions" },
|
|
|
|
{ ALTIUM_PCB_DIR::CLASSES6, "Classes6" },
|
|
|
|
{ ALTIUM_PCB_DIR::COMPONENTS6, "Components6" },
|
|
|
|
{ ALTIUM_PCB_DIR::COMPONENTBODIES6, "ComponentBodies6" },
|
|
|
|
{ ALTIUM_PCB_DIR::DIMENSIONS6, "Dimensions6" },
|
|
|
|
{ ALTIUM_PCB_DIR::EXTENDPRIMITIVEINFORMATION, "ExtendedPrimitiveInformation" },
|
|
|
|
{ ALTIUM_PCB_DIR::FILLS6, "Fills6" },
|
|
|
|
{ ALTIUM_PCB_DIR::MODELS, "Models" },
|
|
|
|
{ ALTIUM_PCB_DIR::NETS6, "Nets6" },
|
|
|
|
{ ALTIUM_PCB_DIR::PADS6, "Pads6" },
|
|
|
|
{ ALTIUM_PCB_DIR::POLYGONS6, "Polygons6" },
|
|
|
|
{ ALTIUM_PCB_DIR::REGIONS6, "Regions6" },
|
|
|
|
{ ALTIUM_PCB_DIR::RULES6, "Rules6" },
|
|
|
|
{ ALTIUM_PCB_DIR::SHAPEBASEDREGIONS6, "ShapeBasedRegions6" },
|
|
|
|
{ ALTIUM_PCB_DIR::TEXTS6, "Texts6" },
|
|
|
|
{ ALTIUM_PCB_DIR::TRACKS6, "Tracks6" },
|
|
|
|
{ ALTIUM_PCB_DIR::VIAS6, "Vias6" },
|
|
|
|
{ ALTIUM_PCB_DIR::WIDESTRINGS6, "WideStrings6" }
|
2020-04-03 23:22:24 +00:00
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
2022-01-10 12:59:20 +00:00
|
|
|
ALTIUM_COMPOUND_FILE altiumPcbFile( aFileName );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Parse File
|
2024-05-01 01:21:48 +00:00
|
|
|
ALTIUM_PCB pcb( m_board, m_progressReporter, m_reporter );
|
2022-01-10 12:59:20 +00:00
|
|
|
pcb.Parse( altiumPcbFile, mapping );
|
|
|
|
}
|
|
|
|
catch( CFB::CFBException& exception )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( exception.what() );
|
|
|
|
}
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
|
|
return m_board;
|
|
|
|
}
|
2022-01-06 14:01:05 +00:00
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
long long PCB_IO_ALTIUM_DESIGNER::GetLibraryTimestamp( const wxString& aLibraryPath ) const
|
2022-01-06 14:01:05 +00:00
|
|
|
{
|
|
|
|
// File hasn't been loaded yet.
|
|
|
|
if( aLibraryPath.IsEmpty() )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFileName fn( aLibraryPath );
|
|
|
|
|
2023-10-01 13:42:41 +00:00
|
|
|
if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
|
2022-01-06 14:01:05 +00:00
|
|
|
{
|
|
|
|
return fn.GetModificationTime().GetValue().GetValue();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
void PCB_IO_ALTIUM_DESIGNER::loadAltiumLibrary( const wxString& aLibraryPath )
|
2023-11-17 04:20:15 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto it = m_fplibFiles.find( aLibraryPath );
|
|
|
|
|
|
|
|
if( it != m_fplibFiles.end() )
|
|
|
|
return; // Already loaded
|
|
|
|
|
|
|
|
if( aLibraryPath.Lower().EndsWith( wxS( ".pcblib" ) ) )
|
|
|
|
{
|
|
|
|
m_fplibFiles[aLibraryPath].emplace_back(
|
|
|
|
std::make_unique<ALTIUM_COMPOUND_FILE>( aLibraryPath ) );
|
|
|
|
}
|
|
|
|
else if( aLibraryPath.Lower().EndsWith( wxS( ".intlib" ) ) )
|
|
|
|
{
|
|
|
|
std::unique_ptr<ALTIUM_COMPOUND_FILE> intCom =
|
|
|
|
std::make_unique<ALTIUM_COMPOUND_FILE>( aLibraryPath );
|
|
|
|
|
|
|
|
std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> pcbLibFiles =
|
|
|
|
intCom->EnumDir( L"PCBLib" );
|
|
|
|
|
|
|
|
for( const auto& [pcbLibName, pcbCfe] : pcbLibFiles )
|
|
|
|
m_fplibFiles[aLibraryPath].push_back( intCom->DecodeIntLibStream( *pcbCfe ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( CFB::CFBException& exception )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( exception.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
void PCB_IO_ALTIUM_DESIGNER::FootprintEnumerate( wxArrayString& aFootprintNames,
|
2022-01-06 14:01:05 +00:00
|
|
|
const wxString& aLibraryPath, bool aBestEfforts,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2022-01-06 14:01:05 +00:00
|
|
|
{
|
2023-11-17 04:20:15 +00:00
|
|
|
loadAltiumLibrary( aLibraryPath );
|
|
|
|
|
2023-08-31 21:46:15 +00:00
|
|
|
auto it = m_fplibFiles.find( aLibraryPath );
|
|
|
|
|
|
|
|
if( it == m_fplibFiles.end() )
|
2023-11-17 04:20:15 +00:00
|
|
|
return; // No footprint libraries in file, ignore.
|
2022-01-10 13:31:23 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2023-11-17 04:20:15 +00:00
|
|
|
for( auto& altiumLibFile : it->second )
|
2022-01-10 13:31:23 +00:00
|
|
|
{
|
2023-11-17 04:20:15 +00:00
|
|
|
// Map code-page-dependent names to unicode names
|
|
|
|
std::map<wxString, wxString> patternMap = altiumLibFile->ListLibFootprints();
|
2022-01-10 13:31:23 +00:00
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
const std::vector<std::string> streamName = { "Library", "Data" };
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* libraryData = altiumLibFile->FindStream( streamName );
|
2022-01-10 13:31:23 +00:00
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
if( libraryData == nullptr )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( wxString::Format( _( "File not found: '%s'." ),
|
|
|
|
FormatPath( streamName ) ) );
|
|
|
|
}
|
2022-01-10 13:31:23 +00:00
|
|
|
|
2024-02-04 17:14:19 +00:00
|
|
|
ALTIUM_BINARY_PARSER parser( *altiumLibFile, libraryData );
|
2023-08-18 20:42:39 +00:00
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
std::map<wxString, wxString> properties = parser.ReadProperties();
|
2023-07-22 03:55:49 +00:00
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
uint32_t numberOfFootprints = parser.Read<uint32_t>();
|
|
|
|
aFootprintNames.Alloc( numberOfFootprints );
|
2023-07-22 03:55:49 +00:00
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
for( size_t i = 0; i < numberOfFootprints; i++ )
|
2023-07-22 03:55:49 +00:00
|
|
|
{
|
2023-11-17 04:20:15 +00:00
|
|
|
parser.ReadAndSetSubrecordLength();
|
|
|
|
|
|
|
|
wxScopedCharBuffer charBuffer = parser.ReadCharBuffer();
|
|
|
|
wxString fpPattern( charBuffer, wxConvISO8859_1 );
|
|
|
|
|
|
|
|
auto it = patternMap.find( fpPattern );
|
|
|
|
if( it != patternMap.end() )
|
|
|
|
{
|
|
|
|
aFootprintNames.Add( it->second ); // Proper unicode name
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR(
|
|
|
|
wxString::Format( "Component name not found: '%s'", fpPattern ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
parser.SkipSubrecord();
|
2023-07-22 03:55:49 +00:00
|
|
|
}
|
2023-11-17 04:20:15 +00:00
|
|
|
|
|
|
|
if( parser.HasParsingError() )
|
2023-07-22 03:55:49 +00:00
|
|
|
{
|
2023-11-17 04:20:15 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( "%s stream was not parsed correctly",
|
|
|
|
FormatPath( streamName ) ) );
|
2023-07-22 03:55:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
if( parser.GetRemainingBytes() != 0 )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( wxString::Format( "%s stream is not fully parsed",
|
|
|
|
FormatPath( streamName ) ) );
|
|
|
|
}
|
2022-01-10 13:31:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch( CFB::CFBException& exception )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( exception.what() );
|
|
|
|
}
|
2022-01-06 14:01:05 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
|
2023-12-24 01:21:58 +00:00
|
|
|
FOOTPRINT* PCB_IO_ALTIUM_DESIGNER::FootprintLoad( const wxString& aLibraryPath,
|
2022-01-06 14:01:05 +00:00
|
|
|
const wxString& aFootprintName, bool aKeepUUID,
|
2022-11-06 16:51:52 +00:00
|
|
|
const STRING_UTF8_MAP* aProperties )
|
2022-01-06 14:01:05 +00:00
|
|
|
{
|
2023-11-17 04:20:15 +00:00
|
|
|
loadAltiumLibrary( aLibraryPath );
|
|
|
|
|
2023-08-31 21:46:15 +00:00
|
|
|
auto it = m_fplibFiles.find( aLibraryPath );
|
|
|
|
|
|
|
|
if( it == m_fplibFiles.end() )
|
2024-02-19 12:24:13 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( _( "No footprints in library '%s'" ), aLibraryPath ) );
|
2022-01-10 13:31:23 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2024-02-19 12:24:13 +00:00
|
|
|
for( std::unique_ptr<ALTIUM_COMPOUND_FILE>& altiumLibFile : it->second )
|
2023-11-17 04:20:15 +00:00
|
|
|
{
|
|
|
|
auto [dirName, fpCfe] = altiumLibFile->FindLibFootprintDirName( aFootprintName );
|
|
|
|
|
|
|
|
if( dirName.IsEmpty() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Parse File
|
2024-05-01 01:21:48 +00:00
|
|
|
ALTIUM_PCB pcb( m_board, nullptr, m_reporter, aLibraryPath, aFootprintName );
|
2023-11-17 04:20:15 +00:00
|
|
|
return pcb.ParseFootprint( *altiumLibFile, aFootprintName );
|
|
|
|
}
|
2022-01-10 13:31:23 +00:00
|
|
|
}
|
|
|
|
catch( CFB::CFBException& exception )
|
|
|
|
{
|
|
|
|
THROW_IO_ERROR( exception.what() );
|
|
|
|
}
|
2023-11-17 04:20:15 +00:00
|
|
|
|
2024-02-19 12:24:13 +00:00
|
|
|
THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in '%s'." ),
|
|
|
|
aFootprintName,
|
|
|
|
aLibraryPath ) );
|
2024-05-01 01:21:48 +00:00
|
|
|
}
|