KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_test_utils.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "odb_test_utils.h"
21
22#include <algorithm>
23#include <fstream>
24#include <map>
25#include <memory>
26#include <sstream>
27
28#include <board.h>
29#include <core/utf8.h>
30#include <footprint.h>
31#include <pad.h>
32#include <reporter.h>
34#include <richio.h>
35#include <vector>
36
37namespace fs = std::filesystem;
38
39
40fs::path ExportOdb( const BOARD& aBoard, const fs::path& aDir, const std::string& aUnits,
41 const std::string& aSigfig, REPORTER* aReporter )
42{
43 fs::create_directories( aDir );
44
45 PCB_IO_ODBPP odbExporter;
46
47 if( aReporter )
48 odbExporter.SetReporter( aReporter );
49
50 std::map<std::string, UTF8> props;
51 props["units"] = aUnits;
52 props["sigfig"] = aSigfig;
53
54 odbExporter.SaveBoard( wxString::FromUTF8( aDir.string() ), const_cast<BOARD&>( aBoard ), &props );
55
56 return aDir;
57}
58
59
60std::vector<std::string> ReadLines( const fs::path& aFile )
61{
62 std::vector<std::string> lines;
63 std::ifstream stream( aFile );
64 std::string line;
65
66 while( std::getline( stream, line ) )
67 lines.push_back( line );
68
69 return lines;
70}
71
72
73std::vector<std::string> ReadLineFields( const std::string& aLine )
74{
75 // From C++23, we could use ispanstream and take a string_view instead of copying the string.
76 std::istringstream stream( aLine );
77 std::vector<std::string> fields;
78 std::string token;
79
80 while( stream >> token )
81 fields.push_back( token );
82
83 return fields;
84}
85
86
88{
89 static std::vector<std::unique_ptr<KI_TEST::SCOPED_TEMP_DIR>> dirs;
90 dirs.emplace_back( std::make_unique<KI_TEST::SCOPED_TEMP_DIR>( wxS( "odb_attributes" ) ) );
91 return *dirs.back();
92}
93
94
95size_t CountPads( const BOARD& aBoard, PAD_PROP aProperty, PCB_LAYER_ID aLayer )
96{
97 size_t count = 0;
98
99 for( const FOOTPRINT* footprint : aBoard.Footprints() )
100 {
101 for( const PAD* pad : footprint->Pads() )
102 {
103 if( pad->GetProperty() == aProperty && pad->IsOnLayer( aLayer ) )
104 ++count;
105 }
106 }
107
108 return count;
109}
110
111
112OdbFeatureFile::OdbFeatureFile( const fs::path& aFile )
113{
114 for( const std::string& line : ReadLines( aFile ) )
115 {
116 if( line.rfind( "@", 0 ) == 0 )
117 {
118 size_t space = line.find( ' ' );
119
120 if( space != std::string::npos )
121 m_names.emplace( line.substr( space + 1 ), line.substr( 1, space - 1 ) );
122 }
123 else if( line.rfind( "CMP ", 0 ) == 0
124 || ( line.size() > 2 && line[1] == ' '
125 && ( line[0] == 'P' || line[0] == 'L' || line[0] == 'A' || line[0] == 'S' ) ) )
126 {
127 m_records.push_back( line );
128 }
129 }
130}
131
132
133size_t OdbFeatureFile::CountRecordsWithOption( const std::string& aName, const std::string& aOption ) const
134{
135 static const std::map<std::string, std::vector<std::string>> options = {
136 { ".pad_usage", { "toeprint", "via", "g_fiducial", "l_fiducial", "tooling_hole", "bond_finger" } },
137 { ".drill", { "plated", "non_plated", "via" } },
138 { ".plated_type", { "standard", "press_fit" } },
139 { ".via_type", { "drilled", "laser", "photo" } },
140 { ".comp_mount_type", { "other", "smd", "tht", "pressfit" } }
141 };
142
143 auto found = options.find( aName );
144
145 if( found == options.end() )
146 return 0;
147
148 const auto& values = found->second;
149 auto option = std::find( values.begin(), values.end(), aOption );
150
151 if( option == values.end() )
152 return 0;
153
154 return CountRecordsWithValue( aName, std::to_string( std::distance( values.begin(), option ) ) );
155}
156
157
158size_t OdbFeatureFile::CountRecordsWithFlag( const std::string& aName ) const
159{
160 return CountRecordsWithValue( aName, std::string() );
161}
162
163
164size_t OdbFeatureFile::CountRecordsWithInteger( const std::string& aName, int aValue ) const
165{
166 return CountRecordsWithValue( aName, std::to_string( aValue ) );
167}
168
169
170size_t OdbFeatureFile::CountRecordsWithValue( const std::string& aName, const std::string& aValue ) const
171{
172 auto name = m_names.find( aName );
173
174 if( name == m_names.end() )
175 return 0;
176
177 size_t count = 0;
178
179 for( const std::string& record : m_records )
180 {
181 size_t start = record.find( ';' );
182
183 if( start == std::string::npos )
184 continue;
185
186 std::stringstream attributes( record.substr( start + 1 ) );
187 std::string attribute;
188
189 while( std::getline( attributes, attribute, ',' ) )
190 {
191 if( attribute == name->second + ( aValue.empty() ? "" : "=" + aValue ) )
192 {
193 ++count;
194 break;
195 }
196 }
197 }
198
199 return count;
200}
201
202
203ODB_PRODUCT::ODB_PRODUCT( const fs::path& aRoot )
204{
205 wxString stepName;
206 wxString block;
207 ODB_MATRIX_ROW row;
208
209 FILE_LINE_READER reader( wxString::FromUTF8( ( aRoot / "matrix" / "matrix" ).string() ) );
210
211 while( const char* line = reader.ReadLine() )
212 {
213 wxString text = wxString::FromUTF8( line ).Strip( wxString::both );
214
215 if( text.EndsWith( wxS( "{" ) ) )
216 {
217 block = text.BeforeLast( '{' ).Strip( wxString::both );
218 row = ODB_MATRIX_ROW();
219 continue;
220 }
221
222 if( text == wxS( "}" ) )
223 {
224 if( block == wxS( "LAYER" ) )
225 m_matrix.push_back( row );
226
227 block.clear();
228 continue;
229 }
230
231 wxString key = text.BeforeFirst( '=' ).Strip( wxString::both );
232 wxString value = text.AfterFirst( '=' ).Strip( wxString::both );
233
234 if( block == wxS( "STEP" ) && key == wxS( "NAME" ) && stepName.IsEmpty() )
235 {
236 stepName = value;
237 }
238 else if( block == wxS( "LAYER" ) )
239 {
240 if( key == wxS( "ROW" ) )
241 row.m_row = wxAtoi( value );
242 else if( key == wxS( "CONTEXT" ) )
243 row.m_context = value;
244 else if( key == wxS( "TYPE" ) )
245 row.m_type = value;
246 else if( key == wxS( "NAME" ) )
247 row.m_name = value;
248 else if( key == wxS( "POLARITY" ) )
249 row.m_polarity = value;
250 else if( key == wxS( "START_NAME" ) )
251 row.m_startName = value;
252 else if( key == wxS( "END_NAME" ) )
253 row.m_endName = value;
254 else if( key == wxS( "ADD_TYPE" ) )
255 row.m_addType = value;
256 else if( key == wxS( "ID" ) )
257 row.m_id = value;
258 else if( key == wxS( "REF" ) )
259 row.m_ref = value;
260 else if( key == wxS( "CU_TOP" ) )
261 row.m_cuTop = value;
262 else if( key == wxS( "CU_BOTTOM" ) )
263 row.m_cuBottom = value;
264 else if( key == wxS( "DIELECTRIC_TYPE" ) )
265 row.m_dielectricType = value;
266 }
267 }
268
269 std::error_code ec;
270
271 for( const fs::directory_entry& entry : fs::directory_iterator( aRoot / "steps", ec ) )
272 {
273 if( entry.is_directory() && wxString::FromUTF8( entry.path().filename().string() ).IsSameAs( stepName, false ) )
274 m_stepDir = entry.path();
275 }
276}
277
278
279wxString ODB_PRODUCT::LayerFile( const wxString& aLayer, const wxString& aFile ) const
280{
281 fs::path layersRoot = m_stepDir / "layers";
282 fs::path layerDir = layersRoot / aLayer.Lower().ToStdString();
283 std::error_code ec;
284
285 for( const fs::directory_entry& entry : fs::directory_iterator( layersRoot, ec ) )
286 {
287 if( entry.is_directory() && wxString::FromUTF8( entry.path().filename().string() ).IsSameAs( aLayer, false ) )
288 layerDir = entry.path();
289 }
290
291 return wxString::FromUTF8( ( layerDir / aFile.ToStdString() ).string() );
292}
const char * name
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:411
const FOOTPRINTS & Footprints() const
Definition board.h:465
A LINE_READER that reads from an open file.
Definition richio.h:157
char * ReadLine() override
Read a line of text into the buffer and increments the line number counter.
Definition richio.cpp:202
virtual void SetReporter(REPORTER *aReporter)
Set an optional reporter for warnings/errors.
Definition io_base.h:89
std::vector< ODB_MATRIX_ROW > m_matrix
std::filesystem::path m_stepDir
wxString LayerFile(const wxString &aLayer, const wxString &aFile) const
Path of a file in a layer directory, matched to the directory name without regard to case.
ODB_PRODUCT(const std::filesystem::path &aRoot)
std::map< std::string, std::string > m_names
std::vector< std::string > m_records
size_t CountRecordsWithInteger(const std::string &aName, int aValue) const
size_t CountRecordsWithOption(const std::string &aName, const std::string &aOption) const
OdbFeatureFile(const std::filesystem::path &aFile)
size_t CountRecordsWithValue(const std::string &aName, const std::string &aValue) const
size_t CountRecordsWithFlag(const std::string &aName) const
Definition pad.h:61
void SaveBoard(const wxString &aFileName, BOARD &aBoard, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Write aBoard to a storage file in a format that this PCB_IO implementation knows about or it can be u...
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
size_t CountPads(const BOARD &aBoard, PAD_PROP aProperty, PCB_LAYER_ID aLayer)
fs::path ExportOdb(const BOARD &aBoard, const fs::path &aDir, const std::string &aUnits, const std::string &aSigfig, REPORTER *aReporter)
std::vector< std::string > ReadLineFields(const std::string &aLine)
Return the tokenised fields of a line, split by whitespace.
std::vector< std::string > ReadLines(const fs::path &aFile)
const KI_TEST::SCOPED_TEMP_DIR & TempDir()
std::vector< std::string > ReadLines(const std::filesystem::path &aFile)
Read a text file into one string per line, with no trailing newline characters.
PAD_PROP
The set of pad properties used in Gerber files (Draw files, and P&P files) to define some properties ...
Definition padstack.h:118
One LAYER block of matrix/matrix.
wxString m_startName
wxString m_cuBottom
wxString m_dielectricType
wxString m_polarity