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 <ki_exception.h>
29#include <reporter.h>
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.x, 0x0016 = revision 19
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 std::vector<uint8_t> v19Magic = { 0x16, 0x00 };
62 static const size_t magicMajorOffset = 2;
63
64 if( IO_UTILS::fileHasBinaryHeader( aFileName, v16Magic, magicMajorOffset ) )
65 return true;
66
67 if( IO_UTILS::fileHasBinaryHeader( aFileName, v17Magic, magicMajorOffset ) )
68 return true;
69
70 if( IO_UTILS::fileHasBinaryHeader( aFileName, v18Magic, magicMajorOffset ) )
71 return true;
72
73 return IO_UTILS::fileHasBinaryHeader( aFileName, v19Magic, magicMajorOffset );
74}
75
76
77static std::map<wxString, PCB_LAYER_ID>
78allegroDefaultLayerMappingCallback( const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
79{
80 std::map<wxString, PCB_LAYER_ID> retval;
81
82 for( const INPUT_LAYER_DESC& layerDesc : aInputLayerDescriptionVector )
83 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
84
85 return retval;
86}
87
88
93
94
95bool PCB_IO_ALLEGRO::CanReadBoard( const wxString& aFileName ) const
96{
97 if( !PCB_IO::CanReadBoard( aFileName ) )
98 return false;
99
100 return checkFileHeader( aFileName );
101}
102
103
104bool PCB_IO_ALLEGRO::CanReadLibrary( const wxString& aFileName ) const
105{
106 if( !PCB_IO::CanReadLibrary( aFileName ) )
107 return false;
108
109 return false;
110}
111
112
113void PCB_IO_ALLEGRO::loadBoard( const wxString& aFileName, BOARD& aBoard, bool aIsNewLoad,
114 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
115{
116 m_props = aProperties;
117 m_board = &aBoard;
118
119 std::unique_ptr<KIPLATFORM::IO::MAPPED_FILE> mappedFile;
120
121 try
122 {
123 mappedFile = std::make_unique<KIPLATFORM::IO::MAPPED_FILE>( aFileName );
124 }
125 catch( const std::runtime_error& e )
126 {
127 THROW_IO_ERROR( e.what() );
128 }
129
130 if( !mappedFile->Data() || mappedFile->Size() == 0 )
131 THROW_IO_ERRORF( _( "File is empty: %s" ), aFileName );
132
133 if( !LoadBoardFromData( mappedFile->Data(), mappedFile->Size(), *m_board ) )
134 THROW_IO_ERRORF( _( "Failed to load Allegro board from file: %s" ), aFileName );
135}
136
137
138bool PCB_IO_ALLEGRO::LoadBoardFromData( const uint8_t* aData, size_t aSize, BOARD& aBoard )
139{
140 ALLEGRO::FILE_STREAM allegroStream( aData, aSize );
141
142 ALLEGRO::PARSER parser( allegroStream, m_progressReporter );
143
144 // When parsing a file "for real", encountering an unknown block is fatal, as we then
145 // cannot know how long that block is, and thus can't proceed to find any later blocks.
146 parser.EndAtUnknownBlock( false );
147
148 PROF_TIMER totalTimer;
149
150 wxLogTrace( traceAllegroPerf, wxT( "=== Allegro Import Performance ===" ) );
151
152 // Import phase 1: turn the file into the C++ structs
153 PROF_TIMER phaseTimer;
154 std::unique_ptr<ALLEGRO::BRD_DB> brdDb = parser.Parse();
155 phaseTimer.Stop();
156
157 wxLogTrace( traceAllegroPerf, wxT( "Phase 1 (binary parse): %.3f ms" ), phaseTimer.msecs() ); //format:allow
158
160
161 // Import Phase 2: turn the C++ structs into the KiCad BOARD
163
164 phaseTimer.Start();
165 const bool phase2Ok = builder.BuildBoard();
166 phaseTimer.Stop();
167
168 wxLogTrace( traceAllegroPerf, wxT( "Phase 2 (board construction): %.3f ms" ), phaseTimer.msecs() ); //format:allow
169
170 if( !phase2Ok )
171 {
172 wxLogTrace( wxT( "KICAD_ALLEGRO" ), "Phase 2 board construction failed" );
173 reporter.Report( _( "Failed to build board from Allegro data" ), RPT_SEVERITY_ERROR );
174 return false;
175 }
176
177 wxLogTrace( wxT( "KICAD_ALLEGRO" ), "Board construction completed successfully" );
178 wxLogTrace( traceAllegroPerf, wxT( "LoadBoard total (Phase 1 + Phase 2): %.3f ms" ), totalTimer.msecs() ); //format:allow
179
180 aBoard.m_LegacyNetclassesLoaded = true;
181 aBoard.m_LegacyDesignSettingsLoaded = true;
182
183 return true;
184}
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:409
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition board.h:555
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition board.h:559
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition io_base.h:238
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:241
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:207
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.
void loadBoard(const wxString &aFileName, BOARD &aBoard, bool aIsNewLoad, const std::map< std::string, UTF8 > *aProperties, PROJECT *aProject) override
Parse aFileName into aBoard.
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:368
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:40
PCB_IO(const wxString &aName)
Definition pcb_io.h:351
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:371
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:63
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
#define _(s)
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
#define THROW_IO_ERRORF(msg,...)
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:60
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