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 (C) 2020-2023 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
32#include <altium_pcb.h>
33#include <io/io_utils.h>
35#include <pcb_io/pcb_io.h>
36#include <reporter.h>
37
38#include <board.h>
39
40#include <compoundfilereader.h>
41#include <utf.h>
42
44{
46
48}
49
50
52{
53}
54
55
57 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
58{
59 std::map<wxString, PCB_LAYER_ID> retval;
60
61 // Just return a the auto-mapped layers
62 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
63 {
64 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
65 }
66
67 return retval;
68}
69
70
71bool PCB_IO_ALTIUM_DESIGNER::checkFileHeader( const wxString& aFileName )
72{
73 // Compound File Binary Format header
75}
76
77
78bool PCB_IO_ALTIUM_DESIGNER::CanReadBoard( const wxString& aFileName ) const
79{
80 if( !PCB_IO::CanReadBoard( aFileName ) )
81 return false;
82
83 return checkFileHeader( aFileName );
84}
85
86
87bool PCB_IO_ALTIUM_DESIGNER::CanReadLibrary( const wxString& aFileName ) const
88{
89 if( !PCB_IO::CanReadLibrary( aFileName ) )
90 return false;
91
92 return checkFileHeader( aFileName );
93}
94
95
96BOARD* PCB_IO_ALTIUM_DESIGNER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
97 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
98{
99 m_props = aProperties;
100
101 m_board = aAppendToMe ? aAppendToMe : new BOARD();
102
103 // Give the filename to the board if it's new
104 if( !aAppendToMe )
105 m_board->SetFileName( aFileName );
106
107 // clang-format off
108 const std::map<ALTIUM_PCB_DIR, std::string> mapping = {
109 { ALTIUM_PCB_DIR::FILE_HEADER, "FileHeader" },
110 { ALTIUM_PCB_DIR::ARCS6, "Arcs6" },
111 { ALTIUM_PCB_DIR::BOARD6, "Board6" },
112 { ALTIUM_PCB_DIR::BOARDREGIONS, "BoardRegions" },
113 { ALTIUM_PCB_DIR::CLASSES6, "Classes6" },
114 { ALTIUM_PCB_DIR::COMPONENTS6, "Components6" },
115 { ALTIUM_PCB_DIR::COMPONENTBODIES6, "ComponentBodies6" },
116 { ALTIUM_PCB_DIR::DIMENSIONS6, "Dimensions6" },
117 { ALTIUM_PCB_DIR::EXTENDPRIMITIVEINFORMATION, "ExtendedPrimitiveInformation" },
118 { ALTIUM_PCB_DIR::FILLS6, "Fills6" },
119 { ALTIUM_PCB_DIR::MODELS, "Models" },
120 { ALTIUM_PCB_DIR::NETS6, "Nets6" },
121 { ALTIUM_PCB_DIR::PADS6, "Pads6" },
122 { ALTIUM_PCB_DIR::POLYGONS6, "Polygons6" },
123 { ALTIUM_PCB_DIR::REGIONS6, "Regions6" },
124 { ALTIUM_PCB_DIR::RULES6, "Rules6" },
125 { ALTIUM_PCB_DIR::SHAPEBASEDREGIONS6, "ShapeBasedRegions6" },
126 { ALTIUM_PCB_DIR::TEXTS6, "Texts6" },
127 { ALTIUM_PCB_DIR::TRACKS6, "Tracks6" },
128 { ALTIUM_PCB_DIR::VIAS6, "Vias6" },
129 { ALTIUM_PCB_DIR::WIDESTRINGS6, "WideStrings6" }
130 };
131 // clang-format on
132
133 ALTIUM_COMPOUND_FILE altiumPcbFile( aFileName );
134
135 try
136 {
137 // Parse File
139 pcb.Parse( altiumPcbFile, mapping );
140 }
141 catch( CFB::CFBException& exception )
142 {
143 THROW_IO_ERROR( exception.what() );
144 }
145
146 return m_board;
147}
148
149
150long long PCB_IO_ALTIUM_DESIGNER::GetLibraryTimestamp( const wxString& aLibraryPath ) const
151{
152 // File hasn't been loaded yet.
153 if( aLibraryPath.IsEmpty() )
154 {
155 return 0;
156 }
157
158 wxFileName fn( aLibraryPath );
159
160 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
161 {
162 return fn.GetModificationTime().GetValue().GetValue();
163 }
164 else
165 {
166 return 0;
167 }
168}
169
170
171void PCB_IO_ALTIUM_DESIGNER::loadAltiumLibrary( const wxString& aLibraryPath )
172{
173 try
174 {
175 auto it = m_fplibFiles.find( aLibraryPath );
176
177 if( it != m_fplibFiles.end() )
178 return; // Already loaded
179
180 if( aLibraryPath.Lower().EndsWith( wxS( ".pcblib" ) ) )
181 {
182 m_fplibFiles[aLibraryPath].emplace_back(
183 std::make_unique<ALTIUM_COMPOUND_FILE>( aLibraryPath ) );
184 }
185 else if( aLibraryPath.Lower().EndsWith( wxS( ".intlib" ) ) )
186 {
187 std::unique_ptr<ALTIUM_COMPOUND_FILE> intCom =
188 std::make_unique<ALTIUM_COMPOUND_FILE>( aLibraryPath );
189
190 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> pcbLibFiles =
191 intCom->EnumDir( L"PCBLib" );
192
193 for( const auto& [pcbLibName, pcbCfe] : pcbLibFiles )
194 m_fplibFiles[aLibraryPath].push_back( intCom->DecodeIntLibStream( *pcbCfe ) );
195 }
196 }
197 catch( CFB::CFBException& exception )
198 {
199 THROW_IO_ERROR( exception.what() );
200 }
201}
202
203
204void PCB_IO_ALTIUM_DESIGNER::FootprintEnumerate( wxArrayString& aFootprintNames,
205 const wxString& aLibraryPath, bool aBestEfforts,
206 const STRING_UTF8_MAP* aProperties )
207{
208 loadAltiumLibrary( aLibraryPath );
209
210 auto it = m_fplibFiles.find( aLibraryPath );
211
212 if( it == m_fplibFiles.end() )
213 return; // No footprint libraries in file, ignore.
214
215 try
216 {
217 for( auto& altiumLibFile : it->second )
218 {
219 // Map code-page-dependent names to unicode names
220 std::map<wxString, wxString> patternMap = altiumLibFile->ListLibFootprints();
221
222 const std::vector<std::string> streamName = { "Library", "Data" };
223 const CFB::COMPOUND_FILE_ENTRY* libraryData = altiumLibFile->FindStream( streamName );
224
225 if( libraryData == nullptr )
226 {
227 THROW_IO_ERROR( wxString::Format( _( "File not found: '%s'." ),
228 FormatPath( streamName ) ) );
229 }
230
231 ALTIUM_BINARY_PARSER parser( *altiumLibFile, libraryData );
232
233 std::map<wxString, wxString> properties = parser.ReadProperties();
234
235 uint32_t numberOfFootprints = parser.Read<uint32_t>();
236 aFootprintNames.Alloc( numberOfFootprints );
237
238 for( size_t i = 0; i < numberOfFootprints; i++ )
239 {
241
242 wxScopedCharBuffer charBuffer = parser.ReadCharBuffer();
243 wxString fpPattern( charBuffer, wxConvISO8859_1 );
244
245 auto it = patternMap.find( fpPattern );
246 if( it != patternMap.end() )
247 {
248 aFootprintNames.Add( it->second ); // Proper unicode name
249 }
250 else
251 {
253 wxString::Format( "Component name not found: '%s'", fpPattern ) );
254 }
255
256 parser.SkipSubrecord();
257 }
258
259 if( parser.HasParsingError() )
260 {
261 THROW_IO_ERROR( wxString::Format( "%s stream was not parsed correctly",
262 FormatPath( streamName ) ) );
263 }
264
265 if( parser.GetRemainingBytes() != 0 )
266 {
267 THROW_IO_ERROR( wxString::Format( "%s stream is not fully parsed",
268 FormatPath( streamName ) ) );
269 }
270 }
271 }
272 catch( CFB::CFBException& exception )
273 {
274 THROW_IO_ERROR( exception.what() );
275 }
276}
277
278
280 const wxString& aFootprintName, bool aKeepUUID,
281 const STRING_UTF8_MAP* aProperties )
282{
283 loadAltiumLibrary( aLibraryPath );
284
285 auto it = m_fplibFiles.find( aLibraryPath );
286
287 if( it == m_fplibFiles.end() )
288 THROW_IO_ERROR( wxString::Format( _( "No footprints in library '%s'" ), aLibraryPath ) );
289
290 try
291 {
292 for( std::unique_ptr<ALTIUM_COMPOUND_FILE>& altiumLibFile : it->second )
293 {
294 auto [dirName, fpCfe] = altiumLibFile->FindLibFootprintDirName( aFootprintName );
295
296 if( dirName.IsEmpty() )
297 continue;
298
299 // Parse File
300 ALTIUM_PCB pcb( m_board, nullptr, m_layer_mapping_handler, m_reporter, aLibraryPath, aFootprintName );
301 return pcb.ParseFootprint( *altiumLibFile, aFootprintName );
302 }
303 }
304 catch( CFB::CFBException& exception )
305 {
306 THROW_IO_ERROR( exception.what() );
307 }
308
309 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in '%s'." ),
310 aFootprintName,
311 aLibraryPath ) );
312}
std::string FormatPath(const std::vector< std::string > &aVectorPath)
Helper for debug logging (vector -> string)
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 >();})
void Parse(const ALTIUM_COMPOUND_FILE &aAltiumPcbFile, const std::map< ALTIUM_PCB_DIR, std::string > &aFileMapping)
Definition: altium_pcb.cpp:314
FOOTPRINT * ParseFootprint(ALTIUM_COMPOUND_FILE &altiumLibFile, const wxString &aFootprintName)
Definition: altium_pcb.cpp:655
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:282
void SetFileName(const wxString &aFileName)
Definition: board.h:317
REPORTER * m_reporter
Reporter to log errors/warnings to, may be nullptr.
Definition: io_base.h:215
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:218
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:69
LAYER_MAPPING_HANDLER m_layer_mapping_handler
Callback to get layer mapping.
virtual void RegisterLayerMappingCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler)
Register a different handler to be called when mapping of input layers to KiCad layers occurs.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
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.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
std::map< wxString, std::vector< std::unique_ptr< ALTIUM_COMPOUND_FILE > > > m_fplibFiles
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:72
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:343
const STRING_UTF8_MAP * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:346
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition: pcb_io.cpp:43
Container for project specific data.
Definition: project.h:62
A name/value tuple with unique names and optional values.
static REPORTER & GetInstance()
Definition: reporter.cpp:212
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
static const std::vector< uint8_t > COMPOUND_FILE_HEADER
Definition: io_utils.h:35
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:57
Describes an imported layer and how it could be mapped to KiCad Layers.