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 <reporter.h>
32
33#include <ranges>
34#include <wx/filename.h>
35#include <wx/wfstream.h>
36
37
39 PCB_IO( wxS( "Sprint Layout" ) )
40{
41}
42
43
45{
46 for( std::unique_ptr<FOOTPRINT>& fp : m_loadedFootprints | std::views::values )
47 fp->SetParent( nullptr );
48}
49
50
51bool PCB_IO_SPRINT_LAYOUT::CanReadBoard( const wxString& aFileName ) const
52{
53 const wxFileName fn( aFileName );
54
55 if( !fn.FileExists() )
56 return false;
57
58 wxString ext = fn.GetExt().Lower();
59
60 if( ext != wxS( "lay6" ) && ext != wxS( "lay" ) )
61 return false;
62
63 // Check magic bytes: version (<=6), 0x33, 0xAA, 0xFF
64 wxFFileInputStream stream( aFileName );
65
66 if( !stream.IsOk() || stream.GetLength() < 8 )
67 return false;
68
69 uint8_t header[4];
70 stream.Read( header, 4 );
71
72 if( stream.LastRead() != 4 )
73 return false;
74
75 if( header[0] > 6 || header[1] != 0x33 || header[2] != 0xAA || header[3] != 0xFF )
76 return false;
77
78 return true;
79}
80
81
82BOARD* PCB_IO_SPRINT_LAYOUT::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
83 const std::map<std::string, UTF8>* aProperties,
84 PROJECT* aProject )
85{
87
88 m_props = aProperties;
89 m_loadedFootprints.clear();
90
92
93 if( !parser.Parse( aFileName ) )
94 {
95 THROW_IO_ERROR( wxString::Format( _( "Failed to parse Sprint Layout file '%s'" ),
96 aFileName ) );
97 }
98
99 const auto& fileData = parser.GetFileData();
100 size_t boardIndex = 0;
101
102 if( m_props && m_props->contains( "pcb_id" ) )
103 {
104 unsigned long idx = std::stoul( m_props->at( "pcb_id" ) );
105 boardIndex = static_cast<size_t>( idx );
106 }
107 else if( fileData.boards.size() > 1 && m_choose_project_handler )
108 {
109 std::vector<IMPORT_PROJECT_DESC> options;
110
111 for( size_t i = 0; i < fileData.boards.size(); i++ )
112 {
114 wxString name = wxString::FromUTF8( fileData.boards[i].name );
115
116 if( name.empty() )
117 name = wxString::Format( wxS( "Board %zu" ), i + 1 );
118
119 desc.PCBName = name;
120 desc.PCBId = wxString::Format( wxS( "%zu" ), i );
121 options.push_back( desc );
122 }
123
124 std::vector<IMPORT_PROJECT_DESC> chosen = m_choose_project_handler( options );
125
126 if( chosen.empty() )
127 return nullptr;
128
129 unsigned long idx = std::stoul( chosen[0].PCBId.ToStdString() );
130 boardIndex = static_cast<size_t>( idx );
131 }
132
133 std::unique_ptr<BOARD> newBoard( parser.CreateBoard( m_loadedFootprints, boardIndex ) );
134
135 if( !newBoard )
136 {
137 THROW_IO_ERROR( wxString::Format( _( "Failed to create board from Sprint Layout file '%s'" ),
138 aFileName ) );
139 }
140
141 if( aAppendToMe )
142 {
143 for( FOOTPRINT* fp : newBoard->Footprints() )
144 aAppendToMe->Add( static_cast<FOOTPRINT*>( fp->Clone() ) );
145
146 for( BOARD_ITEM* item : newBoard->Drawings() )
147 aAppendToMe->Add( static_cast<BOARD_ITEM*>( item->Clone() ) );
148
149 for( ZONE* zone : newBoard->Zones() )
150 aAppendToMe->Add( static_cast<ZONE*>( zone->Clone() ) );
151
152 return aAppendToMe;
153 }
154
155 newBoard->SetFileName( aFileName );
156 return newBoard.release();
157}
158
159
161{
162 std::vector<FOOTPRINT*> result;
163
164 for( auto& [name, footprint] : m_loadedFootprints )
165 result.push_back( static_cast<FOOTPRINT*>( footprint->Clone() ) );
166
167 return result;
168}
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
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.
std::map< wxString, std::unique_ptr< FOOTPRINT > > m_loadedFootprints
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
bool Parse(const wxString &aFileName)
BOARD * CreateBoard(std::map< wxString, std::unique_ptr< FOOTPRINT > > &aFootprintMap, size_t aBoardIndex=0)
Handle a list of polygons defining a copper zone.
Definition zone.h:74
#define _(s)
#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.