KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_sprint_layout.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
26
27#include <board.h>
28#include <footprint.h>
29#include <zone.h>
30#include <font/fontconfig.h>
31#include <gestfich.h>
32#include <reporter.h>
33
34#include <ranges>
35#include <wx/filename.h>
36#include <wx/wfstream.h>
37#include <wx/dir.h>
38
39
41 PCB_IO( wxS( "Sprint Layout" ) )
42{
43}
44
45
46void PCB_IO_SPRINT_LAYOUT::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
47 bool aBestEfforts, const std::map<std::string, UTF8>* aProperties )
48{
49 wxFileName libFn( aLibraryPath );
50
51 if( libFn.FileExists() && libFn.GetExt().Upper() == wxS( "LMK" ) )
52 {
53 aFootprintNames.Add( libFn.GetName() );
54 return;
55 }
56
57 if( wxDir::Exists( aLibraryPath ) )
58 {
59 wxArrayString files;
60 CollectFilesLoopSafe( aLibraryPath, files, wxEmptyString, wxDIR_FILES | wxDIR_DIRS );
61
62 for( const wxString& filePath : files )
63 {
64 wxFileName file( filePath );
65
66 if( file.GetExt().Upper() != wxS( "LMK" ) )
67 continue;
68
69 file.MakeRelativeTo( aLibraryPath );
70 aFootprintNames.Add( file.GetFullPath().BeforeLast( '.' ) );
71 }
72 }
73}
74
75
76FOOTPRINT* PCB_IO_SPRINT_LAYOUT::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
77 bool aKeepUUID, const std::map<std::string, UTF8>* aProperties )
78{
79 wxFileName libFn( aLibraryPath );
80 wxFileName lmkPath;
81
82 if( libFn.FileExists() && libFn.GetExt().Upper() == wxS( "LMK" ) )
83 {
84 lmkPath = libFn;
85 }
86 else
87 {
88 if( !wxDir::Exists( aLibraryPath ) )
89 return nullptr;
90
91 lmkPath = wxFileName( aLibraryPath + wxFileName::GetPathSeparator() + aFootprintName + wxS( ".LMK" ) );
92
93 if( !lmkPath.FileExists() )
94 {
95 lmkPath.SetExt( "lmk" );
96
97 if( !lmkPath.FileExists() )
98 return nullptr;
99 }
100 }
101
103
104 if( !parser.ParseMacroFile( lmkPath.GetFullPath() ) )
105 return nullptr;
106
107 return parser.CreateFootprint();
108}
109
110
112{
113 for( std::unique_ptr<FOOTPRINT>& fp : m_loadedFootprints | std::views::values )
114 fp->SetParent( nullptr );
115}
116
117
118bool PCB_IO_SPRINT_LAYOUT::CanReadBoard( const wxString& aFileName ) const
119{
120 const wxFileName fn( aFileName );
121
122 if( !fn.FileExists() )
123 return false;
124
125 wxString ext = fn.GetExt().Lower();
126
127 if( ext != wxS( "lay6" ) && ext != wxS( "lay" ) )
128 return false;
129
130 // Check magic bytes: version (<=6), 0x33, 0xAA, 0xFF
131 wxFFileInputStream stream( aFileName );
132
133 if( !stream.IsOk() || stream.GetLength() < 8 )
134 return false;
135
136 uint8_t header[4];
137 stream.Read( header, 4 );
138
139 if( stream.LastRead() != 4 )
140 return false;
141
142 if( header[0] > 6 || header[1] != 0x33 || header[2] != 0xAA || header[3] != 0xFF )
143 return false;
144
145 return true;
146}
147
148
149BOARD* PCB_IO_SPRINT_LAYOUT::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
150 const std::map<std::string, UTF8>* aProperties,
151 PROJECT* aProject )
152{
154
155 m_props = aProperties;
156 m_loadedFootprints.clear();
157
159
160 if( !parser.ParseBoard( aFileName ) )
161 {
162 THROW_IO_ERROR( wxString::Format( _( "Failed to parse Sprint Layout file '%s'" ),
163 aFileName ) );
164 }
165
166 const auto& fileData = parser.GetFileData();
167 size_t boardIndex = 0;
168
169 if( m_props && m_props->contains( "pcb_id" ) )
170 {
171 unsigned long idx = std::stoul( m_props->at( "pcb_id" ) );
172 boardIndex = static_cast<size_t>( idx );
173 }
174 else if( fileData.boards.size() > 1 && m_choose_project_handler )
175 {
176 std::vector<IMPORT_PROJECT_DESC> options;
177
178 for( size_t i = 0; i < fileData.boards.size(); i++ )
179 {
181 wxString name = wxString::FromUTF8( fileData.boards[i].name );
182
183 if( name.empty() )
184 name = wxString::Format( wxS( "Board %zu" ), i + 1 );
185
186 desc.PCBName = name;
187 desc.PCBId = wxString::Format( wxS( "%zu" ), i );
188 options.push_back( desc );
189 }
190
191 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler( options );
192
193 if( chosen.empty() )
194 return nullptr;
195
196 unsigned long idx = std::stoul( chosen[0].PCBId.ToStdString() );
197 boardIndex = static_cast<size_t>( idx );
198 }
199
200 std::unique_ptr<BOARD> newBoard( parser.CreateBoard( m_loadedFootprints, boardIndex ) );
201
202 if( !newBoard )
203 {
204 THROW_IO_ERROR( wxString::Format( _( "Failed to create board from Sprint Layout file '%s'" ),
205 aFileName ) );
206 }
207
208 if( aAppendToMe )
209 {
210 for( FOOTPRINT* fp : newBoard->Footprints() )
211 aAppendToMe->Add( static_cast<FOOTPRINT*>( fp->Clone() ) );
212
213 for( BOARD_ITEM* item : newBoard->Drawings() )
214 aAppendToMe->Add( static_cast<BOARD_ITEM*>( item->Clone() ) );
215
216 for( ZONE* zone : newBoard->Zones() )
217 aAppendToMe->Add( static_cast<ZONE*>( zone->Clone() ) );
218
219 return aAppendToMe;
220 }
221
222 newBoard->SetFileName( aFileName );
223 return newBoard.release();
224}
225
226
228{
229 std::vector<FOOTPRINT*> result;
230
231 for( auto& [name, footprint] : m_loadedFootprints )
232 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
233
234 return result;
235}
const char * name
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition board_item.h:84
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:323
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1247
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:93
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:336
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:249
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
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...
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_loadedFootprints
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.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
PCB_IO(const wxString &aName)
Definition pcb_io.h:346
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:356
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
Definition project.h:66
const SPRINT_LAYOUT::FILE_DATA & GetFileData() const
BOARD * CreateBoard(std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, size_t aBoardIndex=0)
bool ParseMacroFile(const wxString &aFileName)
bool ParseBoard(const wxString &aFileName)
Handle a list of polygons defining a copper zone.
Definition zone.h:74
#define _(s)
void CollectFilesLoopSafe(const wxString &aRoot, wxArrayString &aFiles, const wxString &aFileSpec, int aFlags)
Recursively collect every file under aRoot, deduplicating subdirectories by their resolved path.
Definition gestfich.cpp:821
#define THROW_IO_ERROR(msg)
macro which captures the "call site" values of FILE_, __FUNCTION & LINE
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
std::vector< std::string > header
wxString result
Test unit parsing edge cases and error handling.