--- a/src/ldrawalgorithm.cpp Sat Jul 23 01:38:06 2022 +0300 +++ b/src/ldrawalgorithm.cpp Sat Jul 23 01:38:43 2022 +0300 @@ -1,3 +1,4 @@ +#include <QTextBlock> #include "src/ldrawalgorithm.h" std::pair<Triangle, Triangle> splitTriangles( @@ -17,31 +18,28 @@ return result; } -std::vector<ModelAction> ldraw::makeUnofficial(const Model* model) +std::vector<ModelAction> ldraw::makeUnofficial(const QTextDocument* model) { std::vector<ModelAction> actions; -#if 0 - if (model->size() >= 4) { - if (const Comment* comment = std::get_if<Comment>(&(*model)[3])) { - const QString& body = comment->text; - if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_")) + constexpr int ldrawOrgLinePosition = 3; + const QTextBlock block = model->findBlockByLineNumber(ldrawOrgLinePosition); + if (block.isValid()) { + const QString body = block.text().simplified(); + if (body.startsWith("0 !LDRAW_ORG ") and not body.startsWith("0 !LDRAW_ORG Unofficial_")) { + // Add Unofficial_ to part type + QStringList tokens = body.split(" "); + tokens[2] = "Unofficial_" + tokens[2]; + // Remove the UPDATE tag if it's there + if (tokens.size() >= 5 && tokens[3] == "UPDATE") { - // Add Unofficial_ to part type - QStringList tokens = body.split(" "); - tokens[1] = "Unofficial_" + tokens[1]; - // Remove the UPDATE tag if it's there - if (tokens.size() >= 4 && tokens[2] == "UPDATE") - { - tokens.removeAt(3); - tokens.removeAt(2); - } - actions.push_back(ModifyModel{ - .position = 3, - .newElement = Comment{.text = tokens.join(" ")} - }); + tokens.removeAt(4); + tokens.removeAt(3); } + actions.push_back(ModifyModel{ + .position = ldrawOrgLinePosition, + .newElement = Comment{.text = tokens.mid(1).join(" ")} + }); } } -#endif return actions; }