1 #include "StepHdrFile.h"
2 #include "../invalid_odb_error.h"
4 #include "../../Constants.h"
5 #include "../parse_error.h"
8 namespace Odb::Lib::FileModel::Design
10 StepHdrFile::~StepHdrFile()
12 m_stepRepeatRecords.clear();
15 bool StepHdrFile::Parse(std::filesystem::path path)
17 std::ifstream stepHdrFile;
23 if (!OdbFile::Parse(path))
return false;
25 auto stepHdrFilePath = path /
"stephdr";
26 if (!std::filesystem::exists(stepHdrFilePath))
28 auto message =
"stephdr file does not exist: [" + stepHdrFilePath.string() +
"]";
29 throw invalid_odb_error(message.c_str());
32 stepHdrFile.open(stepHdrFilePath, std::ios::in);
33 if (!stepHdrFile.is_open())
35 auto message =
"unable to open stephdr file: [" + stepHdrFilePath.string() +
"]";
36 throw invalid_odb_error(message.c_str());
39 m_path = stepHdrFilePath;
41 std::shared_ptr<StepRepeatRecord> pCurrentStepRepeatRecord;
42 bool openBraceFound =
false;
44 while (std::getline(stepHdrFile, line))
48 Utils::str_trim(line);
51 std::stringstream lineStream(line);
52 if (line.find(Constants::COMMENT_TOKEN) == 0)
56 else if (line.find(StepRepeatRecord::ARRAY_HEADER_TOKEN) == 0)
59 if (!(lineStream >> token))
61 throw_parse_error(m_path, line, token, lineNumber);
64 if (token != StepRepeatRecord::ARRAY_HEADER_TOKEN)
66 throw_parse_error(m_path, line, token, lineNumber);
69 if (lineStream >> token)
72 if (token == Constants::ARRAY_RECORD_OPEN_TOKEN)
74 openBraceFound =
true;
80 pCurrentStepRepeatRecord = std::make_shared<StepRepeatRecord>();
84 else if (line.find(Constants::ARRAY_RECORD_OPEN_TOKEN) == 0)
89 if (pCurrentStepRepeatRecord ==
nullptr)
91 throw_parse_error(m_path, line,
"", lineNumber);
97 throw_parse_error(m_path, line,
"", lineNumber);
100 openBraceFound =
true;
108 else if (line.find(Constants::ARRAY_RECORD_CLOSE_TOKEN) == 0)
110 if (pCurrentStepRepeatRecord !=
nullptr && openBraceFound)
112 m_stepRepeatRecords.push_back(pCurrentStepRepeatRecord);
113 pCurrentStepRepeatRecord.reset();
114 openBraceFound =
false;
119 throw_parse_error(m_path, line,
"", lineNumber);
125 std::string attribute;
128 if (!std::getline(lineStream, attribute,
'='))
130 throw_parse_error(m_path, line, attribute, lineNumber);
133 if (std::getline(lineStream, value))
135 Utils::str_trim(attribute);
136 Utils::str_trim(value);
138 if (pCurrentStepRepeatRecord !=
nullptr && openBraceFound)
140 if (attribute ==
"NAME" || attribute ==
"name")
142 pCurrentStepRepeatRecord->name = value;
144 else if (attribute ==
"X" || attribute ==
"x")
146 pCurrentStepRepeatRecord->x = std::stof(value);
148 else if (attribute ==
"Y" || attribute ==
"y")
150 pCurrentStepRepeatRecord->y = std::stof(value);
152 else if (attribute ==
"DX" || attribute ==
"dx")
154 pCurrentStepRepeatRecord->dx = std::stof(value);
156 else if (attribute ==
"DY" || attribute ==
"dy")
158 pCurrentStepRepeatRecord->dy = std::stof(value);
160 else if (attribute ==
"NX" || attribute ==
"nx")
162 pCurrentStepRepeatRecord->nx = std::stoi(value);
164 else if (attribute ==
"NY" || attribute ==
"ny")
166 pCurrentStepRepeatRecord->ny = std::stoi(value);
168 else if (attribute ==
"ANGLE" || attribute ==
"angle")
170 pCurrentStepRepeatRecord->angle = std::stof(value);
172 else if (attribute ==
"FLIP" || attribute ==
"flip")
174 if (value ==
"YES" || value ==
"yes")
176 pCurrentStepRepeatRecord->flip =
true;
178 else if (value ==
"NO" || value ==
"no")
180 pCurrentStepRepeatRecord->flip =
false;
184 throw_parse_error(m_path, line, attribute, lineNumber);
187 else if (attribute ==
"MIRROR" || attribute ==
"mirror")
189 if (value ==
"YES" || value ==
"yes")
191 pCurrentStepRepeatRecord->mirror =
true;
193 else if (value ==
"NO" || value ==
"no")
195 pCurrentStepRepeatRecord->mirror =
false;
199 throw_parse_error(m_path, line, attribute, lineNumber);
204 throw_parse_error(m_path, line, attribute, lineNumber);
209 if (attribute ==
"X_DATUM" || attribute ==
"x_datum")
211 xDatum = std::stof(value);
213 else if (attribute ==
"Y_DATUM" || attribute ==
"y_datum")
215 yDatum = stof(value);
217 else if (attribute ==
"ID" || attribute ==
"id")
219 id = (
unsigned int)std::stoul(value);
221 else if (attribute ==
"X_ORIGIN" || attribute ==
"x_origin")
223 xOrigin = std::stof(value);
225 else if (attribute ==
"Y_ORIGIN" || attribute ==
"y_origin")
227 yOrigin = std::stof(value);
229 else if (attribute ==
"TOP_ACTIVE" || attribute ==
"top_active")
231 topActive = std::stof(value);
233 else if (attribute ==
"BOTTOM_ACTIVE" || attribute ==
"bottom_active")
235 bottomActive = std::stof(value);
237 else if (attribute ==
"RIGHT_ACTIVE" || attribute ==
"right_active")
239 rightActive = std::stof(value);
241 else if (attribute ==
"LEFT_ACTIVE" || attribute ==
"left_active")
243 leftActive = std::stof(value);
245 else if (attribute ==
"AFFECTING_BOM" || attribute ==
"affecting_bom")
247 affectingBom = value;
249 else if (attribute ==
"AFFECTING_BOM_CHANGED" || attribute ==
"affecting_bom_changed")
251 affectingBomChanged = std::stoi(value);
253 else if (attribute ==
"UNITS" || attribute ==
"units")
257 else if (attribute.find(
"ONLINE_") == 0)
259 m_onlineValues[attribute] = value;
263 throw_parse_error(m_path, line, attribute, lineNumber);
277 catch (parse_error& pe)
279 auto m = pe.toString(
"Parse Error:");
285 catch (invalid_odb_error& ioe)
287 parse_info pi(m_path, line, lineNumber);
288 const auto m = pi.toString();
289 logexception_msg(ioe, m);
298 std::unique_ptr<Odb::Lib::Protobuf::StepHdrFile> StepHdrFile::to_protobuf()
const
300 auto message = std::make_unique<Odb::Lib::Protobuf::StepHdrFile>();
301 message->set_xdatum(xDatum);
302 message->set_ydatum(yDatum);
304 message->set_xorigin(xOrigin);
305 message->set_yorigin(yOrigin);
306 message->set_topactive(topActive);
307 message->set_bottomactive(bottomActive);
308 message->set_leftactive(leftActive);
309 message->set_rightactive(rightActive);
310 message->set_affectingbom(affectingBom);
311 message->set_affectingbomchanged(affectingBomChanged);
313 for (
const auto& stepRepeatRecord : m_stepRepeatRecords)
315 auto stepRepeatRecordPtr = message->add_steprepeatrecords();
316 stepRepeatRecordPtr->CopyFrom(*stepRepeatRecord->to_protobuf());
319 for (
const auto& kvOnlineValue : m_onlineValues)
321 (*message->mutable_onlinevalues())[kvOnlineValue.first] = kvOnlineValue.second;
327 void StepHdrFile::from_protobuf(
const Odb::Lib::Protobuf::StepHdrFile& message)
329 xDatum = message.xdatum();
330 yDatum = message.ydatum();
332 xOrigin = message.xorigin();
333 yOrigin = message.yorigin();
334 topActive = message.topactive();
335 bottomActive = message.bottomactive();
336 leftActive = message.leftactive();
337 rightActive = message.rightactive();
338 affectingBom = message.affectingbom();
339 affectingBomChanged = message.affectingbomchanged();
341 for (
const auto& stepRepeatRecord : message.steprepeatrecords())
343 auto stepRepeatRecordPtr = std::make_unique<StepRepeatRecord>();
344 stepRepeatRecordPtr->from_protobuf(stepRepeatRecord);
345 m_stepRepeatRecords.push_back(std::move(stepRepeatRecordPtr));
348 for (
const auto& kvOnlineValue : message.onlinevalues())
350 m_onlineValues[kvOnlineValue.first] = kvOnlineValue.second;
354 bool StepHdrFile::Save(std::ostream& os)
359 std::unique_ptr<Odb::Lib::Protobuf::StepHdrFile::StepRepeatRecord> StepHdrFile::StepRepeatRecord::to_protobuf()
const
361 auto message = std::make_unique<Odb::Lib::Protobuf::StepHdrFile::StepRepeatRecord>();
362 message->set_name(name);
369 message->set_angle(angle);
370 message->set_flip(flip);
371 message->set_mirror(mirror);
375 void StepHdrFile::StepRepeatRecord::from_protobuf(
const Odb::Lib::Protobuf::StepHdrFile::StepRepeatRecord& message)
377 name = message.name();
384 flip = message.flip();
385 mirror = message.mirror();
386 angle = message.angle();