KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_allegro.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 Quilter
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
24
25#include "pcb_io_allegro.h"
26
27#include <board.h>
28#include <reporter.h>
29#include <fstream>
30#include <io/io_utils.h>
31#include <kiplatform/io.h>
32
34#include <allegro_builder.h>
35
36#include <core/profile.h>
37
38#include <stdexcept>
39
40
41static const wxChar* const traceAllegroPerf = wxT( "KICAD_ALLEGRO_PERF" );
42
43
44static bool checkFileHeader( const wxString& aFileName )
45{
46 // Pre-v18 files contain the string "all" at offset 0xF8 (start of version string)
47 static const std::vector<uint8_t> allegroVString = { 'a', 'l', 'l' };
48 static const size_t allegroVStringOffset = 0xf8;
49
50 if( IO_UTILS::fileHasBinaryHeader( aFileName, allegroVString, allegroVStringOffset ) )
51 return true;
52
53 // Files processed by Cadence dbdoctor replace the version string at 0xF8 with a
54 // database version string (e.g. "dbd..."), so the "all" check above fails.
55 // Detect these by checking the magic number at offset 0. The upper two bytes
56 // of the little-endian magic identify the major Allegro format family:
57 // 0x0013 = v16.x, 0x0014 = v17.x, 0x0015 = v18+
58 static const std::vector<uint8_t> v16Magic = { 0x13, 0x00 };
59 static const std::vector<uint8_t> v17Magic = { 0x14, 0x00 };
60 static const std::vector<uint8_t> v18Magic = { 0x15, 0x00 };
61 static const size_t magicMajorOffset = 2;
62
63 if( IO_UTILS::fileHasBinaryHeader( aFileName, v16Magic, magicMajorOffset ) )
64 return true;
65
66 if( IO_UTILS::fileHasBinaryHeader( aFileName, v17Magic, magicMajorOffset ) )
67 return true;
68
69 return IO_UTILS::fileHasBinaryHeader( aFileName, v18Magic, magicMajorOffset );
70}
71
72
73static std::map<wxString, PCB_LAYER_ID>
74allegroDefaultLayerMappingCallback( const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
75{
76 std::map<wxString, PCB_LAYER_ID> retval;
77
78 for( const INPUT_LAYER_DESC& layerDesc : aInputLayerDescriptionVector )
79 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
80
81 return retval;
82}
83
84
91
92
93bool PCB_IO_ALLEGRO::CanReadBoard( const wxString& aFileName ) const
94{
95 if( !PCB_IO::CanReadBoard( aFileName ) )
96 return false;
97
98 return checkFileHeader( aFileName );
99}
100
101
102bool PCB_IO_ALLEGRO::CanReadLibrary( const wxString& aFileName ) const
103{
104 if( !PCB_IO::CanReadLibrary( aFileName ) )
105 return false;
106
107 return false;
108}
109
110
111BOARD* PCB_IO_ALLEGRO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
112 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
113{
114 m_props = aProperties;
115 m_board = aAppendToMe ? aAppendToMe : new BOARD();
116
117 if( !aAppendToMe )
118 m_board->SetFileName( aFileName );
119
120 std::unique_ptr<BOARD> deleter( aAppendToMe ? nullptr : m_board );
121
122 std::unique_ptr<KIPLATFORM::IO::MAPPED_FILE> mappedFile;
123
124 try
125 {
126 mappedFile = std::make_unique<KIPLATFORM::IO::MAPPED_FILE>( aFileName );
127 }
128 catch( const std::runtime_error& e )
129 {
130 THROW_IO_ERROR( wxString::Format( wxS( "%s" ), e.what() ) );
131 }
132
133 if( !mappedFile->Data() || mappedFile->Size() == 0 )
134 THROW_IO_ERROR( wxString::Format( wxS( "File is empty: %s" ), aFileName ) );
135
136 if( !LoadBoardFromData( mappedFile->Data(), mappedFile->Size(), *m_board ) )
137 return nullptr;
138
139 (void) deleter.release();
140 return m_board;
141}
142
143
144bool PCB_IO_ALLEGRO::LoadBoardFromData( const uint8_t* aData, size_t aSize, BOARD& aBoard )
145{
146 ALLEGRO::FILE_STREAM allegroStream( aData, aSize );
147
148 ALLEGRO::PARSER parser( allegroStream, m_progressReporter );
149
150 // When parsing a file "for real", encountering an unknown block is fatal, as we then
151 // cannot know how long that block is, and thus can't proceed to find any later blocks.
152 parser.EndAtUnknownBlock( false );
153
154 PROF_TIMER totalTimer;
155
156 wxLogTrace( traceAllegroPerf, wxT( "=== Allegro Import Performance ===" ) );
157
158 // Import phase 1: turn the file into the C++ structs
159 PROF_TIMER phaseTimer;
160 std::unique_ptr<ALLEGRO::BRD_DB> brdDb = parser.Parse();
161 phaseTimer.Stop();
162
163 wxLogTrace( traceAllegroPerf, wxT( "Phase 1 (binary parse): %.3f ms" ), phaseTimer.msecs() ); //format:allow
164
166
167 // Import Phase 2: turn the C++ structs into the KiCad BOARD
169
170 phaseTimer.Start();
171 const bool phase2Ok = builder.BuildBoard();
172 phaseTimer.Stop();
173
174 wxLogTrace( traceAllegroPerf, wxT( "Phase 2 (board construction): %.3f ms" ), phaseTimer.msecs() ); //format:allow
175
176 if( !phase2Ok )
177 {
178 wxLogTrace( wxT( "KICAD_ALLEGRO" ), "Phase 2 board construction failed" );
179 reporter.Report( _( "Failed to build board from Allegro data" ), RPT_SEVERITY_ERROR );
180 return false;
181 }
182
183 wxLogTrace( wxT( "KICAD_ALLEGRO" ), "Board construction completed successfully" );
184 wxLogTrace( traceAllegroPerf, wxT( "LoadBoard total (Phase 1 + Phase 2): %.3f ms" ), totalTimer.msecs() ); //format:allow
185
186 aBoard.m_LegacyNetclassesLoaded = true;
187 aBoard.m_LegacyDesignSettingsLoaded = true;
188
189 return true;
190}
Class that builds a KiCad board from a BRD_DB (= FILE_HEADER + STRINGS + OBJECTS + bookkeeping)
Stream that reads primitive types from a memory buffer containing Allegro .brd (or ....
Class that parses a single FILE_STREAM into a BRD_DB, and handles any state involved in that parsing.
std::unique_ptr< BRD_DB > Parse()
void EndAtUnknownBlock(bool aEndAtUnknownBlock)
When set to true, the parser will stop at the first unknown block, rather than throwing an error.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition board.h:493
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition board.h:497
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:237
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition io_base.cpp:71
virtual void RegisterCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler)
Register a different handler to be called when mapping of input layers to KiCad layers occurs.
LAYER_MAPPING_HANDLER m_layer_mapping_handler
Callback to get layer mapping.
static REPORTER & GetInstance()
Definition reporter.cpp:120
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties, PROJECT *aProject) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
bool LoadBoardFromData(const uint8_t *aData, size_t aSize, BOARD &aBoard)
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:349
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:38
PCB_IO(const wxString &aName)
Definition pcb_io.h:342
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:352
A small class to help profiling.
Definition profile.h:46
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:86
void Start()
Start or restart the counter.
Definition profile.h:74
double msecs(bool aSinceLast=false)
Definition profile.h:147
Container for project specific data.
Definition project.h:62
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
static REPORTER & GetInstance()
Definition reporter.cpp:220
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
bool fileHasBinaryHeader(const wxString &aFilePath, const std::vector< uint8_t > &aHeader, size_t aOffset)
Check if a file starts with a defined binary header.
Definition io_utils.cpp:57
static std::map< wxString, PCB_LAYER_ID > allegroDefaultLayerMappingCallback(const std::vector< INPUT_LAYER_DESC > &aInputLayerDescriptionVector)
static bool checkFileHeader(const wxString &aFileName)
static const wxChar *const traceAllegroPerf
@ RPT_SEVERITY_ERROR
Describes an imported layer and how it could be mapped to KiCad Layers.
IbisParser parser & reporter