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
28#include <board.h>
29#include <footprint.h>
30#include <string_utf8_map.h>
31#include <io/io_utils.h>
32#include <pcb_io/pcb_io.h>
33
34
36 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
37{
38 std::map<wxString, PCB_LAYER_ID> retval;
39
40 // Just return a the auto-mapped layers
41 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
42 {
43 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
44 }
45
46 return retval;
47}
48
49
51 LAYER_MAPPING_HANDLER aLayerMappingHandler )
52{
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{
63}
64
65
67{
69}
70
71
73{
75 {
76 delete fp;
77 }
78
79 m_loaded_footprints.clear();
80}
81
82
84{
85 std::vector<FOOTPRINT*> retval;
86
88 {
89 retval.push_back( static_cast<FOOTPRINT*>( fp->Clone() ) );
90 }
91
92 return retval;
93}
94
95
96BOARD* PCB_IO_CADSTAR_ARCHIVE::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
97 const STRING_UTF8_MAP* aProperties, PROJECT* aProject )
98{
99 m_props = aProperties;
100 m_board = aAppendToMe ? aAppendToMe : new BOARD();
102
105 tempPCB.Load( m_board, aProject );
106
107 //center the board:
108 if( aProperties )
109 {
110 UTF8 page_width;
111 UTF8 page_height;
112
113 if( aProperties->Value( "page_width", &page_width )
114 && aProperties->Value( "page_height", &page_height ) )
115 {
117
118 int w = atoi( page_width.c_str() );
119 int h = atoi( page_height.c_str() );
120
121 int desired_x = ( w - bbbox.GetWidth() ) / 2;
122 int desired_y = ( h - bbbox.GetHeight() ) / 2;
123
124 m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
125 }
126 }
127
128 // Need to set legacy loading so that netclassess and design rules are loaded correctly
131
133
134 return m_board;
135}
136
137
138bool PCB_IO_CADSTAR_ARCHIVE::checkBoardHeader( const wxString& aFileName ) const
139{
140 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true );
141}
142
143
144bool PCB_IO_CADSTAR_ARCHIVE::CanReadBoard( const wxString& aFileName ) const
145{
146 if( !PCB_IO::CanReadBoard( aFileName ) )
147 return false;
148
149 return checkBoardHeader( aFileName );
150}
151
152
153bool PCB_IO_CADSTAR_ARCHIVE::CanReadLibrary( const wxString& aFileName ) const
154{
155 if( !PCB_IO::CanReadLibrary( aFileName ) )
156 return false;
157
158 return checkBoardHeader( aFileName );
159}
160
161
162bool PCB_IO_CADSTAR_ARCHIVE::CanReadFootprint( const wxString& aFileName ) const
163{
164 if( !PCB_IO::CanReadFootprint( aFileName ) )
165 return false;
166
167 return checkBoardHeader( aFileName );
168}
169
170
171void PCB_IO_CADSTAR_ARCHIVE::FootprintEnumerate( wxArrayString& aFootprintNames,
172 const wxString& aLibraryPath,
173 bool aBestEfforts,
174 const STRING_UTF8_MAP* aProperties )
175{
176 ensureLoadedLibrary( aLibraryPath );
177
178 if( !m_cache.count( aLibraryPath ) )
179 return; // not found
180
181 for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
182 aFootprintNames.Add( name );
183}
184
185
186bool PCB_IO_CADSTAR_ARCHIVE::FootprintExists( const wxString& aLibraryPath,
187 const wxString& aFootprintName,
188 const STRING_UTF8_MAP* aProperties )
189{
190 ensureLoadedLibrary( aLibraryPath );
191
192 if( !m_cache.count( aLibraryPath ) )
193 return false;
194
195 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
196 return false;
197
198 return true;
199}
200
201
203 const wxString& aFootprintName,
204 bool aKeepUUID,
205 const STRING_UTF8_MAP* aProperties )
206{
207 ensureLoadedLibrary( aLibraryPath );
208
209 if( !m_cache.count( aLibraryPath ) )
210 return nullptr;
211
212 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
213 return nullptr;
214
215 if( !m_cache.at( aLibraryPath ).at( aFootprintName ) )
216 return nullptr;
217
218 return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate() );
219}
220
221
222long long PCB_IO_CADSTAR_ARCHIVE::GetLibraryTimestamp( const wxString& aLibraryPath ) const
223{
224 wxFileName fn( aLibraryPath );
225
226 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
227 return fn.GetModificationTime().GetValue().GetValue();
228 else
229 return wxDateTime( 0.0 ).GetValue().GetValue();
230}
231
232
233void PCB_IO_CADSTAR_ARCHIVE::ensureLoadedLibrary( const wxString& aLibraryPath )
234{
235 if( m_cache.count( aLibraryPath ) )
236 {
237 wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
238
239 if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
240 return;
241 }
242
244 false /*don't log stackup warnings*/, nullptr );
245
246 NAME_TO_FOOTPRINT_MAP footprintMap;
247 std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
248
249 for( std::unique_ptr<FOOTPRINT>& fp : footprints )
250 {
251 footprintMap.insert( { fp->GetFPID().GetLibItemName().wx_str(), std::move( fp ) } );
252 }
253
254 m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
255 m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
256}
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:282
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition: board.h:365
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:911
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: board.cpp:493
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition: board.h:369
size_type GetHeight() const
Definition: box2.h:205
size_type GetWidth() const
Definition: box2.h:204
coord_type GetY() const
Definition: box2.h:198
coord_type GetX() const
Definition: box2.h:197
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:213
virtual bool CanReadLibrary(const wxString &aFileName) const
Checks if this IO object can read the specified library file/directory.
Definition: io_base.cpp:67
LAYER_MAPPING_HANDLER m_layer_mapping_handler
Callback to get layer mapping.
virtual void RegisterLayerMappingCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler)
Register a different handler to be called when mapping of input layers to KiCad layers occurs.
void ensureLoadedLibrary(const wxString &aLibraryPath)
std::map< const wxString, std::unique_ptr< FOOTPRINT > > NAME_TO_FOOTPRINT_MAP
void RegisterLayerMappingCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler) override
Register a different handler to be called when mapping of Cadstar to KiCad layers occurs.
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.
FOOTPRINT * FootprintLoad(const wxString &aLibraryPath, const wxString &aFootprintName, bool aKeepUUID=false, const STRING_UTF8_MAP *aProperties=nullptr) override
Load a footprint having aFootprintName from the aLibraryPath containing a library format that this PC...
bool CanReadLibrary(const wxString &aFileName) const override
Checks if this IO object can read the specified library file/directory.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
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 STRING_UTF8_MAP *aProperties=nullptr) override
Check for the existence of a footprint.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aLibraryPath, bool aBestEfforts, const STRING_UTF8_MAP *aProperties=nullptr) override
Return a list of footprint names contained within the library at aLibraryPath.
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:72
BOARD * m_board
The board BOARD being worked on, no ownership here.
Definition: pcb_io.h:343
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:59
const STRING_UTF8_MAP * m_props
Properties passed via Save() or Load(), no ownership, may be NULL.
Definition: pcb_io.h:346
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PCB_IO can read the specified board file.
Definition: pcb_io.cpp:43
Container for project specific data.
Definition: project.h:62
A name/value tuple with unique names and optional values.
bool Value(const char *aName, UTF8 *aFetchedValue=nullptr) const
Fetch a property by aName and returns true if that property was found, else false.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
const char * c_str() const
Definition: utf8.h:103
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition: io_utils.cpp:32
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< int > VECTOR2I
Definition: vector2d.h:588