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 <string_utf8_map.h>
32#include <io/io_utils.h>
33#include <pcb_io/pcb_io.h>
34#include <reporter.h>
35
36
38 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
39{
40 std::map<wxString, PCB_LAYER_ID> retval;
41
42 // Just return a the auto-mapped layers
43 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
44 {
45 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
46 }
47
48 return retval;
49}
50
51
53{
54 LAYER_MAPPABLE_PLUGIN::RegisterCallback( aLayerMappingHandler );
55 m_show_layer_mapping_warnings = false; // only show warnings with default callback
56}
57
58
59PCB_IO_CADSTAR_ARCHIVE::PCB_IO_CADSTAR_ARCHIVE() : PCB_IO( wxS( "CADSTAR PCB Archive" ) )
60{
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
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( aProperties->Value( "page_width", &page_width )
116 && aProperties->Value( "page_height", &page_height ) )
117 {
119
120 int w = atoi( page_width.c_str() );
121 int h = atoi( page_height.c_str() );
122
123 int desired_x = ( w - bbbox.GetWidth() ) / 2;
124 int desired_y = ( h - bbbox.GetHeight() ) / 2;
125
126 m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
127 }
128 }
129
130 // Need to set legacy loading so that netclassess and design rules are loaded correctly
133
135
136 return m_board;
137}
138
139
140bool PCB_IO_CADSTAR_ARCHIVE::checkBoardHeader( const wxString& aFileName ) const
141{
142 return IO_UTILS::fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true );
143}
144
145
146bool PCB_IO_CADSTAR_ARCHIVE::CanReadBoard( const wxString& aFileName ) const
147{
148 if( !PCB_IO::CanReadBoard( aFileName ) )
149 return false;
150
151 return checkBoardHeader( aFileName );
152}
153
154
155bool PCB_IO_CADSTAR_ARCHIVE::CanReadLibrary( const wxString& aFileName ) const
156{
157 if( !PCB_IO::CanReadLibrary( aFileName ) )
158 return false;
159
160 return checkBoardHeader( aFileName );
161}
162
163
164bool PCB_IO_CADSTAR_ARCHIVE::CanReadFootprint( const wxString& aFileName ) const
165{
166 if( !PCB_IO::CanReadFootprint( aFileName ) )
167 return false;
168
169 return checkBoardHeader( aFileName );
170}
171
172
173void PCB_IO_CADSTAR_ARCHIVE::FootprintEnumerate( wxArrayString& aFootprintNames,
174 const wxString& aLibraryPath,
175 bool aBestEfforts,
176 const STRING_UTF8_MAP* aProperties )
177{
178 ensureLoadedLibrary( aLibraryPath );
179
180 if( !m_cache.count( aLibraryPath ) )
181 return; // not found
182
183 for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
184 aFootprintNames.Add( name );
185}
186
187
188bool PCB_IO_CADSTAR_ARCHIVE::FootprintExists( const wxString& aLibraryPath,
189 const wxString& aFootprintName,
190 const STRING_UTF8_MAP* aProperties )
191{
192 ensureLoadedLibrary( aLibraryPath );
193
194 if( !m_cache.count( aLibraryPath ) )
195 return false;
196
197 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
198 return false;
199
200 return true;
201}
202
203
205 const wxString& aFootprintName,
206 bool aKeepUUID,
207 const STRING_UTF8_MAP* aProperties )
208{
209 ensureLoadedLibrary( aLibraryPath );
210
211 if( !m_cache.count( aLibraryPath ) )
212 return nullptr;
213
214 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
215 return nullptr;
216
217 if( !m_cache.at( aLibraryPath ).at( aFootprintName ) )
218 return nullptr;
219
220 return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate() );
221}
222
223
224long long PCB_IO_CADSTAR_ARCHIVE::GetLibraryTimestamp( const wxString& aLibraryPath ) const
225{
226 wxFileName fn( aLibraryPath );
227
228 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
229 return fn.GetModificationTime().GetValue().GetValue();
230 else
231 return wxDateTime( 0.0 ).GetValue().GetValue();
232}
233
234
235void PCB_IO_CADSTAR_ARCHIVE::ensureLoadedLibrary( const wxString& aLibraryPath )
236{
238
239 if( m_cache.count( aLibraryPath ) )
240 {
241 wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
242
243 if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
244 return;
245 }
246
248 false /*don't log stackup warnings*/, nullptr );
249
250 NAME_TO_FOOTPRINT_MAP footprintMap;
251 std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
252
253 for( std::unique_ptr<FOOTPRINT>& fp : footprints )
254 {
255 footprintMap.insert( { fp->GetFPID().GetLibItemName().wx_str(), std::move( fp ) } );
256 }
257
258 m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
259 m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
260}
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:289
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition: board.h:372
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:922
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: board.cpp:500
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition: board.h:376
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:218
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.
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.
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.
void RegisterCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler) override
Register a different handler to be called when mapping of Cadstar to KiCad layers occurs.
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
static REPORTER & GetInstance()
Definition: reporter.cpp:212
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: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< int32_t > VECTOR2I
Definition: vector2d.h:673