OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
FeaturesFile.h
Go to the documentation of this file.
1#pragma once
2
3#include "ContourPolygon.h"
4#include <filesystem>
5#include <vector>
6#include <string>
7#include <optional>
8#include "../../enums.h"
9#include "../../IProtoBuffable.h"
10#include "featuresfile.pb.h"
11#include "SymbolName.h"
13#include "../IStreamSaveable.h"
14#include "../../odbdesign_export.h"
15
16
18{
19 class ODBDESIGN_EXPORT FeaturesFile : public IProtoBuffable<Odb::Lib::Protobuf::FeaturesFile>, public IStreamSaveable
20 {
21 public:
24
25 struct FeatureRecord : public IProtoBuffable<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord>, public AttributeLookupTable
26 {
28
29 enum class Type
30 {
31 Arc,
32 Pad,
33 Surface,
34 Barcode,
35 Text,
36 Line
37 };
38
39 Type type = Type::Arc;
40
41 // Line
42 double xs = 0.0, ys = 0.0;
43 double xe = 0.0, ye = 0.0;
44
45 // Pad / Text
46 double x = 0.0, y = 0.0;
47 int apt_def_symbol_num = -1;
48 double apt_def_resize_factor = 0.0;
49
50 // Arc
51 double xc = 0.0, yc = 0.0;
52 bool cw = false;
53
54 // Text
55 std::string font;
56 double xsize = 0.0, ysize = 0.0;
57 double width_factor = 0.0;
58 std::string text;
59 int version = 0;
60
61 //TODO: Barcode
62
63 // common
64 int sym_num = -1;
65 Polarity polarity = Polarity::Positive;
66 int dcode = 0;
67 // ODB++ product-model-wide unique id from the feature record ;ID= trailer.
68 // Optional: absent stays absent (never reported as id 0).
69 std::optional<unsigned int> id;
70
71 int orient_def = 0;
72 double orient_def_rotation = 0.0;
73
75
76 const ContourPolygon::Vector& GetContourPolygons() const;
77
78 typedef std::vector<std::shared_ptr<FeatureRecord>> Vector;
79
80 // Inherited via IProtoBuffable
81 std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile::FeatureRecord> to_protobuf() const override;
82 void from_protobuf(const Odb::Lib::Protobuf::FeaturesFile::FeatureRecord& message) override;
83
84 constexpr inline static const char* LINE_TOKEN = "L";
85 constexpr inline static const char* PAD_TOKEN = "P";
86 constexpr inline static const char* TEXT_TOKEN = "T";
87 constexpr inline static const char* ARC_TOKEN = "A";
88 constexpr inline static const char* BARCODE_TOKEN = "B";
89 constexpr inline static const char* SURFACE_START_TOKEN = "S";
90 constexpr inline static const char* SURFACE_END_TOKEN = "SE";
91 };
92
93 bool Parse(std::filesystem::path directory, const std::string& alternateFilename = "");
94 // Inherited via IStreamSaveable
95 bool Save(std::ostream& os) override;
96
97 std::string GetUnits() const;
98 std::filesystem::path GetPath();
99 std::filesystem::path GetDirectory();
100 int GetNumFeatures() const;
101 unsigned int GetId() const;
102
103 const SymbolName::StringMap& GetSymbolNamesByName() const;
104 const SymbolName::Vector& GetSymbolNames() const;
105 const FeatureRecord::Vector& GetFeatureRecords() const;
106
107 // Inherited via IProtoBuffable
108 std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> to_protobuf() const override;
109 // M6 streaming: same message minus the featureRecords list (the
110 // multi-GB bulk on large designs). Every other field is produced by
111 // the identical code path, so skeleton + re-appended featureRecords
112 // serializes to the exact to_protobuf() bytes.
113 std::unique_ptr<Odb::Lib::Protobuf::FeaturesFile> to_protobuf_skeleton() const;
114 void from_protobuf(const Odb::Lib::Protobuf::FeaturesFile& message) override;
115
116 private:
117 std::string m_units = "";
118 std::filesystem::path m_path;
119 std::filesystem::path m_directory;
120 int m_numFeatures; // from field in file
121 unsigned int m_id;
122
123 FeatureRecord::Vector m_featureRecords;
124 SymbolName::StringMap m_symbolNamesByName;
125 SymbolName::Vector m_symbolNames; // for serialization
126
127 // Shared by to_protobuf() and to_protobuf_skeleton(): everything past
128 // the featureRecords loop is identical so the skeleton stays
129 // byte-compatible with the full serialization.
130 void FillSymbolNames(Odb::Lib::Protobuf::FeaturesFile& featuresFileMessage) const;
131
132
133 std::vector<std::string> m_attributeNames;
134 std::vector<std::string> m_attributeTextValues;
135
136 constexpr inline static const char* FEATURES_FILENAMES[] =
137 {
138 "features",
139 "features2",
140 "features3"
141 };
142
143 constexpr inline static const char* UNITS_TOKEN = "UNITS";
144 constexpr inline static const char* ID_TOKEN = "ID";
145 constexpr inline static const char* ATTRIBUTE_NAME_TOKEN = "@";
146 constexpr inline static const char* ATTRIBUTE_VALUE_TOKEN = "&";
147 constexpr inline static const char* SYMBOL_NAME_TOKEN = "$";
148 constexpr inline static const char* COMMENT_TOKEN = "#";
149 constexpr inline static const char* NUM_FEATURES_TOKEN = "F";
150
151 };
152
153 // Extract symbols as a vector irrespective of whether the source is a vector or map.
154 // When the features file already stores symbols in a vector, preserve that ordering;
155 // otherwise collect the map values using the map's natural iteration order (typically key-sorted).
156 ODBDESIGN_EXPORT SymbolName::Vector collect_symbols(const FeaturesFile& featuresFile);
157
158 // Infer unit type from a unit string. Handles various unit name formats (mm, millimeter, inches, etc.)
159 // Returns UnitType::Metric for metric units, UnitType::Imperial for imperial units, UnitType::None otherwise.
160 // If rawUnits is empty, returns UnitType::None.
162}
163
164
std::vector< std::shared_ptr< SymbolName > > Vector
Definition SymbolName.h:33
std::map< std::string, std::shared_ptr< SymbolName > > StringMap
Definition SymbolName.h:34
SymbolName::Vector collect_symbols(const FeaturesFile &featuresFile)
UnitType inferred_unit_type_from_features_units(const std::string &rawUnits)
Polarity
Definition enums.h:19
UnitType
Definition enums.h:25
#define ODBDESIGN_EXPORT
std::vector< std::shared_ptr< ContourPolygon > > Vector
std::vector< std::shared_ptr< FeatureRecord > > Vector