I can sure try,
But it depends the way you created your code, so let me give you some ideas:
However, you need to convert from QJson to Json by yourself.
First you need to create a general class in order to dump each entity:
class EntityDump
{
public:
EntityDump( const QString& fileName );
void getLineData(const AcDbEntity* pEnt);
void write();
private:
void addEntity(const QJsonObject& obj);
private:
QString m_fileName;
QJsonObject m_root;
QJsonArray m_array;
};The constructor should look like:
EntityDump::EntityDump( const QString& fileName )
: m_fileName( fileName )
{
}The method to take the line data:
void EntityDump::getLineData(const AcDbEntity* pEnt)
{
static int lineCount = 1;
QString lineName = QString("line %1").arg(lineCount++);
QJsonObject ent, line;
// Specify that the pEnt is a AcDbLine type
AcDbLine *pLine = AcDbLine::cast(pEnt);
// Get the start point
AcGePoint3d startPoint;
pLine->getStartPoint(startPoint);
// Get the end point
AcGePoint3d endPoint;
pLine->getEndPoint(endPoint);
// Print the end points
acutPrintf(L"\nStart point: [%.3f, %.3f, %.3f]",
startPoint.x, startPoint.y, startPoint.z);
acutPrintf(L"\nEnd point: [%.3f, %.3f, %.3f]",
endPoint.x, endPoint.y, endPoint.z);
// Save to json file
QJsonArray startCoords;
startCoords.append(startPoint.x);
startCoords.append(startPoint.y);
startCoords.append(startPoint.z);
line.insert( "start", startCoords);
QJsonArray endCoords;
endCoords.append(endPoint.x);
endCoords.append(endPoint.y);
endCoords.append(endPoint.z);
line.insert("end", endCoords);
ent.insert(lineName, line);
addEntity(ent);
}
Then we need to write it to the array:
void EntityDump::addEntity(const QJsonObject& obj)
{
m_array.append(obj);
}
Finally we need to add the QJson object:
void EntityDump::write()
{
QFile f(m_fileName);
if (!f.open(QIODevice::WriteOnly)) {
acutPrintf(L"\nEntityDump failed to write!");
return;
}
acutPrintf(L"\nEntityDump writing to file");
m_root["model"] = m_array;
QJsonDocument doc(m_root);
QString tmp = doc.toJson();
//acutPrintf(L"\ncontents: \n%s", tmp.toStdWString().c_str() );
f.write(doc.toJson());
}
As you have this class, you can use it when you read the entities to create your QJson file, So you can have a function like the following.
Here we initialize the class EntityDump and it will be ready to be populated.
void readEntities(AcDbDatabase* pDb)
{
acutPrintf(L"\nFunction \"readEntities\" is initialized!");
AcDbBlockTable *pBlkTbl;
pDb->getSymbolTable(pBlkTbl, AcDb::kForRead);
AcDbBlockTableRecord *pBlkTblRcd;
pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd, AcDb::kForRead);
pBlkTbl->close();
AcDbBlockTableRecordIterator *pBlkTblRcdItr;
pBlkTblRcd->newIterator(pBlkTblRcdItr);
AcDbEntity *pEnt;
EntityDump d("C:\\YourPath\\export.json");
for (pBlkTblRcdItr->start(); !pBlkTblRcdItr->done();
pBlkTblRcdItr->step())
{
pBlkTblRcdItr->getEntity(pEnt, AcDb::kForRead);
acutPrintf(L"\nEntity found!");
exportGeoProps(pEnt, pDb, d);
pEnt->close();
}
pBlkTblRcd->close();
delete pBlkTblRcdItr;
d.write();
}
The last part now is to use the function exportGeoProps to add a line in your QJson file:
void exportGeoProps(AcDbEntity* pEnt, AcDbDatabase* pDb, EntityDump& d)
{
acutPrintf(L"\nFunction \"exportGeoProps\" is initialized!");
acutPrintf(L"\nClass type: \"%s\"", pEnt->isA()->name());
if (pEnt->isKindOf(AcDbLine::desc()))
{
d.getLineData(pEnt);
}
else
{
acutPrintf(L"\n...The found class type is not yet supported");
}
}So, this is a general process you may use to generate a proper QJson file.
As I said you need to convert the latter process to Json file format but this is not hard at all. QJson and Json are very similar.
Using this process you can dump all the entites you get from AutoCAD but for each entity you must create a different structure in the Json file.
Please not that this is a functional code but it may not be the most efficient one so you can develop it even more 🙂
Kind regards,
Evangelos