{
AcDbDatabase* pDbUse = acdbHostApplicationServices()->workingDatabase();
AcDbDictionary* pNOD;
Acad::ErrorStatus eStat;
// 打开数据库的命名对象字典 (NamedObjects)
eStat = pDbUse->getNamedObjectsDictionary(pNOD, AcDb::kForRead);
if (eStat != Acad::eOk)
return;
AcDbObjectId idDict;
eStat = pNOD->getAt(_T("ACAD_LAYOUT"), idDict);
pNOD->close();
if (eStat != Acad::eOk)
return;
AcDbDictionary* pDict;
eStat = acdbOpenObject(pDict, idDict, AcDb::kForRead);
if (eStat != Acad::eOk)
return;
AcDbObjectId idLyt;
eStat = pDict->getAt(_T("Layout1"), idLyt);
pDict->close();
// 布局管理器
AcApLayoutManager* pLayM = (AcApLayoutManager*)acdbHostApplicationServices()->layoutManager();
// 设置当前布局
pLayM->setCurrentLayout(_T("Layout1"));
AcDbLayout* pLyt;
if (acdbOpenObject(pLyt, idLyt, AcDb::kForRead) != Acad::eOk)
return;
AcDbObjectId idBtr = pLyt->getBlockTableRecordId();
pLyt->close();
AddViewPort(idBtr, AcGePoint3d(2, 5, 0), AcGeVector3d(0, 0, 1));
AddViewPort(idBtr, AcGePoint3d(5, 5, 0), AcGeVector3d(-1, -1, 1));
AddViewPort(idBtr, AcGePoint3d(8, 5, 0), AcGeVector3d(1, 1, 1));
}
// 添加视口
static void AddViewPort(AcDbObjectId idToBtr, AcGePoint3d centerPoint, AcGeVector3d vecVPoint)
{
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
AcDbViewport* pVp = new AcDbViewport;
// 将新视口附加到图纸空间
AcDbBlockTableRecord* pBTR;
if (acdbOpenObject(pBTR, idToBtr, AcDb::kForWrite) != Acad::eOk)
{
acutPrintf(_T("\n无法访问图纸空间."));
delete pVp;
return;
}
if (pBTR->appendAcDbEntity(pVp) != Acad::eOk)
{
acutPrintf(_T("\n无法将视口附加到图纸空间."));
pBTR->close();
delete pVp;
return;
}
pBTR->close();
pVp->setCenterPoint(centerPoint);
// 设置查看方向
pVp->setViewDirection(vecVPoint);
// 假设目标点是WCS(0,0,0)
AcGeMatrix3d ucsMatrix;
getTrnsMatrix(ucsMatrix, vecVPoint, AcGePoint3d(0, 0, 0));
// 在视口的x-y平面中获取一个矩形窗口,该平面表示绘图的范围
AcGePoint2d maxExt, minExt;
getUcsExts(maxExt, minExt, ucsMatrix);
// 这里2是视图的高度。 您可以将其更改为任何高度
SetViewportExtents(pVp, maxExt, minExt, 2);
pVp->setOn();
pVp->close();
}
// 获取 WCS 到 UCS 转换矩阵
static void getTrnsMatrix(AcGeMatrix3d& ucsMatrix, AcGeVector3d ViewDirection, AcGePoint3d origin)
{
AcGePlane XYPlane(origin, ViewDirection);
ucsMatrix.setToWorldToPlane(XYPlane);
}
页:
[1]