AcGePoint3d ucsToDcs(const AcGePoint3d& pt) {
resbuf fromRb, toRb;
ads_point newPt;
fromRb.restype = toRb.restype = RTSHORT;
fromRb.resval.rint = AcDb::kUserCS;
toRb.resval.rint = AcDb::kCurDisplayCS;
short result = acedTrans(asDblArray(pt), &fromRb, &toRb, FALSE, newPt);
ASSERT(result == RTNORM);
return asPnt3d(newPt);
}
AcGePoint3d dcsToWcs(const AcGePoint3d& pt) {
resbuf fromRb, toRb;
ads_point newPt;
fromRb.restype = toRb.restype = RTSHORT;
fromRb.resval.rint = AcDb::kCurDisplayCS;
toRb.resval.rint = AcDb::kWorldCS;
short result = acedTrans(asDblArray(pt), &fromRb, &toRb, FALSE, newPt);
ASSERT(result == RTNORM);
return asPnt3d(newPt);
}
void viewRect() {
resbuf rb;
acedGetVar(_T("VIEWCTR"), &rb);
AcGePoint3d center(rb.resval.rpoint[X], rb.resval.rpoint[Y], rb.resval.rpoint[Z]);
center = ucsToDcs(center);
acedGetVar(_T("VIEWSIZE"), &rb);
double heigth = rb.resval.rreal;
acedGetVar(_T("SCREENSIZE"), &rb);
double width = heigth * rb.resval.rpoint[X] / rb.resval.rpoint[Y];
AcGePoint3d topLeft((center.x - (width / 2)), (center.y + (heigth / 2)), 0);
AcGePoint3d bottomright((center.x + (width / 2)), (center.y - (heigth / 2)), 0);
topLeft = dcsToWcs(topLeft);
bottomright = dcsToWcs(bottomright);
AcDbBlockTable *btbl; acdbCurDwg()->getBlockTable(btbl);
AcDbBlockTableRecord *btr; btbl->getAt(ACDB_MODEL_SPACE, btr, AcDb::kForWrite);
btbl->close();
AcDbObjectPointer<AcDbLine> l1, l2, l3, l4;
l1.create(); l2.create(); l3.create(); l4.create();
l1->setStartPoint(topLeft); l1->setEndPoint(AcGePoint3d(topLeft.x, bottomright.y, 0));
l2->setStartPoint(l1->endPoint()); l2->setEndPoint(bottomright);
l3->setStartPoint(bottomright); l3->setEndPoint(AcGePoint3d(bottomright.x, topLeft.y, 0));
l4->setStartPoint(l3->endPoint()); l4->setEndPoint(topLeft);
btr->appendAcDbEntity(l1); l1.close();
btr->appendAcDbEntity(l2); l2.close();
btr->appendAcDbEntity(l3); l3.close();
btr->appendAcDbEntity(l4); l4.close();
btr->close();
}