KiCad PCB EDA Suite
Loading...
Searching...
No Matches
cadstar_pcb_archive_plugin.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
29#include <board.h>
30#include <footprint.h>
31#include <string_utf8_map.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
59{
60 m_board = nullptr;
61 m_props = nullptr;
65}
66
67
69{
71}
72
73
75{
77 {
78 delete fp;
79 }
80
81 m_loaded_footprints.clear();
82}
83
84
86{
87 std::vector<FOOTPRINT*> retval;
88
90 {
91 retval.push_back( static_cast<FOOTPRINT*>( fp->Clone() ) );
92 }
93
94 return retval;
95}
96
97
98BOARD* CADSTAR_PCB_ARCHIVE_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
99 const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
100 PROGRESS_REPORTER* aProgressReporter )
101{
102 m_props = aProperties;
103 m_board = aAppendToMe ? aAppendToMe : new BOARD();
105
107 m_show_layer_mapping_warnings, aProgressReporter );
108 tempPCB.Load( m_board, aProject );
109
110 //center the board:
111 if( aProperties )
112 {
113 UTF8 page_width;
114 UTF8 page_height;
115
116 if( aProperties->Value( "page_width", &page_width )
117 && aProperties->Value( "page_height", &page_height ) )
118 {
120
121 int w = atoi( page_width.c_str() );
122 int h = atoi( page_height.c_str() );
123
124 int desired_x = ( w - bbbox.GetWidth() ) / 2;
125 int desired_y = ( h - bbbox.GetHeight() ) / 2;
126
127 m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
128 }
129 }
130
131 // Need to set legacy loading so that netclassess and design rules are loaded correctly
134
136
137 return m_board;
138}
139
140
141bool CADSTAR_PCB_ARCHIVE_PLUGIN::checkBoardHeader( const wxString& aFileName ) const
142{
143 return PLUGIN_UTILS::fileStartsWithPrefix( aFileName, wxT( "(CADSTARPCB" ), true );
144}
145
146
147bool CADSTAR_PCB_ARCHIVE_PLUGIN::CanReadBoard( const wxString& aFileName ) const
148{
149 if( !PLUGIN::CanReadBoard( aFileName ) )
150 return false;
151
152 return checkBoardHeader( aFileName );
153}
154
155
156bool CADSTAR_PCB_ARCHIVE_PLUGIN::CanReadFootprintLib( const wxString& aFileName ) const
157{
158 if( !PLUGIN::CanReadFootprintLib( aFileName ) )
159 return false;
160
161 return checkBoardHeader( aFileName );
162}
163
164
165bool CADSTAR_PCB_ARCHIVE_PLUGIN::CanReadFootprint( const wxString& aFileName ) const
166{
167 if( !PLUGIN::CanReadFootprint( aFileName ) )
168 return false;
169
170 return checkBoardHeader( aFileName );
171}
172
173
174void CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames,
175 const wxString& aLibraryPath,
176 bool aBestEfforts,
177 const STRING_UTF8_MAP* aProperties )
178{
179 ensureLoadedLibrary( aLibraryPath );
180
181 if( !m_cache.count( aLibraryPath ) )
182 return; // not found
183
184 for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
185 aFootprintNames.Add( name );
186}
187
188
189bool CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintExists( const wxString& aLibraryPath,
190 const wxString& aFootprintName,
191 const STRING_UTF8_MAP* aProperties )
192{
193 ensureLoadedLibrary( aLibraryPath );
194
195 if( !m_cache.count( aLibraryPath ) )
196 return false;
197
198 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
199 return false;
200
201 return true;
202}
203
204
206 const wxString& aFootprintName,
207 bool aKeepUUID,
208 const STRING_UTF8_MAP* aProperties )
209{
210 ensureLoadedLibrary( aLibraryPath );
211
212 if( !m_cache.count( aLibraryPath ) )
213 return nullptr;
214
215 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
216 return nullptr;
217
218 if( !m_cache.at( aLibraryPath ).at( aFootprintName ) )
219 return nullptr;
220
221 return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate() );
222}
223
224
225long long CADSTAR_PCB_ARCHIVE_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
226{
227 wxFileName fn( aLibraryPath );
228
229 if( fn.IsFileReadable() && fn.GetModificationTime().IsValid() )
230 return fn.GetModificationTime().GetValue().GetValue();
231 else
232 return wxDateTime( 0.0 ).GetValue().GetValue();
233}
234
235
236void CADSTAR_PCB_ARCHIVE_PLUGIN::ensureLoadedLibrary( const wxString& aLibraryPath )
237{
238 if( m_cache.count( aLibraryPath ) )
239 {
240 wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
241
242 if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
243 return;
244 }
245
247 false /*don't log stackup warnings*/, nullptr );
248
249 NAME_TO_FOOTPRINT_MAP footprintMap;
250 std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
251
252 for( std::unique_ptr<FOOTPRINT>& fp : footprints )
253 {
254 footprintMap.insert( { fp->GetFPID().GetLibItemName().wx_str(), std::move( fp ) } );
255 }
256
257 m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
258 m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
259}
const char * name
Definition: DXF_plotter.cpp:57
Loads a cpa file into a KiCad BOARD object.
Pcbnew PLUGIN for CADSTAR PCB Archive (*.cpa) format: an ASCII format based on S-expressions.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:271
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition: board.h:351
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:861
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: board.cpp:427
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition: board.h:355
coord_type GetHeight() const
Definition: box2.h:189
coord_type GetY() const
Definition: box2.h:182
coord_type GetWidth() const
Definition: box2.h:188
coord_type GetX() const
Definition: box2.h:181
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.
bool FootprintExists(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
Check for the existence of a footprint.
std::vector< FOOTPRINT * > GetImportedCachedLibraryFootprints() override
Return a container with the cached library footprints generated in the last call to Load.
long long GetLibraryTimestamp(const wxString &aLibraryPath) const override
Generate a timestamp representing all the files in the library (including the library directory).
bool CanReadBoard(const wxString &aFileName) const override
Checks if this PLUGIN can read the specified board file.
void ensureLoadedLibrary(const wxString &aLibraryPath)
std::map< wxString, long long > m_timestamps
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 PL...
std::map< wxString, NAME_TO_FOOTPRINT_MAP > m_cache
static std::map< wxString, PCB_LAYER_ID > DefaultLayerMappingCallback(const std::vector< INPUT_LAYER_DESC > &aInputLayerDescriptionVector)
Return the automapped layers.
void RegisterLayerMappingCallback(LAYER_MAPPING_HANDLER aLayerMappingHandler) override
Register a different handler to be called when mapping of Cadstar to KiCad layers occurs.
bool checkBoardHeader(const wxString &aFileName) const
std::vector< FOOTPRINT * > m_loaded_footprints
std::map< const wxString, std::unique_ptr< FOOTPRINT > > NAME_TO_FOOTPRINT_MAP
bool CanReadFootprint(const wxString &aFileName) const override
Checks if this PLUGIN can read a footprint from specified file or directory.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const STRING_UTF8_MAP *aProperties=nullptr, PROJECT *aProject=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Load information from some input file format that this PLUGIN implementation knows about into either ...
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.
bool CanReadFootprintLib(const wxString &aFileName) const override
Checks if this PLUGIN can read footprint library from specified file or directory.
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.
virtual bool CanReadFootprintLib(const wxString &aFileName) const
Checks if this PLUGIN can read footprint library from specified file or directory.
Definition: plugin.cpp:93
virtual bool CanReadFootprint(const wxString &aFileName) const
Checks if this PLUGIN can read a footprint from specified file or directory.
Definition: plugin.cpp:77
virtual bool CanReadBoard(const wxString &aFileName) const
Checks if this PLUGIN can read the specified board file.
Definition: plugin.cpp:61
A progress reporter interface for use in multi-threaded environments.
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:71
const char * c_str() const
Definition: utf8.h:102
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
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