|
objectarx之当前视图坐标转换
static void GetCurrentView( AcDbViewTableRecord &view )
{
struct resbuf rb;
struct resbuf wcs, ucs, dcs; // 转换坐标时使用的坐标系统标记
wcs.restype = RTSHORT;
wcs.resval.rint = 0;
ucs.restype = RTSHORT;
ucs.resval.rint = 1;
dcs.restype = RTSHORT;
dcs.resval.rint = 2;
// 获得当前视口的"查看"模式
acedGetVar(_T("VIEWMODE"), &rb);
view.setPerspectiveEnabled((rb.resval.rint & 1) != 0);
view.setFrontClipEnabled((rb.resval.rint & 2) != 0);
view.setBackClipEnabled((rb.resval.rint & 4) != 0);
view.setFrontClipAtEye((rb.resval.rint & 16) == 0);
// 当前视口中视图的中心点(UCS坐标)
acedGetVar(_T("VIEWCTR"), &rb);
acedTrans(rb.resval.rpoint, &ucs, &dcs, 0, rb.resval.rpoint);
view.setCenterPoint(AcGePoint2d(rb.resval.rpoint[X],
rb.resval.rpoint[Y]));
// 当前视口透视图中的镜头焦距长度(单位为毫米)
acedGetVar(_T("LENSLENGTH"), &rb);
view.setLensLength(rb.resval.rreal);
// 当前视口中目标点的位置(以 UCS 坐标表示)
acedGetVar(_T("TARGET"), &rb);
acedTrans(rb.resval.rpoint, &ucs, &wcs, 0, rb.resval.rpoint);
view.setTarget(AcGePoint3d(rb.resval.rpoint[X],
rb.resval.rpoint[Y], rb.resval.rpoint[Z]));
// 当前视口的观察方向(UCS)
acedGetVar(_T("VIEWDIR"), &rb);
acedTrans(rb.resval.rpoint, &ucs, &wcs, 1, rb.resval.rpoint);
view.setViewDirection(AcGeVector3d(rb.resval.rpoint[X],
rb.resval.rpoint[Y], rb.resval.rpoint[Z]));
// 当前视口的视图高度(图形单位)
acedGetVar(_T("VIEWSIZE"), &rb);
view.setHeight(rb.resval.rreal);
double height = rb.resval.rreal;
// 以像素为单位的当前视口的大小(X 和 Y 值)
acedGetVar(_T("SCREENSIZE"), &rb);
view.setWidth(rb.resval.rpoint[X] / rb.resval.rpoint[Y] * height);
// 当前视口的视图扭转角
acedGetVar(_T("VIEWTWIST"), &rb);
view.setViewTwist(rb.resval.rreal);
// 将模型选项卡或最后一个布局选项卡置为当前
acedGetVar(_T("TILEMODE"), &rb);
int tileMode = rb.resval.rint;
// 设置当前视口的标识码
acedGetVar(_T("CVPORT"), &rb);
int cvport = rb.resval.rint;
// 是否是模型空间的视图
bool paperspace = ((tileMode == 0) && (cvport == 1)) ? true : false;
view.setIsPaperspaceView(paperspace);
if (!paperspace)
{
// 当前视口中前向剪裁平面到目标平面的偏移量
acedGetVar(_T("FRONTZ"), &rb);
view.setFrontClipDistance(rb.resval.rreal);
// 获得当前视口后向剪裁平面到目标平面的偏移值
acedGetVar(_T("BACKZ"), &rb);
view.setBackClipDistance(rb.resval.rreal);
}
else
{
view.setFrontClipDistance(0.0);
view.setBackClipDistance(0.0);
}
}
static void SetView( const AcGePoint3d &ptMin, const AcGePoint3d &ptMax, double scale )
{
AcDbViewTableRecord view;
GetCurrentView(view);
// 将参数的两个点从世界坐标系转换到显示坐标系
AcGePoint3d ptMinDcs = WcsToDcsPoint(ptMin);
AcGePoint3d ptMaxDcs = WcsToDcsPoint(ptMax);
// 设置视图的中心点
view.setCenterPoint(AcGePoint2d((ptMinDcs.x + ptMaxDcs.x) / 2, (ptMinDcs.y + ptMaxDcs.y) / 2));
// 设置视图的高度和宽度
view.setHeight(fabs(ptMinDcs.y - ptMaxDcs.y) * scale);
view.setWidth(fabs(ptMinDcs.x - ptMaxDcs.x) * scale);
// 将视图对象设置为当前视图
acedSetCurrentView(&view, NULL);
}
static AcGePoint3d WcsToDcsPoint(const AcGePoint3d &point)
{
// 转换成世界坐标
AcGePoint3d pt;
struct resbuf rbFrom, rbTo;
rbFrom.restype = RTSHORT;
rbFrom.resval.rint = 0; // from WCS
rbTo.restype = RTSHORT;
rbTo.resval.rint = 2; // to DCS
acedTrans(asDblArray(point), &rbFrom, &rbTo, Adesk::kFalse, asDblArray(pt));
return pt;
}
static AcGePoint3d UcsToWcsPoint( const AcGePoint3d &point )
{
// 转换成世界坐标
AcGePoint3d pt;
struct resbuf rbFrom, rbTo;
rbFrom.restype = RTSHORT;
rbFrom.resval.rint = 1; // from UCS
rbTo.restype = RTSHORT;
rbTo.resval.rint = 0; // to WCS
acedTrans(asDblArray(point), &rbFrom, &rbTo, Adesk::kFalse, asDblArray(pt));
return pt;
} |
|