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 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>
32
33
35 const std::vector<INPUT_LAYER_DESC>& aInputLayerDescriptionVector )
36{
37 std::map<wxString, PCB_LAYER_ID> retval;
38
39 // Just return a the auto-mapped layers
40 for( INPUT_LAYER_DESC layerDesc : aInputLayerDescriptionVector )
41 {
42 retval.insert( { layerDesc.Name, layerDesc.AutoMapLayer } );
43 }
44
45 return retval;
46}
47
48
50 LAYER_MAPPING_HANDLER aLayerMappingHandler )
51{
53 m_show_layer_mapping_warnings = false; // only show warnings with default callback
54}
55
56
58{
59 m_board = nullptr;
60 m_props = nullptr;
64}
65
66
68{
69}
70
71
73{
75 {
76 delete fp;
77 }
78
79 m_loaded_footprints.clear();
80}
81
82
84{
85 return wxT( "CADSTAR PCB Archive" );
86}
87
88
90{
91 return wxT( "cpa" );
92}
93
94
96{
97 std::vector<FOOTPRINT*> retval;
98
100 {
101 retval.push_back( static_cast<FOOTPRINT*>( fp->Clone() ) );
102 }
103
104 return retval;
105}
106
107
108BOARD* CADSTAR_PCB_ARCHIVE_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe,
109 const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
110 PROGRESS_REPORTER* aProgressReporter )
111{
112 m_props = aProperties;
113 m_board = aAppendToMe ? aAppendToMe : new BOARD();
115
117 m_show_layer_mapping_warnings, aProgressReporter );
118 tempPCB.Load( m_board, aProject );
119
120 //center the board:
121 if( aProperties )
122 {
123 UTF8 page_width;
124 UTF8 page_height;
125
126 if( aProperties->Value( "page_width", &page_width )
127 && aProperties->Value( "page_height", &page_height ) )
128 {
130
131 int w = atoi( page_width.c_str() );
132 int h = atoi( page_height.c_str() );
133
134 int desired_x = ( w - bbbox.GetWidth() ) / 2;
135 int desired_y = ( h - bbbox.GetHeight() ) / 2;
136
137 m_board->Move( VECTOR2I( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
138 }
139 }
140
141 // Need to set legacy loading so that netclassess and design rules are loaded correctly
144
146
147 return m_board;
148}
149
150
151void CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintEnumerate( wxArrayString& aFootprintNames,
152 const wxString& aLibraryPath,
153 bool aBestEfforts,
154 const STRING_UTF8_MAP* aProperties )
155{
156 ensureLoadedLibrary( aLibraryPath );
157
158 if( !m_cache.count( aLibraryPath ) )
159 return; // not found
160
161 for( const auto& [name, fp] : m_cache.at( aLibraryPath ) )
162 aFootprintNames.Add( name );
163}
164
165
166const FOOTPRINT*
168 const wxString& aFootprintName,
169 const STRING_UTF8_MAP* aProperties )
170{
171 ensureLoadedLibrary( aLibraryPath );
172
173 if( !m_cache.count( aLibraryPath ) )
174 return nullptr;
175
176 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
177 return nullptr;
178
179 return m_cache.at( aLibraryPath ).at( aFootprintName ).get();
180}
181
182
183bool CADSTAR_PCB_ARCHIVE_PLUGIN::FootprintExists( const wxString& aLibraryPath,
184 const wxString& aFootprintName,
185 const STRING_UTF8_MAP* aProperties )
186{
187 ensureLoadedLibrary( aLibraryPath );
188
189 if( !m_cache.count( aLibraryPath ) )
190 return false;
191
192 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
193 return false;
194
195 return true;
196}
197
198
200 const wxString& aFootprintName,
201 bool aKeepUUID,
202 const STRING_UTF8_MAP* aProperties )
203{
204 ensureLoadedLibrary( aLibraryPath );
205
206 if( !m_cache.count( aLibraryPath ) )
207 return nullptr;
208
209 if( !m_cache.at( aLibraryPath ).count( aFootprintName ) )
210 return nullptr;
211
212 return static_cast<FOOTPRINT*>( m_cache.at( aLibraryPath ).at( aFootprintName )->Duplicate() );
213}
214
215
216long long CADSTAR_PCB_ARCHIVE_PLUGIN::GetLibraryTimestamp( const wxString& aLibraryPath ) const
217{
218 wxFileName fn( aLibraryPath );
219
220 if( fn.IsFileReadable() )
221 return fn.GetModificationTime().GetValue().GetValue();
222 else
223 return wxDateTime( 0.0 ).GetValue().GetValue();
224}
225
226
227void CADSTAR_PCB_ARCHIVE_PLUGIN::ensureLoadedLibrary( const wxString& aLibraryPath )
228{
229 if( m_cache.count( aLibraryPath ) )
230 {
231 wxCHECK( m_timestamps.count( aLibraryPath ), /*void*/ );
232
233 if( m_timestamps.at( aLibraryPath ) == GetLibraryTimestamp( aLibraryPath ) )
234 return;
235 }
236
238 false /*don't log stackup warnings*/, nullptr );
239
240 NAME_TO_FOOTPRINT_MAP footprintMap;
241 std::vector<std::unique_ptr<FOOTPRINT>> footprints = csLoader.LoadLibrary();
242
243 for( std::unique_ptr<FOOTPRINT>& fp : footprints )
244 {
245 footprintMap.insert( { fp->GetFPID().GetLibItemName(), std::move( fp ) } );
246 }
247
248 m_cache.insert( { aLibraryPath, std::move( footprintMap ) } );
249 m_timestamps[aLibraryPath] = GetLibraryTimestamp( aLibraryPath );
250}
const char * name
Definition: DXF_plotter.cpp:56
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:270
bool m_LegacyDesignSettingsLoaded
True if the legacy board design settings were loaded from a file.
Definition: board.h:350
const BOX2I GetBoardEdgesBoundingBox() const
Return the board bounding box calculated using exclusively the board edges (graphics on Edge....
Definition: board.h:858
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition: board.cpp:424
bool m_LegacyNetclassesLoaded
True if netclasses were loaded from the file.
Definition: board.h:354
coord_type GetHeight() const
Definition: box2.h:188
coord_type GetY() const
Definition: box2.h:181
coord_type GetWidth() const
Definition: box2.h:187
coord_type GetX() const
Definition: box2.h:180
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).
void ensureLoadedLibrary(const wxString &aLibraryPath)
std::map< wxString, long long > m_timestamps
const FOOTPRINT * GetEnumeratedFootprint(const wxString &aLibraryPath, const wxString &aFootprintName, const STRING_UTF8_MAP *aProperties=nullptr) override
A version of FootprintLoad() for use after FootprintEnumerate() for more efficient cache management.
const wxString GetFileExtension() const override
Returns the file extension for the PLUGIN.
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
const wxString PluginName() const override
Return a brief hard coded name for this PLUGIN.
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.
std::vector< FOOTPRINT * > m_loaded_footprints
std::map< const wxString, std::unique_ptr< FOOTPRINT > > NAME_TO_FOOTPRINT_MAP
BOARD * Load(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.
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.
A progress reporter interface for use in multi-threaded environments.
Container for project specific data.
Definition: project.h:64
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
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