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
28
29#include <wx/string.h>
30
31#include <font/fontconfig.h>
33#include <altium_pcb.h>
35#include <io/io_utils.h>
38#include <pcb_io/pcb_io.h>
39#include <reporter.h>
40
41#include <board.h>
42#include <footprint.h>
43
44#include <compoundfilereader.h>
45#include <utf.h>
46
47
49 const std::vector<ALTIUM_PROJECT_VARIANT>& aVariants )
50{
51 std::map<wxString, FOOTPRINT*> fpByRef;
52 std::map<KIID, FOOTPRINT*> fpByUid;
53
54 for( FOOTPRINT* fp : aBoard->Footprints() )
55 {
56 fpByRef[fp->GetReference()] = fp;
57
58 // The Altium PCB importer stores sourceUniqueID as the last element of the
59 // footprint path. Use it to disambiguate repeated designators.
60 const KIID_PATH& path = fp->GetPath();
61
62 if( path.size() >= 2 )
63 fpByUid[path.back()] = fp;
64 }
65
66 for( const ALTIUM_PROJECT_VARIANT& pv : aVariants )
67 {
68 aBoard->AddVariant( pv.name );
69
70 if( !pv.description.empty() && pv.description != pv.name )
71 aBoard->SetVariantDescription( pv.name, pv.description );
72
73 for( const ALTIUM_VARIANT_ENTRY& entry : pv.variations )
74 {
75 FOOTPRINT* target = nullptr;
76
77 // Prefer UniqueId matching to handle repeated designators correctly
78 if( !entry.uniqueId.empty() )
79 {
80 wxString normalizedUid = entry.uniqueId;
81
82 if( normalizedUid.starts_with( wxT( "\\" ) ) )
83 normalizedUid = normalizedUid.Mid( 1 );
84
85 auto it = fpByUid.find( KIID( normalizedUid ) );
86
87 if( it != fpByUid.end() )
88 target = it->second;
89 }
90
91 if( !target )
92 {
93 auto it = fpByRef.find( entry.designator );
94
95 if( it != fpByRef.end() )
96 target = it->second;
97 }
98
99 if( !target )
100 continue;
101
102 FOOTPRINT_VARIANT* fpVariant = target->AddVariant( pv.name );
103
104 if( !fpVariant )
105 continue;
106
107 if( entry.kind == 1 )
108 {
109 fpVariant->SetDNP( true );
110 fpVariant->SetExcludedFromBOM( true );
111 fpVariant->SetExcludedFromPosFiles( true );
112 }
113 else if( entry.kind == 0 )
114 {
115 for( const auto& [key, value] : entry.alternateFields )
116 {
117 if( key.CmpNoCase( wxS( "LibReference" ) ) == 0 )
118 fpVariant->SetFieldValue( wxS( "Value" ), value );
119 else if( key.CmpNoCase( wxS( "Description" ) ) == 0 )
120 fpVariant->SetFieldValue( wxS( "Description" ), value );
121 else if( key.CmpNoCase( wxS( "Footprint" ) ) == 0 )
122 fpVariant->SetFieldValue( wxS( "Footprint" ), value );
123 }
124 }
125 }
126 }
127}
128
136
137
141
142
144 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
145{
146 std::map<wxString, PCB_LAYER_ID> retval;
147
148 // Just return a the auto-mapped layers
149 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
150 {
151 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
152 }
153
154 return retval;
155}
156
157
158bool PCB_IO_ALTIUM_DESIGNER::checkFileHeader( const wxString& aFileName )
159{
160 // Compound File Binary Format header
162}
163
164
165bool PCB_IO_ALTIUM_DESIGNER::CanReadBoard( const wxString& aFileName ) const
166{
167 if( !PCB_IO::CanReadBoard( aFileName ) )
168 return false;
169
170 return checkFileHeader( aFileName );
171}
172
173
174bool PCB_IO_ALTIUM_DESIGNER::CanReadLibrary( const wxString& aFileName ) const
175{
176 if( !PCB_IO::CanReadLibrary( aFileName ) )
177 return false;
178
179 return checkFileHeader( aFileName );
180}
181
182
183BOARD* PCB_IO_ALTIUM_DESIGNER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
184 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
185{
186
187 m_props = aProperties;
188
189 m_board = aAppendToMe ? aAppendToMe : new BOARD();
190
191 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
193
194 // Give the filename to the board if it's new
195 if( !aAppendToMe )
196 m_board->SetFileName( aFileName );
197
198 // clang-format off
199 const std::map<ALTIUM_PCB_DIR, std::string> mapping = {
200 { ALTIUM_PCB_DIR::FILE_HEADER, "FileHeader" },
201 { ALTIUM_PCB_DIR::ARCS6, "Arcs6" },
202 { ALTIUM_PCB_DIR::BOARD6, "Board6" },
203 { ALTIUM_PCB_DIR::BOARDREGIONS, "BoardRegions" },
204 { ALTIUM_PCB_DIR::CLASSES6, "Classes6" },
205 { ALTIUM_PCB_DIR::COMPONENTS6, "Components6" },
206 { ALTIUM_PCB_DIR::COMPONENTBODIES6, "ComponentBodies6" },
207 { ALTIUM_PCB_DIR::DIMENSIONS6, "Dimensions6" },
208 { ALTIUM_PCB_DIR::EXTENDPRIMITIVEINFORMATION, "ExtendedPrimitiveInformation" },
209 { ALTIUM_PCB_DIR::FILLS6, "Fills6" },
210 { ALTIUM_PCB_DIR::MODELS, "Models" },
211 { ALTIUM_PCB_DIR::NETS6, "Nets6" },
212 { ALTIUM_PCB_DIR::PADS6, "Pads6" },
213 { ALTIUM_PCB_DIR::POLYGONS6, "Polygons6" },
214 { ALTIUM_PCB_DIR::REGIONS6, "Regions6" },
215 { ALTIUM_PCB_DIR::RULES6, "Rules6" },
216 { ALTIUM_PCB_DIR::SHAPEBASEDREGIONS6, "ShapeBasedRegions6" },
217 { ALTIUM_PCB_DIR::TEXTS6, "Texts6" },
218 { ALTIUM_PCB_DIR::TRACKS6, "Tracks6" },
219 { ALTIUM_PCB_DIR::VIAS6, "Vias6" },
220 { ALTIUM_PCB_DIR::WIDESTRINGS6, "WideStrings6" }
221 };
222 // clang-format on
223
224 ALTIUM_PCB_COMPOUND_FILE altiumPcbFile( aFileName );
225
226 try
227 {
228 // Parse File
230 pcb.Parse( altiumPcbFile, mapping );
231 }
232 catch( CFB::CFBException& exception )
233 {
234 THROW_IO_ERROR( exception.what() );
235 }
236
237 if( m_props && m_props->count( "project_file" ) )
238 {
239 auto variants = ParseAltiumProjectVariants( m_props->at( "project_file" ) );
240
241 if( !variants.empty() )
243 }
244
245 return m_board;
246}
247
248
249long long PCB_IO_ALTIUM_DESIGNER::GetLibraryTimestamp( const wxString& aLibraryPath ) const
250{
251 // File hasn't been loaded yet.
252 if( aLibraryPath.IsEmpty() )
253 return 0;
254
255 wxFileName fn( aLibraryPath );
256
257 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
258 return fn.GetModificationTime().GetValue().GetValue();
259 else
260 return 0;
261}
262
263
264void PCB_IO_ALTIUM_DESIGNER::loadAltiumLibrary( const wxString& aLibraryPath )
265{
266 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
267 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
268
269 long long timestamp = GetLibraryTimestamp( aLibraryPath );
270
271 if( m_fplibFiles.contains( aLibraryPath ) && m_fplibFiles[aLibraryPath].m_Timestamp == timestamp )
272 return; // Already loaded
273
274 try
275 {
276 ALTIUM_FILE_CACHE& libFiles = m_fplibFiles[aLibraryPath];
277
278 if( aLibraryPath.Lower().EndsWith( wxS( ".pcblib" ) ) )
279 {
280 libFiles.m_Files.emplace_back( std::make_unique<ALTIUM_PCB_COMPOUND_FILE>( aLibraryPath ) );
281 }
282 else if( aLibraryPath.Lower().EndsWith( wxS( ".intlib" ) ) )
283 {
284 std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE> lib = std::make_unique<ALTIUM_PCB_COMPOUND_FILE>( aLibraryPath );
285
286 for( const auto& [pcbLibName, pcbCfe] : lib->EnumDir( L"PCBLib" ) )
287 {
288 std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE> libFile = std::make_unique<ALTIUM_PCB_COMPOUND_FILE>();
289
290 if( lib->DecodeIntLibStream( *pcbCfe, libFile.get() ) )
291 libFiles.m_Files.emplace_back( std::move( libFile ) );
292 }
293 }
294
295 libFiles.m_Timestamp = timestamp;
296 }
297 catch( CFB::CFBException& exception )
298 {
299 THROW_IO_ERROR( exception.what() );
300 }
301}
302
303
304void PCB_IO_ALTIUM_DESIGNER::FootprintEnumerate( wxArrayString& aFootprintNames,
305 const wxString& aLibraryPath, bool aBestEfforts,
306 const std::map<std::string, UTF8>* aProperties )
307{
308 loadAltiumLibrary( aLibraryPath );
309
310 if( !m_fplibFiles.contains( aLibraryPath ) )
311 return; // No footprint libraries in file, ignore.
312
313 try
314 {
315 for( std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE>& altiumLibFile : m_fplibFiles[aLibraryPath].m_Files )
316 {
317 // Map code-page-dependent names to unicode names
318 CASE_INSENSITIVE_MAP<wxString> patternMap = altiumLibFile->ListLibFootprints();
319
320 const std::vector<std::string> streamName = { "Library", "Data" };
321 const CFB::COMPOUND_FILE_ENTRY* libraryData = altiumLibFile->FindStream( streamName );
322
323 if( libraryData == nullptr )
324 {
325 THROW_IO_ERROR( wxString::Format( _( "File not found: '%s'." ),
326 FormatPath( streamName ) ) );
327 }
328
329 ALTIUM_BINARY_PARSER parser( *altiumLibFile, libraryData );
330
331 std::map<wxString, wxString> properties = parser.ReadProperties();
332
333 uint32_t numberOfFootprints = parser.Read<uint32_t>();
334
335 // The number of footprints might be bogus. Following it is a list of index strings to
336 // find the footprints, and this list might actually be longer than how many footprints
337 // actually are in the library.
338 // If this case was detected, truncate the list to the number of footprints.
339 // Sample in #18452 shows that by simply truncating can get the correct footprints.
340 // Fixes https://gitlab.com/kicad/code/kicad/issues/18452.
341 // After truncation we shall no longer detect if the stream is fully parsed.
342 bool footprintListNotTruncated = true;
343
344 if ( patternMap.size() < numberOfFootprints )
345 {
346 numberOfFootprints = patternMap.size();
347 footprintListNotTruncated = false;
348 }
349
350 aFootprintNames.Alloc( numberOfFootprints );
351
352 for( size_t i = 0; i < numberOfFootprints; i++ )
353 {
355
356 wxScopedCharBuffer charBuffer = parser.ReadCharBuffer();
357 wxString fpPattern( charBuffer, wxConvISO8859_1 );
358
359 auto it = patternMap.find( fpPattern );
360
361 if( it != patternMap.end() )
362 {
363 aFootprintNames.Add( it->second ); // Proper unicode name
364 }
365 else
366 {
367 THROW_IO_ERROR( wxString::Format( "Component name not found: '%s'", fpPattern ) );
368 }
369
370 parser.SkipSubrecord();
371 }
372
373 if( parser.HasParsingError() )
374 {
375 THROW_IO_ERROR( wxString::Format( "%s stream was not parsed correctly",
376 FormatPath( streamName ) ) );
377 }
378
379 if( footprintListNotTruncated && parser.GetRemainingBytes() != 0 )
380 {
381 THROW_IO_ERROR( wxString::Format( "%s stream is not fully parsed",
382 FormatPath( streamName ) ) );
383 }
384 }
385 }
386 catch( CFB::CFBException& exception )
387 {
388 THROW_IO_ERROR( exception.what() );
389 }
390}
391
392
394 const wxString& aFootprintName, bool aKeepUUID,
395 const std::map<std::string, UTF8>* aProperties )
396{
397 loadAltiumLibrary( aLibraryPath );
398
399 if( !m_fplibFiles.contains( aLibraryPath ) )
400 THROW_IO_ERROR( wxString::Format( _( "No footprints in library '%s'" ), aLibraryPath ) );
401
402 try
403 {
404 for( std::unique_ptr<ALTIUM_PCB_COMPOUND_FILE>& altiumLibFile : m_fplibFiles[aLibraryPath].m_Files )
405 {
406 altiumLibFile->CacheLibModels();
407 auto [dirName, fpCfe] = altiumLibFile->FindLibFootprintDirName( aFootprintName );
408
409 if( dirName.IsEmpty() )
410 continue;
411
412 // Parse File
413 ALTIUM_PCB pcb( m_board, nullptr, m_layer_mapping_handler, m_reporter, aLibraryPath, aFootprintName );
414 return pcb.ParseFootprint( *altiumLibFile, aFootprintName );
415 }
416 }
417 catch( CFB::CFBException& exception )
418 {
419 THROW_IO_ERROR( exception.what() );
420 }
421
422 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in '%s'." ),
423 aFootprintName,
424 aLibraryPath ) );
425}
426
427
429{
430 std::vector<FOOTPRINT*> footprints;
431
432 for( FOOTPRINT* fp : m_board->Footprints() )
433 footprints.push_back( fp );
434
435 return footprints;
436}
std::string FormatPath(const std::vector< std::string > &aVectorPath)
Helper for debug logging (vector -> string)
@ EXTENDPRIMITIVEINFORMATION
Definition altium_pcb.h:56
std::vector< ALTIUM_PROJECT_VARIANT > ParseAltiumProjectVariants(const wxString &aPrjPcbPath)
Parse all [ProjectVariantN] sections from an Altium .PrjPcb project file.
std::map< wxString, ValueType, DETAIL::CASE_INSENSITIVE_COMPARER > CASE_INSENSITIVE_MAP
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)
void Parse(const ALTIUM_PCB_COMPOUND_FILE &aAltiumPcbFile, const std::map< ALTIUM_PCB_DIR, std::string > &aFileMapping)
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
void AddVariant(const wxString &aVariantName)
Definition board.cpp:2538
const FOOTPRINTS & Footprints() const
Definition board.h:363
void SetVariantDescription(const wxString &aVariantName, const wxString &aDescription)
Definition board.cpp:2644
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:334
Variant information for a footprint.
Definition footprint.h:146
void SetExcludedFromPosFiles(bool aExclude)
Definition footprint.h:166
void SetDNP(bool aDNP)
Definition footprint.h:160
void SetFieldValue(const wxString &aFieldName, const wxString &aValue)
Set a field value override for this variant.
Definition footprint.h:188
void SetExcludedFromBOM(bool aExclude)
Definition footprint.h:163
FOOTPRINT_VARIANT * AddVariant(const wxString &aVariantName)
Add a new variant with the given name.
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
Definition kiid.h:49
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 LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:222
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)
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.
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
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
const std::vector< uint8_t > COMPOUND_FILE_HEADER
Definition io_utils.cpp:31
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
void ApplyAltiumProjectVariantsToBoard(BOARD *aBoard, const std::vector< ALTIUM_PROJECT_VARIANT > &aVariants)
Pcbnew PLUGIN for Altium *.PcbDoc format.
void ApplyAltiumProjectVariantsToBoard(BOARD *aBoard, const std::vector< ALTIUM_PROJECT_VARIANT > &aVariants)
Apply parsed Altium project variants to a board by setting FOOTPRINT_VARIANT data on each footprint w...
A project-level assembly variant parsed from an Altium .PrjPcb file.
A single component variation within an Altium project variant.
int kind
wxString uniqueId
wxString designator
std::map< wxString, wxString > alternateFields
Describes an imported layer and how it could be mapped to KiCad Layers.
std::vector< std::unique_ptr< ALTIUM_PCB_COMPOUND_FILE > > m_Files
std::string path