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