KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_altium_designer.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 (C) 2019 Thomas Pointhuber <[email protected]>
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 2
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/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 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
29#include <wx/string.h>
30
31#include <font/fontconfig.h>
33#include <altium_pcb.h>
35#include <io/io_utils.h>
37#include <pcb_io/pcb_io.h>
38#include <reporter.h>
39
40#include <board.h>
41
42#include <compoundfilereader.h>
43#include <utf.h>
44
46 PCB_IO( wxS( "Altium Designer" ) )
47{
49
51}
52
53
55{
56}
57
58
60 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
61{
62 std::map<wxString, PCB_LAYER_ID> retval;
63
64 // Just return a the auto-mapped layers
65 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
66 {
67 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
68 }
69
70 return retval;
71}
72
73
74bool PCB_IO_ALTIUM_DESIGNER::checkFileHeader( const wxString& aFileName )
75{
76 // Compound File Binary Format header
78}
79
80
81bool PCB_IO_ALTIUM_DESIGNER::CanReadBoard( const wxString& aFileName ) const
82{
83 if( !PCB_IO::CanReadBoard( aFileName ) )
84 return false;
85
86 return checkFileHeader( aFileName );
87}
88
89
90bool PCB_IO_ALTIUM_DESIGNER::CanReadLibrary( const wxString& aFileName ) const
91{
92 if( !PCB_IO::CanReadLibrary( aFileName ) )
93 return false;
94
95 return checkFileHeader( aFileName );
96}
97
98
99BOARD* PCB_IO_ALTIUM_DESIGNER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
100 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
101{
102
103 m_props = aProperties;
104
105 m_board = aAppendToMe ? aAppendToMe : new BOARD();
106
108
109 // Give the filename to the board if it's new
110 if( !aAppendToMe )
111 m_board->SetFileName( aFileName );
112
113 // clang-format off
114 const std::map<ALTIUM_PCB_DIR, std::string> mapping = {
115 { ALTIUM_PCB_DIR::FILE_HEADER, "FileHeader" },
116 { ALTIUM_PCB_DIR::ARCS6, "Arcs6" },
117 { ALTIUM_PCB_DIR::BOARD6, "Board6" },
118 { ALTIUM_PCB_DIR::BOARDREGIONS, "BoardRegions" },
119 { ALTIUM_PCB_DIR::CLASSES6, "Classes6" },
120 { ALTIUM_PCB_DIR::COMPONENTS6, "Components6" },
121 { ALTIUM_PCB_DIR::COMPONENTBODIES6, "ComponentBodies6" },
122 { ALTIUM_PCB_DIR::DIMENSIONS6, "Dimensions6" },
123 { ALTIUM_PCB_DIR::EXTENDPRIMITIVEINFORMATION, "ExtendedPrimitiveInformation" },
124 { ALTIUM_PCB_DIR::FILLS6, "Fills6" },
125 { ALTIUM_PCB_DIR::MODELS, "Models" },
126 { ALTIUM_PCB_DIR::NETS6, "Nets6" },
127 { ALTIUM_PCB_DIR::PADS6, "Pads6" },
128 { ALTIUM_PCB_DIR::POLYGONS6, "Polygons6" },
129 { ALTIUM_PCB_DIR::REGIONS6, "Regions6" },
130 { ALTIUM_PCB_DIR::RULES6, "Rules6" },
131 { ALTIUM_PCB_DIR::SHAPEBASEDREGIONS6, "ShapeBasedRegions6" },
132 { ALTIUM_PCB_DIR::TEXTS6, "Texts6" },
133 { ALTIUM_PCB_DIR::TRACKS6, "Tracks6" },
134 { ALTIUM_PCB_DIR::VIAS6, "Vias6" },
135 { ALTIUM_PCB_DIR::WIDESTRINGS6, "WideStrings6" }
136 };
137 // clang-format on
138
139 ALTIUM_PCB_COMPOUND_FILE altiumPcbFile( aFileName );
140
141 try
142 {
143 // Parse File
145 pcb.Parse( altiumPcbFile, mapping );
146 }
147 catch( CFB::CFBException& exception )
148 {
149 THROW_IO_ERROR( exception.what() );
150 }
151
152 return m_board;
153}
154
155
156long long PCB_IO_ALTIUM_DESIGNER::GetLibraryTimestamp( const wxString& aLibraryPath ) const
157{
158 // File hasn't been loaded yet.
159 if( aLibraryPath.IsEmpty() )
160 return 0;
161
162 wxFileName fn( aLibraryPath );
163
164 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
165 return fn.GetModificationTime().GetValue().GetValue();
166 else
167 return 0;
168}
169
170
171void PCB_IO_ALTIUM_DESIGNER::loadAltiumLibrary( const wxString& aLibraryPath )
172{
174
175 long long timestamp = GetLibraryTimestamp( aLibraryPath );
176
177 if( m_fplibFiles.contains( aLibraryPath ) && m_fplibFiles[aLibraryPath].m_Timestamp == timestamp )
178 return; // Already loaded
179
180 try
181 {
182 ALTIUM_FILE_CACHE& libFiles = m_fplibFiles[aLibraryPath];
183
184 if( aLibraryPath.Lower().EndsWith( wxS( ".pcblib" ) ) )
185 {
186 libFiles.m_Files.emplace_back( std::make_unique<ALTIUM_PCB_COMPOUND_FILE>( aLibraryPath ) );
187 }
188 else if( aLibraryPath.Lower().EndsWith( wxS( ".intlib" ) ) )
189 {
190 std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE> lib = std::make_unique<ALTIUM_PCB_COMPOUND_FILE>( aLibraryPath );
191
192 for( const auto& [pcbLibName, pcbCfe] : lib->EnumDir( L"PCBLib" ) )
193 {
194 std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE> libFile = std::make_unique<ALTIUM_PCB_COMPOUND_FILE>();
195
196 if( lib->DecodeIntLibStream( *pcbCfe, libFile.get() ) )
197 libFiles.m_Files.emplace_back( std::move( libFile ) );
198 }
199 }
200
201 libFiles.m_Timestamp = timestamp;
202 }
203 catch( CFB::CFBException& exception )
204 {
205 THROW_IO_ERROR( exception.what() );
206 }
207}
208
209
210void PCB_IO_ALTIUM_DESIGNER::FootprintEnumerate( wxArrayString& aFootprintNames,
211 const wxString& aLibraryPath, bool aBestEfforts,
212 const std::map<std::string, UTF8>* aProperties )
213{
214 loadAltiumLibrary( aLibraryPath );
215
216 if( !m_fplibFiles.contains( aLibraryPath ) )
217 return; // No footprint libraries in file, ignore.
218
219 try
220 {
221 for( std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE>& altiumLibFile : m_fplibFiles[aLibraryPath].m_Files )
222 {
223 // Map code-page-dependent names to unicode names
224 CASE_INSENSITIVE_MAP<wxString> patternMap = altiumLibFile->ListLibFootprints();
225
226 const std::vector<std::string> streamName = { "Library", "Data" };
227 const CFB::COMPOUND_FILE_ENTRY* libraryData = altiumLibFile->FindStream( streamName );
228
229 if( libraryData == nullptr )
230 {
231 THROW_IO_ERROR( wxString::Format( _( "File not found: '%s'." ),
232 FormatPath( streamName ) ) );
233 }
234
235 ALTIUM_BINARY_PARSER parser( *altiumLibFile, libraryData );
236
237 std::map<wxString, wxString> properties = parser.ReadProperties();
238
239 uint32_t numberOfFootprints = parser.Read<uint32_t>();
240
241 // The number of footprints might be bogus. Following it is a list of index strings to
242 // find the footprints, and this list might actually be longer than how many footprints
243 // actually are in the library.
244 // If this case was detected, truncate the list to the number of footprints.
245 // Sample in #18452 shows that by simply truncating can get the correct footprints.
246 // Fixes https://gitlab.com/kicad/code/kicad/issues/18452.
247 // After truncation we shall no longer detect if the stream is fully parsed.
248 bool footprintListNotTruncated = true;
249
250 if ( patternMap.size() < numberOfFootprints )
251 {
252 numberOfFootprints = patternMap.size();
253 footprintListNotTruncated = false;
254 }
255
256 aFootprintNames.Alloc( numberOfFootprints );
257
258 for( size_t i = 0; i < numberOfFootprints; i++ )
259 {
261
262 wxScopedCharBuffer charBuffer = parser.ReadCharBuffer();
263 wxString fpPattern( charBuffer, wxConvISO8859_1 );
264
265 auto it = patternMap.find( fpPattern );
266
267 if( it != patternMap.end() )
268 {
269 aFootprintNames.Add( it->second ); // Proper unicode name
270 }
271 else
272 {
273 THROW_IO_ERROR( wxString::Format( "Component name not found: '%s'", fpPattern ) );
274 }
275
276 parser.SkipSubrecord();
277 }
278
279 if( parser.HasParsingError() )
280 {
281 THROW_IO_ERROR( wxString::Format( "%s stream was not parsed correctly",
282 FormatPath( streamName ) ) );
283 }
284
285 if( footprintListNotTruncated && parser.GetRemainingBytes() != 0 )
286 {
287 THROW_IO_ERROR( wxString::Format( "%s stream is not fully parsed",
288 FormatPath( streamName ) ) );
289 }
290 }
291 }
292 catch( CFB::CFBException& exception )
293 {
294 THROW_IO_ERROR( exception.what() );
295 }
296}
297
298
300 const wxString& aFootprintName, bool aKeepUUID,
301 const std::map<std::string, UTF8>* aProperties )
302{
303 loadAltiumLibrary( aLibraryPath );
304
305 if( !m_fplibFiles.contains( aLibraryPath ) )
306 THROW_IO_ERROR( wxString::Format( _( "No footprints in library '%s'" ), aLibraryPath ) );
307
308 try
309 {
310 for( std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE>& altiumLibFile : m_fplibFiles[aLibraryPath].m_Files )
311 {
312 altiumLibFile->CacheLibModels();
313 auto [dirName, fpCfe] = altiumLibFile->FindLibFootprintDirName( aFootprintName );
314
315 if( dirName.IsEmpty() )
316 continue;
317
318 // Parse File
319 ALTIUM_PCB pcb( m_board, nullptr, m_layer_mapping_handler, m_reporter, aLibraryPath, aFootprintName );
320 return pcb.ParseFootprint( *altiumLibFile, aFootprintName );
321 }
322 }
323 catch( CFB::CFBException& exception )
324 {
325 THROW_IO_ERROR( exception.what() );
326 }
327
328 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in '%s'." ),
329 aFootprintName,
330 aLibraryPath ) );
331}
332
333
335{
336 std::vector<FOOTPRINT*> footprints;
337
338 for( FOOTPRINT* fp : m_board->Footprints() )
339 footprints.push_back( fp );
340
341 return footprints;
342}
std::string FormatPath(const std::vector< std::string > &aVectorPath)
Helper for debug logging (vector -> string)
std::map< wxString, ValueType, DETAIL::CASE_INSENSITIVE_COMPARER > CASE_INSENSITIVE_MAP
size_t GetRemainingBytes() const
wxScopedCharBuffer ReadCharBuffer()
std::map< wxString, wxString > ReadProperties(std::function< std::map< wxString, wxString >(const std::string &)> handleBinaryData=[](const std::string &) { return std::map< wxString, wxString >();})
FOOTPRINT * ParseFootprint(ALTIUM_PCB_COMPOUND_FILE &altiumLibFile, const wxString &aFootprintName)
Definition: altium_pcb.cpp:681
void Parse(const ALTIUM_PCB_COMPOUND_FILE &aAltiumPcbFile, const std::map< ALTIUM_PCB_DIR, std::string > &aFileMapping)
Definition: altium_pcb.cpp:340
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:317
void SetFileName(const wxString &aFileName)
Definition: board.h:352
const FOOTPRINTS & Footprints() const
Definition: board.h:358
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition: io_base.h:220
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:223
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.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
static bool checkFileHeader(const wxString &aFileName)
PCB_IO_ALTIUM_DESIGNER()
Pcbnew PLUGIN for Altium *.PcbDoc format.
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
void loadAltiumLibrary(const wxString &aLibraryPath)
static std::map< wxString, PCB_LAYER_ID > DefaultLayerMappingCallback(const std::vector< INPUT_LAYER_DESC > &aInputLayerDescriptionVector)
Return the automapped layers.
std::map< wxString, ALTIUM_FILE_CACHE > m_fplibFiles
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:71
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:324
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition: pcb_io.cpp:42
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:327
Container for project specific data.
Definition: project.h:65
static REPORTER & GetInstance()
Definition: reporter.cpp:202
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:64
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
bool fileStartsWithBinaryHeader(const wxString &aFilePath, const std::vector< uint8_t > &aHeader)
Check if a file starts with a defined binary header.
Definition: io_utils.cpp:59
const std::vector< uint8_t > COMPOUND_FILE_HEADER
Definition: io_utils.cpp:31
Describes an imported layer and how it could be mapped to KiCad Layers.
std::vector< std::unique_ptr< ALTIUM_PCB_COMPOUND_FILE > > m_Files