创建矩形主要用到AcDbPolyline类,实现方法是通过点选对角点。
void CreateRectangle()//创建矩形命令
{
ads_point startpoint;
int nReturn;
acedInitGet(NULL, _T("R"));
nReturn = acedGetPoint(NULL, _RXST("指定起点:"), startpoint);
if (nReturn == RTNORM)
{
ads_point endpoint;
nReturn = acedGetPoint(startpoint, _RXST("\n指定对角点:"), endpoint);
//根据对角点找出四个角的顶点
ads_real x1 = startpoint[X];
ads_real x2 = endpoint[X];
ads_real y1 = startpoint[Y];
ads_real y2 = endpoint[Y];
AcGePoint2d ptLeftBottom(min(x1, x2), min(y1, y2));
AcGePoint2d ptRightBottom(max(x1, x2), min(y1, y2));
AcGePoint2d ptRightTop(max(x1, x2), max(y1, y2));
AcGePoint2d ptLeftTop(min(x1, x2), max(y1, y2));
AcGePoint2dArray points;
points.append(ptLeftBottom);
points.append(ptRightBottom);
points.append(ptRightTop);
points.append(ptLeftTop);
int numVerts = points.length();
AcDbPolyline *pPline = new AcDbPolyline(numVerts);
for (int i = 0; i < points.length(); i++)
{
pPline->addVertexAt(i, points.at(i));
}
pPline->setClosed(true);
HysuEditor::PostToModelSpace(pPline);
acutPrintf(_RXST("\n创建矩形完成."));
}
else
{
acutPrintf(_RXST("\n获取点失败."));
acutPrintf(_RXST("\n创建矩形失败."));
}
}
AcDbObjectId HysuEditor::PostToModelSpace(AcDbEntity *pEnt, AcDbDatabase *pDb)
{
AcDbBlockTable *pBlkTbl = NULL;
Acad::ErrorStatus es;
es = pDb->getBlockTable(pBlkTbl, AcDb::kForRead);
if (es != Acad::eOk)
{
acutPrintf(_T("\n块表打开失败,错误代码:%s"), acadErrorStatusText(es));
return AcDbObjectId::kNull;
}
AcDbBlockTableRecord *pBlkTblRcd = NULL;
es = pBlkTbl->getAt(ACDB_MODEL_SPACE, pBlkTblRcd, AcDb::kForWrite);
if (es != Acad::eOk)
{
acutPrintf(_T("\n模型空间块表记录打开失败,错误代码:%s"), acadErrorStatusText(es));
pBlkTbl->close();
return AcDbObjectId::kNull;
}
pBlkTbl->close();
AcDbObjectId outId;
es = pBlkTblRcd->appendAcDbEntity(outId, pEnt);
if (es != Acad::eOk)
{
acutPrintf(_T("\n无法添加实体到模型空间,错误代码:%s"), acadErrorStatusText(es));
pBlkTblRcd->close();
return AcDbObjectId::kNull;
}
pEnt->close();
pBlkTblRcd->close();
return outId;
}