KiCad PCB EDA Suite
Loading...
Searching...
No Matches
odb_attribute.h
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) 2024 KiCad Developers, see AUTHORS.txt for contributors.
5 * Author: SYSUEric <[email protected]>.
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
21#ifndef _ATTRIBUTE_PROVIDER_H_
22#define _ATTRIBUTE_PROVIDER_H_
23
24#include "odb_util.h"
25#include "stroke_params.h"
26#include <wx/string.h>
27#include <string>
28#include <type_traits>
29
30
31namespace ODB_ATTR
32{
33
34enum class TYPE
35{
36 FLOAT,
37 BOOLEAN,
38 TEXT,
39 OPTION,
41};
42
43// Base class template for attributes
44template <typename T, TYPE AttrType>
46{
47 using ValueType = T;
48 static constexpr TYPE type = AttrType;
49 constexpr AttributeBase( T v ) : value( v ) {}
51};
52
53// Specialized attribute types
54template <typename T, unsigned int N>
55struct FloatAttribute : AttributeBase<double, TYPE::FLOAT>
56{
57 static constexpr unsigned int digits = N;
58 using AttributeBase<double, TYPE::FLOAT>::AttributeBase;
59};
60
61template <typename T>
62struct BooleanAttribute : AttributeBase<bool, TYPE::BOOLEAN>
63{
64 using AttributeBase<bool, TYPE::BOOLEAN>::AttributeBase;
65};
66
67template <typename T>
68struct TextAttribute : AttributeBase<std::string, TYPE::TEXT>
69{
70 constexpr TextAttribute( const std::string& t ) :
71 AttributeBase<std::string, TYPE::TEXT>( ODB::GenLegalEntityName( t ).ToStdString() )
72 {
73 }
74};
75
76template <typename T>
77struct OPTION_Attribute : AttributeBase<int, TYPE::OPTION>
78{
79 using AttributeBase<int, TYPE::OPTION>::AttributeBase;
80};
81
82
83template <typename T>
85{
86};
87
88// Attribute name and type definitions
89template <typename Tag, template <typename, unsigned int> class Attr, TYPE AttrType, unsigned int N>
91{
92 using TYPE = Attr<Tag, N>;
93};
94
95template <typename Tag, template <typename> class Attr, TYPE AttrType>
97{
98 using TYPE = Attr<Tag>;
99};
100
101// TYPE traits for attributes
102template <typename T>
103struct IsFeature : std::false_type
104{
105};
106template <typename T>
107struct IsNet : std::false_type
108{
109};
110template <typename T>
111struct IsPkg : std::false_type
112{
113};
114template <typename T>
115struct IsLayer : std::false_type
116{
117};
118template <typename T>
119struct IsStep : std::false_type
120{
121};
122template <typename T>
123struct IsComp : std::false_type
124{
125};
126template <typename T>
127struct IsProductModel : std::false_type
128{
129};
130template <typename T>
131struct IsSymbol : std::false_type
132{
133};
134
135
136#define DEFINE_ATTR( Tag, Attr, AttrType, AttrName, ... ) \
137 struct Tag##_t \
138 { \
139 }; \
140 constexpr const char Tag##_name[] = AttrName; \
141 using Tag = Attribute<Tag##_t, Attr, AttrType, __VA_ARGS__>::TYPE; \
142 template <> \
143 struct AttributeName<Tag> \
144 { \
145 static constexpr const char* name = Tag##_name; \
146 };
147
148#define DEFINE_ATTR_SIMPLE( Tag, Attr, AttrType, AttrName ) \
149 struct Tag##_t \
150 { \
151 }; \
152 constexpr const char Tag##_name[] = AttrName; \
153 using Tag = AttributeSimple<Tag##_t, Attr, AttrType>::TYPE; \
154 template <> \
155 struct AttributeName<Tag> \
156 { \
157 static constexpr const char* name = Tag##_name; \
158 };
159
160
161#define DEFINE_FLOAT_ATTR( NAME, N ) DEFINE_ATTR( NAME, FloatAttribute, TYPE::FLOAT, #NAME, N )
162
163#define DEFINE_BOOLEAN_ATTR( NAME ) \
164 DEFINE_ATTR_SIMPLE( NAME, BooleanAttribute, TYPE::BOOLEAN, #NAME )
165
166#define DEFINE_TEXT_ATTR( NAME ) DEFINE_ATTR_SIMPLE( NAME, TextAttribute, TYPE::TEXT, #NAME )
167
168#define DEFINE_OPTION_ATTR( NAME ) \
169 struct NAME##_t \
170 { \
171 }; \
172 template <> \
173 struct AttributeSimple<NAME##_t, OPTION_Attribute, TYPE::OPTION>; \
174 template <> \
175 struct AttributeName<NAME> \
176 { \
177 static constexpr const char* name = #NAME; \
178 };
179
180
181// used by which entity
182#define USED_BY_FEATURE_ENTITY( NAME ) \
183 template <> \
184 struct IsFeature<NAME> : std::true_type \
185 { \
186 };
187
188#define USED_BY_NET_ENTITY( NAME ) \
189 template <> \
190 struct IsNet<NAME> : std::true_type \
191 { \
192 };
193
194#define USED_BY_PKG_ENTITY( NAME ) \
195 template <> \
196 struct IsPkg<NAME> : std::true_type \
197 { \
198 };
199
200// Attribute definitions
201// BOOLEAN ATTRIBUTES
204
205DEFINE_BOOLEAN_ATTR( NET_POINT )
206USED_BY_FEATURE_ENTITY( NET_POINT )
207
208DEFINE_BOOLEAN_ATTR( ROUT_PLATED )
209USED_BY_FEATURE_ENTITY( ROUT_PLATED )
210
212
213DEFINE_BOOLEAN_ATTR( MOUNT_HOLE )
214USED_BY_FEATURE_ENTITY( MOUNT_HOLE )
215
216DEFINE_BOOLEAN_ATTR( TEAR_DROP )
217USED_BY_FEATURE_ENTITY( TEAR_DROP )
218
219DEFINE_BOOLEAN_ATTR( TEST_POINT )
220USED_BY_FEATURE_ENTITY( TEST_POINT )
221
222// TEXT ATTRIBUTES
225
226DEFINE_TEXT_ATTR( GEOMETRY )
227USED_BY_FEATURE_ENTITY( GEOMETRY )
228
229DEFINE_TEXT_ATTR( NET_NAME )
230USED_BY_FEATURE_ENTITY( NET_NAME )
231
232
233// FLOAT ATTRIBUTES
234DEFINE_FLOAT_ATTR( BOARD_THICKNESS, 1 ) // 0.0~10.0
235
236DEFINE_FLOAT_ATTR( STRING_ANGLE, 1 ) // 0.0~360.0
237USED_BY_FEATURE_ENTITY( STRING_ANGLE )
238
239
240// OPTION ATTRIBUTES
241enum class DRILL
242{
243 PLATED,
245 VIA
246};
249
250enum class PAD_USAGE
251{
252 TOEPRINT,
253 VIA,
258};
261
262enum class PLATED_TYPE
263{
264 STANDARD,
266};
269
270enum class VIA_TYPE
271{
272 DRILLED,
273 LASER,
274 PHOTO
275};
278
279} // namespace ODB_ATTR
280
281
283{
284public:
285 ATTR_MANAGER() = default;
286 virtual ~ATTR_MANAGER() = default;
287
288 template <typename Tr, typename Ta>
289 void AddFeatureAttribute( Tr& r, Ta v )
290 {
292
293 if constexpr( std::is_enum_v<Ta> )
294 r.attributes.emplace( id, std::to_string( static_cast<int>( v ) ) );
295 else
296 r.attributes.emplace( id, AttrValue2String( v ) );
297 }
298
299protected:
300 size_t GetAttrNameNumber( const wxString& name );
301
302 void WriteAttributes( std::ostream& ost, const std::string& prefix = "" ) const;
303 void WriteAttributesName( std::ostream& ost, const std::string& prefix = "" ) const;
304 void WriteAttributesText( std::ostream& ost, const std::string& prefix = "" ) const;
305
306
307private:
308 size_t GetAttrTextNumber( const wxString& aName );
309 size_t GetTextIndex( std::unordered_map<std::string, size_t>& aMap,
310 std::vector<std::pair<size_t, std::string>>& aVec,
311 const std::string& aText );
312
313 template <typename T, unsigned int n>
315 {
316 return ODB::Double2String( a.value, a.digits );
317 }
318
319 template <typename T>
321 {
322 return "";
323 }
324
325 template <typename T>
327 {
328 return std::to_string( GetAttrTextNumber( a.value ) );
329 }
330
331
332 std::unordered_map<std::string, size_t> m_attrNames;
333 std::vector<std::pair<size_t, std::string>> m_attrNameVec;
334
335 std::unordered_map<std::string, size_t> m_attrTexts;
336 std::vector<std::pair<size_t, std::string>> m_attrTextVec;
337};
338
340{
341public:
343 virtual ~ATTR_RECORD_WRITER() = default;
344
345 void WriteAttributes( std::ostream& ost ) const;
346
347public:
348 std::map<unsigned int, std::string> attributes;
349};
350
351
352#endif // ATTRIBUTE_PROVIDER_H_
const char * name
Definition: DXF_plotter.cpp:57
ATTR_MANAGER()=default
void WriteAttributesText(std::ostream &ost, const std::string &prefix="") const
std::vector< std::pair< size_t, std::string > > m_attrTextVec
std::vector< std::pair< size_t, std::string > > m_attrNameVec
virtual ~ATTR_MANAGER()=default
size_t GetTextIndex(std::unordered_map< std::string, size_t > &aMap, std::vector< std::pair< size_t, std::string > > &aVec, const std::string &aText)
std::string AttrValue2String(ODB_ATTR::FloatAttribute< T, n > a)
void WriteAttributes(std::ostream &ost, const std::string &prefix="") const
void AddFeatureAttribute(Tr &r, Ta v)
size_t GetAttrTextNumber(const wxString &aName)
std::unordered_map< std::string, size_t > m_attrNames
size_t GetAttrNameNumber(const wxString &name)
std::string AttrValue2String(ODB_ATTR::BooleanAttribute< T > a)
std::string AttrValue2String(ODB_ATTR::TextAttribute< T > a)
void WriteAttributesName(std::ostream &ost, const std::string &prefix="") const
std::unordered_map< std::string, size_t > m_attrTexts
std::map< unsigned int, std::string > attributes
void WriteAttributes(std::ostream &ost) const
virtual ~ATTR_RECORD_WRITER()=default
ATTR_RECORD_WRITER()=default
Definition: odb_util.cpp:32
wxString Double2String(double aVal)
Definition: odb_util.cpp:118
STL namespace.
#define DEFINE_OPTION_ATTR(NAME)
#define DEFINE_FLOAT_ATTR(NAME, N)
#define DEFINE_BOOLEAN_ATTR(NAME)
#define DEFINE_TEXT_ATTR(NAME)
#define USED_BY_FEATURE_ENTITY(NAME)
@ SMD
Smd pad, appears on the solder paste layer (default)
@ MECHANICAL
a pad used for mechanical support
constexpr AttributeBase(T v)
Definition: odb_attribute.h:49
static constexpr TYPE type
Definition: odb_attribute.h:48
Attr< Tag, N > TYPE
Definition: odb_attribute.h:92
static constexpr unsigned int digits
Definition: odb_attribute.h:57
constexpr TextAttribute(const std::string &t)
Definition: odb_attribute.h:70