How to export json file from autocad?

How to export json file from autocad?

Anonymous
Not applicable
11,822 Views
12 Replies
Message 1 of 13

How to export json file from autocad?

Anonymous
Not applicable

I am fetching some information from the autocad file and i am saving those information in the json format.Now i need to export this json.

How can i export the the json to my desktop.

I am using c++ and want to save the file on my desktop.

0 Likes
11,823 Views
12 Replies
Replies (12)
Message 2 of 13

moogalm
Autodesk Support
Autodesk Support

Which file you want to export to Json, can please explain your question in more detail.

0 Likes
Message 3 of 13

Anonymous
Not applicable

Actually i want to save information of Line objects from autocad like start point,endpoint and layer name.now I need to export these values in the form of json to my desktop.

 

Please Check the attached file.

0 Likes
Message 4 of 13

moogalm
Autodesk Support
Autodesk Support

"now I need to export these values in the form of JSON to my desktop."

 

For this you need to write you app logic to insert streams in to Json, AutoCAD doesn't have such API which will dump Line information to JSON.

I suggest you search in StackOverFlow or ask any other C++ forum as it is not relevant to AutoCAD API.

 

 

 

 

 

 

Message 5 of 13

Anonymous
Not applicable

ok man..thanks a lot

0 Likes
Message 6 of 13

Alexander.Rivilis
Mentor
Mentor

@Anonymous

 

For example: https://github.com/nlohmann/json

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 7 of 13

Anonymous
Not applicable

thank you man..

0 Likes
Message 8 of 13

Anonymous
Not applicable

Hello!

 

I did the same process some months ago.

 

I am attaching you an example of a 3dpolyline which may help you.

 

This structure worked for me fine and I was able to use the json files with no problem.

 I changed the extension of the file from .json to .txt because I could not post my reply.

 

I hope that this will help you.

Message 9 of 13

Anonymous
Not applicable

thanx man..i will look  into it

0 Likes
Message 10 of 13

Anonymous
Not applicable

Sir, the format you provided is exactly what i want.

 

Please guide me about how you fetched that data. Can you please share your code so that i can get some help. As i haven't worked on C++ before so it will be so kind of you if you share the code or guide me to that output.

 

Thank you

0 Likes
Message 11 of 13

Anonymous
Not applicable

Hello again saurabh,

 

Unfortunately, I cannot share my entire code.

 

However, I can give some guidelines.

First, create a code which prints the data of your entities, for example if a line is found print the start and end point.

When your code is functional and you can read many different entities start adding json commands in order to start creating your json file.

 

You need to understand how to generate a proper structure for your json file so you can then represent your entity in another software.

I would suggest to see some examples in Internet, e.g. https://stackoverflow.com/questions/4289986/jsoncpp-writing-to-files

Just start creating a simple json file for a line and then you can develop it even more.

 

 

In addition, take a look to the samples of ObjectARX there is very useful information for the different entities.

This will help you to create your own code to read your AutoCAD files. (especially for the 3d solids)

 

Do you have a proper code to print your entities geometric data?

 

Kind regards,

Evangelos

0 Likes
Message 12 of 13

Anonymous
Not applicable

Yes, I do have a proper code and fortunately i am able to print that all in the json file.

I want to use the class structure or struct structure to represent my data in a proper way. But while using struct i am facing the problem in serializing/deserializing data.

 

Can you help ?

0 Likes
Message 13 of 13

Anonymous
Not applicable

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

 

0 Likes