KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pcb_io_cadstar_archive.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) 2020 Roberto Fernandez Bautista <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
25
27#include <font/fontconfig.h>
29#include <board.h>
30#include <footprint.h>
31#include <io/io_utils.h>
32#include <pcb_io/pcb_io.h>
33#include <reporter.h>
34
35
37 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
38{
39 std::map<wxString, PCB_LAYER_ID> retval;
40
41 // Just return a the auto-mapped layers
42 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
43 {
44 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
45 }
46
47 return retval;
48}
49
50
52{
53 LAYER_MAPPABLE_PLUGIN::RegisterCallback( aLayerMappingHandler );
54 m_show_layer_mapping_warnings = false; // only show warnings with default callback
55}
56
57
63
64
69
70
72{
74 {
75 delete fp;
76 }
77
78 m_loaded_footprints.clear();
79}
80
81
83{
84 std::vector<FOOTPRINT*> retval;
85
87 {
88 retval.push_back( static_cast<FOOTPRINT*>( fp->Clone() ) );
89 }
90
91 return retval;
92}
93
94
95BOARD* PCB_IO_CADSTAR_ARCHIVE::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
96 const std::map<std::string, UTF8>* aProperties, PROJECT* aProject )
97{
98 m_props = aProperties;
99 m_board = aAppendToMe ? aAppendToMe : new BOARD();
101
102 // Collect the font substitution warnings (RAII - automatically reset on scope exit)
104
107 tempPCB.Load( m_board, aProject );
108
109 //center the board:
110 if( aProperties )
111 {
112 UTF8 page_width;
113 UTF8 page_height;
114
115 if( auto it = aProperties->find( "page_width" ); it != aProperties->end() )
116 page_width = it->second;
117
118 if( auto it = aProperties->find( "page_height" ); it != aProperties->end() )
119 page_height = it->second;
120
121 if( !page_width.empty() && !page_height.empty() )
122 {
123 BOX2I bbbox = m_board->GetBoardEdgesBoundingBox();
124
125 int w = atoi( page_width.c_str() );
126 int h = atoi( page_height.c_str() );
127
128 int desired_x = ( w - bbbox.GetWidth() ) / 2;
129 int desired_y = ( h - bbbox.GetHeight() ) / 2;
130
131 m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
132 }
133 }
134
135 // Need to set legacy loading so that netclassess and design rules are loaded correctly
136 m_board->m_LegacyNetclassesLoaded = true;
137 m_board->m_LegacyDesignSettingsLoaded = true;
138
140
141 return m_board;
142}
143
144
145bool PCB_IO_CADSTAR_ARCHIVE::checkBoardHeader( const wxString& aFileName ) const
146{
147 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true );
148}
149
150
151bool PCB_IO_CADSTAR_ARCHIVE::CanReadBoard( const wxString& aFileName ) const
152{
153 if( !PCB_IO::CanReadBoard( aFileName ) )
154 return false;
155
156 return checkBoardHeader( aFileName );
157}
158
159
160bool PCB_IO_CADSTAR_ARCHIVE::CanReadLibrary( const wxString& aFileName ) const
161{
162 if( !PCB_IO::CanReadLibrary( aFileName ) )
163 return false;
164
165 return checkBoardHeader( aFileName );
166}
167
168
169bool PCB_IO_CADSTAR_ARCHIVE::CanReadFootprint( const wxString& aFileName ) const
170{
171 if( !PCB_IO::CanReadFootprint( aFileName ) )
172 return false;
173
174 return checkBoardHeader( aFileName );
175}
176
177
178void PCB_IO_CADSTAR_ARCHIVE::FootprintEnumerate( wxArrayString& aFootprintNames,
179 const wxString& aLibraryPath,
180 bool aBestEfforts,
181 const std::map<std::string, UTF8>* aProperties )
182{
183 ensureLoadedLibrary( aLibraryPath );
184
185 if( !m_cache.count( aLibraryPath ) )
186 return; // not found
187
188 for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
189 aFootprintNames.Add( name );
190}
191
192
193bool PCB_IO_CADSTAR_ARCHIVE::FootprintExists( const wxString& aLibraryPath,
194 const wxString& aFootprintName,
195 const std::map<std::string, UTF8>* aProperties )
196{
197 ensureLoadedLibrary( aLibraryPath );
198
199 if( !m_cache.count( aLibraryPath ) )
200 return false;
201
202 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
203 return false;
204
205 return true;
206}
207
208
210 const wxString& aFootprintName,
211 bool aKeepUUID,
212 const std::map<std::string, UTF8>* aProperties )
213{
214 ensureLoadedLibrary( aLibraryPath );
215
216 if( !m_cache.count( aLibraryPath ) )
217 return nullptr;
218
219 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
220 return nullptr;
221
222 if( !m_cache.at( aLibraryPath ).at( aFootprintName ) )
223 return nullptr;
224
225 return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate( IGNORE_PARENT_GROUP ) );
226}
227
228
229long long PCB_IO_CADSTAR_ARCHIVE::GetLibraryTimestamp( const wxString& aLibraryPath ) const
230{
231 wxFileName fn( aLibraryPath );
232
233 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
234 return fn.GetModificationTime().GetValue().GetValue();
235 else
236 return wxDateTime( 0.0 ).GetValue().GetValue();
237}
238
239
240void PCB_IO_CADSTAR_ARCHIVE::ensureLoadedLibrary( const wxString& aLibraryPath )
241{
242 // Suppress font substitution warnings (RAII - automatically restored on scope exit)
243 FONTCONFIG_REPORTER_SCOPE fontconfigScope( nullptr );
244
245 if( m_cache.count( aLibraryPath ) )
246 {
247 wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
248
249 if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
250 return;
251 }
252
254 false /*don't log stackup warnings*/, nullptr );
255
256 NAME_TO_FOOTPRINT_MAP footprintMap;
257 std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
258
259 for( std::unique_ptr<FOOTPRINT>& fp : footprints )
260 {
261 footprintMap.insert( { fp->GetFPID().GetLibItemName().wx_str(), std::move( fp ) } );
262 }
263
264 m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
265 m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
266}
const char * name
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
Loads a cpa file into a KiCad BOARD object.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:322
constexpr coord_type GetY() const
Definition box2.h:208
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr coord_type GetX() const
Definition box2.h:207
constexpr size_type GetHeight() const
Definition box2.h:215
void Load(BOARD *aBoard, PROJECT *aProject)
Loads a CADSTAR PCB Archive file into the KiCad BOARD object given.
std::vector< FOOTPRINT * > GetLoadedLibraryFootpints() const
Return a copy of the loaded library footprints (caller owns the objects)
std::vector< std::unique_ptr< FOOTPRINT > > LoadLibrary()
Parse a CADSTAR PCB Archive and load the footprints contained within.
RAII class to set and restore the fontconfig reporter.
Definition reporter.h:322
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition io_base.h:240
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition io_base.cpp:71
virtual void RegisterCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler)
Register a different handler to be called when mapping of input layers to KiCad layers occurs.
LAYER_MAPPING_HANDLER m_layer_mapping_handler
Callback to get layer mapping.
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:218
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 ...
void ensureLoadedLibrary(const wxString &aLibraryPath)
std::map< const wxString, std::unique_ptr< FOOTPRINT > > NAME_TO_FOOTPRINT_MAP
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PCB_IO can read the specified board file.
static std::map< wxString, PCB_LAYER_ID > DefaultLayerMappingCallback(const std::vector< INPUT_LAYER_DESC > &aInputLayerDescriptionVector)
Return the automapped layers.
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
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.
void RegisterCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler) override
Register a different handler to be called when mapping of Cadstar to KiCad layers occurs.
std::map< wxString, NAME_TO_FOOTPRINT_MAP > m_cache
bool CanReadFootprint(const wxString &aFileName) const override
Checks if this PCB_IO can read a footprint from specified file or directory.
bool checkBoardHeader(const wxString &aFileName) const
std::map< wxString, long long > m_timestamps
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
std::vector< FOOTPRINT * > m_loaded_footprints
bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Check for the existence of a footprint.
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...
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition pcb_io.h:326
virtual bool CanReadFootprint(const wxString &aFileName) const
Checks if this PCB_IO can read a footprint from specified file or directory.
Definition pcb_io.cpp:58
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition pcb_io.cpp:42
PCB_IO(const wxString &aName)
Definition pcb_io.h:319
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition pcb_io.h:329
Container for project specific data.
Definition project.h:65
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:71
bool empty() const
Definition utf8.h:109
const char * c_str() const
Definition utf8.h:108
#define IGNORE_PARENT_GROUP
Definition eda_item.h:55
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition io_utils.cpp:34
std::function< std::map< wxString, PCB_LAYER_ID >(const std::vector< INPUT_LAYER_DESC > &)> LAYER_MAPPING_HANDLER
Pointer to a function that takes a map of source and KiCad layers and returns a re-mapped version.
Describes an imported layer and how it could be mapped to KiCad Layers.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695