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, see <https://www.gnu.org/licenses/>.
18 */
19
22
23#include <board.h>
24#include <footprint.h>
25#include <zone.h>
26#include <font/fontconfig.h>
27#include <gestfich.h>
28#include <reporter.h>
29
30#include <ranges>
31#include <wx/filename.h>
32#include <wx/wfstream.h>
33#include <wx/dir.h>
34
35
37 PCB_IO( wxS( "Sprint Layout" ) )
38{
39}
40
41
42void PCB_IO_SPRINT_LAYOUT::FootprintEnumerate( wxArrayString& aFootprintNames, const wxString& aLibraryPath,
43 bool aBestEfforts, const std::map<std::string, UTF8>* aProperties )
44{
45 wxFileName libFn( aLibraryPath );
46
47 if( libFn.FileExists() && libFn.GetExt().Upper() == wxS( "LMK" ) )
48 {
49 aFootprintNames.Add( libFn.GetName() );
50 return;
51 }
52
53 if( wxDir::Exists( aLibraryPath ) )
54 {
55 wxArrayString files;
56 CollectFilesLoopSafe( aLibraryPath, files, wxEmptyString, wxDIR_FILES | wxDIR_DIRS );
57
58 for( const wxString& filePath : files )
59 {
60 wxFileName file( filePath );
61
62 if( file.GetExt().Upper() != wxS( "LMK" ) )
63 continue;
64
65 file.MakeRelativeTo( aLibraryPath );
66 aFootprintNames.Add( file.GetFullPath().BeforeLast( '.' ) );
67 }
68 }
69}
70
71
72std::unique_ptr<FOOTPRINT> PCB_IO_SPRINT_LAYOUT::FootprintLoad( const wxString& aLibraryPath,
73 const wxString& aFootprintName, bool aKeepUUID,
74 const std::map<std::string, UTF8>* aProperties )
75{
76 wxFileName libFn( aLibraryPath );
77 wxFileName lmkPath;
78
79 if( libFn.FileExists() && libFn.GetExt().Upper() == wxS( "LMK" ) )
80 {
81 lmkPath = libFn;
82 }
83 else
84 {
85 if( !wxDir::Exists( aLibraryPath ) )
86 return nullptr;
87
88 lmkPath = wxFileName( aLibraryPath + wxFileName::GetPathSeparator() + aFootprintName + wxS( ".LMK" ) );
89
90 if( !lmkPath.FileExists() )
91 {
92 lmkPath.SetExt( "lmk" );
93
94 if( !lmkPath.FileExists() )
95 return nullptr;
96 }
97 }
98
100
101 if( !parser.ParseMacroFile( lmkPath.GetFullPath() ) )
102 return nullptr;
103
104 return parser.CreateFootprint();
105}
106
107
109{
110 for( std::unique_ptr<FOOTPRINT>& fp : m_loadedFootprints | std::views::values )
111 fp->SetParent( nullptr );
112}
113
114
115bool PCB_IO_SPRINT_LAYOUT::CanReadBoard( const wxString& aFileName ) const
116{
117 const wxFileName fn( aFileName );
118
119 if( !fn.FileExists() )
120 return false;
121
122 wxString ext = fn.GetExt().Lower();
123
124 if( ext != wxS( "lay6" ) && ext != wxS( "lay" ) )
125 return false;
126
127 // Check magic bytes: version (<=6), 0x33, 0xAA, 0xFF
128 wxFFileInputStream stream( aFileName );
129
130 if( !stream.IsOk() || stream.GetLength() < 8 )
131 return false;
132
133 uint8_t header[4];
134 stream.Read( header, 4 );
135
136 if( stream.LastRead() != 4 )
137 return false;
138
139 if( header[0] > 6 || header[1] != 0x33 || header[2] != 0xAA || header[3] != 0xFF )
140 return false;
141
142 return true;
143}
144
145
146void PCB_IO_SPRINT_LAYOUT::loadBoard( const wxString& aFileName, BOARD& aBoard, bool aIsNewLoad,
147 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
148{
150
151 m_props = aProperties;
152 m_loadedFootprints.clear();
153
155
156 if( !parser.ParseBoard( aFileName ) )
157 THROW_IO_ERRORF( _( "Failed to parse Sprint Layout file '%s'" ), aFileName );
158
159 const auto& fileData = parser.GetFileData();
160 size_t boardIndex = 0;
161
162 if( m_props && m_props->contains( "pcb_id" ) )
163 {
164 unsigned long idx = std::stoul( m_props->at( "pcb_id" ) );
165 boardIndex = static_cast<size_t>( idx );
166 }
167 else if( fileData.boards.size() > 1 && m_choose_project_handler )
168 {
169 std::vector<IMPORT_PROJECT_DESC> options;
170
171 for( size_t i = 0; i < fileData.boards.size(); i++ )
172 {
174 wxString name = wxString::FromUTF8( fileData.boards[i].name );
175
176 if( name.empty() )
177 name = wxString::Format( wxS( "Board %zu" ), i + 1 );
178
179 desc.PCBName = name;
180 desc.PCBId = wxString::Format( wxS( "%zu" ), i );
181 options.push_back( desc );
182 }
183
184 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler( options );
185
186 if( chosen.empty() )
188
189 unsigned long idx = std::stoul( chosen[0].PCBId.ToStdString() );
190 boardIndex = static_cast<size_t>( idx );
191 }
192
193 if( aIsNewLoad )
194 {
195 if( !parser.CreateBoard( aBoard, m_loadedFootprints, boardIndex ) )
196 THROW_IO_ERRORF( _( "Failed to create board from Sprint Layout file '%s'" ), aFileName );
197 }
198 else
199 {
200 // Parse into a scratch board, then copy the design's items into the caller's
201 // board. The destination keeps its own setup when merging.
202 std::unique_ptr<BOARD> scratchBoard = std::make_unique<BOARD>();
203
204 if( !parser.CreateBoard( *scratchBoard, m_loadedFootprints, boardIndex ) )
205 THROW_IO_ERRORF( _( "Failed to create board from Sprint Layout file '%s'" ), aFileName );
206
207 for( FOOTPRINT* fp : scratchBoard->Footprints() )
208 aBoard.Add( static_cast<FOOTPRINT*>( fp->Clone() ) );
209
210 for( BOARD_ITEM* item : scratchBoard->Drawings() )
211 aBoard.Add( static_cast<BOARD_ITEM*>( item->Clone() ) );
212
213 for( ZONE* zone : scratchBoard->Zones() )
214 aBoard.Add( static_cast<ZONE*>( zone->Clone() ) );
215 }
216}
217
218
220{
221 std::vector<FOOTPRINT*> result;
222
223 for( auto& [name, footprint] : m_loadedFootprints )
224 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
225
226 return result;
227}
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:409
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1497
virtual void SetParent(EDA_ITEM *aParent)
Definition eda_item.cpp:153
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:385
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:351
std::unique_ptr< 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::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
void loadBoard(const wxString &aFileName, BOARD &aBoard, bool aIsNewLoad, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Parse aFileName into aBoard.
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
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.
PCB_IO(const wxString &aName)
Definition pcb_io.h:351
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:371
CHOOSE_PROJECT_HANDLER m_choose_project_handler
Callback to choose projects to import.
Container for project specific data.
Definition project.h:63
std::unique_ptr< FOOTPRINT > CreateFootprint()
bool CreateBoard(BOARD &aBoard, std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, size_t aBoardIndex=0)
const SPRINT_LAYOUT::FILE_DATA & GetFileData() const
bool ParseMacroFile(const wxString &aFileName)
bool ParseBoard(const wxString &aFileName)
Handle a list of polygons defining a copper zone.
Definition zone.h:70
#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:873
#define THROW_IO_ERRORF(msg,...)
#define THROW_IO_CANCELLED()
Describes how non-KiCad boards and schematics should be imported as KiCad projects.
wxString result
Test unit parsing edge cases and error handling.