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