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 (C) 2020-2023 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
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
58PCB_IO_CADSTAR_ARCHIVE::PCB_IO_CADSTAR_ARCHIVE() : PCB_IO( wxS( "CADSTAR PCB Archive" ) )
59{
62}
63
64
66{
68}
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
103
106 tempPCB.Load( m_board, aProject );
107
108 //center the board:
109 if( aProperties )
110 {
111 UTF8 page_width;
112 UTF8 page_height;
113
114 if( auto it = aProperties->find( "page_width" ); it != aProperties->end() )
115 page_width = it->second;
116
117 if( auto it = aProperties->find( "page_height" ); it != aProperties->end() )
118 page_height = it->second;
119
120 if( !page_width.empty() && !page_height.empty() )
121 {
123
124 int w = atoi( page_width.c_str() );
125 int h = atoi( page_height.c_str() );
126
127 int desired_x = ( w - bbbox.GetWidth() ) / 2;
128 int desired_y = ( h - bbbox.GetHeight() ) / 2;
129
130 m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
131 }
132 }
133
134 // Need to set legacy loading so that netclassess and design rules are loaded correctly
137
139
140 return m_board;
141}
142
143
144bool PCB_IO_CADSTAR_ARCHIVE::checkBoardHeader( const wxString& aFileName ) const
145{
146 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true );
147}
148
149
150bool PCB_IO_CADSTAR_ARCHIVE::CanReadBoard( const wxString& aFileName ) const
151{
152 if( !PCB_IO::CanReadBoard( aFileName ) )
153 return false;
154
155 return checkBoardHeader( aFileName );
156}
157
158
159bool PCB_IO_CADSTAR_ARCHIVE::CanReadLibrary( const wxString& aFileName ) const
160{
161 if( !PCB_IO::CanReadLibrary( aFileName ) )
162 return false;
163
164 return checkBoardHeader( aFileName );
165}
166
167
168bool PCB_IO_CADSTAR_ARCHIVE::CanReadFootprint( const wxString& aFileName ) const
169{
170 if( !PCB_IO::CanReadFootprint( aFileName ) )
171 return false;
172
173 return checkBoardHeader( aFileName );
174}
175
176
177void PCB_IO_CADSTAR_ARCHIVE::FootprintEnumerate( wxArrayString& aFootprintNames,
178 const wxString& aLibraryPath,
179 bool aBestEfforts,
180 const std::map<std::string, UTF8>* aProperties )
181{
182 ensureLoadedLibrary( aLibraryPath );
183
184 if( !m_cache.count( aLibraryPath ) )
185 return; // not found
186
187 for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
188 aFootprintNames.Add( name );
189}
190
191
192bool PCB_IO_CADSTAR_ARCHIVE::FootprintExists( const wxString& aLibraryPath,
193 const wxString& aFootprintName,
194 const std::map<std::string, UTF8>* aProperties )
195{
196 ensureLoadedLibrary( aLibraryPath );
197
198 if( !m_cache.count( aLibraryPath ) )
199 return false;
200
201 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
202 return false;
203
204 return true;
205}
206
207
209 const wxString& aFootprintName,
210 bool aKeepUUID,
211 const std::map<std::string, UTF8>* aProperties )
212{
213 ensureLoadedLibrary( aLibraryPath );
214
215 if( !m_cache.count( aLibraryPath ) )
216 return nullptr;
217
218 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
219 return nullptr;
220
221 if( !m_cache.at( aLibraryPath ).at( aFootprintName ) )
222 return nullptr;
223
224 return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate() );
225}
226
227
228long long PCB_IO_CADSTAR_ARCHIVE::GetLibraryTimestamp( const wxString& aLibraryPath ) const
229{
230 wxFileName fn( aLibraryPath );
231
232 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
233 return fn.GetModificationTime().GetValue().GetValue();
234 else
235 return wxDateTime( 0.0 ).GetValue().GetValue();
236}
237
238
239void PCB_IO_CADSTAR_ARCHIVE::ensureLoadedLibrary( const wxString& aLibraryPath )
240{
242
243 if( m_cache.count( aLibraryPath ) )
244 {
245 wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
246
247 if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
248 return;
249 }
250
252 false /*don't log stackup warnings*/, nullptr );
253
254 NAME_TO_FOOTPRINT_MAP footprintMap;
255 std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
256
257 for( std::unique_ptr<FOOTPRINT>& fp : footprints )
258 {
259 footprintMap.insert( { fp->GetFPID().GetLibItemName().wx_str(), std::move( fp ) } );
260 }
261
262 m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
263 m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
264}
const char * name
Definition: DXF_plotter.cpp:57
Loads a cpa file into a KiCad BOARD object.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:290
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition: board.h:373
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:929
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: board.cpp:502
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition: board.h:377
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.
PROGRESS_REPORTER * m_progressReporter
Progress reporter to track the progress of the operation, may be nullptr.
Definition: io_base.h:221
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:69
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.
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).
A base class that BOARD loading and saving plugins should derive from.
Definition: pcb_io.h:71
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:342
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
const std::map< std::string, UTF8 > * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:345
Container for project specific data.
Definition: project.h:64
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
bool empty() const
Definition: utf8.h:104
const char * c_str() const
Definition: utf8.h:103
static REPORTER & GetInstance()
Definition: reporter.cpp:199
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
Definition: fontconfig.cpp:64
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:691