|
[code]class BlockDrawCar
{
public:
void DrawBlock(AcDbBlockTableRecord *btr)
{
Acad::ErrorStatus es;
// Draw a rectangle polyline
AcDbPolyline *pline = new AcDbPolyline();
AcGePoint2d p0(0.0, 0.0), p1(m_Length, 0.0);
AcGePoint2d p2(m_Length, m_Width), p3(0.0, m_Width);
pline->addVertexAt(0, p0);
pline->addVertexAt(1, p1);
pline->addVertexAt(2, p2);
pline->addVertexAt(3, p3);
pline->setClosed(true);
pline->setDatabaseDefaults();
pline->setColorIndex(m_Color);
es = btr->appendAcDbEntity(pline);
if (!es)
pline->close();
else
delete pline;
// Draw a circle
AcDbCircle *circle = new AcDbCircle();
AcGePoint3d center(m_Width / 2, m_Width / 2, 0.0);
double radius = m_Width * 0.4;
circle->setCenter(center);
circle->setRadius(radius);
circle->setDatabaseDefaults();
circle->setColorIndex(m_Color);
es = btr->appendAcDbEntity(circle);
if (!es)
circle->close();
else
delete circle;
}
public:
int m_Color=7;
double m_Width=200;
double m_Length=340;
};
AcDbObjectId CreateCarBlock(AcDbDatabase *db, LPCWSTR blockname)
{
Acad::ErrorStatus es;
AcDbBlockTableRecord *btr = new AcDbBlockTableRecord;
BlockDrawCar *car = new BlockDrawCar;
car->DrawBlock(btr);
btr->setName(blockname);
// Add the BTR to the Blocktable of the current drawing
AcDbObjectId BtrID;
AcDbBlockTable *blocktable;
es = db->getBlockTable(blocktable, AcDb::kForWrite);
if (!es)
{
es = blocktable->add(BtrID, btr);
if (!es)
btr->close();
else
delete btr;
blocktable->close();
}
return BtrID;
}
AcDbObjectId FindBlock(AcDbDatabase *db, LPCWSTR blockname)
{
AcDbObjectId BlockID;
Acad::ErrorStatus es;
AcDbBlockTable *blocktable;
es = db->getBlockTable(blocktable, AcDb::kForRead);
if (!es)
{
es = blocktable->getAt(blockname, BlockID);
blocktable->close();
}
return BlockID;
}
Acad::ErrorStatus PostToDb(AcDbDatabase *db, AcDbObjectId& objId, AcDbEntity* pEnt)
{
Acad::ErrorStatus es;
AcDbBlockTable* blocktable = 0;
AcDbBlockTableRecord* modelspace = 0;
if ((es = db->getBlockTable(blocktable, AcDb::kForRead)) != Acad::eOk)
return es;
if ((es = blocktable->getAt(ACDB_MODEL_SPACE, modelspace, AcDb::kForWrite)) == Acad::eOk)
{
es = modelspace->appendAcDbEntity(objId, pEnt);
modelspace->close();
}
blocktable->close();
return es;
}
void cmdDrawCar()
{
Acad::ErrorStatus es;
LPCWSTR blockname = L"*U"; // Blockname. This is for an unnamed block.
blockname = L"car"; // And this for a named block
AcDbDatabase *db = acdbHostApplicationServices()->workingDatabase();
AcDbObjectId BtrID;
if (blockname[0]!=L'*') // '*' as first char means: Unnamed block. Don't search for it.
BtrID = FindBlock(db, blockname); // Search for an existing named block
if (BtrID.isNull())
BtrID = CreateCarBlock(db, blockname); // For "*U" AutoCAD will create unique names like "*U1", "*U2", ...
if (BtrID.isNull())
{
acutPrintf(L"\nFailed.");
return;
}
// Now create a block reference
AcDbBlockReference *bref = new AcDbBlockReference;
bref->setDatabaseDefaults();
bref->setBlockTableRecord(BtrID);
AcGeVector3d insertionPoint;
acedGetPoint(nullptr, L"\nInsertion point: ", asDblArray(insertionPoint));
AcGeMatrix3d trans; // the transformation matrix
trans.setToTranslation(insertionPoint);
bref->setBlockTransform(trans);
// Append it to modelspace
AcDbObjectId BrefID;
es = PostToDb(db, BrefID, bref);
if (!es)
bref->close();
else
delete bref;
}[/code] |
|