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, you may find one here:
19 * http://www.gnu.org/licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
28
29#include "pcb_io_allegro.h"
30
31#include <board.h>
32#include <reporter.h>
33#include <fstream>
34#include <io/io_utils.h>
35#include <kiplatform/io.h>
36
38#include <allegro_builder.h>
39
40#include <core/profile.h>
41
42#include <stdexcept>
43
44
45static const wxChar* const traceAllegroPerf = wxT( "KICAD_ALLEGRO_PERF" );
46
47
48static bool checkFileHeader( const wxString& aFileName )
49{
50 // Pre-v18 files contain the string "all" at offset 0xF8 (start of version string)
51 static const std::vector<uint8_t> allegroVString = { 'a', 'l', 'l' };
52 static const size_t allegroVStringOffset = 0xf8;
53
54 if( IO_UTILS::fileHasBinaryHeader( aFileName, allegroVString, allegroVStringOffset ) )
55 return true;
56
57 // Files processed by Cadence dbdoctor replace the version string at 0xF8 with a
58 // database version string (e.g. "dbd..."), so the "all" check above fails.
59 // Detect these by checking the magic number at offset 0. The upper two bytes
60 // of the little-endian magic identify the major Allegro format family:
61 // 0x0013 = v16.x, 0x0014 = v17.x, 0x0015 = v18+
62 static const std::vector<uint8_t> v16Magic = { 0x13, 0x00 };
63 static const std::vector<uint8_t> v17Magic = { 0x14, 0x00 };
64 static const std::vector<uint8_t> v18Magic = { 0x15, 0x00 };
65 static const size_t magicMajorOffset = 2;
66
67 if( IO_UTILS::fileHasBinaryHeader( aFileName, v16Magic, magicMajorOffset ) )
68 return true;
69
70 if( IO_UTILS::fileHasBinaryHeader( aFileName, v17Magic, magicMajorOffset ) )
71 return true;
72
73 return IO_UTILS::fileHasBinaryHeader( aFileName, v18Magic, 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
95
96
97bool PCB_IO_ALLEGRO::CanReadBoard( const wxString& aFileName ) const
98{
99 if( !PCB_IO::CanReadBoard( aFileName ) )
100 return false;
101
102 return checkFileHeader( aFileName );
103}
104
105
106bool PCB_IO_ALLEGRO::CanReadLibrary( const wxString& aFileName ) const
107{
108 if( !PCB_IO::CanReadLibrary( aFileName ) )
109 return false;
110
111 return false;
112}
113
114
115BOARD* PCB_IO_ALLEGRO::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
116 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
117{
118 m_props = aProperties;
119 m_board = aAppendToMe ? aAppendToMe : new BOARD();
120
121 if( !aAppendToMe )
122 m_board->SetFileName( aFileName );
123
124 std::unique_ptr<BOARD> deleter( aAppendToMe ? nullptr : m_board );
125
126 std::unique_ptr<KIPLATFORM::IO::MAPPED_FILE> mappedFile;
127
128 try
129 {
130 mappedFile = std::make_unique<KIPLATFORM::IO::MAPPED_FILE>( aFileName );
131 }
132 catch( const std::runtime_error& e )
133 {
134 THROW_IO_ERROR( wxString::Format( wxS( "%s" ), e.what() ) );
135 }
136
137 if( !mappedFile->Data() || mappedFile->Size() == 0 )
138 THROW_IO_ERROR( wxString::Format( wxS( "File is empty: %s" ), aFileName ) );
139
140 ALLEGRO::FILE_STREAM allegroStream( mappedFile->Data(), mappedFile->Size() );
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
159 // Import Phase 2: turn the C++ structs into the KiCad BOARD
162
163 phaseTimer.Start();
164 const bool phase2Ok = builder.BuildBoard();
165 phaseTimer.Stop();
166
167 wxLogTrace( traceAllegroPerf, wxT( "Phase 2 (board construction): %.3f ms" ), phaseTimer.msecs() ); //format:allow
168
169 if( !phase2Ok )
170 {
171 wxLogTrace( wxT( "KICAD_ALLEGRO" ), "Phase 2 board construction failed" );
172 m_reporter->Report( _( "Failed to build board from Allegro data" ), RPT_SEVERITY_ERROR );
173 return nullptr;
174 }
175
176 wxLogTrace( wxT( "KICAD_ALLEGRO" ), "Board construction completed successfully" );
177 wxLogTrace( traceAllegroPerf, wxT( "LoadBoard total (Phase 1 + Phase 2): %.3f ms" ), totalTimer.msecs() ); //format:allow
178
179 m_board->m_LegacyNetclassesLoaded = true;
180 m_board->m_LegacyDesignSettingsLoaded = true;
181
182 (void) deleter.release();
183 return m_board;
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 RAW_BOARD, and handles any state involved in that parsi...
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:322
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.
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 ...
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:344
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:42
PCB_IO(const wxString &aName)
Definition pcb_io.h:337
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:347
A small class to help profiling.
Definition profile.h:49
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:88
void Start()
Start or restart the counter.
Definition profile.h:77
double msecs(bool aSinceLast=false)
Definition profile.h:149
Container for project specific data.
Definition project.h:65
static REPORTER & GetInstance()
Definition reporter.cpp:195
#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:59
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.