OdbDesignLib
OdbDesign ODB++ Parsing Library
 
Loading...
Searching...
No Matches
FeaturesFile.h
1#pragma once
2
3#include "ContourPolygon.h"
4#include <filesystem>
5#include <vector>
6#include <string>
7#include "../../enums.h"
8#include "../../IProtoBuffable.h"
9#include "featuresfile.pb.h"
10#include "SymbolName.h"
11#include "AttributeLookupTable.h"
12#include "../IStreamSaveable.h"
13#include "../../odbdesign_export.h"
14
15
16namespace Odb::Lib::FileModel::Design
17{
18 class ODBDESIGN_EXPORT FeaturesFile : public IProtoBuffable<Odb::Lib::Protobuf::FeaturesFile>, public IStreamSaveable
19 {
20 public:
23
24 struct FeatureRecord : public IProtoBuffable<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord>, public AttributeLookupTable
25 {
27
28 enum class Type
29 {
30 Arc,
31 Pad,
32 Surface,
33 Barcode,
34 Text,
35 Line
36 };
37
38 Type type;
39
40 // Line
41 double xs, ys;
42 double xe, ye;
43
44 // Pad / Text
45 double x, y;
46 int apt_def_symbol_num = -1;
47 double apt_def_resize_factor;
48
49 // Arc
50 double xc, yc;
51 bool cw;
52
53 // Text
54 std::string font;
55 double xsize, ysize;
56 double width_factor;
57 std::string text;
58 int version;
59
60 //TODO: Barcode
61
62 // common
63 int sym_num = -1;
64 Polarity polarity;
65 int dcode;
66 unsigned int id;
67
68 int orient_def;
69 double orient_def_rotation;
70
71 ContourPolygon::Vector m_contourPolygons;
72
73 const ContourPolygon::Vector& GetContourPolygons() const;
74
75 typedef std::vector<std::shared_ptr<FeatureRecord>> Vector;
76
77 // Inherited via IProtoBuffable
78 std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord> to_protobuf() const override;
79 void from_protobuf(const Odb::Lib::Protobuf::FeaturesFile::FeatureRecord& message) override;
80
81 constexpr inline static const char* LINE_TOKEN = "L";
82 constexpr inline static const char* PAD_TOKEN = "P";
83 constexpr inline static const char* TEXT_TOKEN = "T";
84 constexpr inline static const char* ARC_TOKEN = "A";
85 constexpr inline static const char* BARCODE_TOKEN = "B";
86 constexpr inline static const char* SURFACE_START_TOKEN = "S";
87 constexpr inline static const char* SURFACE_END_TOKEN = "SE";
88 };
89
90 bool Parse(std::filesystem::path directory, const std::string& alternateFilename = "");
91 // Inherited via IStreamSaveable
92 bool Save(std::ostream& os) override;
93
94 std::string GetUnits() const;
95 std::filesystem::path GetPath();
96 std::filesystem::path GetDirectory();
97 int GetNumFeatures() const;
98 unsigned int GetId() const;
99
100 const SymbolName::StringMap& GetSymbolNamesByName() const;
101 const SymbolName::Vector& GetSymbolNames() const;
102 const FeatureRecord::Vector& GetFeatureRecords() const;
103
104 // Inherited via IProtoBuffable
105 std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> to_protobuf() const override;
106 void from_protobuf(const Odb::Lib::Protobuf::FeaturesFile& message) override;
107
108 private:
109 std::string m_units = "";
110 std::filesystem::path m_path;
111 std::filesystem::path m_directory;
112 int m_numFeatures; // from field in file
113 unsigned int m_id;
114
115 FeatureRecord::Vector m_featureRecords;
116 SymbolName::StringMap m_symbolNamesByName;
117 SymbolName::Vector m_symbolNames; // for serialization
118
119
120 std::vector<std::string> m_attributeNames;
121 std::vector<std::string> m_attributeTextValues;
122
123 constexpr inline static const char* FEATURES_FILENAMES[] =
124 {
125 "features",
126 "features2",
127 "features3"
128 };
129
130 constexpr inline static const char* UNITS_TOKEN = "UNITS";
131 constexpr inline static const char* ID_TOKEN = "ID";
132 constexpr inline static const char* ATTRIBUTE_NAME_TOKEN = "@";
133 constexpr inline static const char* ATTRIBUTE_VALUE_TOKEN = "&";
134 constexpr inline static const char* SYMBOL_NAME_TOKEN = "$";
135 constexpr inline static const char* COMMENT_TOKEN = "#";
136 constexpr inline static const char* NUM_FEATURES_TOKEN = "F";
137
138 };
139
140 // Extract symbols as a vector irrespective of whether the source is a vector or map.
141 // When the features file already stores symbols in a vector, preserve that ordering;
142 // otherwise collect the map values using the map's natural iteration order (typically key-sorted).
143 ODBDESIGN_EXPORT SymbolName::Vector collect_symbols(const FeaturesFile& featuresFile);
144
145 // Infer unit type from a unit string. Handles various unit name formats (mm, millimeter, inches, etc.)
146 // Returns UnitType::Metric for metric units, UnitType::Imperial for imperial units, UnitType::None otherwise.
147 // If rawUnits is empty, returns UnitType::None.
148 ODBDESIGN_EXPORT UnitType inferred_unit_type_from_features_units(const std::string& rawUnits);
149}
150
151