KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_util.h
Go to the documentation of this file.
1#ifndef _ODB_UTIL_H_
2#define _ODB_UTIL_H_
3
4#include <map>
5#include <iostream>
6#include <fstream>
7#include <vector>
8#include <string>
9#include <wx/string.h>
10#include "pcb_shape.h"
11#include <wx/filename.h>
12
13
14enum class ODB_POLARITY
15{
18};
19
20enum class ODB_CONTEXT
21{
22 BOARD,
23 MISC
24};
25
27{
28 NONE,
29 PREPREG,
30 CORE
31};
32
33enum class ODB_TYPE
34{
36
37 SIGNAL,
40 MIXED,
44 DRILL,
45 ROUT,
48 MASK,
50};
51
52enum class ODB_SUBTYPE
53{
60 PSA,
63};
64
65enum class ODB_FID_TYPE
66{
67 COPPER,
69 HOLE
70};
71
72
73namespace ODB
74{
75wxString GenODBString( const wxString& aStr );
76
77wxString GenLegalNetName( const wxString& aStr );
78
79wxString GenLegalEntityName( const wxString& aStr );
80
81void RemoveWhitespace( wxString& aStr );
82
83wxString Double2String( double aVal );
84
85std::string Double2String( double aVal, int32_t aDigits );
86
87wxString Data2String( double aVal );
88
89wxString SymDouble2String( double aVal );
90
91std::pair<wxString, wxString> AddXY( const VECTOR2I& aVec );
92
93VECTOR2I GetShapePosition( const PCB_SHAPE& aShape );
94
95template <typename T>
97{
98public:
99 static std::map<T, std::string>& GetMap()
100 {
101 static_assert( std::is_enum_v<T>, "Template parameter T must be an enum type" );
102
103 static std::map<T, std::string> map = []()
104 {
105 std::map<T, std::string> result;
106
107 if constexpr( std::is_same_v<T, ODB_POLARITY> )
108 {
109 result[ODB_POLARITY::POSITIVE] = "POSITIVE";
110 result[ODB_POLARITY::NEGATIVE] = "NEGATIVE";
111 }
112
113 if constexpr( std::is_same_v<T, ODB_CONTEXT> )
114 {
115 result[ODB_CONTEXT::BOARD] = "BOARD";
116 result[ODB_CONTEXT::MISC] = "MISC";
117 }
118
119 if constexpr( std::is_same_v<T, ODB_TYPE> )
120 {
121 //just for logical reasons.TYPE field must be defined.
122 result[ODB_TYPE::UNDEFINED] = "";
123
124 result[ODB_TYPE::SIGNAL] = "SIGNAL";
125 result[ODB_TYPE::POWER_GROUND] = "POWER_GROUND";
126 result[ODB_TYPE::DIELECTRIC] = "DIELECTRIC";
127 result[ODB_TYPE::MIXED] = "MIXED";
128 result[ODB_TYPE::SOLDER_MASK] = "SOLDER_MASK";
129 result[ODB_TYPE::SOLDER_PASTE] = "SOLDER_PASTE";
130 result[ODB_TYPE::SILK_SCREEN] = "SILK_SCREEN";
131 result[ODB_TYPE::DRILL] = "DRILL";
132 result[ODB_TYPE::ROUT] = "ROUT";
133 result[ODB_TYPE::DOCUMENT] = "DOCUMENT";
134 result[ODB_TYPE::COMPONENT] = "COMPONENT";
135 result[ODB_TYPE::MASK] = "MASK";
136 result[ODB_TYPE::CONDUCTIVE_PASTE] = "CONDUCTIVE_PASTE";
137 }
138
139 if constexpr( std::is_same_v<T, ODB_SUBTYPE> )
140 {
141 result[ODB_SUBTYPE::COVERLAY] = "COVERLAY";
142 result[ODB_SUBTYPE::COVERCOAT] = "COVERCOAT";
143 result[ODB_SUBTYPE::STIFFENER] = "STIFFENER";
144 result[ODB_SUBTYPE::BEND_AREA] = "BEND_AREA";
145 result[ODB_SUBTYPE::FLEX_AREA] = "FLEX_AREA";
146 result[ODB_SUBTYPE::RIGID_AREA] = "RIGID_AREA";
147 result[ODB_SUBTYPE::PSA] = "PSA";
148 result[ODB_SUBTYPE::SILVER_MASK] = "SILVER_MASK";
149 result[ODB_SUBTYPE::CARBON_MASK] = "CARBON_MASK";
150 }
151
152 if constexpr( std::is_same_v<T, ODB_DIELECTRIC_TYPE> )
153 {
154 result[ODB_DIELECTRIC_TYPE::NONE] = "NONE";
155 result[ODB_DIELECTRIC_TYPE::PREPREG] = "PREPREG";
156 result[ODB_DIELECTRIC_TYPE::CORE] = "CORE";
157 }
158
159 if constexpr( std::is_same_v<T, ODB_FID_TYPE> )
160 {
161 result[ODB_FID_TYPE::COPPER] = "C";
162 result[ODB_FID_TYPE::LAMINATE] = "L";
163 result[ODB_FID_TYPE::HOLE] = "H";
164 }
165
166 return result;
167 }();
168
169 return map;
170 }
171};
172
173template <typename T>
174std::string Enum2String( T value )
175{
176 const auto& map = EnumStringMap<T>::GetMap();
177 auto it = map.find( value );
178 if( it != map.end() )
179 {
180 return it->second;
181 }
182 else
183 {
184 throw std::out_of_range( "Enum value not found in map" );
185 }
186}
187
189{
190public:
192 {
193 if( first )
194 {
195 first = false;
196 return true;
197 }
198 return false;
199 }
200
201private:
202 bool first = true;
203};
204
205} // namespace ODB
206
207class ODB_TREE_WRITER;
209{
210public:
211 ODB_FILE_WRITER( ODB_TREE_WRITER& aTreeWriter, const wxString& aFileName );
212
214
217
220
221 void CreateFile( const wxString& aFileName );
222 bool CloseFile();
223 inline std::ostream& GetStream() { return m_ostream; }
224
225private:
227 std::ofstream m_ostream;
228};
229
230
232{
233public:
234 ODB_TREE_WRITER( const wxString& aDir ) : m_currentPath( aDir ) {}
235
236 ODB_TREE_WRITER( const wxString& aPareDir, const wxString& aSubDir )
237 {
238 CreateEntityDirectory( aPareDir, aSubDir );
239 }
240
241 virtual ~ODB_TREE_WRITER() {}
242
243 [[nodiscard]] ODB_FILE_WRITER CreateFileProxy( const wxString& aFileName )
244 {
245 return ODB_FILE_WRITER( *this, aFileName );
246 }
247
248 void CreateEntityDirectory( const wxString& aPareDir, const wxString& aSubDir = wxEmptyString );
249
250 inline const wxString GetCurrentPath() const { return m_currentPath; }
251
252 inline void SetCurrentPath( const wxString& aDir ) { m_currentPath = aDir; }
253
254 inline void SetRootPath( const wxString& aDir ) { m_rootPath = aDir; }
255
256 inline const wxString GetRootPath() const { return m_rootPath; }
257
258
259private:
261 wxString m_rootPath;
262};
263
264
266{
267public:
268 ODB_TEXT_WRITER( std::ostream& aStream ) : m_ostream( aStream ) {}
269 virtual ~ODB_TEXT_WRITER() {}
270
271 // void WriteEquationLine( const std::string &var, const std::string &value );
272 void WriteEquationLine( const std::string& var, int value );
273 void WriteEquationLine( const wxString& var, const wxString& value );
274 template <typename T>
275 void write_line_enum( const std::string& var, const T& value )
276 {
277 WriteEquationLine( var, ODB::Enum2String( value ) );
278 }
279
281 {
283
284 public:
285 ~ARRAY_PROXY();
286
287 private:
288 ARRAY_PROXY( ODB_TEXT_WRITER& aWriter, const std::string& aStr );
289
291
293
295
296 ARRAY_PROXY( ARRAY_PROXY const& ) = delete;
297
298 ARRAY_PROXY& operator=( ARRAY_PROXY const& ) = delete;
299 };
300
301 [[nodiscard]] ARRAY_PROXY MakeArrayProxy( const std::string& aStr )
302 {
303 return ARRAY_PROXY( *this, aStr );
304 }
305
306private:
307 void WriteIndent();
308
309 void BeginArray( const std::string& a );
310
311 void EndArray();
312
313 std::ostream& m_ostream;
314 bool in_array = false;
315};
316
317
319{
320public:
321 struct TOOLS
322 {
323 uint32_t m_num;
324 wxString m_type;
325 wxString m_type2 = wxT( "STANDARD" );
326 uint32_t m_minTol;
327 uint32_t m_maxTol;
328 wxString m_bit = wxEmptyString;
329 wxString m_finishSize;
330 wxString m_drillSize;
331
332 TOOLS() : m_num( 0 ), m_minTol( 0 ), m_maxTol( 0 ) {}
333 };
334
335 ODB_DRILL_TOOLS( const wxString& aUnits, const wxString& aThickness = "0",
336 const wxString& aUserParams = wxEmptyString );
337
338 void AddDrillTools( const wxString& aType, const wxString& aFinishSize )
339 {
340 TOOLS tool;
341 tool.m_num = m_tools.size() + 1;
342 tool.m_type = aType;
343 tool.m_finishSize = aFinishSize;
344 tool.m_drillSize = aFinishSize;
345
346 m_tools.push_back( tool );
347 }
348
349 void GenerateFile( std::ostream& aStream );
350
351 wxString m_units;
352 wxString m_thickness;
353 wxString m_userParams;
354 std::vector<ODB_DRILL_TOOLS::TOOLS> m_tools;
355};
356
357#endif // _ODB_UTIL_H_
bool operator()()
Definition: odb_util.h:191
static std::map< T, std::string > & GetMap()
Definition: odb_util.h:99
wxString m_units
Definition: odb_util.h:351
void GenerateFile(std::ostream &aStream)
Definition: odb_util.cpp:344
wxString m_thickness
Definition: odb_util.h:352
void AddDrillTools(const wxString &aType, const wxString &aFinishSize)
Definition: odb_util.h:338
wxString m_userParams
Definition: odb_util.h:353
std::vector< ODB_DRILL_TOOLS::TOOLS > m_tools
Definition: odb_util.h:354
void CreateFile(const wxString &aFileName)
Definition: odb_util.cpp:235
ODB_FILE_WRITER & operator=(ODB_FILE_WRITER const &)=delete
ODB_TREE_WRITER & m_treeWriter
Definition: odb_util.h:226
ODB_FILE_WRITER(ODB_FILE_WRITER &&)=delete
virtual ~ODB_FILE_WRITER()
Definition: odb_util.h:213
ODB_FILE_WRITER & operator=(ODB_FILE_WRITER &&)=delete
ODB_FILE_WRITER(ODB_FILE_WRITER const &)=delete
bool CloseFile()
Definition: odb_util.cpp:268
std::ostream & GetStream()
Definition: odb_util.h:223
std::ofstream m_ostream
Definition: odb_util.h:227
ARRAY_PROXY(ARRAY_PROXY &&)=delete
ARRAY_PROXY & operator=(ARRAY_PROXY const &)=delete
ARRAY_PROXY & operator=(ARRAY_PROXY &&)=delete
ARRAY_PROXY(ARRAY_PROXY const &)=delete
ODB_TEXT_WRITER & m_writer
Definition: odb_util.h:290
void WriteEquationLine(const std::string &var, int value)
Definition: odb_util.cpp:285
ARRAY_PROXY MakeArrayProxy(const std::string &aStr)
Definition: odb_util.h:301
virtual ~ODB_TEXT_WRITER()
Definition: odb_util.h:269
void write_line_enum(const std::string &var, const T &value)
Definition: odb_util.h:275
ODB_TEXT_WRITER(std::ostream &aStream)
Definition: odb_util.h:268
std::ostream & m_ostream
Definition: odb_util.h:313
void BeginArray(const std::string &a)
Definition: odb_util.cpp:306
void WriteIndent()
Definition: odb_util.cpp:299
wxString m_rootPath
Definition: odb_util.h:261
const wxString GetRootPath() const
Definition: odb_util.h:256
void CreateEntityDirectory(const wxString &aPareDir, const wxString &aSubDir=wxEmptyString)
Definition: odb_util.cpp:206
ODB_FILE_WRITER CreateFileProxy(const wxString &aFileName)
Definition: odb_util.h:243
wxString m_currentPath
Definition: odb_util.h:260
void SetRootPath(const wxString &aDir)
Definition: odb_util.h:254
ODB_TREE_WRITER(const wxString &aPareDir, const wxString &aSubDir)
Definition: odb_util.h:236
virtual ~ODB_TREE_WRITER()
Definition: odb_util.h:241
void SetCurrentPath(const wxString &aDir)
Definition: odb_util.h:252
const wxString GetCurrentPath() const
Definition: odb_util.h:250
ODB_TREE_WRITER(const wxString &aDir)
Definition: odb_util.h:234
Definition: odb_util.cpp:33
std::string Enum2String(T value)
Definition: odb_util.h:174
wxString Data2String(double aVal)
Definition: odb_util.cpp:161
void RemoveWhitespace(wxString &aStr)
Definition: odb_util.cpp:119
wxString GenODBString(const wxString &aStr)
Definition: odb_util.cpp:35
std::pair< wxString, wxString > AddXY(const VECTOR2I &aVec)
Definition: odb_util.cpp:167
wxString GenLegalEntityName(const wxString &aStr)
Definition: odb_util.cpp:82
wxString GenLegalNetName(const wxString &aStr)
Definition: odb_util.cpp:57
wxString Double2String(double aVal)
Definition: odb_util.cpp:127
wxString SymDouble2String(double aVal)
Definition: odb_util.cpp:155
VECTOR2I GetShapePosition(const PCB_SHAPE &aShape)
Definition: odb_util.cpp:178
ODB_SUBTYPE
Definition: odb_util.h:53
ODB_DIELECTRIC_TYPE
Definition: odb_util.h:27
ODB_TYPE
Definition: odb_util.h:34
@ CONDUCTIVE_PASTE
ODB_POLARITY
Definition: odb_util.h:15
ODB_CONTEXT
Definition: odb_util.h:21
ODB_FID_TYPE
Definition: odb_util.h:66