Why do I get an error exporting an object in DXF format?

Why do I get an error exporting an object in DXF format?

zhengyunyang2019
Advocate Advocate
319 Views
2 Replies
Message 1 of 3

Why do I get an error exporting an object in DXF format?

zhengyunyang2019
Advocate
Advocate

 

	CFileDialog fileDlg(FALSE, L"dxf", L"output", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"DXF Files (*.dxf)|*.dxf||", NULL);
	if (fileDlg.DoModal() != IDOK)
	{
		return;
	}

	CString filePath = fileDlg.GetPathName();

	AcDbDatabase* tempDb = new AcDbDatabase();
	if (!tempDb) 
	{
		return;
	}

	AcDbBlockTable* blockTable;
	acdbHostApplicationServices()->workingDatabase()->getBlockTable(blockTable, AcDb::kForRead);

	AcDbBlockTableRecord* modelSpace;
	blockTable->getAt(ACDB_MODEL_SPACE, modelSpace, AcDb::kForRead);
	blockTable->close();

	AcDbBlockTableRecordIterator* iter;
	modelSpace->newIterator(iter);

	for (; !iter->done(); iter->step())
	{
		AcDbEntity* entity;
		if (iter->getEntity(entity, AcDb::kForRead) == Acad::eOk)
		{
			if (wcscmp(entity->layer(), mLayerName) == 0)
			{
				AcDbEntity* clonedEntity = static_cast<AcDbEntity*>(entity->clone());

				AcDbBlockTable* tempBlockTable = nullptr;
				Acad::ErrorStatus es = tempDb->getBlockTable(tempBlockTable, AcDb::kForWrite);
				if (es != Acad::eOk || !tempBlockTable) 
				{
					delete tempDb;
					return;
				}

				AcDbBlockTableRecord* tempModelSpace;
				tempBlockTable->getAt(ACDB_MODEL_SPACE, tempModelSpace, AcDb::kForWrite);
				tempBlockTable->close();

				tempModelSpace->appendAcDbEntity(clonedEntity);
				clonedEntity->close();
				tempModelSpace->close();
			}
			entity->close();
		}
	}

	delete iter;
	modelSpace->close();

	if (tempDb->dxfOut(filePath, 16, AcDb::kDHL_CURRENT, false) != Acad::eOk)
	{
		MessageBox(L"error");
	}
	else
	{
		MessageBox(L"Success");
	}

	delete tempDb;

In the attached file Drawing1.dwg, there are two lines - one short and one long. I copy one of the lines to a new layer and export all objects from this new layer to a DXF file.

The issue I'm encountering is:

  • When I copy only the short line and export, the DXF file (attached output.dxf) works correctly.

  • However, when I copy only the long line and export, the resulting DXF file (attached output2.dxf) appears to be corrupted.

Notably, tempDb->dxfOut always returns Acad::eOk in both cases.

 

0 Likes
Accepted solutions (1)
320 Views
2 Replies
Replies (2)
Message 2 of 3

tbrammer
Advisor
Advisor
Accepted solution

It isn't safe to clone() an entity and append the clone to a different AcDbDatabase. Each entity has references to objects in its own DB like layer, material, linetype and so on.  These references are missing if you try to append the clone to a different database. The safe way to copy entities from one DB to another is to use deep cloning:

 

Acad::ErrorStatus AcDbDatabase::wblockCloneObjects(
AcDbObjectIdArray& ids, AcDbObjectId& owner, AcDbIdMapping& idMap, AcDb::DuplicateRecordCloning drc, bool deferXlation=false
);  

First collect the AcDbObjectIds of the entities you want to clone in an iterator. Then use to wblockCloneObjects() clone them to your new AcDbDatabase.

AcDb::DuplicateRecordCloning drc defines how wblockCloneObjects() shall deal with references that already exist in the target DB.

 


Thomas Brammer ● Software Developer ● imos AGLinkedIn
If an answer solves your problem please [ACCEPT SOLUTION]. Otherwise explain why not.

Message 3 of 3

zhengyunyang2019
Advocate
Advocate

Thank you, your method is correct, now it can work properly

0 Likes