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}
47
48
50{
51}
52
53
54bool PCB_IO_ALTIUM_DESIGNER::checkFileHeader( const wxString& aFileName )
55{
56 // Compound File Binary Format header
58}
59
60
61bool PCB_IO_ALTIUM_DESIGNER::CanReadBoard( const wxString& aFileName ) const
62{
63 if( !PCB_IO::CanReadBoard( aFileName ) )
64 return false;
65
66 return checkFileHeader( aFileName );
67}
68
69
70bool PCB_IO_ALTIUM_DESIGNER::CanReadLibrary( const wxString& aFileName ) const
71{
72 if( !PCB_IO::CanReadLibrary( aFileName ) )
73 return false;
74
75 return checkFileHeader( aFileName );
76}
77
78
79BOARD* PCB_IO_ALTIUM_DESIGNER::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
80 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
81{
82 m_props = aProperties;
83
84 m_board = aAppendToMe ? aAppendToMe : new BOARD();
85
86 // Give the filename to the board if it's new
87 if( !aAppendToMe )
88 m_board->SetFileName( aFileName );
89
90 // clang-format off
91 const std::map<ALTIUM_PCB_DIR, std::string> mapping = {
92 { ALTIUM_PCB_DIR::FILE_HEADER, "FileHeader" },
93 { ALTIUM_PCB_DIR::ARCS6, "Arcs6" },
94 { ALTIUM_PCB_DIR::BOARD6, "Board6" },
95 { ALTIUM_PCB_DIR::BOARDREGIONS, "BoardRegions" },
96 { ALTIUM_PCB_DIR::CLASSES6, "Classes6" },
97 { ALTIUM_PCB_DIR::COMPONENTS6, "Components6" },
98 { ALTIUM_PCB_DIR::COMPONENTBODIES6, "ComponentBodies6" },
99 { ALTIUM_PCB_DIR::DIMENSIONS6, "Dimensions6" },
100 { ALTIUM_PCB_DIR::EXTENDPRIMITIVEINFORMATION, "ExtendedPrimitiveInformation" },
101 { ALTIUM_PCB_DIR::FILLS6, "Fills6" },
102 { ALTIUM_PCB_DIR::MODELS, "Models" },
103 { ALTIUM_PCB_DIR::NETS6, "Nets6" },
104 { ALTIUM_PCB_DIR::PADS6, "Pads6" },
105 { ALTIUM_PCB_DIR::POLYGONS6, "Polygons6" },
106 { ALTIUM_PCB_DIR::REGIONS6, "Regions6" },
107 { ALTIUM_PCB_DIR::RULES6, "Rules6" },
108 { ALTIUM_PCB_DIR::SHAPEBASEDREGIONS6, "ShapeBasedRegions6" },
109 { ALTIUM_PCB_DIR::TEXTS6, "Texts6" },
110 { ALTIUM_PCB_DIR::TRACKS6, "Tracks6" },
111 { ALTIUM_PCB_DIR::VIAS6, "Vias6" },
112 { ALTIUM_PCB_DIR::WIDESTRINGS6, "WideStrings6" }
113 };
114 // clang-format on
115
116 ALTIUM_COMPOUND_FILE altiumPcbFile( aFileName );
117
118 try
119 {
120 // Parse File
122 pcb.Parse( altiumPcbFile, mapping );
123 }
124 catch( CFB::CFBException& exception )
125 {
126 THROW_IO_ERROR( exception.what() );
127 }
128
129 return m_board;
130}
131
132
133long long PCB_IO_ALTIUM_DESIGNER::GetLibraryTimestamp( const wxString& aLibraryPath ) const
134{
135 // File hasn't been loaded yet.
136 if( aLibraryPath.IsEmpty() )
137 {
138 return 0;
139 }
140
141 wxFileName fn( aLibraryPath );
142
143 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
144 {
145 return fn.GetModificationTime().GetValue().GetValue();
146 }
147 else
148 {
149 return 0;
150 }
151}
152
153
154void PCB_IO_ALTIUM_DESIGNER::loadAltiumLibrary( const wxString& aLibraryPath )
155{
156 try
157 {
158 auto it = m_fplibFiles.find( aLibraryPath );
159
160 if( it != m_fplibFiles.end() )
161 return; // Already loaded
162
163 if( aLibraryPath.Lower().EndsWith( wxS( ".pcblib" ) ) )
164 {
165 m_fplibFiles[aLibraryPath].emplace_back(
166 std::make_unique<ALTIUM_COMPOUND_FILE>( aLibraryPath ) );
167 }
168 else if( aLibraryPath.Lower().EndsWith( wxS( ".intlib" ) ) )
169 {
170 std::unique_ptr<ALTIUM_COMPOUND_FILE> intCom =
171 std::make_unique<ALTIUM_COMPOUND_FILE>( aLibraryPath );
172
173 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> pcbLibFiles =
174 intCom->EnumDir( L"PCBLib" );
175
176 for( const auto& [pcbLibName, pcbCfe] : pcbLibFiles )
177 m_fplibFiles[aLibraryPath].push_back( intCom->DecodeIntLibStream( *pcbCfe ) );
178 }
179 }
180 catch( CFB::CFBException& exception )
181 {
182 THROW_IO_ERROR( exception.what() );
183 }
184}
185
186
187void PCB_IO_ALTIUM_DESIGNER::FootprintEnumerate( wxArrayString& aFootprintNames,
188 const wxString& aLibraryPath, bool aBestEfforts,
189 const STRING_UTF8_MAP* aProperties )
190{
191 loadAltiumLibrary( aLibraryPath );
192
193 auto it = m_fplibFiles.find( aLibraryPath );
194
195 if( it == m_fplibFiles.end() )
196 return; // No footprint libraries in file, ignore.
197
198 try
199 {
200 for( auto& altiumLibFile : it->second )
201 {
202 // Map code-page-dependent names to unicode names
203 std::map<wxString, wxString> patternMap = altiumLibFile->ListLibFootprints();
204
205 const std::vector<std::string> streamName = { "Library", "Data" };
206 const CFB::COMPOUND_FILE_ENTRY* libraryData = altiumLibFile->FindStream( streamName );
207
208 if( libraryData == nullptr )
209 {
210 THROW_IO_ERROR( wxString::Format( _( "File not found: '%s'." ),
211 FormatPath( streamName ) ) );
212 }
213
214 ALTIUM_BINARY_PARSER parser( *altiumLibFile, libraryData );
215
216 std::map<wxString, wxString> properties = parser.ReadProperties();
217
218 uint32_t numberOfFootprints = parser.Read<uint32_t>();
219 aFootprintNames.Alloc( numberOfFootprints );
220
221 for( size_t i = 0; i < numberOfFootprints; i++ )
222 {
224
225 wxScopedCharBuffer charBuffer = parser.ReadCharBuffer();
226 wxString fpPattern( charBuffer, wxConvISO8859_1 );
227
228 auto it = patternMap.find( fpPattern );
229 if( it != patternMap.end() )
230 {
231 aFootprintNames.Add( it->second ); // Proper unicode name
232 }
233 else
234 {
236 wxString::Format( "Component name not found: '%s'", fpPattern ) );
237 }
238
239 parser.SkipSubrecord();
240 }
241
242 if( parser.HasParsingError() )
243 {
244 THROW_IO_ERROR( wxString::Format( "%s stream was not parsed correctly",
245 FormatPath( streamName ) ) );
246 }
247
248 if( parser.GetRemainingBytes() != 0 )
249 {
250 THROW_IO_ERROR( wxString::Format( "%s stream is not fully parsed",
251 FormatPath( streamName ) ) );
252 }
253 }
254 }
255 catch( CFB::CFBException& exception )
256 {
257 THROW_IO_ERROR( exception.what() );
258 }
259}
260
261
263 const wxString& aFootprintName, bool aKeepUUID,
264 const STRING_UTF8_MAP* aProperties )
265{
266 loadAltiumLibrary( aLibraryPath );
267
268 auto it = m_fplibFiles.find( aLibraryPath );
269
270 if( it == m_fplibFiles.end() )
271 THROW_IO_ERROR( wxString::Format( _( "No footprints in library '%s'" ), aLibraryPath ) );
272
273 try
274 {
275 for( std::unique_ptr<ALTIUM_COMPOUND_FILE>& altiumLibFile : it->second )
276 {
277 auto [dirName, fpCfe] = altiumLibFile->FindLibFootprintDirName( aFootprintName );
278
279 if( dirName.IsEmpty() )
280 continue;
281
282 // Parse File
283 ALTIUM_PCB pcb( m_board, nullptr, m_reporter, aLibraryPath, aFootprintName );
284 return pcb.ParseFootprint( *altiumLibFile, aFootprintName );
285 }
286 }
287 catch( CFB::CFBException& exception )
288 {
289 THROW_IO_ERROR( exception.what() );
290 }
291
292 THROW_IO_ERROR( wxString::Format( _( "Footprint '%s' not found in '%s'." ),
293 aFootprintName,
294 aLibraryPath ) );
295}
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:303
FOOTPRINT * ParseFootprint(ALTIUM_COMPOUND_FILE &altiumLibFile, const wxString &aFootprintName)
Definition: altium_pcb.cpp:644
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:210
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:213
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:67
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)
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:203
#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