Moved bricklink color table to the mapping file.

This commit is contained in:
Leonardo Zide 2021-02-28 13:02:58 -08:00
parent 310504775e
commit 27734f2469
7 changed files with 664 additions and 718 deletions

View file

@ -1,312 +1,79 @@
#include <locale.h>
#include "lc_colors.h"
#include "lc_global.h"
#include "lc_file.h"
#include "lc_library.h"
#include "lc_mainwindow.h"
#include "lc_model.h"
#include "pieceinf.h"
#include "project.h"
namespace {
static QJsonDocument lcLoadBrickLinkMapping()
{
std::map<std::string, std::string> Remapping;
std::string BrickLinkRemap(const char* LDBrick)
QFile File(lcGetPiecesLibrary()->mLibraryDir.absoluteFilePath(QLatin1String("ldraw2bl.txt")));
QByteArray Data;
if (File.open(QIODevice::ReadOnly))
Data = File.readAll();
else
{
auto Remapped = Remapping.find(LDBrick);
if (Remapped != Remapping.end())
{
return Remapped->second;
}
return LDBrick;
QFile DefaultFile(":/resources/ldraw2bl.txt");
if (DefaultFile.open(QIODevice::ReadOnly))
Data = DefaultFile.readAll();
}
bool LoadRemapping()
{
if (!Remapping.empty())
{
return false;
}
QFile File(lcGetPiecesLibrary()->mLibraryDir.absoluteFilePath(QLatin1String("ldraw2bl.txt")));
QByteArray Data;
if (File.open(QIODevice::ReadOnly))
Data = File.readAll();
else
{
QFile DefaultFile(":/resources/ldraw2bl.txt");
if (DefaultFile.open(QIODevice::ReadOnly))
Data = DefaultFile.readAll();
}
if (Data.isEmpty())
return false;
QTextStream Stream(Data);
QString Line;
while (!Stream.atEnd())
{
Line = Stream.readLine(1024);
if (!Line.isEmpty() > 0)
{
// Commented line
if (Line.startsWith('!'))
{
continue;
}
auto parts = Line.trimmed().split(QLatin1Char(' '));
if (parts.size() < 2)
{
qDebug() << "BL remappings: Invalid line" << Line;
continue;
}
auto result = Remapping.insert(std::make_pair(parts.value(0).toStdString(),
parts.value(1).toUtf8().data()));
if (!result.second)
qDebug() << "Duplicate" << parts.value(0);
}
}
return true;
}
int lcGetBrickLinkColor(int ColorIndex)
{
struct lcBrickLinkEntry
{
int LDraw;
int BrickLink;
};
lcBrickLinkEntry BrickLinkColors[] =
{
{ 0, 11 }, // Black
{ 1, 7 }, // Blue
{ 2, 6 }, // Green
{ 3, 39 }, // Dark Turquoise
{ 4, 5 }, // Red
{ 5, 47 }, // Dark Pink
{ 6, 8 }, // Brown
{ 7, 9 }, // Light Gray
{ 8, 10 }, // Dark Gray
{ 9, 62 }, // Light Blue
{ 10, 36 }, // Bright Green
{ 11, 40 }, // Light Turquoise
{ 12, 25 }, // Salmon
{ 13, 23 }, // Pink
{ 14, 3 }, // Yellow
{ 15, 1 }, // White
{ 16, -1 }, // None
{ 17, 38 }, // Light Green
{ 18, 33 }, // Light Yellow
{ 19, 2 }, // Tan
{ 20, 44 }, // Light Violet
{ 21, 46 }, // Glow in Dark Opaque
{ 22, 24 }, // Purple
{ 23, 109 }, // Dark Blue-Violet
{ 24, -1 }, // None
{ 25, 4 }, // Orange
{ 26, 71 }, // Magenta
{ 27, 34 }, // Lime
{ 28, 69 }, // Dark Tan
{ 29, 104 }, // Bright Pink
{ 30, 157 }, // Medium Lavender
{ 31, 154 }, // Lavender
{ 32, 11 }, // Black
{ 33, 14 }, // Trans-Dark Blue
{ 34, 20 }, // Trans-Green
{ 35, 108 }, // Trans-Bright Green
{ 36, 17 }, // Trans-Red
{ 37, 50 }, // Trans-Dark Pink
{ 38, 18 }, // Trans-Neon Orange
{ 39, 113 }, // Trans-Very Lt Blue
{ 40, 13 }, // Trans-Black
{ 41, 74 }, // Trans-Medium Blue
{ 42, 16 }, // Trans-Neon Green
{ 43, 15 }, // Trans-Light Blue
{ 44, 114 }, // Trans-Light Purple
{ 45, 107 }, // Trans-Pink
{ 46, 19 }, // Trans-Yellow
{ 47, 12 }, // Trans-Clear
{ 52, 51 }, // Trans-Purple
{ 54, 121 }, // Trans-Neon Yellow
{ 57, 98 }, // Trans-Orange
{ 60, 57 }, // Chrome Antique Brass
{ 61, 52 }, // Chrome Blue
{ 62, 64 }, // Chrome Green
{ 63, 82 }, // Chrome Pink
{ 64, 122 }, // Chrome Black
{ 65, 3 }, // Yellow
{ 66, 19 }, // Trans-Yellow
{ 67, 12 }, // Trans-Clear
{ 68, 96 }, // Very Light Orange
{ 69, 93 }, // Light Purple
{ 70, 88 }, // Reddish Brown
{ 71, 86 }, // Light Bluish Gray
{ 72, 85 }, // Dark Bluish Gray
{ 73, 42 }, // Medium Blue
{ 74, 37 }, // Medium Green
{ 75, 116 }, // Speckle Black-Copper
{ 76, 117 }, // Speckle DBGray-Silver
{ 77, 56 }, // Light Pink
{ 78, 90 }, // Light Flesh
{ 79, 60 }, // Milky White
{ 80, 67 }, // Metallic Silver
{ 81, 70 }, // Metallic Green
{ 82, 65 }, // Metallic Gold
{ 83, 11 }, // Black
{ 84, 150 }, // Medium Dark Flesh
{ 85, 89 }, // Dark Purple
{ 86, 91 }, // Dark Flesh
{ 87, 77 }, // Pearl Dark Gray
{ 89, 97 }, // Blue - Violet
{ 92, 28 }, // Flesh
{ 100, 26 }, // Light Salmon
{ 110, 43 }, // Violet
{ 112, 73 }, // Medium Violet
{ 114, 100 }, // Glitter Trans-Dark Pink
{ 115, 76 }, // Medium Lime
{ 117, 101 }, // Glitter Trans-Clear
{ 118, 41 }, // Aqua
{ 120, 35 }, // Light Lime
{ 125, 32 }, // Light Orange
{ 128, 68 }, // Dark Orange
{ 129, 102 }, // Glitter Trans-Purple
{ 132, 111 }, // Speckle Black-Silver
{ 133, 151 }, // Speckle Black-Gold
{ 134, 84 }, // Copper
{ 135, 66 }, // Pearl Light Gray
{ 137, 78 }, // Metal Blue
{ 142, 61 }, // Pearl Light Gold
{ 148, 77 }, // Pearl Dark Gray
{ 150, 119 }, // Pearl Very Light Gray
{ 151, 99 }, // Very Light Bluish Gray
{ 178, 81 }, // Flat Dark Gold
{ 179, 95 }, // Flat Silver
{ 183, 83 }, // Pearl White
{ 191, 110 }, // Bright Light Orange
{ 212, 105 }, // Bright Light Blue
{ 216, 27 }, // Rust
{ 226, 103 }, // Bright Light Yellow
{ 232, 87 }, // Sky Blue
{ 256, 11 }, // Black
{ 272, 63 }, // Dark Blue
{ 273, 7 }, // Blue
{ 284, 114 }, // Trans-Light Purple
{ 288, 80 }, // Dark Green
{ 294, 118 }, // Glow In Dark Trans
{ 297, 115 }, // Pearl Gold
{ 308, 120 }, // Dark Brown
{ 313, 72 }, // Maersk Blue
{ 320, 59 }, // Dark Red
{ 321, 153 }, // Dark Azure
{ 322, 156 }, // Medium Azure
{ 323, 152 }, // Light Aqua
{ 324, 5 }, // Red
{ 326, 158 }, // Yellowish Green
{ 330, 155 }, // Olive Green
{ 334, 21 }, // Chrome Gold
{ 335, 58 }, // Sand Red
{ 350, 4 }, // Orange
{ 351, 94 }, // Medium Dark Pink
{ 366, 29 }, // Earth Orange
{ 373, 54 }, // Sand Purple
{ 375, 9 }, // Light Gray
{ 378, 48 }, // Sand Green
{ 379, 55 }, // Sand Blue
{ 383, 22 }, // Chrome Silver
{ 406, 63 }, // Dark Blue
{ 449, 24 }, // Purple
{ 450, 106 }, // Fabuland Brown
{ 462, 31 }, // Medium Orange
{ 484, 68 }, // Dark Orange
{ 490, 34 }, // Lime
{ 493, -10 }, // Magnet
{ 494, -11 }, // Electric_Contact_Alloy
{ 495, -12 }, // Electric_Contact_Copper
{ 496, 86 }, // Light Bluish Gray
{ 503, 49 }, // Very Light Gray
{ 504, 95 }, // Flat Silver
{ 511, 1 }, // White
{ 10001, 159 }, // Glow In Dark White
{ 10002, 160 }, // Fabuland Orange
{ 10003, 161 }, // Dark Yellow
{ 10004, 162 }, // Glitter Trans-Light Blue
{ 10005, 163 }, // Glitter Trans-Neon Green
{ 10006, 164 }, // Trans-Light Orange
{ 10007, 165 }, // Neon Orange
{ 10008, 220 }, // Coral
{ 10009, 166 }, // Neon Green
{ 10010, 221 } // Trans-Light Green
};
int ColorCode = gColorList[ColorIndex].Code;
for (unsigned int Color = 0; Color < LC_ARRAY_COUNT(BrickLinkColors); Color++)
if (BrickLinkColors[Color].LDraw == ColorCode)
return BrickLinkColors[Color].BrickLink;
return 0;
}
return QJsonDocument::fromJson(Data);
}
class Item
static std::string lcGetBrickLinkPart(const QJsonObject& PartMapping, const QString& LDrawPart)
{
QJsonObject::const_iterator PartIt = PartMapping.find(LDrawPart);
return (PartIt != PartMapping.end()) ? PartIt.value().toString().toStdString() : LDrawPart.toStdString();
}
static int lcGetBrickLinkColor(const QJsonObject& ColorMapping, int ColorIndex)
{
int ColorCode = gColorList[ColorIndex].Code;
QJsonObject::const_iterator ColorIt = ColorMapping.find(QString::number(ColorCode));
return (ColorIt != ColorMapping.end()) ? ColorIt.value().toString().toInt() : 0;
}
class lcBrickLinkItem
{
public:
Item(const std::string& id, int color, int count)
lcBrickLinkItem(const std::string& id, int color, int count)
: mId(id), mColor(color), mCount(count)
{
}
{
}
void AddToCount(int count)
{
mCount += count;
}
{
mCount += count;
}
std::string mId;
int mColor;
int mCount;
};
void ExportBrickLink(const Project& project)
void lcExportBrickLink(const QString& SaveFileName, const lcPartsList& PartsList)
{
lcPartsList PartsList;
auto Models = project.GetModels();
if (!Models.IsEmpty())
Models[0]->GetPartsList(gDefaultColor, true, false, PartsList);
if (PartsList.empty())
{
QMessageBox::information(gMainWindow, project.tr("LeoCAD"), project.tr("Nothing to export."));
return;
}
QString SaveFileName = project.GetExportFileName(QString(), "xml", project.tr("Export BrickLink"), project.tr("XML Files (*.xml);;All Files (*.*)"));
if (SaveFileName.isEmpty())
return;
if (Remapping.empty())
{
if (!LoadRemapping())
{
qDebug() << "Loading BL remappings failed.";
}
}
QJsonDocument Document = lcLoadBrickLinkMapping();
QJsonObject Root = Document.object();
QJsonObject PartMapping = Root["Parts"].toObject();
QJsonObject ColorMapping = Root["Colors"].toObject();
lcDiskFile BrickLinkFile(SaveFileName);
char Line[1024];
if (!BrickLinkFile.Open(QIODevice::WriteOnly))
{
QMessageBox::warning(gMainWindow, project.tr("LeoCAD"), project.tr("Could not open file '%1' for writing.").arg(SaveFileName));
QMessageBox::warning(gMainWindow, QObject::tr("LeoCAD"), QObject::tr("Could not open file '%1' for writing.").arg(SaveFileName));
return;
}
std::map<std::string, Item> Inventory;
std::map<std::string, lcBrickLinkItem> Inventory;
BrickLinkFile.WriteLine("<INVENTORY>\n");
@ -316,21 +83,20 @@ void ExportBrickLink(const Project& project)
for (const auto& ColorIt : PartIt.second)
{
char FileName[LC_PIECE_NAME_LEN];
strcpy(FileName, Info->mFileName);
char* Ext = strchr(FileName, '.');
if (Ext)
*Ext = 0;
std::string Remapped = BrickLinkRemap(FileName);
int Color = lcGetBrickLinkColor(ColorIt.first);
std::string Remapped = lcGetBrickLinkPart(PartMapping, FileName);
int Color = lcGetBrickLinkColor(ColorMapping, ColorIt.first);
std::string key(Remapped);
key += "-" + std::to_string(Color);
auto iter = Inventory.find(key);
if (iter == Inventory.end())
{
Inventory.emplace(std::make_pair(key, Item(Remapped, Color, ColorIt.second)));
Inventory.emplace(std::make_pair(key, lcBrickLinkItem(Remapped, Color, ColorIt.second)));
}
else
{

View file

@ -1,6 +1,3 @@
#pragma once
class Project;
void ExportBrickLink(const Project& project);
void lcExportBrickLink(const QString& FileName, const lcPartsList& PartsList);

View file

@ -31,6 +31,11 @@ QSize lcViewWidget::sizeHint() const
return mPreferredSize.isEmpty() ? QOpenGLWidget::sizeHint() : mPreferredSize;
}
lcView* lcViewWidget::GetView() const
{
return mView.get();
}
void lcViewWidget::SetView(lcView* View)
{
if (View)

View file

@ -7,11 +7,7 @@ class lcViewWidget : public QOpenGLWidget
public:
lcViewWidget(QWidget* Parent, lcView* View);
lcView* GetView() const
{
return mView.get();
}
lcView* GetView() const;
void SetView(lcView* View);
QSize sizeHint() const override;

View file

@ -1130,7 +1130,23 @@ bool Project::Export3DStudio(const QString& FileName)
void Project::ExportBrickLink()
{
::ExportBrickLink(*this);
lcPartsList PartsList;
if (!mModels.IsEmpty())
mModels[0]->GetPartsList(gDefaultColor, true, false, PartsList);
if (PartsList.empty())
{
QMessageBox::information(gMainWindow, tr("LeoCAD"), tr("Nothing to export."));
return;
}
QString SaveFileName = GetExportFileName(QString(), "xml", tr("Export BrickLink"), tr("XML Files (*.xml);;All Files (*.*)"));
if (SaveFileName.isEmpty())
return;
lcExportBrickLink(SaveFileName, PartsList);
}
bool Project::ExportCOLLADA(const QString& FileName)

View file

@ -98,9 +98,9 @@ public:
void UpdatePieceInfo(PieceInfo* Info) const;
protected:
QString GetExportFileName(const QString& FileName, const QString& DefaultExtension, const QString& DialogTitle, const QString& DialogFilter) const;
protected:
std::vector<lcModelPartsEntry> GetModelParts();
void SetFileName(const QString& FileName);

File diff suppressed because it is too large Load diff