|
//ObjectARX中如何选择块参照中的对象
BOOL SelectEntsFromBlock(CString csPrompt, PickSet &ssResult,AcGePoint3d& ptBase)
{
ads_name ent;
ads_point pt;
ads_matrix mat;
struct resbuf* pHead = NULL;
int iRet = acedNEntSelP(csPrompt, ent, pt, FALSE, mat, &pHead);
ads_name entOuterBlock;
if (iRet == RTNORM)
{
if (!pHead)
{
acutPrintf(_T("这并不是一个块参照中的对象"));
return FALSE;
}
ptBase.x = pt[0];
ptBase.y = pt[1];
ptBase.z = pt[2];
//如果ent为块参照中的嵌套块参照对象,例如块参照A嵌套块参照B,块参照B嵌套块参照C,
//用户点取了嵌套块C的某个实体,以下循环可以将ent修改为块参照B;并且获得最外层的块参照的ads_name
while (pHead && pHead->restype == RTENAME)
{
if (pHead && !pHead->rbnext) //如果点取的是块参照中的非嵌套对象
{
ads_name_set(pHead->resval.rlname, entOuterBlock);
break;
}
else if (pHead->rbnext && !pHead->rbnext->rbnext) //如果点取的是块参照的嵌套对象
{
ads_name_set(pHead->resval.rlname, ent);
ads_name_set(pHead->rbnext->resval.rlname, entOuterBlock);
break;
}
pHead = pHead->rbnext;
}
//求解块参照的变换矩阵
AcGeMatrix3d matBlockRef;
AcDbObjectId id;
acdbGetObjectId(id, entOuterBlock); //获取最外层块参照的ID
AcDbBlockReference* pOuterBlockRef = NULL;
if (Acad::eOk == acdbOpenObject(pOuterBlockRef, id, AcDb::kForRead))
{
if (pOuterBlockRef)
{
//获取转换矩阵,从块表记录中的MCS坐标转为当前图形的WCS坐标
matBlockRef = pOuterBlockRef->blockTransform();
}
pOuterBlockRef->close();
}
acdbGetObjectId(id, ent); //获取用户选中实体的ID
AcDbEntity* pEnt = NULL;
AcDbEntity* pEntClone = NULL;
if (Acad::eOk == acdbOpenObject(pEnt, id, AcDb::kForRead))
{
if (pEnt)
{
pEntClone = (AcDbEntity*)pEnt->clone();
//此步骤是关键,复制出来实体以后,将实体从块参照自己的WCS坐标系转换为当前数据库的WCS
pEntClone->transformBy(matBlockRef);
CMDatabase::PostToModelSpace(pEntClone); //加入到当前数据库
}
pEnt->close();
}
//这里省略了对选择对象进行拖动的代码
//...........
return TRUE;
}
else
return FALSE;
} |
|