The readDwgFile is actually pretty simple to
use. First you create a new AcDbDatabase, sending it an Adesk::kFalse to
ensure that the new DB is completely empty (since we're going to be reading a DB
in with the next command). Next we call the readDwgFile with the path to
the file we wish to examine. Once we've read in the file, we can now do
whatever we like to the DWG. For example, perhaps we want to count the
number of circles in the drawing:
(i haven't actually tested this code, so don't
assume it's going to compile and run flawlessly! Most of the code came
from one of my apps though, so most of it should be fine)
// Create a new AcDbDatabase pointer, and point it
at a completely empty DB
AcDbDatabase *pDb = new
AcDbDatabase(Adesk::kFalse);
// Read in the DWG file you wish to
manipulate
pDb->readDwgFile(fileName);
// Get the block table of our new DB
AcDbBlockTable *pBlockTable;
es =
pDb->getBlockTable(pBlockTable, AcDb::kForRead);
if(es !=
Acad::eOk)
{
CString a;
a.Format("Failed to get block table %d", es);
AfxMessageBox(a);
return;
}
// Now retrive
the block table record that corresponds to model space.
AcDbBlockTableRecord
*pBlockTableRecord;
es =
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,
AcDb::kForWrite);
if(es != Acad::eOk)
{
CString
a;
a.Format("Failed to get block table record : %d",
es);
AfxMessageBox(a);
pBlockTable->close();
return;
}
// We
have model space, so close the main
table.
pBlockTable->close();
// Create an iterator to step through the
entities
// contained in model space.
AcDbBlockTableRecordIterator
*pBlockIterator;
es =
pBlockTableRecord->newIterator(pBlockIterator);
if(es !=
Acad::eOk)
{
CString a;
a.Format("%d",es);
pBlockTable->close();
pBlockTableRecord->close();
AfxMessageBox("Failed to
create a new iterator: " + a);
return;
}
// Integer variable to store the number of
circles.
int circleCount;
// We now have a valid iterator. Step through
all of the entities and
// count the circles.
for (;
!pBlockIterator->done(); pBlockIterator->step())
{
// Create a NULL entity pointer
to be used in the next
// statement.
AcDbEntity *pEntity =
NULL;
// Open the entity pointed
to by the
iterator
pBlockIterator->getEntity(pEntity,
AcDb::kForRead);
// Now check to see if the
entity is a circle
size=2> if(pEntity->isKindOf(AcDbCirlce))
{
circleCount++;
}
}
// We're done iterating. Delete the iterator
and close the block table record
delete
pBlockIterator;
pBlockTableRecord->close();
// Now do something with the
circleCount...